gitpython git diff_gitpython and 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?

回答1:

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.

With the actual branch it's:

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

Hope this help, regards.

回答2:

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.

回答3:

Git does not store the diffs, as you have noticed. Given two blobs (before and after a change), you can use Python's difflib module to compare the data.

回答4:

I'd suggest you to use PyDriller instead (it uses GitPython internally). Much easier to use:

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")

回答5:

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.

回答6:

If you want to do git diff on a file between two commits this is the way to do it:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值