Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ pipx install git-draft[openai]
* Mechanism for reporting feedback from a bot, and possibly allowing user to
interactively respond.
* Support file rename tool.
* https://stackoverflow.com/q/49853177/1062617
* https://stackoverflow.com/q/6658313/1062617
* Add MCP bot.
5 changes: 4 additions & 1 deletion docs/git-draft.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ IMPORTANT: `git-draft` is WIP.
== Synopsis

[verse]
git draft [options] [--generate] [--accept... | no-accept] [--bot BOT] [--edit] [--reset | --no-reset] [--sync | --no-sync] [TEMPLATE [VARIABLE...]]
git draft [options] [--generate] [--accept... | --no-accept] [--bot BOT]
[--edit] [--reset | --no-reset] [--sync | --no-sync]
[TEMPLATE [VARIABLE...]]
git draft [options] --finalize [--delete] [--sync | --no-sync]
git draft [options] --show-drafts [--json]
git draft [options] --show-prompts [--json] [PROMPT]
Expand Down Expand Up @@ -96,6 +98,7 @@ git draft [options] --show-templates [--json | [--edit] TEMPLATE]

-s::
--sync::
--no-sync::
Create a sync commit with any changes.

-t TIMEOUT::
Expand Down
8 changes: 4 additions & 4 deletions src/git_draft/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def main() -> None: # noqa: PLR0912 PLR0915
prompt = sys.stdin.read()

accept = Accept(opts.accept or 0)
name = drafter.generate_draft(
draft = drafter.generate_draft(
prompt,
bot,
accept=accept,
Expand All @@ -225,13 +225,13 @@ def main() -> None: # noqa: PLR0912 PLR0915
reset=config.reset if opts.reset is None else opts.reset,
sync=config.sync if opts.sync is None else opts.sync,
)
print(f"Generated change in {name}.")
print(f"Generated change in {draft.branch_name}.")
case "finalize":
name = drafter.finalize_draft(
draft = drafter.finalize_draft(
delete=opts.delete,
sync=config.sync if opts.sync is None else opts.sync,
)
print(f"Finalized {name}.")
print(f"Finalized {draft.branch_name}.")
case "show-drafts":
table = drafter.history_table(args[0] if args else None)
if table:
Expand Down
15 changes: 11 additions & 4 deletions src/git_draft/drafter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class Accept(enum.Enum):
NO_REGRETS = enum.auto()


@dataclasses.dataclass(frozen=True)
class Draft:
"""Collection of generated changes"""

branch_name: str


@dataclasses.dataclass(frozen=True)
class _Branch:
"""Draft branch"""
Expand Down Expand Up @@ -94,7 +101,7 @@ def generate_draft( # noqa: PLR0913
sync: bool = False,
timeout: float | None = None,
tool_visitors: Sequence[ToolVisitor] | None = None,
) -> str:
) -> Draft:
if timeout is not None:
raise NotImplementedError() # TODO: Implement

Expand Down Expand Up @@ -165,7 +172,7 @@ def generate_draft( # noqa: PLR0913
delta.apply()
if accept.value >= Accept.FINALIZE.value:
self.finalize_draft(delete=accept == Accept.NO_REGRETS, sync=sync)
return str(branch)
return Draft(str(branch))

def _prepare_prompt(
self,
Expand Down Expand Up @@ -214,7 +221,7 @@ def _generate_change(

def finalize_draft(
self, *, delete: bool = False, sync: bool = False
) -> str:
) -> Draft:
branch = _Branch.active(self._repo)
if not branch:
raise RuntimeError("Not currently on a draft branch")
Expand All @@ -240,7 +247,7 @@ def finalize_draft(
_logger.debug("Deleted branch %s.", branch)

_logger.info("Exited %s.", branch)
return branch.name
return Draft(branch.name)

def _create_branch(self, sync: bool) -> _Branch:
if self._repo.head.is_detached:
Expand Down
4 changes: 2 additions & 2 deletions tests/git_draft/drafter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ def test_finalize_keeps_changes(self) -> None:
assert self._read("PROMPT") == "hello"

def test_finalize_and_sync(self) -> None:
branch = self._drafter.generate_draft(
draft = self._drafter.generate_draft(
"hello",
_SimpleBot.prompt(),
accept=sut.Accept.CHECKOUT,
)
self._write("PROMPT", "a2")
self._drafter.finalize_draft(sync=True)
assert self._read("PROMPT") == "a2"
commits = self._commits(branch)
commits = self._commits(draft.branch_name)
assert len(commits) == 3 # init, prompt, sync
assert "sync" in commits[0].message

Expand Down