python - Git Python 找不到提交

标签 python git gitpython

我无法通过导航提交树找到标记指向的提交。对于这个具体示例,我使用直接从 Github 克隆的 Tornado Web 存储库。

import sys
import git

if len(sys.argv) < 2:
    print 'python git_test.py <repo path>'
    sys.exit(0)

repo = git.Repo(sys.argv[1])
commits = {}

for git_commit in repo.iter_commits():
    commits[git_commit.hexsha] = git_commit

print len(commits)

for git_tag in repo.tags:
    print 'Tag %s points to Commit %s' % (
        git_tag.name,
        commits[git_tag.commit.hexsha]
    )

这应该找到 git 的直接非循环图中的所有提交,但是,我尝试了另一种方法,通过递归函数递归地导航 dag,并且它提供了相同的结果。

ian@ian-EP45-UD3L:~/code/playground$ python git_test.py ~/code/tornado/
459
Tag v1.0.0 points to Commit eb5b3d8df7a305ac1ffa0a12c813e5d7ee4d6cd3
Traceback (most recent call last):
  File "git_test.py", line 19, in <module>
    commits[git_tag.commit.hexsha]
KeyError: '2b5064b7ed962603ade0db0c8e21846a9879e30e'

我是否做错了什么,如何解决这个问题?如有任何帮助,我们将不胜感激!

我正在使用 git-python v0.3.1。

最佳答案

我之前没有使用过 gitpython,所以我很好奇并在虚拟存储库上尝试了你的脚本。我没有收到任何错误并且标签打印正确。但我有一个怀疑:

我添加了一个分支,添加了一个提交,并标记了它。然后它给出了您遇到的错误,事情就变得显而易见了。

repo.iter_commits() 仅获取当前分支中的提交。因此,另一个分支中提交的任何标记都不会在 commits 中包含该提交。我尝试将分支更改为我创建的新分支,但失败了,说找不到不同的提交,这当然是在我的虚拟存储库的 master 中。

这是你的问题。您需要找到一种方法来获取所有分支的所有提交。

PS:您确实知道您正在采用一种迂回的方式来获得标签指向正确的提交吗?

关于python - Git Python 找不到提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807143/

相关文章:

git - sbt-ghpages 致命 : Not a git repository (or any of the parent directories): . git

python - GitPython 无法设置 git config 用户名和电子邮件

git - 如何使用带密码的 SSH key 使用 GitPython 克隆 git 存储库

python - 如何使用 await 表达式?

python-twitter 检索所有关注者

python - 使用剪贴板将 excel 数据复制到 IPython 中的 python 列表中?

eclipse - 从 Github 导入 Ant 项目

python - opencv python使用roxPolyDP在植物托盘中找到正方形

git - ssh-keygen 没有找到 ssh_askpass

python - 想要检查存储库是否存在以及是否是公共(public)的(gitpython)