catch git errors for commit and diff

This commit is contained in:
Paul Gauthier 2024-09-02 11:02:33 -07:00
parent 2cfdd7ab5b
commit bd012d63e9

View file

@ -121,7 +121,10 @@ class GitRepo:
if fnames: if fnames:
fnames = [str(self.abs_root_path(fn)) for fn in fnames] fnames = [str(self.abs_root_path(fn)) for fn in fnames]
for fname in fnames: for fname in fnames:
self.repo.git.add(fname) try:
self.repo.git.add(fname)
except ANY_GIT_ERROR as err:
self.io.tool_error(f"Unable to add {fname}: {err}")
cmd += ["--"] + fnames cmd += ["--"] + fnames
else: else:
cmd += ["-a"] cmd += ["-a"]
@ -137,25 +140,27 @@ class GitRepo:
original_auther_name_env = os.environ.get("GIT_AUTHOR_NAME") original_auther_name_env = os.environ.get("GIT_AUTHOR_NAME")
os.environ["GIT_AUTHOR_NAME"] = committer_name os.environ["GIT_AUTHOR_NAME"] = committer_name
self.repo.git.commit(cmd) try:
commit_hash = self.get_head_commit_sha(short=True) self.repo.git.commit(cmd)
self.io.tool_output(f"Commit {commit_hash} {commit_message}", bold=True) commit_hash = self.get_head_commit_sha(short=True)
self.io.tool_output(f"Commit {commit_hash} {commit_message}", bold=True)
return commit_hash, commit_message
except ANY_GIT_ERROR as err:
self.io.tool_error(f"Unable to commit: {err}")
finally:
# Restore the env
# Restore the env if self.attribute_committer:
if original_committer_name_env is not None:
os.environ["GIT_COMMITTER_NAME"] = original_committer_name_env
else:
del os.environ["GIT_COMMITTER_NAME"]
if self.attribute_committer: if aider_edits and self.attribute_author:
if original_committer_name_env is not None: if original_auther_name_env is not None:
os.environ["GIT_COMMITTER_NAME"] = original_committer_name_env os.environ["GIT_AUTHOR_NAME"] = original_auther_name_env
else: else:
del os.environ["GIT_COMMITTER_NAME"] del os.environ["GIT_AUTHOR_NAME"]
if aider_edits and self.attribute_author:
if original_auther_name_env is not None:
os.environ["GIT_AUTHOR_NAME"] = original_auther_name_env
else:
del os.environ["GIT_AUTHOR_NAME"]
return commit_hash, commit_message
def get_rel_repo_dir(self): def get_rel_repo_dir(self):
try: try:
@ -210,7 +215,7 @@ class GitRepo:
current_branch_has_commits = any(commits) current_branch_has_commits = any(commits)
except ANY_GIT_ERROR: except ANY_GIT_ERROR:
pass pass
except TypeError: except (TypeError,) + ANY_GIT_ERROR:
pass pass
if not fnames: if not fnames:
@ -221,18 +226,21 @@ class GitRepo:
if not self.path_in_repo(fname): if not self.path_in_repo(fname):
diffs += f"Added {fname}\n" diffs += f"Added {fname}\n"
if current_branch_has_commits: try:
args = ["HEAD", "--"] + list(fnames) if current_branch_has_commits:
diffs += self.repo.git.diff(*args) args = ["HEAD", "--"] + list(fnames)
diffs += self.repo.git.diff(*args)
return diffs
wd_args = ["--"] + list(fnames)
index_args = ["--cached"] + wd_args
diffs += self.repo.git.diff(*index_args)
diffs += self.repo.git.diff(*wd_args)
return diffs return diffs
except ANY_GIT_ERROR as err:
wd_args = ["--"] + list(fnames) self.io.tool_error(f"Unable to diff: {err}")
index_args = ["--cached"] + wd_args
diffs += self.repo.git.diff(*index_args)
diffs += self.repo.git.diff(*wd_args)
return diffs
def diff_commits(self, pretty, from_commit, to_commit): def diff_commits(self, pretty, from_commit, to_commit):
args = [] args = []