diff --git a/PRESUBMIT.py b/PRESUBMIT.py index b72acc7623..0c9b71edab 100644 --- a/PRESUBMIT.py +++ b/PRESUBMIT.py @@ -79,7 +79,6 @@ def CommonChecks(input_api, output_api, tests_to_black_list, run_on_python3): r'.*ninjalog_uploader_test\.py$', r'.*recipes_test\.py$', r'.*roll_dep_test\.py$', - r'.*scm_unittest\.py$', r'.*subprocess2_test\.py$', ] diff --git a/scm.py b/scm.py index 27ab16f8e9..eb413a235e 100644 --- a/scm.py +++ b/scm.py @@ -375,25 +375,31 @@ class GIT(object): """Cleans up untracked file inside |relative_dir|.""" return bool(GIT.Capture(['clean', '-df', relative_dir], cwd=cwd)) + @staticmethod + def ResolveCommit(cwd, rev): + if sys.platform.startswith('win'): + # Windows .bat scripts use ^ as escape sequence, which means we have to + # escape it with itself for every .bat invocation. + needle = '%s^^{commit}' % rev + else: + needle = '%s^{commit}' % rev + try: + return GIT.Capture(['rev-parse', '--quiet', '--verify', needle], cwd=cwd) + except subprocess2.CalledProcessError: + return None + @staticmethod def IsValidRevision(cwd, rev, sha_only=False): """Verifies the revision is a proper git revision. sha_only: Fail unless rev is a sha hash. """ - if sys.platform.startswith('win'): - # Windows .bat scripts use ^ as escape sequence, which means we have to - # escape it with itself for every .bat invocation. - needle = '%s^^^^{commit}' % rev - else: - needle = '%s^{commit}' % rev - try: - sha = GIT.Capture(['rev-parse', '--verify', needle], cwd=cwd) - if sha_only: - return sha == rev.lower() - return True - except subprocess2.CalledProcessError: + sha = GIT.ResolveCommit(cwd, rev) + if sha is None: return False + if sha_only: + return sha == rev.lower() + return True @classmethod def AssertVersion(cls, min_version): diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index 70453467cf..41cc3b9ec5 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -92,6 +92,14 @@ class RealGitTest(fake_repos.FakeReposTestBase): else: self.skipTest('git fake repos not available') + def testResolveCommit(self): + self.assertIsNone(scm.GIT.ResolveCommit(self.cwd, 'zebra')) + self.assertIsNone(scm.GIT.ResolveCommit(self.cwd, 'r123456')) + first_rev = self.githash('repo_1', 1) + self.assertEqual(first_rev, scm.GIT.ResolveCommit(self.cwd, first_rev)) + self.assertEqual( + self.githash('repo_1', 2), scm.GIT.ResolveCommit(self.cwd, 'HEAD')) + def testIsValidRevision(self): # Sha1's are [0-9a-z]{32}, so starting with a 'z' or 'r' should always fail. self.assertFalse(scm.GIT.IsValidRevision(cwd=self.cwd, rev='zebra'))