使用GitPython中的2个指定提交迭代提交 导入git 回购=吉特回购(回购方向) ref_name='master' 对于repo.iter\u提交中的提交(rev=ref\u name):
此代码迭代所有提交。我想迭代b/w 2提交。 就像git log commit1…commit2一样使用GitPython中的2个指定提交迭代提交 导入git 回购=吉特回购(回购方向) ref_name='master' 对于repo.iter\u提交中的提交(rev=ref\u name):,python,git,loops,commit,gitpython,Python,Git,Loops,Commit,Gitpython,此代码迭代所有提交。我想迭代b/w 2提交。 就像git log commit1…commit2一样 如何使用GitPython的iter_commits()方法实现同样的功能。首先,创建一个函数来运行git命令 import git repo = git.Repo(repo_dir) ref_name = 'master' for commit in repo.iter_commits(rev=ref_name): <some code here> 然后编写您在终端上使
如何使用GitPython的iter_commits()方法实现同样的功能。首先,创建一个函数来运行
git
命令
import git
repo = git.Repo(repo_dir)
ref_name = 'master'
for commit in repo.iter_commits(rev=ref_name):
<some code here>
然后编写您在终端上使用的任何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()
最后,调用您的函数:
gitcmd = "git log -n1 --oneline"
希望这能有所帮助。我建议您使用(GitPython的包装器,使事情更简单)。您所要求的可以这样做:
log = (execute_gitcmd(gitcmd, your_repository))
您可以使用纯gitpython来实现这一点 如果您希望能够遍历某些提交(假设第一个 提交是头),只需使用
max\u count
。看
two\u提交=列表(repo.iter\u提交('master',max\u count=2))
断言len(两次提交)=2
如果您想要与您提到的git log commit1…commit2类似的功能:
logs=repo.git.log(“--oneline”,“f5035ce..f63d26b”)
将为您提供:
>日志
“f63d26b修复urxvt名称以匹配debian repo\n571f449为头盔组织步枪添加更多密钥\nBA2697掉落bm包”
您也可以使用logs=repo.git.log(“f5035ce..f63d26b”)
但它会提供所有信息(就像您使用git log
而不使用--oneline
)
如果您想获得好的输出,请使用漂亮的打印:
从pprint导入pprint作为pp
>>>pp(日志)
('f63d26b修复urxvt名称以匹配debian回购\n'
'571f449为头盔组织步枪添加更多钥匙\n'
‘bea2697投递bm包’)
有关repo.git.log
的更多说明,请参见repo.iter\u提交(rev='1234abc..5678def')
在GitPython==2.1.11
例如:
for commit in RepositoryMining("path_to_repo", from_commit="first", to_commit="second").traverse_commits():
# your code
repo=git.repo(repo\u dir)
对于repo.iter_提交中的提交(rev='master..HEAD'):
但您知道在终端上执行git
命令的代码吗?如果是,则可以使用子流程
创建函数。然后您可以调用函数来运行git
命令。是的,我可以这样做。但我不想走那条路。我想使用这个GitPython库。FromCommit.iter\u items()
接受一个修订说明符,我认为修订范围也是它的一部分。只要通过“commit1…commit2”
就可以了。如果你的问题完全是关于“我如何在gitpython中做X件事”,gitpython标签是合适的,但是其他标签都不合适,因为你会得到像@Yusufsn这样的答案。请注意,这是关于数据库事务的,而不是关于Git提交。问题是关于GitPython的。2.该函数有一个主要错误-返回之前的管道.wait()
。
repo = git.Repo(repo_dir)
for commit in repo.iter_commits(rev='master..HEAD'):
<some code here>