3

我是 Python/Git 新手,但我正在尝试编写一个脚本,该脚本将两个分支或提交作为参数,并显示两者之间已更改文件的列表,而不是常规差异附带的所有无关信息。

这是通过使用 bash 脚本完成的

git diff --name-only FIRSTBRANCH...SECONDBRANCH

但它并不像使用 gitpython 那样容易地转换为 Python 脚本。如果有人知道如何做到这一点,那就太好了。

编辑:继承人一些代码

user = str(sys.argv[1])
password = str(sys.argv[2])
currentBranch = str(sys.argv[3])
compBranch = str(sys.argv[4])

repo = Repo(directory)
currentCommit = repo.commit(currentBranch)
compCommit = repo.commit(compBranch)
diffed = repo.diff(currentBranch, compBranch)

当我只想要已更改文件的列表时,print diff 将返回所有差异详细信息

4

2 回答 2

7

这是在python中执行此操作的一种方法。

 #Dif two branches, returns list
import git 


def gitDiff(branch1, branch2):
    format = '--name-only'
    commits = []
    g = git.Git('/path/to/git/repo')
    differ = g.diff('%s..%s' % (branch1, branch2), format).split("\n")
    for line in differ:
        if len(line):
            commits.append(line)

    #for commit in commits:
    #    print '*%s' % (commit)
    return commits
于 2015-12-21T17:50:47.980 回答
3

修复或至少在正确的轨道上使用以下内容(受到删除答案的人的启发......谢谢,伙计)

subprocess.check_output(['git', 'diff', '--name-only', currentBranch + '..' + compBranch])

这基本上可以满足我的需要,尽管如果有更优雅的解决方案,我很想听听!

于 2014-08-28T20:34:59.150 回答