【问题标题】:How to get a list of tags and create new tags with python and dulwich in git?如何在git中使用python和dulwich获取标签列表并创建新标签?
【发布时间】:2013-04-09 18:40:11
【问题描述】:

我在使用 python 检索 git repo 的以下信息时遇到问题:

  1. 我想获取此存储库的所有标签的列表。
  2. 我想签出另一个分支并创建一个新分支以进行暂存。
  3. 我想用带注释的标签标记提交。

我查看了德威的文档,它的工作方式似乎非常简单。还有更容易使用的替代品吗?

【问题讨论】:

    标签: python git dulwich


    【解决方案1】:

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

    from dulwich.repo import Repo
    r = Repo("/path/to/repo")
    tags = r.refs.as_dict("refs/tags")
    

    tags 现在是一个字典,将标签映射到提交 SHA1。

    检查另一个分支:

    r.refs.set_symbolic_ref("HEAD", "refs/heads/foo")
    r.reset_index()
    

    创建一个分支:

    r.refs["refs/heads/foo"] = head_sha1_of_new_branch
    

    【讨论】:

    • 嗨@jelmer,感谢您的项目:) 是否有机会举例说明如何将标签 b.push 标签添加到远程?
    • 太好了,很有用!请就此提出一个新问题,我认为这是一个不同的话题。
    【解决方案2】:

    现在您还可以获得alphabetically 标签标签的排序列表。

    from dulwich.repo import Repo
    from dulwich.porcelain import tag_list
    
    repo = Repo('.')
    tag_labels = tag_list(repo)
    

    【讨论】:

    • 太棒了!似乎没有办法从瓷器中获取 SHA1 部分
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-01-11
      • 1970-01-01
      • 2016-06-29
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      相关资源
      最近更新 更多