Commit 2f3c820a authored by Michael Spang's avatar Michael Spang Committed by Commit Bot

Simplify GIT.IsValidRevision

We should pass --verify to tell rev-parse that we're just testing,
and use the ^{commit} dereference operator to avoid the hack of
removing a character from the commit hash to see if it's really
in the object database.

Bug: 938627

Change-Id: Ic6ea898b0a5a6a1a5d706c7586c7208ec8ca2ce2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1504104
Commit-Queue: Michael Spang <spang@chromium.org>
Reviewed-by: 's avatarEdward Lesmes <ehmaldonado@chromium.org>
parent b139178f
...@@ -355,20 +355,11 @@ class GIT(object): ...@@ -355,20 +355,11 @@ class GIT(object):
sha_only: Fail unless rev is a sha hash. sha_only: Fail unless rev is a sha hash.
""" """
# 'git rev-parse foo' where foo is *any* 40 character hex string will return
# the string and return code 0. So strip one character to force 'git
# rev-parse' to do a hash table look-up and returns 128 if the hash is not
# present.
lookup_rev = rev
if re.match(r'^[0-9a-fA-F]{40}$', rev):
lookup_rev = rev[:-1]
try: try:
sha = GIT.Capture(['rev-parse', lookup_rev], cwd=cwd).lower() sha = GIT.Capture(['rev-parse', '--verify', '%s^{commit}' % rev],
if lookup_rev != rev: cwd=cwd)
# Make sure we get the original 40 chars back.
return rev.lower() == sha
if sha_only: if sha_only:
return sha.startswith(rev.lower()) return sha == rev.lower()
return True return True
except subprocess2.CalledProcessError: except subprocess2.CalledProcessError:
return False return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment