Skip to content

Exception suggestion: raise when making a Commit with non-commit binsha #2254

Description

@jeffro256

Consider the following code snippet:

import git
repo = git.Repo('GitPython')
tag = repo.rev_parse('3.1.62')
commit = git.Commit(repo, tag.binsha)
index_file = git.IndexFile.new(repo, commit.tree)

If GitPython is a repo in the CWD, then upon running the above code, you will see a traceback like so:

Traceback (most recent call last):
  File "my_python_file.py", line 5, in <module>
    index_file = git.IndexFile.new(repo, commit.tree)
  File "/usr/lib/python3.14/site-packages/git/index/base.py", line 330, in new
    base_entries = aggressive_tree_merge(repo.odb, tree_sha_bytes)
  File "/usr/lib/python3.14/site-packages/git/index/fun.py", line 505, in aggressive_tree_merge
    for entry in traverse_tree_recursive(odb, tree_shas[-1], ""):
                 ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.14/site-packages/git/objects/fun.py", line 271, in traverse_tree_recursive
    data = tree_entries_from_data(odb.stream(tree_sha).read())
  File "/usr/lib/python3.14/site-packages/git/objects/fun.py", line 111, in tree_entries_from_data
    while data[i] != 0:
          ~~~~^^^
IndexError: index out of range

This throws an out-of-bounds array index exception when trying to parse the tag's database stream as a tree, which should fail. However, the real user error occurred when trying to make a Commit object from a tag object's binsha: commit = git.Commit(repo, tag.binsha). The Commit constructor should raise an Exception with a message to this effect. For example, "Given sha value does not refer to a commit`.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions