Skip to content

Commit 3ba6d1c

Browse files
authored
feat: remove reason field (#96)
1 parent 3a1cf44 commit 3ba6d1c

File tree

5 files changed

+44
-76
lines changed

5 files changed

+44
-76
lines changed

src/git_draft/drafter.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ async def generate_draft(
202202
{
203203
"prompt_id": prompt_id,
204204
"tool": o.tool,
205-
"reason": o.reason,
206205
"details": json.dumps(o.details),
207206
"started_at": o.start,
208207
}
@@ -440,53 +439,43 @@ def __init__(self, progress: Progress) -> None:
440439
self.operations = list[_Operation]()
441440
self._progress = progress
442441

443-
def on_list_files(
444-
self, paths: Sequence[PurePosixPath], reason: str | None
445-
) -> None:
442+
def on_list_files(self, paths: Sequence[PurePosixPath]) -> None:
446443
count = len(paths)
447444
self._progress.report("Listed available files.", count=count)
448-
self._record(reason, "list_files", count=count)
445+
self._record("list_files", count=count)
449446

450-
def on_read_file(
451-
self, path: PurePosixPath, contents: str | None, reason: str | None
452-
) -> None:
447+
def on_read_file(self, path: PurePosixPath, contents: str | None) -> None:
453448
size = -1 if contents is None else len(contents)
454449
self._progress.report(f"Read {path}.", length=size)
455-
self._record(reason, "read_file", path=str(path), size=size)
450+
self._record("read_file", path=str(path), size=size)
456451

457-
def on_write_file(
458-
self, path: PurePosixPath, contents: str, reason: str | None
459-
) -> None:
452+
def on_write_file(self, path: PurePosixPath, contents: str) -> None:
460453
size = len(contents)
461454
self._progress.report(f"Wrote {path}.", length=size)
462-
self._record(reason, "write_file", path=str(path), size=size)
455+
self._record("write_file", path=str(path), size=size)
463456

464-
def on_delete_file(self, path: PurePosixPath, reason: str | None) -> None:
457+
def on_delete_file(self, path: PurePosixPath) -> None:
465458
self._progress.report(f"Deleted {path}.")
466-
self._record(reason, "delete_file", path=str(path))
459+
self._record("delete_file", path=str(path))
467460

468461
def on_rename_file(
469462
self,
470463
src_path: PurePosixPath,
471464
dst_path: PurePosixPath,
472-
reason: str | None,
473465
) -> None:
474466
self._progress.report(f"Renamed {src_path} to {dst_path}.")
475467
self._record(
476-
reason,
477468
"rename_file",
478469
src_path=str(src_path),
479470
dst_path=str(dst_path),
480471
)
481472

482473
def on_expose_files(self) -> None:
483474
self._progress.report("Exposed files.")
484-
self._record(None, "expose_files")
475+
self._record("expose_files")
485476

486-
def _record(self, reason: str | None, tool: str, **kwargs) -> None:
487-
op = _Operation(
488-
tool=tool, details=kwargs, reason=reason, start=datetime.now()
489-
)
477+
def _record(self, tool: str, **kwargs) -> None:
478+
op = _Operation(tool=tool, details=kwargs, start=datetime.now())
490479
_logger.debug("Recorded operation. [op=%s]", op)
491480
self.operations.append(op)
492481

@@ -497,7 +486,6 @@ class _Operation:
497486

498487
tool: str
499488
details: JSONObject
500-
reason: str | None
501489
start: datetime
502490

503491

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
insert into operations (prompt_id, tool, reason, details, started_at)
2-
values (:prompt_id, :tool, :reason, :details, :started_at)
1+
insert into operations (prompt_id, tool, details, started_at)
2+
values (:prompt_id, :tool, :details, :started_at)

src/git_draft/queries/create-tables.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ create table if not exists operations (
3434
id integer primary key,
3535
prompt_id integer not null,
3636
tool text not null,
37-
reason text,
3837
details text not null,
3938
started_at timestamp not null,
4039
foreign key (prompt_id) references actions (prompt_id) on delete cascade

src/git_draft/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Store:
2020
"""Lightweight sqlite wrapper"""
2121

22-
_name = "v3.sqlite3"
22+
_name = "v4.sqlite3"
2323

2424
def __init__(self, conn: sqlite3.Connection) -> None:
2525
self._connection = conn

src/git_draft/toolbox.py

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -36,58 +36,41 @@ class Toolbox:
3636
# feedback more than once during a bot action, which leads to a better
3737
# experience when used interactively.
3838

39-
# TODO: Remove all reason arguments. They are not currently used, and there
40-
# is no obvious use-case at the moment.
41-
4239
def __init__(self, visitors: Sequence[ToolVisitor] | None = None) -> None:
4340
self._visitors = visitors or []
4441

4542
def _dispatch(self, effect: Callable[[ToolVisitor], None]) -> None:
4643
for visitor in self._visitors:
4744
effect(visitor)
4845

49-
def list_files(self, reason: str | None = None) -> Sequence[PurePosixPath]:
46+
def list_files(self) -> Sequence[PurePosixPath]:
5047
paths = self._list()
51-
self._dispatch(lambda v: v.on_list_files(paths, reason))
48+
self._dispatch(lambda v: v.on_list_files(paths))
5249
return paths
5350

54-
def read_file(
55-
self,
56-
path: PurePosixPath,
57-
reason: str | None = None,
58-
) -> str | None:
51+
def read_file(self, path: PurePosixPath) -> str | None:
5952
try:
6053
contents = self._read(path)
6154
except FileNotFoundError:
6255
contents = None
63-
self._dispatch(lambda v: v.on_read_file(path, contents, reason))
56+
self._dispatch(lambda v: v.on_read_file(path, contents))
6457
return contents
6558

66-
def write_file(
67-
self,
68-
path: PurePosixPath,
69-
contents: str,
70-
reason: str | None = None,
71-
) -> None:
72-
self._dispatch(lambda v: v.on_write_file(path, contents, reason))
59+
def write_file(self, path: PurePosixPath, contents: str) -> None:
60+
self._dispatch(lambda v: v.on_write_file(path, contents))
7361
return self._write(path, contents)
7462

75-
def delete_file(
76-
self,
77-
path: PurePosixPath,
78-
reason: str | None = None,
79-
) -> None:
80-
self._dispatch(lambda v: v.on_delete_file(path, reason))
63+
def delete_file(self, path: PurePosixPath) -> None:
64+
self._dispatch(lambda v: v.on_delete_file(path))
8165
self._delete(path)
8266

8367
def rename_file(
8468
self,
8569
src_path: PurePosixPath,
8670
dst_path: PurePosixPath,
87-
reason: str | None = None,
8871
) -> None:
8972
"""Rename a single file"""
90-
self._dispatch(lambda v: v.on_rename_file(src_path, dst_path, reason))
73+
self._dispatch(lambda v: v.on_rename_file(src_path, dst_path))
9174
self._rename(src_path, dst_path)
9275

9376
def expose_files(
@@ -134,26 +117,25 @@ class ToolVisitor(Protocol):
134117
"""Tool usage hook"""
135118

136119
def on_list_files(
137-
self, paths: Sequence[PurePosixPath], reason: str | None
120+
self, paths: Sequence[PurePosixPath]
138121
) -> None: ... # pragma: no cover
139122

140123
def on_read_file(
141-
self, path: PurePosixPath, contents: str | None, reason: str | None
124+
self, path: PurePosixPath, contents: str | None
142125
) -> None: ... # pragma: no cover
143126

144127
def on_write_file(
145-
self, path: PurePosixPath, contents: str, reason: str | None
128+
self, path: PurePosixPath, contents: str
146129
) -> None: ... # pragma: no cover
147130

148131
def on_delete_file(
149-
self, path: PurePosixPath, reason: str | None
132+
self, path: PurePosixPath
150133
) -> None: ... # pragma: no cover
151134

152135
def on_rename_file(
153136
self,
154137
src_path: PurePosixPath,
155138
dst_path: PurePosixPath,
156-
reason: str | None,
157139
) -> None: ... # pragma: no cover
158140

159141
def on_expose_files(self) -> None: ... # pragma: no cover
@@ -266,24 +248,6 @@ def _write(self, path: PurePosixPath, contents: str) -> None:
266248
temp.close()
267249
self._write_from_disk(path, Path(temp.name))
268250

269-
@override
270-
@contextlib.contextmanager
271-
def _expose(self) -> Iterator[Path]:
272-
tree_sha = self.tree_sha()
273-
commit_sha = self._repo.git(
274-
"commit-tree", "-m", "draft! worktree", tree_sha
275-
).stdout
276-
with tempfile.TemporaryDirectory() as path_str:
277-
try:
278-
self._repo.git(
279-
"worktree", "add", "--detach", path_str, commit_sha
280-
)
281-
path = Path(path_str)
282-
yield path
283-
self._sync_updates(worktree_path=path)
284-
finally:
285-
self._repo.git("worktree", "remove", "-f", path_str)
286-
287251
def _write_from_disk(
288252
self, path: PurePosixPath, contents_path: Path
289253
) -> None:
@@ -300,6 +264,23 @@ def _write_from_disk(
300264
def _delete(self, path: PurePosixPath) -> None:
301265
self._tree_updates.append(_DeleteBlob(path))
302266

267+
@override
268+
@contextlib.contextmanager
269+
def _expose(self) -> Iterator[Path]:
270+
commit_sha = self._repo.git(
271+
"commit-tree", "-m", "draft! worktree", self.tree_sha()
272+
).stdout
273+
with tempfile.TemporaryDirectory() as path_str:
274+
try:
275+
self._repo.git(
276+
"worktree", "add", "--detach", path_str, commit_sha
277+
)
278+
path = Path(path_str)
279+
yield path
280+
self._sync_updates(worktree_path=path)
281+
finally:
282+
self._repo.git("worktree", "remove", "-f", path_str)
283+
303284

304285
class _TreeUpdate:
305286
"""Generic tree update"""

0 commit comments

Comments
 (0)