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: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/scm.py b/scm.py
index 35be1b0..d7b70ea 100644
--- a/scm.py
+++ b/scm.py
@@ -355,20 +355,11 @@
 
     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