由于项目管理需要,需要找个统计一堆仓库的每个人的工作情况,细粒度到:全部仓库、每人、每天、commits、lines

找了很久没找到好用的,决定自己写一个

import subprocess
import os
import datetime

# 仓库链接,ssh 或者 https 都行,只要能 git clone {repo} 正常执行的链接就行
repos = [
    "repo url 1",
    "repo url 2",
]
num_of_day = 5 # 统计最近 5 天

for repo in repos:
    os.system(f"git clone {repo}")

for path in os.listdir():
    if os.path.isdir(path):
        os.system(f"cd {path} && git pull")

now = datetime.datetime.now()
for i in range(num_of_day):
    since = now - datetime.timedelta(days = i)
    since = since.replace(hour=6, minute=0, second=0, microsecond=0)
    until = since + datetime.timedelta(days = 1)
    stats = {}
    for path in os.listdir():
        if os.path.isdir(path):
            # commits
            output = subprocess.getoutput(f'cd {path} && git shortlog -sn --no-merges --since "{since}" --until "{until}" --all --branches --remotes')
            for line in output.strip().split("\n"):
                data = line.replace("\t", " ").strip().split(" ")
                if len(data) != 2:
                    continue
                user = data[1]
                if user not in stats:
                    stats[user] = {
                        "commit": 0,
                        "file": {
                            "changed": 0,
                        },
                        "line": {
                            "insertion": 0,
                            "deletion": 0,
                        },
                    }
                stats[user]["commit"] += int(data[0])

                output = subprocess.getoutput(f'cd {path} && git log --shortstat --no-merges --author="{user}" --since "{since}" --until "{until}" --all --branches --remotes | grep -E "fil(e|es) changed"')
                for line in output.strip().split("\n"):
                    data = line.replace("\t", " ").strip().split(" ")
                    if len(data) < 6:
                        continue
                    stats[user]["file"]["changed"] += int(data[0])
                    stats[user]["line"]["insertion"] += int(data[3])
                    stats[user]["line"]["deletion"] += int(data[5])
    print(f"{since} - {until}")
    for user in stats.keys():
        print(f'{user}: commits {stats[user]["commit"]}, lines +{stats[user]["line"]["insertion"]} -{stats[user]["line"]["deletion"]}')
    print("")

结果如下

2020-09-01 06:00:00 - 2020-09-02 06:00:00

2020-08-31 06:00:00 - 2020-09-01 06:00:00
A: commits 8, lines +1821 -498
B: commits 5, lines +216 -91
C: commits 1, lines +232 -79

2020-08-30 06:00:00 - 2020-08-31 06:00:00
A: commits 6, lines +1223 -772

2020-08-29 06:00:00 - 2020-08-30 06:00:00
A: commits 10, lines +6648 -640

2020-08-28 06:00:00 - 2020-08-29 06:00:00
B: commits 2, lines +1 -1
A: commits 5, lines +1186 -769

欢迎留言>_<

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据