@@ -59,6 +59,7 @@ class _Toolbox(Toolbox):
5959 def __init__ (self , repo : git .Repo , hook : OperationHook | None ) -> None :
6060 super ().__init__ (hook )
6161 self ._repo = repo
62+ self ._written = set [str ]()
6263
6364 @override
6465 def _list (self ) -> Sequence [PurePosixPath ]:
@@ -72,17 +73,28 @@ def _read(self, path: PurePosixPath) -> str:
7273
7374 @override
7475 def _write (self , path : PurePosixPath , contents : str ) -> None :
76+ self ._written .add (str (path ))
7577 # Update the index without touching the worktree.
7678 # https://stackoverflow.com/a/25352119
7779 with tempfile .NamedTemporaryFile (delete_on_close = False ) as temp :
7880 temp .write (contents .encode ("utf8" ))
7981 temp .close ()
80- sha = self ._repo .git .hash_object ("-w" , "--path" , path , temp .name )
82+ sha = self ._repo .git .hash_object ("-w" , temp .name , path = path )
8183 mode = 644 # TODO: Read from original file if it exists.
8284 self ._repo .git .update_index (
83- "--add" , "--cacheinfo" , f"{ mode } ,{ sha } ,{ path } "
85+ f"{ mode } ,{ sha } ,{ path } " , add = True , cacheinfo = True
8486 )
8587
88+ def update_index (self ) -> None :
89+ diff = self ._repo .git .diff (name_only = True , cached = True )
90+ untouched = [
91+ path
92+ for path in diff .splitlines ()
93+ if path and path not in self ._written
94+ ]
95+ if untouched :
96+ self ._repo .git .reset ("--" , * untouched )
97+
8698
8799class Drafter :
88100 """Draft state orchestrator"""
@@ -151,6 +163,7 @@ def generate_draft(
151163 action = bot .act (prompt_contents , toolbox )
152164 end_time = time .perf_counter ()
153165
166+ toolbox .update_index ()
154167 title = action .title
155168 if not title :
156169 title = _default_title (prompt_contents )
@@ -197,7 +210,7 @@ def _create_branch(self, sync: bool) -> _Branch:
197210 origin_branch = self ._repo .active_branch .name
198211 origin_sha = self ._repo .commit ().hexsha
199212
200- self ._repo .git .checkout ("-- detach" )
213+ self ._repo .git .checkout (detach = True )
201214 sync_sha = self ._stage_changes (sync )
202215 suffix = _Branch .new_suffix ()
203216
@@ -248,7 +261,7 @@ def _exit_draft(self, apply: bool, delete=False) -> None:
248261 # We do a small dance to move back to the original branch, keeping the
249262 # draft branch untouched. See https://stackoverflow.com/a/15993574 for
250263 # the inspiration.
251- self ._repo .git .checkout ("-- detach" )
264+ self ._repo .git .checkout (detach = True )
252265 self ._repo .git .reset ("--mixed" if apply else "--hard" , origin_branch )
253266 self ._repo .git .checkout (origin_branch )
254267
0 commit comments