commit just the edited files, treat named files outside git as dirty

This commit is contained in:
Paul Gauthier 2023-08-18 07:05:48 -07:00
parent d89316147a
commit 389bf9310c
2 changed files with 41 additions and 18 deletions

View file

@ -479,7 +479,7 @@ class Coder:
if edited: if edited:
if self.repo and self.auto_commits and not self.dry_run: if self.repo and self.auto_commits and not self.dry_run:
saved_message = self.auto_commit() saved_message = self.auto_commit(fnames=edited)
elif hasattr(self.gpt_prompts, "files_content_gpt_edits_no_repo"): elif hasattr(self.gpt_prompts, "files_content_gpt_edits_no_repo"):
saved_message = self.gpt_prompts.files_content_gpt_edits_no_repo saved_message = self.gpt_prompts.files_content_gpt_edits_no_repo
else: else:
@ -709,27 +709,28 @@ class Coder:
def check_for_dirty_commit(self, path): def check_for_dirty_commit(self, path):
if not self.repo: if not self.repo:
return True return
if not self.dirty_commits: if not self.dirty_commits:
return True return
if not self.repo.is_dirty(path): if not self.repo.is_dirty(path):
return True return
fullp = self.repo.full_path(path)
if not fullp.stat().st_size:
return
self.io.tool_output(f"Committing {path} before applying edits.") self.io.tool_output(f"Committing {path} before applying edits.")
self.repo.repo.git.add(path) self.repo.repo.git.add(path)
self.need_commit_before_edits = True self.need_commit_before_edits = True
return True return
def allowed_to_edit(self, path): def allowed_to_edit(self, path):
full_path = self.abs_root_path(path) full_path = self.abs_root_path(path)
if self.repo: is_in_repo = self.repo.path_in_repo(path)
tracked_files = set(self.repo.get_tracked_files())
is_in_repo = path in tracked_files
if full_path in self.abs_fnames: if full_path in self.abs_fnames:
if self.check_for_dirty_commit(path): self.check_for_dirty_commit(path)
return full_path return full_path
return
if not Path(full_path).exists(): if not Path(full_path).exists():
if not self.io.confirm_ask(f"Allow creation of new file {path}?"): if not self.io.confirm_ask(f"Allow creation of new file {path}?"):
@ -756,7 +757,7 @@ class Coder:
self.repo.repo.git.add(full_path) self.repo.repo.git.add(full_path)
self.abs_fnames.add(full_path) self.abs_fnames.add(full_path)
if self.check_for_dirty_commit(path): self.check_for_dirty_commit(path)
return full_path return full_path
apply_update_errors = 0 apply_update_errors = 0
@ -864,9 +865,9 @@ class Coder:
context += "\n" + msg["role"].upper() + ": " + msg["content"] + "\n" context += "\n" + msg["role"].upper() + ": " + msg["content"] + "\n"
return context return context
def auto_commit(self): def auto_commit(self, edited):
context = self.get_context_from_history(self.cur_messages) context = self.get_context_from_history(self.cur_messages)
res = self.repo.commit(context=context, prefix="aider: ") res = self.repo.commit(fnames=edited, context=context, prefix="aider: ")
if res: if res:
commit_hash, commit_message = res commit_hash, commit_message = res
self.last_aider_commit_hash = commit_hash self.last_aider_commit_hash = commit_hash

View file

@ -49,8 +49,8 @@ class GitRepo:
self.repo = git.Repo(repo_paths.pop(), odbt=git.GitDB) self.repo = git.Repo(repo_paths.pop(), odbt=git.GitDB)
self.root = utils.safe_abs_path(self.repo.working_tree_dir) self.root = utils.safe_abs_path(self.repo.working_tree_dir)
def commit(self, context=None, prefix=None, message=None): def commit(self, fnames=None, context=None, prefix=None, message=None):
if not self.repo.is_dirty(): if not fnames and not self.repo.is_dirty():
return return
if message: if message:
@ -69,7 +69,16 @@ class GitRepo:
if context: if context:
full_commit_message += "\n\n# Aider chat conversation:\n\n" + context full_commit_message += "\n\n# Aider chat conversation:\n\n" + context
self.repo.git.commit("-a", "-m", full_commit_message, "--no-verify") cmd = ["-m", full_commit_message, "--no-verify"]
if fnames:
fnames = [str(self.full_path(fn)) for fn in fnames]
for fname in fnames:
self.repo.git.add(fname)
cmd += ["--"] + fnames
else:
cmd += ["-a"]
self.repo.git.commit(cmd)
commit_hash = self.repo.head.commit.hexsha[:7] commit_hash = self.repo.head.commit.hexsha[:7]
self.io.tool_output(f"Commit {commit_hash} {commit_message}") self.io.tool_output(f"Commit {commit_hash} {commit_message}")
@ -180,5 +189,18 @@ class GitRepo:
return res return res
def path_in_repo(self, path):
if not self.repo:
return
tracked_files = set(self.get_tracked_files())
return path in tracked_files
def full_path(self, path):
return (Path(self.root) / path).resolve()
def is_dirty(self, path=None): def is_dirty(self, path=None):
if path and not self.path_in_repo(path):
return True
return self.repo.is_dirty(path=path) return self.repo.is_dirty(path=path)