I am writing a prepush githook using GitPython.
Here is a snippet of code:
local_ref, local_sha1, remote_ref, remote_sha1 = [line for line in sys.stdin][0].split(' ')
for i, commit in enumerate(repo.iter_commits('docs-and-config')):
print("remote_sha1 is {}\n".format(remote_sha1), "commit.hexsha is {}\n".format(commit.hexsha))
print(type(remote_sha1), type(commit.hexsha))
print(commit.hexsha == remote_sha1)
if commit.hexsha == remote_sha1:
remote_commit = commit
if i > 10:
break
Here is the result of the first three iterations:
remote_sha1 is 9c5e0c813f8ac8bf95997911c7845aec935f1d43
commit.hexsha is ad0632a1e17c03d65124154a7f1f8d7c23966fbf
<class 'str'> <class 'str'>
False
remote_sha1 is 9c5e0c813f8ac8bf95997911c7845aec935f1d43
commit.hexsha is e63f31ba923dca63917c1ed3a9d332f9c42baf83
<class 'str'> <class 'str'>
False
remote_sha1 is 9c5e0c813f8ac8bf95997911c7845aec935f1d43
commit.hexsha is 9c5e0c813f8ac8bf95997911c7845aec935f1d43
<class 'str'> <class 'str'>
False
Why is the third instance not equal? I've tried stripping whitespace from both strings; that makes no difference.
Apparently you have an extra \n
char in the strings you are parsing from stdin.
Try to do : remote_sha1 = remote_sha1.strip()
in the beginning of your for
loop.
Also, if you use !r
as a rendering type when calling str.format
, Python wll put '
around your strings, so you will be aware of whitespace there:
print("remote_sha1 is {!r}\n".format(remote_sha1), "commit.hexsha is {!r}\n".format(commit.hexsha))