python脚本:git 仓库本地所有分支指定提交人的某段时间内的提交记录输出到csv文件

起因

写周报等报告时,总是需要回顾自己的提交,从而写工作成果。但原生git命令不能很好的格式化输出结果。因此使用一个python脚本进行实现。

脚本

依赖

本脚本使用了gitpython,使用前需要安装:
pip install GitPython

代码

import csv
import datetime
import os
from datetime import timedelta

from git import Commit, Repo

# 配置区

# 仓库目录(.git目录的父目录)
respDir = "D:\Project\xxx"
# 提交作者
author = ""
# 提交起始日期,格式: 2022-12-30,都留空则以本周一零点和周日24点作为起始日期.
# 开始日期(以0点计算)
startDate = ""
# 结束日期(以24点计算)
endDate = ""
# 时间的时区
timeZone = datetime.timezone(datetime.timedelta(hours=8), '中国标准时间')
# ================


def conventTzDate(date: str) -> datetime:
    return datetime.datetime.strptime(
        date, '%Y-%m-%d').replace(tzinfo=timeZone)


if (startDate != ""):
    startDate = conventTzDate(startDate)
if (endDate != ""):
    endDate = (conventTzDate(endDate) +
               datetime.timedelta(hours=23, minutes=59, seconds=59))
if (startDate == "" and endDate == ""):
    now = datetime.datetime.now().replace(tzinfo=timeZone)
    startDate = now - datetime.timedelta(days=now.weekday(), hours=now.hour,
                                         minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
    endDate = now + datetime.timedelta(days=6 - now.weekday(
    ), hours=23 - now.hour, minutes=59 - now.minute, seconds=59 - now.second)


def getFilterCommits(branchName: str) -> list[Commit]:
    revListArgs = {"author": author}
    if (startDate != ""):
        revListArgs["after"] = startDate.strftime("%Y-%m-%d %H:%M:%S%z")
    if (endDate != ""):
        revListArgs["before"] = endDate.strftime("%Y-%m-%d %H:%M:%S%z")
    return list(repo.iter_commits(branchName, **revListArgs))


repo = Repo.init(path=respDir)
respName = os.path.basename(respDir)
nowBranchName = repo.active_branch.name
nameCommitMap = {}


def putToMap(branchName: str):
    list = getFilterCommits(branchName)
    if len(list) != 0:
        nameCommitMap[branchName] = list


putToMap(nowBranchName)

for branch in repo.branches:
    if (branch.name != nowBranchName):
        putToMap(branch.name)

topTitle = respName
if startDate != "":
    topTitle += " "
    topTitle += startDate.strftime("%Y-%m-%d")
    topTitle += "起"

if endDate != "":
    topTitle += " "
    topTitle += endDate.strftime("%Y-%m-%d")
    topTitle += "止"

topTitle += " "
topTitle += author
topTitle += " 的本地所有分支提交记录汇总"


def outputMap(source: dict, layer: int, writer):
    for key, value in source.items():
        keylist = [key]
        for i in range(0, layer):
            keylist.insert(0, "")
        writer.writerow(keylist)
        if isinstance(value, dict):
            outputMap(value, layer+1, writer)
        elif isinstance(value, list):
            headerList = ["mes", "authored date", "committed date", "hexsha"]
            for i in range(0, layer+1):
                headerList.insert(0, "")
            writer.writerow(headerList)
            for c in value:
                c: Commit
                valueList = []
                valueList.append(c.message)
                valueList.append(c.authored_datetime)
                valueList.append(c.committed_datetime)
                valueList.append(c.hexsha)
                for i in range(0, layer+1):
                    valueList.insert(0, "")
                writer.writerow(valueList)


outputdir = os.path.split(os.path.realpath(__file__))[0]
outputFileName = os.path.join(outputdir, topTitle+'.csv')

with open(outputFileName, 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, dialect='excel')
    outputMap({topTitle: nameCommitMap}, 0, writer)
    print("结果已输出到文件:"+outputFileName)
repo.close

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值