The Wayback Machine - https://web.archive.org/web/20250901064905/https://github.com/gitpython-developers/GitPython/issues/1092
Skip to content

Fetching Submodule in Tree raises AttributeError: "Cannot retrieve the name of a submodule if it was not set initially" #1092

@ryan-williams

Description

@ryan-williams

Check out toy example repo w/ 2 submodules (named generate-random-ints and sort):

git clone --recurse-submodules https://gitlab.com/gsmo/examples/submodule-demo.git
cd submodule-demo
git checkout e6f6d8f
git submodule update
python

In Python REPL:

import git
repo = git.Repo()
tree = repo.tree()
tree['sort']
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/Users/ryan/.local/lib/python3.8/site-packages/git/objects/submodule/base.py", line 169, in __repr__
#     % (type(self).__name__, self._name, self.path, self.url, self.branch_path)
#   File "/Users/ryan/.pyenv/versions/3.8.5/lib/python3.8/site-packages/gitdb/util.py", line 253, in __getattr__
#     self._set_cache_(attr)
#   File "/Users/ryan/.local/lib/python3.8/site-packages/git/objects/submodule/base.py", line 132, in _set_cache_
#     raise AttributeError("Cannot retrieve the name of a submodule if it was not set initially")
# AttributeError: Cannot retrieve the name of a submodule if it was not set initially
# >>> tree['generate-random-ints']
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/Users/ryan/.local/lib/python3.8/site-packages/git/objects/submodule/base.py", line 169, in __repr__
#     % (type(self).__name__, self._name, self.path, self.url, self.branch_path)
#   File "/Users/ryan/.pyenv/versions/3.8.5/lib/python3.8/site-packages/gitdb/util.py", line 253, in __getattr__
#     self._set_cache_(attr)
#   File "/Users/ryan/.local/lib/python3.8/site-packages/git/objects/submodule/base.py", line 132, in _set_cache_
#     raise AttributeError("Cannot retrieve the name of a submodule if it was not set initially")
# AttributeError: Cannot retrieve the name of a submodule if it was not set initially

It would be nice (and consistent with the Git CLI) to expose Submodules in Tree objects; analogous Git CLI operation:

git ls-tree HEAD
# 100644 blob 53d0e7837b21f6c964832001a428e2eac023ae3e	.gitmodules
# 160000 commit 1dcac12d613a0e405db510a28123e23fda6bd1c6	generate-random-ints
# 040000 tree a0440560b281de673b23324a9693782f848791b1	nbs
# 100644 blob 85543297722cd8f862ca50aaaf6ca54c5ea9055b	run.ipynb
# 160000 commit de538ba56a5929cab0465ea29478bd6af2ba974b	sort

It seems like this is maybe supposed to work, and the AttributeError above is just a bug?

In any case, given this issue, I can't find a way to access the submodule commit at a given parent commit. The natural interface would seem to be e.g.:

tree = repo.tree('6e2e388')
tree['sort']  # should return a reference to Submodule commit 36a9654716fe3a317aae4a4a96f95bdff4805625, instead raises AttributeError as above

Git CLI makes it easy to introspect trees' submodule states at any prior commit:

git ls-tree 6e2e388
# 100644 blob 53d0e7837b21f6c964832001a428e2eac023ae3e	.gitmodules
# 160000 commit 3ed135bf1b90b90b007f4cbd47c0f9496d7a4924	generate-random-ints
# 100644 blob 85543297722cd8f862ca50aaaf6ca54c5ea9055b	run.ipynb
# 160000 commit 36a9654716fe3a317aae4a4a96f95bdff4805625	sort

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      HTTPS · web.archive.org
      ← Home