0

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.

Katie
  • 808
  • 1
  • 11
  • 28
  • Does this answer your question? [Why are my two python strings not equal in my program but equal in the interpreter?](https://stackoverflow.com/questions/36208446/why-are-my-two-python-strings-not-equal-in-my-program-but-equal-in-the-interpret) – Xhuliano Brace Dec 20 '19 at 17:59

1 Answers1

1

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))
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • I'm so embarrassed! I really tried to test for the effect of stripping whitespace -- I even said so in the post. But I think the way I did it I didn't notice that my test had actually succeeded. Thanks for the tip about `!r`. I'm not said I asked just so I could learn that. – Katie Dec 20 '19 at 18:06