gitpython和git diff [英] gitpython and git diff

查看:358
本文介绍了gitpython和git diff的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望仅从git repo中获取更改的文件的差异.现在,我正在使用gitpython实际获取提交对象和git更改的文件,但是我只想对文件更改的部分进行依赖关系分析.有什么办法可以从git python获取git diff吗?还是我必须逐行阅读来比较每个文件?

I am looking to get only the diff of a file changed from a git repo. Right now, I am using gitpython to actually get the commit objects and the files of git changes, but I want to do a dependency analysis on only the parts of the file changed. Is there any way to get the git diff from git python? Or am I going to have to compare each of the files by reading line by line?

推荐答案

您可以将GitPython与git命令"diff"一起使用,只需要使用要查看的每个提交或分支的"tree"对象差异,例如:

You can use GitPython with the git command "diff", just need to use the "tree" object of each commit or the branch for that you want to see the diffs, for example:

repo = Repo('/git/repository')
t = repo.head.commit.tree
repo.git.diff(t)

这将打印此提交中包含的所有文件的所有差异,因此,如果要每个文件都必须对其进行迭代.

This will print "all" the diffs for all files included in this commit, so if you want each one you must iterate over them.

具有实际分支的是:

repo.git.diff('HEAD~1')

希望得到帮助,

这篇关于gitpython和git diff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