Search code examples
pythongitgitpython

howto use gitpython repo.archive() for a tag


I have to export a specific path of a tag. The git command is git archive <tag> but I didn't found a possibility to do this with gitpython

I've tried

repo.archive(tar, "<tag>")

without luck.


Solution

  • import git
    import os.path
    
    repopath = '/path/to/repo'
    repo = git.Repo(repopath)
    repo.git.archive('<tag>', '-o', '<tag>.zip')
    if os.path.exists('<tag>.zip'):
        pass
    

    You can translate almost all git commands to repo.git.<cmd>(arg0, arg1, ...). Need to replace - in the command name with _.

    git log --oneline    ->    output = repo.git.log('--oneline')
    git commit --allow-empty -m "foo bar" ->  output = repo.git.commit('--allow-empty', '-m', 'foo bar')
    git ls-tree -r -t HEAD -> output = repo.git.ls_tree('-r', '-t', 'HEAD')