相当于gitpython中的“git tag --contains”

时间:2014-03-12 21:16:14

标签: python git tags gitpython

我正在尝试在gitpython中实现git tag --contains <commit>。任何人都可以指向我的文档。我找到了获取所有标签的文档,但没有找到包含特定提交的标签。

2 个答案:

答案 0 :(得分:1)

更新2019:作为评论中提到的fiddle demo,您可以&#34;支持&#34;使用GitPython的命令就像运行git cli一样。例如,在这种情况下,您将使用repo.git.tag("--contains", "<commit>").split("\n")

由于这些限制,我已经放弃了GitPython。它烦人地不是很喜欢。这个简单的类负责所有git(除了新的repo和auth之外):

class PyGit:
    def __init__(self, repo_directory):
        self.repo_directory = repo_directory
        git_check = subprocess.check_output(['git', 'rev-parse', '--git-dir'],
                                            cwd=self.repo_directory).split("\n")[0]
        if git_check != '.git':
            raise Exception("Invalid git repo directory: '{}'.\n"
                            "repo_directory must be a root repo directory "
                            "of git project.".format(self.repo_directory))

    def __call__(self, *args, **kwargs):
        return self._git(args[0])

    def _git(self, *args):
        arguments = ["git"] + [arg for arg in args]
        return subprocess.check_output(arguments, cwd=self.repo_directory).split("\n")

所以现在你可以用git做任何事情了:

>>> git = PyGit("/path/to/repo/")

>>> git("checkout", "master")
["Switched to branch 'master'",
"Your branch is up-to-date with 'origin/master'."]

>>> git("checkout", "develop")
["Switched to branch 'develop'",
"Your branch is up-to-date with 'origin/develop'."]

>>> git("describe", "--tags")
["1.4.0-rev23"]

>>> git("tag", "--contains", "ex4m9le*c00m1t*h4Sh")
["1.4.0-rev23", "MY-SECOND-TAG-rev1"]

答案 1 :(得分:0)

tagref = TagReference.list_items(repo)[0]
print tagref.commit.message

来自docs