我是 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/