Skip to content

Instantly share code, notes, and snippets.

@ddaletski
Created June 21, 2021 11:12
Show Gist options
  • Save ddaletski/4042dd1e9b97c59b2db7de37932ef533 to your computer and use it in GitHub Desktop.
Save ddaletski/4042dd1e9b97c59b2db7de37932ef533 to your computer and use it in GitHub Desktop.
visualize git commits stats
#requirements: click, pandas, matplotlib, GitPython
from matplotlib import pyplot as plt
import pandas as pd
import json
from datetime import datetime
import git
import click
def parse_repo(repo_path: str):
g = git.Repo(repo_path)
data = [
{
"hash": c.hexsha,
"author": c.author,
"date": c.authored_datetime.ctime()
}
for c in g.iter_commits()
]
df = pd.DataFrame(data)
df.index = df["hash"]
df.drop("hash", 1, inplace=True)
df.author = df.author.astype("string")
df.date = pd.to_datetime(df.date)
df["weekday"] = df.date.dt.weekday
return df
@click.command()
@click.argument("repo_path", type=click.Path(file_okay=False, exists=True), default=".")
@click.option("-o", "--out", "out_file", type=click.Path(dir_okay=False), required=True)
@click.option('--visualize', is_flag=True)
def visualize(repo_path: str, out_file: str, visualize: bool):
df = parse_repo(repo_path)
fig, axes = plt.subplots(2, 1, figsize=(16, 16))
day_counts = df.weekday.value_counts()
axes[0].barh(["mo", "tu", "we", "th", "fr", "sa", "su"], day_counts)
auth_counts = df.author.value_counts()
axes[1].barh(auth_counts.index, auth_counts)
plt.savefig(out_file)
if visualize:
plt.show()
if __name__ == "__main__":
visualize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment