bisect-kit: make bisect_git accept shortened id or ref names

BUG=None
TEST=run bisect_git.py manually and pass shortened id to --old and --new

Change-Id: I9ea21b6b15301e47516a31ccfb3df75e7d550808
Reviewed-on: https://chromium-review.googlesource.com/1394604
Commit-Ready: Kuang-che Wu <kcwu@chromium.org>
Tested-by: Kuang-che Wu <kcwu@chromium.org>
Tested-by: Ross Zwisler <zwisler@chromium.org>
Reviewed-by: Chung-yih Wang <cywang@chromium.org>
Reviewed-by: Ross Zwisler <zwisler@chromium.org>
diff --git a/bisect_kit/git_util.py b/bisect_kit/git_util.py
index c0657d7..f633dc0 100644
--- a/bisect_kit/git_util.py
+++ b/bisect_kit/git_util.py
@@ -264,10 +264,21 @@
 
   Returns:
     full git commit hash
+
+  Raises:
+    ValueError: `rev` is not unique or doesn't exist
   """
-  cmd = ['git', 'rev-parse', rev]
-  git_rev = util.check_output(*cmd, cwd=git_repo).strip()
-  assert git_rev
+  try:
+    # Use '^{commit}' to restrict search only commits.
+    # Use '--' to avoid ambiguity, like matching rev against path name.
+    output = util.check_output(
+        'git', 'rev-parse', '%s^{commit}' % rev, '--', cwd=git_repo)
+    git_rev = output.rstrip('-\n')
+  except subprocess.CalledProcessError:
+    # Do not use 'git rev-parse --disambiguate' to determine uniqueness
+    # because it searches objects other than commits as well.
+    raise ValueError('%s is not unique or does not exist' % rev)
+  assert is_git_rev(git_rev)
   return git_rev