Improve error handling in get_counts_for_file function to gracefully handle files that cannot be blamed.

This commit is contained in:
Paul Gauthier (aider) 2024-07-29 11:04:28 -03:00
parent d1abb85445
commit 7508b8c93c

View file

@ -47,7 +47,6 @@ def get_all_commit_hashes_between_tags(start_tag, end_tag=None):
return commit_hashes
def run(cmd):
try:
# Get all commit hashes since the specified tag
result = subprocess.run(
cmd,
@ -56,9 +55,6 @@ def run(cmd):
check=True
)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"Error: {e}", file=sys.stderr)
return
def get_commit_authors(commits):
commit_to_author = dict()
@ -97,6 +93,7 @@ def main():
print(f"\nAider wrote {aider_percentage:.0f}% of the code in this release ({aider_total}/{total_lines} lines).")
def get_counts_for_file(start_tag, end_tag, authors, fname):
try:
if end_tag:
text = run(['git', 'blame', f'{start_tag}..{end_tag}', '--', fname])
else:
@ -116,6 +113,9 @@ def get_counts_for_file(start_tag, end_tag, authors, fname):
line_counts[author] += 1
return dict(line_counts)
except subprocess.CalledProcessError:
print(f"Warning: Unable to blame file {fname}. It may have been added after {start_tag} or removed before {end_tag or 'HEAD'}.", file=sys.stderr)
return None
def get_all_tags_since(start_tag):
all_tags = run(['git', 'tag', '--sort=v:refname']).strip().split('\n')