Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 998
GorillaPatch Avatar asked Apr 09 '13 18:04

GorillaPatch


Video Answer


2 Answers

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")
r.reset_index()

Creating a branch:

r.refs["refs/heads/foo"] = head_sha1_of_new_branch
like image 105
jelmer Avatar answered Oct 04 '22 08:10

jelmer


Now you can also get an alphabetically sorted list of tag labels.

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

repo = Repo('.')
tag_labels = tag_list(repo)
like image 38
Alex Volkov Avatar answered Oct 04 '22 07:10

Alex Volkov