我有问题检索使用python一个混帐回购协议的下列信息:如何在git中使用python和dulwich创建标签列表并创建新标签?
- 我想获得这个仓库的所有标签的列表。
- 我想签出另一个分支,并为分段创建一个新分支。
- 我想用带注释的标签来标记提交。
我查看了德威公司的文件,它的工作方式似乎很简陋。还有更容易使用的替代品吗?
我有问题检索使用python一个混帐回购协议的下列信息:如何在git中使用python和dulwich创建标签列表并创建新标签?
我查看了德威公司的文件,它的工作方式似乎很简陋。还有更容易使用的替代品吗?
获得使用德威所有标签的最简单方法是:
from dulwich.repo import Repo
r = Repo("/path/to/repo")
tags = r.refs.as_dict("refs/tags")
标签现在是一个字典映射标签来提交SHA1。
检查出另一个分支:
r.refs.set_symbolic_ref("HEAD", "refs/heads/foo")
r.reset_index()
创建一个分支:
r.refs["refs/heads/foo"] = head_sha1_of_new_branch
通过subprocess
联系git。从我自己的计划之一:
def gitcmd(cmds, output=False):
"""Run the specified git command.
Arguments:
cmds -- command string or list of strings of command and arguments
output -- wether the output should be captured and returned, or
just the return value
"""
if isinstance(cmds, str):
if ' ' in cmds:
raise ValueError('No spaces in single command allowed.')
cmds = [cmds] # make it into a list.
# at this point we'll assume cmds was a list.
cmds = ['git'] + cmds # prepend with git
if output: # should the output be captured?
rv = subprocess.check_output(cmds, stderr=subprocess.STDOUT).decode()
else:
with open(os.devnull, 'w') as bb:
rv = subprocess.call(cmds, stdout=bb, stderr=bb)
return rv
一些例子:
rv = gitcmd(['gc', '--auto', '--quiet',])
outp = gitcmd('status', True)
现在你也可以获取标记标签的alphabetically排序列表。
from dulwich.repo import Repo
from dulwich.porcelain import tag_list
repo = Repo('.')
tag_labels = tag_list(repo)