pydriller icon indicating copy to clipboard operation
pydriller copied to clipboard

a freshly initialized repository without commits fails here

Open lenntt opened this issue 9 months ago • 3 comments

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:

  • no commits to traverse, empty list
  • or, offer an api call to check if head points to a revision (is there one?)
   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/[email protected]/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/[email protected]/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.

lenntt avatar Apr 30 '24 15:04 lenntt

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 😄

ishepard avatar Apr 30 '24 16:04 ishepard

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?

lenntt avatar May 02 '24 05:05 lenntt

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

Just my two cents: We could make it configurable in the Repository class, with the current behavior as the default. This would allow users to choose to ignore it and return an empty object as alternative behavior. However, I'm not sure how frequent this scenario is.

This could be labeled as PR welcome.

stefanodallapalma avatar Aug 27 '24 12:08 stefanodallapalma