gitpython中等价于“git tag --contains”的命令
我想在gitpython中实现 git tag --contains <commit>
这个功能。有没有人能告诉我相关的文档在哪里?我找到的文档是关于获取所有标签的,但没有找到关于获取包含特定提交的标签的。
2 个回答
0
tagref = TagReference.list_items(repo)[0]
print tagref.commit.message
来自文档。
1
更新于2019年:正如Anentropic在评论中提到的,你可以像使用git命令行一样,通过GitPython来执行命令。例如,在这种情况下,你可以使用repo.git.tag("--contains", "<commit>").split("\n")
。
我已经放弃使用GitPython了,因为它有一些限制。使用起来让人觉得不太像git。这段简单的代码可以处理所有与git相关的事情(除了初始化新仓库和身份验证):
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"]