获取两次提交或分支之间已更改文件的列表

z0d*_*14c 2 python git bash shell gitpython

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

这是通过使用以下命令在bash脚本中完成的

git diff --name-only FIRSTBRANCH...SECONDBRANCH
Run Code Online (Sandbox Code Playgroud)

但是使用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)
Run Code Online (Sandbox Code Playgroud)

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

小智 6

这是在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
Run Code Online (Sandbox Code Playgroud)

  • 尽管此答案可能是正确且有用的,但如果您[包括一些解释以及它](http://meta.stackexchange.com/q/114762/159034)来解释它如何帮助解决问题,则是首选方法。如果发生更改(可能不相关)导致它停止工作,并且用户需要了解它曾经如何工作,那么这在将来特别有用。 (2认同)