Skip to content

Commit f2b4a94

Browse files
committed
Autoformat with Black and lint with Ruff
* Adjust linter from Flake8 to Ruff. Mostly because Flake8 wasn't adhering to pyproject.toml, and Ruff worked without using a wrapper. * Autoformatted everything with Black * Tweaked files accordingly for CI
1 parent a9a4b5b commit f2b4a94

17 files changed

+182
-249
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ jobs:
2727
- name: Install dependencies
2828
run: |
2929
python -m pip install --upgrade pip
30-
pip install flake8 black
30+
pip install ruff black
3131
32-
- name: Lint with Flake8
32+
- name: Lint with Ruff
3333
run: |
34-
flake8 git_py_stats
34+
ruff git_py_stats
3535
3636
- name: Check formatting with Black
3737
run: |

git_py_stats/arg_parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from argparse import ArgumentParser, Namespace
1010
from typing import List, Optional
1111

12+
1213
def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
1314
"""
1415
Parse command-line arguments and return them.
@@ -23,10 +24,10 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
2324
args = parse_arguments(['--detailed-git-stats'])
2425
print(args.detailed_git_stats) # True
2526
"""
26-
27+
2728
parser = ArgumentParser(
2829
description="Git Py Stats - A Python Implementation of Git Quick Stats.",
29-
allow_abbrev=False, # Force users to be explicit. Makes testing sane.
30+
allow_abbrev=False, # Force users to be explicit. Makes testing sane.
3031
)
3132

3233
# Generate Options

git_py_stats/generate_cmds.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@
55
import collections
66
import csv
77
import json
8-
import os
9-
import re
108
from typing import Optional, Dict, Any, List, Union
119
from datetime import datetime, timedelta
1210

1311
from git_py_stats.git_operations import run_git_command
1412

1513

