Python:为给定的git存储库找到所有提交到其差异的映射

时间:2017-08-29 17:59:31

标签: python git gitpython

我正在创建一个分析Git存储库的工具,但是偶然发现应该(看似)非常简单。

我想创建一个提交到差异的映射(即给定提交的逐行实际blob更改);我尝试过使用GitPython,但没有取得任何成功。我想实现这样的目标:

def get_all_commits(REPO_URL):
    chromium_repo = Repo(REPO_URL)
    commits = list(chromium_repo.iter_commits())
    commmit_diffs = {}
    for commit in commits:
        diff = # get all blob changes for commit
        commit_diffs[commit.hexsha] = diff
    return commit_diffs

但我不确定如何为给定的提交获取所有blob更改。 commit_diffs将采用以下形式:

{ 232d8f39bedc0fb64d15eed4f46d6202c75066b6 : '<String detailing all blob changes for given commit>' }

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

我不知道git diff <commit_a> <commit_b>命令。以下(我认为!)解决了这个问题:

def get_all_commits(REPO_URL):
    repo = Repo(REPO_URL)
    commits = list(repo.iter_commits())
    commmit_diffs = {}
    for index, commit in enumerate(commits):
        next_index = index + 1
        if next_index < len(commits):
            commit_diffs[commit.hexsha] = repo.git.diff(commits[next_index], commit)
    return commit_diffs