libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.62k stars 387 forks source link

object not found and KeyError when scanning commits #949

Closed yindaheng98 closed 4 years ago

yindaheng98 commented 5 years ago

Hi there, I'm using a python 3.7.2 and pygit2 in TravisCI for building and deploying my github pages. The python script is used to scan all the commit and find the latest commit time for every file. But now I got an error in TravisCI when building, just like this:

Traceback (most recent call last):
  File "travisPorcess.py", line 1, in <module>
    from meta import meta_data, path
  File "/home/travis/build/yindaheng98/My-docs/meta.py", line 5, in <module>
    from getDate import data
  File "/home/travis/build/yindaheng98/My-docs/getDate.py", line 71, in <module>
    for commit in commits:
KeyError: 'object not found - no match for id (c170b110850200770e9c0d1b35b2616dfe18614d)'

And this error was throw from:

repo = pygit2.Repository('./')
commits=repo.walk(repo.head.target,GIT_SORT_TOPOLOGICAL|GIT_SORT_REVERSE)

last_tree=None
for commit in commits:#<<<<<<<<-throw from here
    date=time.gmtime(commit.commit_time)
    date=time.strftime('%Y-%m-%d %H:%M:%S',date)
    print("scanning commit %s at %s"%(commit.message,date))

    tree=commit.tree
    if last_tree is None:
        last_tree=tree
        continue

    diff=tree.diff_to_tree(last_tree)
    updateData(date,diff)
    last_tree=tree

This error is only appear in TravisCI, it can done its work properly in my Notebook.

Is this error caused by bugs? Or just my improper usage?

PS: The complete python scripts is here

ghost commented 4 years ago

I have encountered the same issue (but running in GitLab)

dbolkensteyn commented 4 years ago

This might be the same issue as https://github.com/AArnott/Nerdbank.GitVersioning/issues/174: walking all commits require a full clone of the repository, not a shallow one.

In Travis CI, the shallow clone is disabled with the following YAML configuration:

git:
  depth: false
ghost commented 4 years ago

However, in my case the code is literally this:

        for commit in git.walk(git.head.target, GIT_SORT_TOPOLOGICAL):
            return commit.message

Is there any other way I could get the message of the latest commit on the current branch?

dbolkensteyn commented 4 years ago

I am not familiar with the Python bindings of libgit2, but you should be able to peel the head reference to a commit, without having to walk the history. Something like this:

return git.head.peel(pygit2.Commit).message
jdavid commented 4 years ago

To get the commit's time or message you need to load the object, if the object is not in the database it will fail. This may happen for example with shallow clones and with submodules (see issue #967).

This works as well:

repo.head.peel().commit_time
repo.head.peel().message

In any case be sure to use the latest version of pygit2 1.1.1, or at least 1.0.3