Skip to content

Commit 9cc28f3

Browse files
authored
feat: add sync option to finalize (#60)
1 parent 38287ca commit 9cc28f3

File tree

8 files changed

+108
-81
lines changed

8 files changed

+108
-81
lines changed

README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ pipx install git-draft[openai]
2323

2424
* Mechanism for reporting feedback from a bot, and possibly allowing user to
2525
interactively respond.
26-
* Add configuration option to auto sync and `--no-sync` flag. Similar to reset.
27-
Also rename both options to `sync` and `reset`, this will make it more natural
28-
to support a similar config option for `accept`.
29-
* Add `--sync` `finalize` option which creates a additional commit when
30-
finalizing if any changes were added to the bot's output. This could be useful
31-
training data, showing what the bot did not get right.
32-
* Convenience `--accept` functionality for simple cases: checkout option which
33-
applies the changes, and finalizes the draft if specified multiple times. For
34-
example `git draft -aa add-test symbol=foo`
3526
* Support file rename tool.
3627
* https://stackoverflow.com/q/49853177/1062617
3728
* https://stackoverflow.com/q/6658313/1062617

src/git_draft/__main__.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ def callback(
5959
)
6060

6161
add_command("finalize", help="apply current draft to original branch")
62-
add_command("generate", help="start a new draft from a prompt")
62+
add_command("generate", help="create or update draft from a prompt")
6363
add_command("show-drafts", short="D", help="show draft history")
6464
add_command("show-prompts", short="P", help="show prompt history")
6565
add_command("show-templates", short="T", help="show template information")
6666

6767
parser.add_option(
6868
"-a",
6969
"--accept",
70-
help="apply generated changes",
70+
help="accept draft, may be repeated",
7171
action="count",
7272
)
7373
parser.add_option(
@@ -79,7 +79,7 @@ def callback(
7979
parser.add_option(
8080
"-d",
8181
"--delete",
82-
help="delete draft after finalizing or discarding",
82+
help="delete draft after finalizing",
8383
action="store_true",
8484
)
8585
parser.add_option(
@@ -101,12 +101,25 @@ def callback(
101101
action="store_true",
102102
)
103103

104+
parser.add_option(
105+
"--no-accept",
106+
help="do not update worktree from draft",
107+
dest="accept",
108+
action="store_const",
109+
const=0,
110+
)
104111
parser.add_option(
105112
"--no-reset",
106113
help="abort if there are any staged changes",
107114
dest="reset",
108115
action="store_false",
109116
)
117+
parser.add_option(
118+
"--no-sync",
119+
help="do not commit intermediate worktree changes",
120+
dest="sync",
121+
action="store_false",
122+
)
110123
parser.add_option(
111124
"--reset",
112125
help="reset index before generating a new draft",
@@ -209,12 +222,15 @@ def main() -> None: # noqa: PLR0912 PLR0915
209222
bot_name=opts.bot,
210223
prompt_transform=open_editor if editable else None,
211224
tool_visitors=[ToolPrinter()],
212-
reset=config.auto_reset if opts.reset is None else opts.reset,
213-
sync=opts.sync,
225+
reset=config.reset if opts.reset is None else opts.reset,
226+
sync=config.sync if opts.sync is None else opts.sync,
214227
)
215228
print(f"Generated change in {name}.")
216229
case "finalize":
217-
name = drafter.finalize_draft(delete=opts.delete)
230+
name = drafter.finalize_draft(
231+
delete=opts.delete,
232+
sync=config.sync if opts.sync is None else opts.sync,
233+
)
218234
print(f"Finalized {name}.")
219235
case "show-drafts":
220236
table = drafter.history_table(args[0] if args else None)

src/git_draft/common.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ def ensure_state_home() -> Path:
3838
class Config:
3939
"""Overall CLI configuration"""
4040

41-
log_level: int = logging.INFO
42-
auto_reset: bool = True
4341
bots: Sequence[BotConfig] = dataclasses.field(default_factory=lambda: [])
42+
log_level: int = logging.INFO
43+
reset: bool = True
44+
sync: bool = False
4445

4546
@staticmethod
4647
def folder_path() -> Path:

src/git_draft/drafter.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def generate_draft( # noqa: PLR0913
164164
if delta and accept.value >= Accept.CHECKOUT.value:
165165
delta.apply()
166166
if accept.value >= Accept.FINALIZE.value:
167-
self.finalize_draft(delete=accept == Accept.NO_REGRETS)
167+
self.finalize_draft(delete=accept == Accept.NO_REGRETS, sync=sync)
168168
return str(branch)
169169

170170
def _prepare_prompt(
@@ -212,18 +212,21 @@ def _generate_change(
212212
commit.hexsha, timedelta(seconds=walltime), action, self._repo
213213
)
214214

215-
def finalize_draft(self, *, delete: bool = False) -> str:
215+
def finalize_draft(
216+
self, *, delete: bool = False, sync: bool = False
217+
) -> str:
216218
branch = _Branch.active(self._repo)
217219
if not branch:
218220
raise RuntimeError("Not currently on a draft branch")
221+
self._stage_repo(sync)
219222

220223
with self._store.cursor() as cursor:
221224
rows = cursor.execute(
222225
sql("get-branch-by-suffix"), {"suffix": branch.suffix}
223226
)
224227
if not rows:
225228
raise RuntimeError("Unrecognized draft branch")
226-
[(origin_branch, origin_sha, sync_sha)] = rows
229+
[(origin_branch, origin_sha)] = rows
227230

228231
# We do a small dance to move back to the original branch, keeping the
229232
# draft branch untouched. See https://stackoverflow.com/a/15993574 for
@@ -246,7 +249,7 @@ def _create_branch(self, sync: bool) -> _Branch:
246249
origin_sha = self._repo.commit().hexsha
247250

248251
self._repo.git.checkout(detach=True)
249-
sync_sha = self._stage_repo(sync)
252+
self._stage_repo(sync)
250253
suffix = _Branch.new_suffix()
251254

252255
with self._store.cursor() as cursor:
@@ -257,7 +260,6 @@ def _create_branch(self, sync: bool) -> _Branch:
257260
"repo_path": self._repo.working_dir,
258261
"origin_branch": origin_branch,
259262
"origin_sha": origin_sha,
260-
"sync_sha": sync_sha,
261263
},
262264
)
263265

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
insert into branches (suffix, repo_path, origin_branch, origin_sha, sync_sha)
2-
values (:suffix, :repo_path, :origin_branch, :origin_sha, :sync_sha);
1+
insert into branches (suffix, repo_path, origin_branch, origin_sha)
2+
values (:suffix, :repo_path, :origin_branch, :origin_sha);

src/git_draft/queries/create-tables.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ create table if not exists branches (
33
repo_path text not null,
44
created_at timestamp default current_timestamp,
55
origin_branch text not null,
6-
origin_sha text not null,
7-
sync_sha text
6+
origin_sha text not null
87
) without rowid;
98

109
create table if not exists prompts (
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
select origin_branch, origin_sha, sync_sha
1+
select origin_branch, origin_sha
22
from branches
33
where suffix = :suffix;

0 commit comments

Comments
 (0)