2013-04-09 52 views

回答

5

获得使用德威所有标签的最简单方法是:

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 
1

通过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) 
0

现在你也可以获取标记标签的alphabetically排序列表。

from dulwich.repo import Repo 
from dulwich.porcelain import tag_list 

repo = Repo('.') 
tag_labels = tag_list(repo)