From 2f3c820ae30d2d91aed5b451af7d9f1a4b73516f Mon Sep 17 00:00:00 2001 From: Michael Spang Date: Thu, 7 Mar 2019 20:05:07 +0000 Subject: [PATCH] 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 Reviewed-by: Edward Lesmes --- scm.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/scm.py b/scm.py index 35be1b0a1..d7b70ea66 100644 --- a/scm.py +++ b/scm.py @@ -355,20 +355,11 @@ class GIT(object): 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: - sha = GIT.Capture(['rev-parse', lookup_rev], cwd=cwd).lower() - if lookup_rev != rev: - # Make sure we get the original 40 chars back. - return rev.lower() == sha + sha = GIT.Capture(['rev-parse', '--verify', '%s^{commit}' % rev], + cwd=cwd) if sha_only: - return sha.startswith(rev.lower()) + return sha == rev.lower() return True except subprocess2.CalledProcessError: return False