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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ pipx install git-draft[openai]
* Add configuration option to auto sync and `--no-sync` flag. Similar to reset.
* Add "amend" commit when finalizing. This could be useful training data,
showing what the bot did not get right.
* Convenience functionality for simple cases: checkout option which applies the
changes, and finalizes the draft if specified multiple times. For example `git
draft -cc add-test symbol=foo`
* 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`
3 changes: 2 additions & 1 deletion src/git_draft/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging

from .bots import Action, Bot, Toolbox
from .bots import Action, Bot, Goal, Toolbox


__all__ = [
"Action",
"Bot",
"Goal",
"Toolbox",
]

Expand Down
35 changes: 22 additions & 13 deletions src/git_draft/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,35 @@ def on_list_files(
def on_read_file(
self, path: PurePosixPath, _contents: str | None, _reason: str | None
) -> None:
print(f"Reading {path}...")
print(f"Reading {path!r}...")

def on_write_file(
self, path: PurePosixPath, _contents: str, _reason: str | None
) -> None:
print(f"Updated {path}.")
print(f"Updated {path!r}.")

def on_delete_file(self, path: PurePosixPath, _reason: str | None) -> None:
print(f"Deleted {path}.")
print(f"Deleted {path!r}.")


def edit(path: Path, text: str | None = None) -> str | None:
def edit(*, path: Path | None = None, text: str | None = None) -> str:
if sys.stdin.isatty():
return open_editor(text or "", path)
else:
if text is not None:
with open(path, "w") as f:
f.write(text)
print(path)
return None
# We exit with a custom code to allow the caller to act accordingly.
# For example we can handle this from Vim by opening the returned path
# or text in a buffer, to then continue to another command on save.
# https://unix.stackexchange.com/q/604260
if path is None:
assert text, "Empty path and text"
print(text)
sys.exit(198)
else:
if text is not None:
with open(path, "w") as f:
f.write(text)
print(path)
sys.exit(199)


_PROMPT_PLACEHOLDER = "Enter your prompt here..."
Expand Down Expand Up @@ -185,8 +194,8 @@ def main() -> None:
prompt = TemplatedPrompt.parse(args[0], *args[1:])
elif opts.edit:
editable = False
prompt = open_editor(
drafter.latest_draft_prompt() or _PROMPT_PLACEHOLDER
prompt = edit(
text=drafter.latest_draft_prompt() or _PROMPT_PLACEHOLDER
)
else:
prompt = sys.stdin.read()
Expand Down Expand Up @@ -219,9 +228,9 @@ def main() -> None:
tpl = Template.find(name)
if opts.edit:
if tpl:
edit(tpl.local_path(), text=tpl.source)
edit(path=tpl.local_path(), text=tpl.source)
else:
edit(Template.local_path_for(name))
edit(path=Template.local_path_for(name))
else:
if not tpl:
raise ValueError(f"No template named {name!r}")
Expand Down