预备知识
pygit2 - libgit2 bindings in Python — pygit2 1.9.0 documentation
提交 - commit类
repo.head.target
得到当前分支的最后一次提交的hash
repo[repo.head.target]
得到最后一次提交的提交信息,也可以通过这个方法获取任意一次提交的信息。
commit.author
作者信息(https://www.pygit2.org/objects.html#pygit2.Signature)
commit.author.name
commit.author.email
1 | print(repo.head.target) # 5b26c4c177c25f0c142c885ee23f2969b8457fe1 |
对比 - diff类
对比两次提交
1 | diff = repo.diff('HEAD~6', 'HEAD~7') |
提交中的每一小块(patch),也就是每一个文件
1 | patches = [p for p in diff] |
文件中的每一个更改区域,第几行到第几行修改了多少这样
1 | hunks = [h for h in patch] |
文件的更改行数
1 | patches = [p.line_stats for p in diff] # ([更改的行的前后留白位置], [删除的行数], [新增的行数]) |
文件更改类型
1 | patches = [p.delta.status for p in diff] # 3 modify, 1 delete, 2 create |
遍历提交
使用repo.walk
1 | last = repo[repo.head.target] |
第一次提交的父级是谁?
使用diff()
函数时,需要填入两个分支的hash进行对比,那么对于第一次提交,其父级固定是4b825dc642cb6eb9a060e54bf8d69288fbee4904
,所有git仓库创建时,其父级分支的hash都是这个。
python脚本实现
1 | import time |