GorillaPatch GorillaPatch - 5 months ago 30
Git Question

How to get a list of tags and create new tags with python and dulwich in git?

I am having problems to retrieve the following information of a git repo using python:

  1. I would like to get a list of all tags of this repository.

  2. I would like to checkout another branch and also create a new branch for staging.

  3. I would like to tag a commit with an annotated tag.

I have looked into the dulwich's documentation and the way it works seems very bare-bones. Are there also alternatives which are easier to use?


The simplest way to get all tags using Dulwich is:

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

tags is now a dictionary mapping tags to commit SHA1s.

Checking out another branch:

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

Creating a branch:

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