From 7508b8c93c217881fc43fbdb9b998b7a4fe3e5a6 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Mon, 29 Jul 2024 11:04:28 -0300 Subject: [PATCH] Improve error handling in `get_counts_for_file` function to gracefully handle files that cannot be blamed. --- scripts/blame.py | 60 ++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/scripts/blame.py b/scripts/blame.py index 61150ee25..7b40a34a0 100755 --- a/scripts/blame.py +++ b/scripts/blame.py @@ -47,18 +47,14 @@ 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, - capture_output=True, - text=True, - check=True - ) - return result.stdout - except subprocess.CalledProcessError as e: - print(f"Error: {e}", file=sys.stderr) - return + # Get all commit hashes since the specified tag + result = subprocess.run( + cmd, + capture_output=True, + text=True, + check=True + ) + return result.stdout def get_commit_authors(commits): 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).") def get_counts_for_file(start_tag, end_tag, authors, fname): - if end_tag: - text = run(['git', 'blame', f'{start_tag}..{end_tag}', '--', fname]) - else: - text = run(['git', 'blame', f'{start_tag}..HEAD', '--', fname]) - if not text: - return None - text = text.splitlines() - line_counts = defaultdict(int) - for line in text: - if line.startswith('^'): - continue - hsh = line[:hash_len] - author = authors.get(hsh, "Unknown") - # Normalize author names with "(aider)" to a single format - if "(aider)" in author.lower(): - author = re.sub(r'\s*\(aider\)', ' (aider)', author, flags=re.IGNORECASE) - line_counts[author] += 1 + try: + if end_tag: + text = run(['git', 'blame', f'{start_tag}..{end_tag}', '--', fname]) + else: + text = run(['git', 'blame', f'{start_tag}..HEAD', '--', fname]) + if not text: + return None + text = text.splitlines() + line_counts = defaultdict(int) + for line in text: + if line.startswith('^'): + continue + hsh = line[:hash_len] + author = authors.get(hsh, "Unknown") + # Normalize author names with "(aider)" to a single format + if "(aider)" in author.lower(): + 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): all_tags = run(['git', 'tag', '--sort=v:refname']).strip().split('\n')