我正在尝试在gitpython中实现git tag --contains <commit>。谁能指出我的文档。我发现文档可以获取所有标签,但不能包含特定提交的标签。

最佳答案

更新2019:正如注释中提到的Anentropic一样,您可以像运行git cli一样使用GitPython掏空命令。例如,在这种情况下,您将使用repo.git.tag("--contains", "<commit>").split("\n")



由于这些限制,我放弃了GitPython。令人讨厌的不是很像git。这个简单的类负责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"]

08-27 21:49