在 GitPython 中迭代提交黑白 2 个指定提交

Sha*_*war 6 python git loops commit gitpython

import git
repo = git.Repo(repo_dir)
ref_name = 'master'
for commit in repo.iter_commits(rev=ref_name):
     <some code here>
Run Code Online (Sandbox Code Playgroud)

此代码迭代所有提交。我想迭代黑白 2 次提交。就像git log commit1...commit2

我如何使用 GitPython 的 iter_commits() 方法执行相同的操作。

ebe*_*zer 6

repo.iter_commits(rev='1234abc..5678def')为我工作GitPython==2.1.11

例子:

repo = git.Repo(repo_dir)
for commit in repo.iter_commits(rev='master..HEAD'):
     <some code here>
Run Code Online (Sandbox Code Playgroud)


Yus*_*UMS -2

首先,创建一个运行git命令的函数。

from git import *
from subprocess import Popen, PIPE

def execute_gitcmd(cmd, repo):
    pipe = subprocess.Popen(cmd, shell=True, cwd=repo, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (out, error) = pipe.communicate()
    return out, error
    pipe.wait()
Run Code Online (Sandbox Code Playgroud)

然后编写git您在终端上使用的任何命令,例如:

gitcmd = "git log -n1 --oneline"
Run Code Online (Sandbox Code Playgroud)

最后,调用你的函数:

log = (execute_gitcmd(gitcmd, your_repository))
Run Code Online (Sandbox Code Playgroud)

希望这能有所帮助。

  • 1.问题是关于GitPython的。2. 该函数有一个重大错误——“return”位于“pipe.wait()”之前。 (3认同)