如何使用git python获取第一个父提交列表?

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

我想从git python运行相当于这个命令,但是还没有找到实现这个目标的方法。

git rev-list --first-parent commit1..HEAD

我希望将该命令的结果放入一个可迭代的git python的Commit对象中。我尝试了repo.iter_commits,但似乎没有能够接受不带参数的rev-list参数。

我的用例是“commit1”将是分支所基于的提交,并且我将在检出分支时运行此代码。因此,即使存在来自分支“commit1”的合并提交,此命令也会向我提供提交给分支的提交列表。

我也试过了

repo.iter_commits('HEAD ^commit1')

但这会导致以下错误:git.exc.GitCommandError:Cmd('git')失败,原因是:退出代码(128)cmdline:git rev-list HEAD ^ commit1 - stderr:'fatal:bad revision'HEAD ^ commit1'

但是,我可以跑

git rev-list HEAD ^commit1 --

在bash中运行正常。此外,命令并没有真正给我我需要的东西。

gitpython
1个回答
1
投票

通过直接使用git python中的commit的父项列表,我能够得到我需要的东西。这是一个对我有用的片段:

       commits = list() 
       c = repo.head.commit
       while (True):         
            firstparent = c.parents[0]
            if (firstparent != commit1):
                c = firstparent
                commits.append(c)
            else:
                break

上面的代码不处理第一次提交(没有父级)。

© www.soinside.com 2019 - 2024. All rights reserved.