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
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ pipx install git-draft[openai]

* Mechanism for reporting feedback from a bot, and possibly allowing user to
interactively respond.
* Add configuration option to auto sync and `--no-sync` flag. Similar to reset.
Also rename both options to `sync` and `reset`, this will make it more natural
to support a similar config option for `accept`.
* Add `--sync` `finalize` option which creates a additional commit when
finalizing if any changes were added to the bot's output. This could be useful
training data, showing what the bot did not get right.
* Convenience `--accept` functionality for simple cases: checkout option which
applies the changes, and finalizes the draft if specified multiple times. For
example `git draft -aa add-test symbol=foo`
* Support file rename tool.
* https://stackoverflow.com/q/49853177/1062617
* https://stackoverflow.com/q/6658313/1062617
28 changes: 22 additions & 6 deletions src/git_draft/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ def callback(
)

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

parser.add_option(
"-a",
"--accept",
help="apply generated changes",
help="accept draft, may be repeated",
action="count",
)
parser.add_option(
Expand All @@ -79,7 +79,7 @@ def callback(
parser.add_option(
"-d",
"--delete",
help="delete draft after finalizing or discarding",
help="delete draft after finalizing",
action="store_true",
)
parser.add_option(
Expand All @@ -101,12 +101,25 @@ def callback(
action="store_true",
)

parser.add_option(
"--no-accept",
help="do not update worktree from draft",
dest="accept",
action="store_const",
const=0,
)
parser.add_option(
"--no-reset",
help="abort if there are any staged changes",
dest="reset",
action="store_false",
)
parser.add_option(
"--no-sync",
help="do not commit intermediate worktree changes",
dest="sync",
action="store_false",
)
parser.add_option(
"--reset",
help="reset index before generating a new draft",
Expand Down Expand Up @@ -209,12 +222,15 @@ def main() -> None: # noqa: PLR0912 PLR0915
bot_name=opts.bot,
prompt_transform=open_editor if editable else None,
tool_visitors=[ToolPrinter()],
reset=config.auto_reset if opts.reset is None else opts.reset,
sync=opts.sync,
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}.")
case "finalize":
name = drafter.finalize_draft(delete=opts.delete)
name = drafter.finalize_draft(
delete=opts.delete,
sync=config.sync if opts.sync is None else opts.sync,
)
print(f"Finalized {name}.")
case "show-drafts":
table = drafter.history_table(args[0] if args else None)
Expand Down
5 changes: 3 additions & 2 deletions src/git_draft/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def ensure_state_home() -> Path:
class Config:
"""Overall CLI configuration"""

log_level: int = logging.INFO
auto_reset: bool = True
bots: Sequence[BotConfig] = dataclasses.field(default_factory=lambda: [])
log_level: int = logging.INFO
reset: bool = True
sync: bool = False

@staticmethod
def folder_path() -> Path:
Expand Down
12 changes: 7 additions & 5 deletions src/git_draft/drafter.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def generate_draft( # noqa: PLR0913
if delta and accept.value >= Accept.CHECKOUT.value:
delta.apply()
if accept.value >= Accept.FINALIZE.value:
self.finalize_draft(delete=accept == Accept.NO_REGRETS)
self.finalize_draft(delete=accept == Accept.NO_REGRETS, sync=sync)
return str(branch)

def _prepare_prompt(
Expand Down Expand Up @@ -212,18 +212,21 @@ def _generate_change(
commit.hexsha, timedelta(seconds=walltime), action, self._repo
)

def finalize_draft(self, *, delete: bool = False) -> str:
def finalize_draft(
self, *, delete: bool = False, sync: bool = False
) -> str:
branch = _Branch.active(self._repo)
if not branch:
raise RuntimeError("Not currently on a draft branch")
self._stage_repo(sync)

with self._store.cursor() as cursor:
rows = cursor.execute(
sql("get-branch-by-suffix"), {"suffix": branch.suffix}
)
if not rows:
raise RuntimeError("Unrecognized draft branch")
[(origin_branch, origin_sha, sync_sha)] = rows
[(origin_branch, origin_sha)] = rows

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

self._repo.git.checkout(detach=True)
sync_sha = self._stage_repo(sync)
self._stage_repo(sync)
suffix = _Branch.new_suffix()

with self._store.cursor() as cursor:
Expand All @@ -257,7 +260,6 @@ def _create_branch(self, sync: bool) -> _Branch:
"repo_path": self._repo.working_dir,
"origin_branch": origin_branch,
"origin_sha": origin_sha,
"sync_sha": sync_sha,
},
)

Expand Down
4 changes: 2 additions & 2 deletions src/git_draft/queries/add-branch.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
insert into branches (suffix, repo_path, origin_branch, origin_sha, sync_sha)
values (:suffix, :repo_path, :origin_branch, :origin_sha, :sync_sha);
insert into branches (suffix, repo_path, origin_branch, origin_sha)
values (:suffix, :repo_path, :origin_branch, :origin_sha);
3 changes: 1 addition & 2 deletions src/git_draft/queries/create-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ create table if not exists branches (
repo_path text not null,
created_at timestamp default current_timestamp,
origin_branch text not null,
origin_sha text not null,
sync_sha text
origin_sha text not null
) without rowid;

create table if not exists prompts (
Expand Down
2 changes: 1 addition & 1 deletion src/git_draft/queries/get-branch-by-suffix.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
select origin_branch, origin_sha, sync_sha
select origin_branch, origin_sha
from branches
where suffix = :suffix;
Loading