使用 GitPython 与 master 分支进行 Git diff

问题描述 投票:0回答:1

我一定很笨,但我还没找到解决办法。

我正在尝试获取在我的 Python 程序中的分支上已更改的所有成员(所有提交或尚未提交)的列表。在命令行上,我使用“git diff --name-only master”,它正是我想要的。

通过使用:

commit = repo.head.commit
master = repo.heads.master
for member in commit.diff(master).iter_change_type("M"):
    members.append(member.a_path)

我从最新提交中获取了修改成员的列表。它不包括之前提交的任何内容或尚未提交的任何内容(我还对添加的任何内容使用类型“A”执行相同的命令,我还不担心重命名或删除)。

这有效:

member_list = repo.git.diff("--name-only", "master").splitlines()

这返回了我想要的,但似乎应该有一种更“Pythonic”的方式来做到这一点。

python git gitpython
1个回答
0
投票

您可以使用

a_path
对象的
Diff
属性,它是“a”侧不同的文件路径,即当前分支,这也是命令打印的内容。

import git

repo = git.Repo("path/to/repo")

for diff in repo.head.commit.diff('master'):
    print(diff.a_path)
© www.soinside.com 2019 - 2024. All rights reserved.