gitpython and git diff

后端 未结 6 1382
终归单人心
终归单人心 2021-02-12 21:48

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

6条回答
  •  温柔的废话
    2021-02-12 22:41

    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.

提交回复
热议问题