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,18 +47,14 @@ def get_all_commit_hashes_between_tags(start_tag, end_tag=None):
return commit_hashes return commit_hashes
def run(cmd): def run(cmd):
try: # Get all commit hashes since the specified tag
# Get all commit hashes since the specified tag result = subprocess.run(
result = subprocess.run( cmd,
cmd, capture_output=True,
capture_output=True, text=True,
text=True, check=True
check=True )
) return result.stdout
return result.stdout
except subprocess.CalledProcessError as e:
print(f"Error: {e}", file=sys.stderr)
return
def get_commit_authors(commits): def get_commit_authors(commits):
commit_to_author = dict() commit_to_author = dict()
@ -97,25 +93,29 @@ def main():
print(f"\nAider wrote {aider_percentage:.0f}% of the code in this release ({aider_total}/{total_lines} lines).") 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): def get_counts_for_file(start_tag, end_tag, authors, fname):
if end_tag: try:
text = run(['git', 'blame', f'{start_tag}..{end_tag}', '--', fname]) if end_tag:
else: text = run(['git', 'blame', f'{start_tag}..{end_tag}', '--', fname])
text = run(['git', 'blame', f'{start_tag}..HEAD', '--', fname]) else:
if not text: text = run(['git', 'blame', f'{start_tag}..HEAD', '--', fname])
return None if not text:
text = text.splitlines() return None
line_counts = defaultdict(int) text = text.splitlines()
for line in text: line_counts = defaultdict(int)
if line.startswith('^'): for line in text:
continue if line.startswith('^'):
hsh = line[:hash_len] continue
author = authors.get(hsh, "Unknown") hsh = line[:hash_len]
# Normalize author names with "(aider)" to a single format author = authors.get(hsh, "Unknown")
if "(aider)" in author.lower(): # Normalize author names with "(aider)" to a single format
author = re.sub(r'\s*\(aider\)', ' (aider)', author, flags=re.IGNORECASE) if "(aider)" in author.lower():
line_counts[author] += 1 author = re.sub(r'\s*\(aider\)', ' (aider)', author, flags=re.IGNORECASE)
line_counts[author] += 1
return dict(line_counts) 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): def get_all_tags_since(start_tag):
all_tags = run(['git', 'tag', '--sort=v:refname']).strip().split('\n') all_tags = run(['git', 'tag', '--sort=v:refname']).strip().split('\n')