ishepard / pydriller

Python Framework to analyse Git repositories
http://pydriller.readthedocs.io/en/latest/
Apache License 2.0
804 stars 139 forks source link

a freshly initialized repository without commits fails here #292

Open lenntt opened 2 weeks ago

lenntt commented 2 weeks ago

doing only a 'git init' and then try to traverse commits on that repo, will fail here: https://github.com/ishepard/pydriller/blob/8bfbbbc82ab31cf9552f22cb79c846478df9ae37/pydriller/repository.py#L236

relates to this: https://stackoverflow.com/questions/15628720/fatal-bad-default-revision-head

Expected behavior: either:

   File "<myproj>/.venv/lib/python3.11/site-packages/pydriller/repository.py", line 236, in traverse_commits
      for job in executor.map(self._iter_commits, git.get_list_commits(rev, **kwargs)):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/homebrew/Cellar/python@3.11/3.11.7/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 608, in map
      fs = [self.submit(fn, *args) for args in zip(*iterables)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/opt/homebrew/Cellar/python@3.11/3.11.7/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 608, in <listcomp>
      fs = [self.submit(fn, *args) for args in zip(*iterables)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "<myproj>/.venv/lib/python3.11/site-packages/pydriller/git.py", line 121, in get_list_commits
      for commit in self.repo.iter_commits(rev=rev, **kwargs):
    File "<myproj>/.venv/lib/python3.11/site-packages/git/objects/commit.py", line 498, in _iter_from_process_or_stream
      finalize_process(proc_or_stream)
    File "<myproj>/.venv/lib/python3.11/site-packages/git/util.py", line 483, in finalize_process
      proc.wait(**kwargs)
    File "/<myproj>/.venv/lib/python3.11/site-packages/git/cmd.py", line 657, in wait
      raise GitCommandError(remove_password_if_present(self.args), status, errstr)
  git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
    cmdline: git rev-list --reverse --since=1999-04-30 17:27:25.723122+00:00 HEAD --
    stderr: 'fatal: bad revision 'HEAD'

I'm on git 2.39.3, MacOS.

ishepard commented 2 weeks ago

This is Git internal behaviour, so I'd keep it the same. I just tested it in my terminal:

> git init
> git rev-list HEAD --
fatal: bad revision 'HEAD'

We could parse the exception and check for this exact string, but if Git returns this, I'd say let's leave it 😄

lenntt commented 2 weeks ago

Fair, but didn't ask to do a git rev-list HEAD, I was asking to traverse_commits :) Other than putting a large try around my code, do you know of a better way? is there a call to pre-check HEAD?