|
1 |
| -""" |
2 |
| -Functions related to the 'Suggest' section. |
3 |
| -""" |
4 |
| - |
5 |
| -from collections import Counter |
| 1 | +import subprocess |
| 2 | +from collections import defaultdict |
6 | 3 |
|
7 | 4 | from git_py_stats.git_operations import run_git_command
|
8 | 5 |
|
9 |
| - |
10 |
| -def code_reviewers() -> None: |
| 6 | +def suggest_reviewers() -> None: |
11 | 7 | """
|
12 | 8 | Suggests potential code reviewers based on commit history.
|
13 | 9 | """
|
14 |
| - cmd = ['git', 'log', '--format=%ae', '--no-merges'] |
15 |
| - output = run_git_command(cmd) |
16 |
| - |
17 |
| - if output: |
18 |
| - emails = output.split('\n') |
19 |
| - counter = Counter(emails) |
20 |
| - reviewers = [email for email, count in counter.items()] |
21 |
| - |
22 |
| - if reviewers: |
| 10 | + |
| 11 | + # Construct the git log command with all options. Original command is: |
| 12 | + # git -c log.showSignature=false log --use-mailmap $_merges "$_since" "$_until" \ |
| 13 | + # --pretty=%aN $_log_options $_pathspec | head -n 100 | sort | uniq -c \ |
| 14 | + # | sort -nr |
| 15 | + # Then some LC_ALL portion which is currently not important |
| 16 | + # Then pipe it all into column -t -s |
| 17 | + # |
| 18 | + # For now, let's just hardcode some of this stuff |
| 19 | + cmd = ['git', '-c', 'log.showSignature=false', 'log', '--use-mailmap', '--no-merges', '--pretty=%aN'] |
| 20 | + |
| 21 | + try: |
| 22 | + # Execute the git command and get the output |
| 23 | + output = run_git_command(cmd) |
| 24 | + |
| 25 | + # Check if output is empty |
| 26 | + if not output: |
| 27 | + print('No data available.') |
| 28 | + return |
| 29 | + |
| 30 | + # Split the output into lines (each line is a commit author) |
| 31 | + lines = output.splitlines() |
| 32 | + |
| 33 | + # Mimic "head -n 100" |
| 34 | + head_lines = lines[:100] |
| 35 | + |
| 36 | + # Mimic "sort" |
| 37 | + sorted_lines = sorted(head_lines) |
| 38 | + |
| 39 | + # Mimic "uniq -c" |
| 40 | + counted_authors = [] |
| 41 | + current_author = None |
| 42 | + current_count = 0 |
| 43 | + |
| 44 | + # Iterate over sorted lines and count consecutive duplicates |
| 45 | + for author in sorted_lines: |
| 46 | + if author == current_author: |
| 47 | + current_count += 1 |
| 48 | + else: |
| 49 | + if current_author is not None: |
| 50 | + counted_authors.append((current_count, current_author)) |
| 51 | + current_author = author |
| 52 | + current_count = 1 |
| 53 | + |
| 54 | + # Append the last counted author |
| 55 | + if current_author is not None: |
| 56 | + counted_authors.append((current_count, current_author)) |
| 57 | + |
| 58 | + # Sort by count descending, and by name ascending |
| 59 | + sorted_authors = sorted(counted_authors, key=lambda x: (-x[0], x[1])) |
| 60 | + |
| 61 | + # Print results similar to "column -t -s" |
| 62 | + if sorted_authors: |
23 | 63 | print("Suggested code reviewers based on git history:")
|
24 |
| - for reviewer in reviewers: |
25 |
| - print(reviewer) |
| 64 | + for count, author in sorted_authors: |
| 65 | + # Pad output with 7 to match original code's behavior |
| 66 | + # TODO: Bit of a magic number we can remove later |
| 67 | + print(f"{count:7} {author}") |
26 | 68 | else:
|
27 |
| - print('No potential reviewers found. Consider using a larger dataset.') |
28 |
| - else: |
29 |
| - print('No data available.') |
| 69 | + print("No potential reviewers found.") |
| 70 | + |
| 71 | + except subprocess.CalledProcessError as e: |
| 72 | + print(f"Error executing git command: {e}") |
30 | 73 |
|
0 commit comments