pygit2
pygit2 copied to clipboard
RuntimeError if submodule has no branch
If a submodule has been added without the --branch option, accessing the Submodule.branch property raises a RuntimeError:
❯❯❯ git init
Initialized empty Git repository in /tmp/gregoire/tmp.R3p5f9ZTwD/.git/
❯❯❯ git submodule add https://github.com/libgit2/pygit2.git pygit2 --branch=master
[...]
❯❯❯ python3 -c 'import pygit2; print(pygit2.Repository(".").lookup_submodule("pygit2").branch)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/gregoire/.local/lib/python3.7/site-packages/pygit2/submodule.py", line 80, in branch
return ffi.string(branch).decode('utf-8')
RuntimeError: cannot use string() on <cdata 'char *' NULL>
It works fine if --branch is added to git submodule add:
❯❯❯ git init
Initialized empty Git repository in /tmp/gregoire/tmp.R3p5f9ZTwD/.git/
❯❯❯ git submodule add https://github.com/libgit2/pygit2.git pygit2
[...]
❯❯❯ python3 -c 'import pygit2; print(pygit2.Repository(".").lookup_submodule("pygit2").branch)'
master
I made a workaround by writing my own branch "getter":
def get_submodule_branch(submodule):
_c_branch = pygit2.C.git_submodule_branch(submodule._subm)
if _c_branch == pygit2.ffi.NULL:
return None
return pygit2.ffi.string(_c_branch).decode('utf-8')