python - 获取两个提交或分支之间更改文件的列表

标签 python git bash shell gitpython

我是 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 将返回所有差异细节

最佳答案

这是在 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

关于python - 获取两个提交或分支之间更改文件的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556696/

相关文章:

linux - 如何在 bash shell 脚本中添加另一个 "for"循环?

linux - 通过 bash 和 ssh 逐字传递引号和其他特殊字符

生成器的 Python 循环行为

python - TensorFlow 权限被拒绝错误/位置

git - 如何在git中维护生产和开发分支?

git - git 如何处理 git 存储库在另一个存储库中的放置?

bash - ssh + ksh + grep + wc

python - 关于Google App Engine可用性的反馈

python - 无法将 M2Crypto 安装到 Linux mint Rafaela

git - 在 jenkinsfile 中使用来自 Jenkins 存储的凭据