Skip to content

Commit 86201cf

Browse files
committed
Fill out generate cmds test and fix some logic
* Filled out test_generate_cmds with a handful of tests * Adjusted generate_cmds to not need to use the regex. We can use --pretty=%H instead to simplify the overall approach. This can be a change back to the parent project
1 parent 30d4595 commit 86201cf

File tree

2 files changed

+277
-45
lines changed

2 files changed

+277
-45
lines changed

git_py_stats/generate_cmds.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def changelogs(
231231
until = config.get("until", "")
232232
log_options = config.get("log_options", "")
233233
pathspec = config.get("pathspec", "")
234-
limit = config.get("limit", "10")
234+
limit = int(config.get("limit", 10))
235235

236236
# Original git command:
237237
# git -c log.showSignature=false log --use-mailmap $_merges --format="%cd" --date=short "${_author}"
@@ -327,28 +327,24 @@ def my_daily_status(config: Dict[str, Union[str, int]]) -> None:
327327
Returns:
328328
None
329329
"""
330-
331330
# Grab the config options from our config.py.
332331
# config.py should give fallbacks for these, but for sanity,
333332
# lets also provide some defaults just in case.
334333
merges = config.get("merges", "--no-merges")
335334
log_options = config.get("log_options", "")
336335

337336
print("My daily status:")
338-
339337
# Equivalent Bash Command:
340338
# git diff --shortstat '@{0 day ago}' | sort -nr | tr ',' '\n' \
341339
# | LC_ALL=C awk '{ args[NR] = $0; } END { for (i = 1; i <= NR; ++i) \
342340
# { printf "\t%s\n", args[i] } }'
343341

344342
# Mimic 'git diff --shortstat "@{0 day ago}"'
345343
diff_cmd = ["git", "diff", "--shortstat", "@{0 day ago}"]
346-
347344
diff_output = run_git_command(diff_cmd)
348345
if diff_output:
349346
# Replace commas with newlines
350347
diff_lines = [line.strip() for line in diff_output.split(",")]
351-
352348
# Print each line prefixed with a tab
353349
for line in diff_lines:
354350
print(f"\t{line}")
@@ -366,6 +362,7 @@ def my_daily_status(config: Dict[str, Union[str, int]]) -> None:
366362

367363
# Get the user's name
368364
# Lets also handle the case if the user's name is not set correctly
365+
369366
git_user = run_git_command(["git", "config", "user.name"])
370367
if not git_user:
371368
git_user = "unknown"
@@ -374,41 +371,37 @@ def my_daily_status(config: Dict[str, Union[str, int]]) -> None:
374371
today = datetime.now().strftime("%Y-%m-%d")
375372
since = f"--since={today}T00:00:00"
376373
until = f"--until={today}T23:59:59"
377-
374+
378375
# Build the final git log command
379376
log_cmd = [
380377
"git",
381378
"-c",
382379
"log.showSignature=false",
383380
"log",
384381
"--use-mailmap",
385-
"--author",
386-
git_user,
382+
f"--author={git_user}", # Ensure the 'f' prefix is present
387383
merges,
388384
since,
389385
until,
390386
"--reverse",
387+
"--pretty=%H", # Output only commit hashes
391388
log_options,
392389
]
393-
390+
394391
# Remove any empty space from the log_cmd
395392
log_cmd = [arg for arg in log_cmd if arg]
396-
397393
# Execute the git log command
398394
log_output = run_git_command(log_cmd)
399395

400396
# Bash version uses grep to count lines matching the hash pattern
401-
# "commit [a-f0-9]{40}"
402-
# We can use re to mimic this in Python
403-
# TODO: Revisit this. We might be able to do --pretty=format:%H to avoid
404-
# having to use a regex to handle this portion. This could be
405-
# an improvement to feed back to the original project
397+
# "commit [a-f0-9]{40}". But it's not necessary with %H.
398+
# TODO: We are be able to do --pretty=format:%H to avoid
399+
# having to use a regex to handle this portion.
400+
# Feed back to the original project
406401
if log_output:
407-
commit_pattern = re.compile(r"^commit [a-f0-9]{40}$", re.MULTILINE)
408-
commit_count = len(commit_pattern.findall(log_output))
402+
commit_count = len(log_output.strip().splitlines())
409403
else:
410404
commit_count = 0
411-
412405
# Print the commit count, prefixed with a tab
413406
print(f"\t{commit_count} commits")
414407

0 commit comments

Comments
 (0)