Skip to content

Instantly share code, notes, and snippets.

@klesouza
Created November 3, 2020 15:14
Show Gist options
  • Save klesouza/8fd8c421f20b091c3d93b3cf292fd849 to your computer and use it in GitHub Desktop.
Save klesouza/8fd8c421f20b091c3d93b3cf292fd849 to your computer and use it in GitHub Desktop.
List git commits from all repositories in a folder
import subprocess
import datetime
import os
import argparse
import re
def get_git_log(root_path: str, month: int, author: str):
year = datetime.datetime.utcnow().year
m = datetime.datetime(year, month, 1)
em = (m + datetime.timedelta(days=32)).month
start_date = f"{year}-{month}-01"
end_date = f"{year}-{em}-01"
commits = {}
for d, _, _ in os.walk(root_path):
if os.path.exists(os.path.join(d, ".git")):
git_log = [
"git",
f"--git-dir={d}/.git",
f"--work-tree={d}",
"log",
'--pretty=%ad %s',
f"--author={author}",
"--before",
end_date,
"--after",
start_date,
"--date=short",
]
p = subprocess.run(git_log, capture_output=True, text=True, check=True)
result = p.stdout.split("\n")
for l in result:
if len(l.strip()) > 0:
dt = l.split()[0]
commits[dt] = commits.get(dt, []) + [f'{l.replace(dt, "").strip()} ({d.split("/")[-1]})']
return commits
def summarize_jira(commits):
r_jira = r'([a-zA-z]{3,4}-[0-9]+)\s'
stories = set()
for c in commits:
js = re.findall(r_jira, c)
stories = stories.union(set(js))
return stories
parser = argparse.ArgumentParser()
parser.add_argument("month", type=int)
parser.add_argument("author", type=str)
parser.add_argument("--path", dest="path", required=False, default=os.getcwd())
parser.add_argument("--s", action="store_true")
if __name__ == "__main__":
args = parser.parse_args()
commits = get_git_log(args.path, args.month, args.author)
for k, v in sorted(commits.items()):
print(k)
to_print = list(summarize_jira(v)) if args.s else v
for c in to_print:
print(f'\t{c}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment