Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GitPython does not contain a 'is_valid_hash' method. #1266

Answered by Byron
bytefluxio asked this question in Q&A
Discussion options

Am I missing something or is that currently missing?

The git command would be git cat-file commit {git_hash}.

I used the following before using GitPython:

def git_hash_is_valid(git_hash):
 return subprocess_return_call(f"git cat-file commit {git_hash}") == 0
def subprocess_return_call(call_string):
 call_parameters = call_string.split(" ")
 return subprocess.call(
 call_parameters,
 stderr=subprocess.STDOUT,
 stdout=open(os.devnull, 'w')
 )

Would a method like that fit into repo directly like repo.is_ancestor()?

I know self.repo.commit( works, but that does not differentiate between refs and commit hashes.

You must be logged in to vote

The fastest way to do this is this one:

oinfo = repo.odb.info(sha1)

As it will call git cat-file under the hood it might be that other forms of rev names are also supported, like branch names and the likes. It's certainly something to test out if that's a problem in this case.

A PR is welcome in case you would like to add a method to test for an objects existence on a Repo.

Replies: 3 comments

Comment options

PS: If this is something that would be ok to include, I'd create the PR, I'm currently doing it with this:

 def hash_is_valid(self, git_hash):
 try:
 self.repo.commit(git_hash)
 except ValueError:
 return False
 return True
You must be logged in to vote
0 replies
Comment options

PPS: We could also add default values to the repo.commit( method, to add
SymbolicReferences=True, Commit=True, TagObject=True, Blob=True, Tree=True

You must be logged in to vote
0 replies
Comment options

The fastest way to do this is this one:

oinfo = repo.odb.info(sha1)

As it will call git cat-file under the hood it might be that other forms of rev names are also supported, like branch names and the likes. It's certainly something to test out if that's a problem in this case.

A PR is welcome in case you would like to add a method to test for an objects existence on a Repo.

You must be logged in to vote
0 replies
Answer selected by Byron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1265 on June 05, 2021 02:15.

AltStyle によって変換されたページ (->オリジナル) /