Skip to content

Commit ab36ff1

Browse files
authored
feat: add explicit draft type (#61)
This will be useful to add extra information, for example whether the last change was a no-op, total number of drafts, ...
1 parent 9cc28f3 commit ab36ff1

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ pipx install git-draft[openai]
2424
* Mechanism for reporting feedback from a bot, and possibly allowing user to
2525
interactively respond.
2626
* Support file rename tool.
27-
* https://stackoverflow.com/q/49853177/1062617
28-
* https://stackoverflow.com/q/6658313/1062617
27+
* Add MCP bot.

docs/git-draft.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ IMPORTANT: `git-draft` is WIP.
1818
== Synopsis
1919

2020
[verse]
21-
git draft [options] [--generate] [--accept... | no-accept] [--bot BOT] [--edit] [--reset | --no-reset] [--sync | --no-sync] [TEMPLATE [VARIABLE...]]
21+
git draft [options] [--generate] [--accept... | --no-accept] [--bot BOT]
22+
[--edit] [--reset | --no-reset] [--sync | --no-sync]
23+
[TEMPLATE [VARIABLE...]]
2224
git draft [options] --finalize [--delete] [--sync | --no-sync]
2325
git draft [options] --show-drafts [--json]
2426
git draft [options] --show-prompts [--json] [PROMPT]
@@ -96,6 +98,7 @@ git draft [options] --show-templates [--json | [--edit] TEMPLATE]
9698

9799
-s::
98100
--sync::
101+
--no-sync::
99102
Create a sync commit with any changes.
100103

101104
-t TIMEOUT::

src/git_draft/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def main() -> None: # noqa: PLR0912 PLR0915
215215
prompt = sys.stdin.read()
216216

217217
accept = Accept(opts.accept or 0)
218-
name = drafter.generate_draft(
218+
draft = drafter.generate_draft(
219219
prompt,
220220
bot,
221221
accept=accept,
@@ -225,13 +225,13 @@ def main() -> None: # noqa: PLR0912 PLR0915
225225
reset=config.reset if opts.reset is None else opts.reset,
226226
sync=config.sync if opts.sync is None else opts.sync,
227227
)
228-
print(f"Generated change in {name}.")
228+
print(f"Generated change in {draft.branch_name}.")
229229
case "finalize":
230-
name = drafter.finalize_draft(
230+
draft = drafter.finalize_draft(
231231
delete=opts.delete,
232232
sync=config.sync if opts.sync is None else opts.sync,
233233
)
234-
print(f"Finalized {name}.")
234+
print(f"Finalized {draft.branch_name}.")
235235
case "show-drafts":
236236
table = drafter.history_table(args[0] if args else None)
237237
if table:

src/git_draft/drafter.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class Accept(enum.Enum):
3636
NO_REGRETS = enum.auto()
3737

3838

39+
@dataclasses.dataclass(frozen=True)
40+
class Draft:
41+
"""Collection of generated changes"""
42+
43+
branch_name: str
44+
45+
3946
@dataclasses.dataclass(frozen=True)
4047
class _Branch:
4148
"""Draft branch"""
@@ -94,7 +101,7 @@ def generate_draft( # noqa: PLR0913
94101
sync: bool = False,
95102
timeout: float | None = None,
96103
tool_visitors: Sequence[ToolVisitor] | None = None,
97-
) -> str:
104+
) -> Draft:
98105
if timeout is not None:
99106
raise NotImplementedError() # TODO: Implement
100107

@@ -165,7 +172,7 @@ def generate_draft( # noqa: PLR0913
165172
delta.apply()
166173
if accept.value >= Accept.FINALIZE.value:
167174
self.finalize_draft(delete=accept == Accept.NO_REGRETS, sync=sync)
168-
return str(branch)
175+
return Draft(str(branch))
169176

170177
def _prepare_prompt(
171178
self,
@@ -214,7 +221,7 @@ def _generate_change(
214221

215222
def finalize_draft(
216223
self, *, delete: bool = False, sync: bool = False
217-
) -> str:
224+
) -> Draft:
218225
branch = _Branch.active(self._repo)
219226
if not branch:
220227
raise RuntimeError("Not currently on a draft branch")
@@ -240,7 +247,7 @@ def finalize_draft(
240247
_logger.debug("Deleted branch %s.", branch)
241248

242249
_logger.info("Exited %s.", branch)
243-
return branch.name
250+
return Draft(branch.name)
244251

245252
def _create_branch(self, sync: bool) -> _Branch:
246253
if self._repo.head.is_detached:

tests/git_draft/drafter_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,15 @@ def test_finalize_keeps_changes(self) -> None:
200200
assert self._read("PROMPT") == "hello"
201201

202202
def test_finalize_and_sync(self) -> None:
203-
branch = self._drafter.generate_draft(
203+
draft = self._drafter.generate_draft(
204204
"hello",
205205
_SimpleBot.prompt(),
206206
accept=sut.Accept.CHECKOUT,
207207
)
208208
self._write("PROMPT", "a2")
209209
self._drafter.finalize_draft(sync=True)
210210
assert self._read("PROMPT") == "a2"
211-
commits = self._commits(branch)
211+
commits = self._commits(draft.branch_name)
212212
assert len(commits) == 3 # init, prompt, sync
213213
assert "sync" in commits[0].message
214214

0 commit comments

Comments
 (0)