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

I am having problems getting the following git repo information using python:

  • I would like to get a list of all the tags of this repository.
  • I would like to check out another branch as well as create a new branch for posting.
  • I would like to tag a commit with an annotated tag.

I looked into the dulwich documentation, and how it works seems very bare. Are there alternatives that are easier to use?

+4
source share
3 answers

The easiest way to get all tags using Dulwich:

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

are now dictionary mapping tags for fixing SHA1.

Checking another branch:

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

Create a branch:

 r.refs["refs/heads/foo"] = head_sha1_of_new_branch 
+5
source

Call git via subprocess . From one of my own programs:

 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 

Some examples:

 rv = gitcmd(['gc', '--auto', '--quiet',]) outp = gitcmd('status', True) 
+1
source

Now you can also get an alphabetically sorted list of tags.

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

All Articles