gitpython 和 git diff
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20061898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gitpython and git diff
提问by user1816561
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?
我希望只获取从 git repo 更改的文件的差异。现在,我正在使用 gitpython 来实际获取提交对象和 git 更改的文件,但我只想对更改的文件部分进行依赖分析。有什么办法可以从 git python 中获取 git diff 吗?还是我必须通过逐行阅读来比较每个文件?
回答by Cairo
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:
您可以将 GitPython 与 git 命令“diff”一起使用,只需要使用每个提交的“树”对象或要查看差异的分支,例如:
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.
这将打印此提交中包含的所有文件的“所有”差异,因此如果您想要每个文件,则必须遍历它们。
With the actual branch it's:
对于实际的分支,它是:
repo.git.diff('HEAD~1')
Hope this help, regards.
希望这有帮助,问候。
回答by D. A.
If you want to access the contents of the diff, try this:
如果要访问差异的内容,请尝试以下操作:
repo = git.Repo(repo_root.as_posix())
commit_dev = repo.commit("dev")
commit_origin_dev = repo.commit("origin/dev")
diff_index = commit_origin_dev.diff(commit_dev)
for diff_item in diff_index.iter_change_type('M'):
print("A blob:\n{}".format(diff_item.a_blob.data_stream.read().decode('utf-8')))
print("B blob:\n{}".format(diff_item.b_blob.data_stream.read().decode('utf-8')))
This will print the contents of each file.
这将打印每个文件的内容。
回答by Greg Hewgill
Git does not store the diffs, as you have noticed. Given two blobs (before and after a change), you can use Python's difflib
moduleto compare the data.
正如您所注意到的,Git 不存储差异。给定两个 blob(更改前后),您可以使用Python 的difflib
模块来比较数据。
回答by Davide Spadini
I'd suggest you to use PyDrillerinstead (it uses GitPython internally). Much easier to use:
我建议您改用PyDriller(它在内部使用 GitPython)。更容易使用:
for commit in RepositoryMining("path_to_repo").traverse_commits():
for modified_file in commit.modifications: # here you have the list of modified files
print(modified_file.diff)
# etc...
You can also analyze a single commit by doing:
您还可以通过执行以下操作来分析单个提交:
for commit in RepositoryMining("path_to_repo", single="123213")
回答by Ciasto piekarz
I am not sure if you got what you were looking for!
我不确定你是否得到了你想要的东西!
Here is how you do it
这是你如何做到的
import git
repo = git.Repo("path/of/repo/")
# the below gives us all commits
repo.commits()
# take the first and last commit
a_commit = repo.commits()[0]
b_commit = repo.commits()[1]
# now get the diff
repo.diff(a_commit,b_commit)
Cheers.
干杯。
回答by Nikola ?uza
If you want to do git diff on a file between two commits this is the way to do it:
如果你想在两次提交之间对文件执行 git diff ,这是这样做的方法:
import git
repo = git.Repo()
path_to_a_file = "diff_this_file_across_commits.txt"
commits_touching_path = list(repo.iter_commits(paths=path))
print repo.git.diff(commits_touching_path[0], commits_touching_path[1], path_to_a_file)
This will show you the differences between two latest commits that were done to the file you specify.
这将向您显示对您指定的文件所做的两次最新提交之间的差异。
Hope this helped.
希望这有帮助。