GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

Possible new feature (a new commit query) if not implemented yet

Open leos313 opened this issue 5 years ago • 4 comments

When executing the following:

string=git.diff('HEAD~1')

it raises an exception when there are no previous commits. It could be a nice feature to have a kind of query that checks if the wanted commit exists (before executing instructions as reported).

I know it is possible to use a try and exception (and it is what I do in my code at line 99). However, I think it is still a nice feature to have.

I am writing this issue following the suggestion of @Byron in this closed issue.

Besides, if there are suggestions or advice to implement a script (using your API) for analyzing your own git repo and reporting the info in a meaningful way, please do not hesitate to write it down here. I am trying to implement it (for fun) in this little project.

leos313 avatar Feb 27 '20 14:02 leos313

this problem also related issues: #993 PR: #999 I think your 'HEAD' is dangling refs, so refs can not found your repo.

dongs0104 avatar Mar 18 '20 17:03 dongs0104

Having looked at it just now, I can only agree with @dongs0104 . However, as the code above calls git directly, this also means that git itself is unable to diff when the head is dangling.

For this issue it means it can be closed as GitPython is probably never going to catch git command failures.

Byron avatar Mar 21 '20 12:03 Byron

I'm not sure if you would find it worthwhile to a function that behaves like this:

repo = ...

def rev_exists(ref):
    try:
        repo.git.rev_parse('--verify', ref)
    except git.exc.GitCommandError:
        return False
    return True

This obviously still uses try/except, but it could be a useful wrapper. You could then run things like

if repo.rev_exists('HEAD~'):
    string=git.diff('HEAD~1')

I know this would be useful for me because it would feel more natural in the code, and if it's included in the repo object, I would be able to reduce the number of imports needed in my code

benthayer avatar Dec 06 '20 18:12 benthayer

I wouldn't mind seeing a PR for this. The method would probably be called ref_exists(…).

Byron avatar Dec 07 '20 02:12 Byron