1614
# TODO: We should really refactor this; It's huge
17-
def detailed_git_stats(
18-
config: Dict[str, Union[str, int]],
19-
branch: Optional[str] = None
20-
) -> None:
15+
def detailed_git_stats(config: Dict[str, Union[str, int]], branch: Optional[str] = None) -> None:
2116
"""
2217
Displays detailed contribution stats by author.
2318
@@ -38,7 +33,6 @@ def detailed_git_stats(
3833

3934
# Variables to track current commit metadata
4035
current_author = ""
41-
current_email = ""
4236
current_date = 0
4337

4438
# Grab the config options from our config.py.
@@ -100,7 +94,6 @@ def detailed_git_stats(
10094
# Commit metadata
10195
commit_hash, author_name, author_email, date_raw = parts
10296
current_author = author_name
103-
current_email = author_email
10497
current_date = int(date_raw.split()[0])
10598

10699
# Initialize stats for the current author if not already done
@@ -175,9 +168,7 @@ def detailed_git_stats(
175168
)
176169

177170
# Calculate percentages
178-
insertions_pct = (
179-
(insertions / total_insertions * 100) if total_insertions else 0
180-
)
171+
insertions_pct = (insertions / total_insertions * 100) if total_insertions else 0
181172
deletions_pct = (deletions / total_deletions * 100) if total_deletions else 0
182173
files_pct = (files / total_files_changed * 100) if total_files_changed else 0
183174
commits_pct = (commits / total_commits * 100) if total_commits else 0
@@ -195,17 +186,14 @@ def detailed_git_stats(
195186
print(f" last commit: {last_commit}\n")
196187

197188
# Perform final calculation of stats
198-
print(f" total:")
189+
print(" total:")
199190
print(f" insertions: {total_insertions:<6} (100%)")
200191
print(f" deletions: {total_deletions:<6} (100%)")
201192
print(f" files: {total_files_changed:<6} (100%)")
202193
print(f" commits: {total_commits:<6} (100%)\n")
203194

204195

205-
def changelogs(
206-
config: Dict[str, Union[str, int]],
207-
author: Optional[str] = None
208-
) -> None:
196+
def changelogs(config: Dict[str, Union[str, int]], author: Optional[str] = None) -> None:
209197
"""
210198
Shows commit messages grouped by date for the last 'limit' dates
211199
where commits occurred.
@@ -234,8 +222,8 @@ def changelogs(
234222
limit = int(config.get("limit", 10))
235223

236224
# Original git command:
237-
# git -c log.showSignature=false log --use-mailmap $_merges --format="%cd" --date=short "${_author}"
238-
# "$_since" "$_until" $_log_options $_pathspec
225+
# git -c log.showSignature=false log --use-mailmap $_merges --format="%cd"
226+
# --date=short "${_author}" "$_since" "$_until" $_log_options $_pathspec
239227
cmd = [
240228
"git",
241229
"-c",
@@ -371,7 +359,7 @@ def my_daily_status(config: Dict[str, Union[str, int]]) -> None:
371359
today = datetime.now().strftime("%Y-%m-%d")
372360
since = f"--since={today}T00:00:00"
373361
until = f"--until={today}T23:59:59"
374-
362+
375363
# Build the final git log command
376364
log_cmd = [
377365
"git",
@@ -387,7 +375,7 @@ def my_daily_status(config: Dict[str, Union[str, int]]) -> None:
387375
"--pretty=%H", # Output only commit hashes
388376
log_options,
389377
]
390-
378+
391379
# Remove any empty space from the log_cmd
392380
log_cmd = [arg for arg in log_cmd if arg]
393381
# Execute the git log command

git_py_stats/interactive_mode.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,26 @@ def handle_interactive_mode(config: Dict[str, Union[str, int]]) -> None:
2323
"""
2424
interactive_map = {
2525
"1": lambda: generate_cmds.detailed_git_stats(config),
26-
"2": lambda: generate_cmds.detailed_git_stats(
27-
config, input("Enter branch name: ")
28-
),
26+
"2": lambda: generate_cmds.detailed_git_stats(config, input("Enter branch name: ")),
2927
"3": lambda: generate_cmds.changelogs(config),
30-
"4": lambda: generate_cmds.changelogs(
31-
config, input("Enter author name: ")
32-
),
28+
"4": lambda: generate_cmds.changelogs(config, input("Enter author name: ")),
3329
"5": lambda: generate_cmds.my_daily_status(config),
3430
"6": lambda: generate_cmds.output_daily_stats_csv(config),
3531
"7": lambda: generate_cmds.save_git_log_output_json(config),
3632
"8": lambda: list_cmds.branch_tree(config),
3733
"9": list_cmds.branches_by_date,
3834
"10": lambda: list_cmds.contributors(config),
39-
"11": lambda: list_cmds.new_contributors(
40-
config, input("Enter cutoff date (YYYY-MM-DD): ")
41-
),
35+
"11": lambda: list_cmds.new_contributors(config, input("Enter cutoff date (YYYY-MM-DD): ")),
4236
"12": lambda: list_cmds.git_commits_per_author(config),
4337
"13": lambda: list_cmds.git_commits_per_date(config),
4438
"14": lambda: list_cmds.git_commits_per_month(config),
4539
"15": lambda: list_cmds.git_commits_per_year(config),
4640
"16": lambda: list_cmds.git_commits_per_weekday(config),
47-
"17": lambda: list_cmds.git_commits_per_weekday(
48-
config, input("Enter author name: ")
49-
),
41+
"17": lambda: list_cmds.git_commits_per_weekday(config, input("Enter author name: ")),
5042
"18": lambda: list_cmds.git_commits_per_hour(config),
51-
"19": lambda: list_cmds.git_commits_per_hour(
52-
config, input("Enter author name: ")
53-
),
43+
"19": lambda: list_cmds.git_commits_per_hour(config, input("Enter author name: ")),
5444
"20": lambda: list_cmds.git_commits_per_timezone(config),
55-
"21": lambda: list_cmds.git_commits_per_timezone(
56-
config, input("Enter author name: ")
57-
),
45+
"21": lambda: list_cmds.git_commits_per_timezone(config, input("Enter author name: ")),
5846
"22": lambda: suggest_cmds.suggest_reviewers(config),
5947
}
6048

git_py_stats/list_cmds.py

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import collections
66
import re
7-
from datetime import datetime, timezone
7+
from datetime import datetime
88
from typing import Dict, Union, Optional
99

1010
from git_py_stats.git_operations import run_git_command
@@ -13,7 +13,7 @@
1313
def branch_tree(config: Dict[str, Union[str, int]]) -> None:
1414
"""
1515
Displays a visual graph of recent commits across all branches.
16-
16+
1717
Args:
1818
config: Dict[str, Union[str, int]]: Config dictionary holding env vars.
1919
@@ -24,19 +24,22 @@ def branch_tree(config: Dict[str, Union[str, int]]) -> None:
2424
# Grab the config options from our config.py.
2525
# config.py should give fallbacks for these, but for sanity, lets
2626
# also provide some defaults just in case.
27-
merges = config.get("merges", "--no-merges")
2827
since = config.get("since", "")
2928
until = config.get("until", "")
3029
log_options = config.get("log_options", "")
3130
limit = config.get("limit", 10)
3231

3332
# Format string for git --format so it gets interpreted correctly
34-
format_str = "--format=--+ Commit: %h%n | Date: %aD (%ar)%n | Message: %s %d%n + Author: %aN %n"
33+
format_str = (
34+
"--format=--+ Commit: %h%n | Date: %aD (%ar)%n | "
35+
"Message: %s %d%n + Author: %aN %n"
36+
)
3537

3638
# Original command:
3739
# git -c log.showSignature=false log --use-mailmap --graph --abbrev-commit \
3840
# "$_since" "$_until" --decorate \
39-
# --format=format:'--+ Commit: %h %n | Date: %aD (%ar) %n'' | Message: %s %d %n'' + Author: %aN %n' \
41+
# --format=format:'--+ Commit: %h %n | Date: %aD (%ar) %n'' \
42+
# | Message: %s %d %n'' + Author: %aN %n' \
4043
# --all $_log_options | head -n $((_limit*5))
4144
cmd = [
4245
"git",
@@ -69,9 +72,9 @@ def branch_tree(config: Dict[str, Union[str, int]]) -> None:
6972
for line in limited_lines:
7073
print(f"{line}")
7174

72-
commit_count = sum(
73-
1 for line in limited_lines if line.strip().startswith("--+ Commit:")
74-
)
75+
# commit_count = sum(
76+
# 1 for line in limited_lines if line.strip().startswith("--+ Commit:")
77+
# )
7578
else:
7679
print("No data available.")
7780

@@ -175,19 +178,14 @@ def contributors(config: Dict[str, Union[str, int]]) -> None:
175178
limited_authors = sorted_authors[:limit]
176179

177180
# Number the authors similar to 'cat -n' and print
178-
numbered_authors = [
179-
f"{idx + 1} {author}" for idx, author in enumerate(limited_authors)
180-
]
181+
numbered_authors = [f"{idx + 1} {author}" for idx, author in enumerate(limited_authors)]
181182
for author in numbered_authors:
182183
print(f"\t{author}")
183184
else:
184185
print("No contributors found.")
185186

186187

187-
def new_contributors(
188-
config: Dict[str, Union[str, int]],
189-
new_date: str
190-
) -> None:
188+
def new_contributors(config: Dict[str, Union[str, int]], new_date: str) -> None:
191189
"""
192190
Lists all new contributors to a repo since the specified date.
193191
@@ -248,10 +246,7 @@ def new_contributors(
248246
email, timestamp = line.split("|")
249247
timestamp = int(timestamp)
250248
# If the contributor is not in the dictionary or the current timestamp is earlier
251-
if (
252-
email not in contributors_dict
253-
or timestamp < contributors_dict[email]
254-
):
249+
if email not in contributors_dict or timestamp < contributors_dict[email]:
255250
contributors_dict[email] = timestamp
256251
except ValueError:
257252
continue # Skip lines that don't match format
@@ -298,9 +293,7 @@ def new_contributors(
298293
# and print all of this out
299294
if new_contributors_list:
300295
print(f"New contributors since {new_date}:\n")
301-
sorted_new_contributors = sorted(
302-
new_contributors_list, key=lambda x: (x[0], x[1])
303-
)
296+
sorted_new_contributors = sorted(new_contributors_list, key=lambda x: (x[0], x[1]))
304297
for idx, (name, email) in enumerate(sorted_new_contributors, 1):
305298
if name:
306299
print(f"{name} <{email}>")
@@ -315,7 +308,7 @@ def new_contributors(
315308
def git_commits_per_author(config: Dict[str, Union[str, int]]) -> None:
316309
"""
317310
Shows the number of commits per author.
318-
311+
319312
Args:
320313
config: Dict[str, Union[str, int]]: Config dictionary holding env vars.
321314
@@ -443,7 +436,7 @@ def extract_name(author_info: str) -> Optional[str]:
443436
def git_commits_per_date(config: Dict[str, Union[str, int]]) -> None:
444437
"""
445438
Displays commits grouped by date.
446-
439+
447440
Args:
448441
config: Dict[str, Union[str, int]]: Config dictionary holding env vars.
449442
@@ -575,9 +568,6 @@ def git_commits_per_month(config: Dict[str, Union[str, int]]) -> None:
575568
if month in commit_counts:
576569
commit_counts[month] += 1
577570

578-
# Calculate total commits
579-
total_commits = sum(commit_counts.values())
580-
581571
# Determine the maximum count to set the scaling factor
582572
max_count = max(commit_counts.values()) if commit_counts else 0
583573

@@ -610,7 +600,7 @@ def git_commits_per_month(config: Dict[str, Union[str, int]]) -> None:
610600
def git_commits_per_year(config: Dict[str, Union[str, int]]) -> None:
611601
"""
612602
Displays commits grouped by year.
613-
603+
614604
Args:
615605
config: Dict[str, Union[str, int]]: Config dictionary holding env vars.
616606
@@ -681,10 +671,6 @@ def git_commits_per_year(config: Dict[str, Union[str, int]]) -> None:
681671
max_count = max(commit_counts.values())
682672
scaling_factor = (max_bar_length / max_count) if max_count > 0 else 0
683673

684-
# Determine the width for alignment
685-
year_width = len(str(end_year))
686-
count_width = len(str(max_count))
687-
688674
# Print the header row
689675
header_year = "Year"
690676
header_sum = "Sum"
@@ -706,8 +692,7 @@ def git_commits_per_year(config: Dict[str, Union[str, int]]) -> None:
706692

707693

708694
def git_commits_per_weekday(
709-
config: Dict[str, Union[str, int]],
710-
author: Optional[str] = None
695+
config: Dict[str, Union[str, int]], author: Optional[str] = None
711696
) -> None:
712697
"""
713698
Shows commits grouped by weekday. If an author is provided, it shows
@@ -784,9 +769,6 @@ def git_commits_per_weekday(
784769
max_count = max(commit_counts.values())
785770
scaling_factor = (max_bar_length / max_count) if max_count > 0 else 0
786771

787-
# Determine the width for alignment based on max_count
788-
count_width = len(str(max_count))
789-
790772
# Print the header row
791773
header_day = "Day"
792774
header_sum = "Sum"
@@ -816,10 +798,7 @@ def git_commits_per_weekday(
816798
print("No commits found.")
817799

818800

819-
def git_commits_per_hour(
820-
config: Dict[str, Union[str, int]],
821-
author: Optional[str] = None
822-
) -> None:
801+
def git_commits_per_hour(config: Dict[str, Union[str, int]], author: Optional[str] = None) -> None:
823802
"""
824803
Shows commits grouped by hour of the day. If an author is provided,
825804
it shows commits grouped by hour for that specific author.
@@ -896,9 +875,6 @@ def git_commits_per_hour(
896875
max_count = max(commit_counts.values())
897876
scaling_factor = (max_bar_length / max_count) if max_count > 0 else 0
898877

899-
# Determine the width for alignment based on max_count
900-
count_width = len(str(max_count))
901-
902878
# Print the header row
903879
header_hour = "Hour"
904880
header_sum = "Sum"
@@ -929,8 +905,7 @@ def git_commits_per_hour(
929905

930906

931907
def git_commits_per_timezone(
932-
config: Dict[str, Union[str, int]],
933-
author: Optional[str] = None
908+
config: Dict[str, Union[str, int]], author: Optional[str] = None
934909
) -> None:
935910
"""
936911
Displays commits grouped by timezone. If an author is provided, it shows

git_py_stats/menu.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ def interactive_menu(config: Dict[str, Union[str, int]]) -> str:
6565
print(f"{NUMS} 21){TEXT} Git commits per timezone by author")
6666
print(f"\n{TITLES} Suggest:{NORMAL}")
6767
print(f"{NUMS} 22){TEXT} Code reviewers (based on git history)")
68-
print(
69-
f"\n{HELP_TXT}Please enter a menu option or {EXIT_TXT}press Enter to exit.{NORMAL}"
70-
)
68+
print(f"\n{HELP_TXT}Please enter a menu option or {EXIT_TXT}press Enter to exit.{NORMAL}")
7169

7270
choice = input(f"{TEXT}> {NORMAL}")
7371

0 commit comments

Comments
 (0)