diff --git a/scm.py b/scm.py index aade53e47..e99acc061 100644 --- a/scm.py +++ b/scm.py @@ -390,15 +390,20 @@ class GIT(object): except (subprocess2.CalledProcessError, ValueError): return None + @staticmethod + def ParseGitSvnSha1(output): + """Parses git-svn output for the first sha1.""" + match = re.search(r'[0-9a-fA-F]{40}', output) + return match.group(0) if match else None + @staticmethod def GetSha1ForSvnRev(cwd, rev): """Returns a corresponding git sha1 for a SVN revision.""" if not GIT.IsGitSvn(cwd=cwd): return None try: - lines = GIT.Capture( - ['svn', 'find-rev', 'r' + str(rev)], cwd=cwd).splitlines() - return lines[-1].strip() if lines else None + output = GIT.Capture(['svn', 'find-rev', 'r' + str(rev)], cwd=cwd) + return GIT.ParseGitSvnSha1(output) except subprocess2.CalledProcessError: return None diff --git a/tests/scm_unittest.py b/tests/scm_unittest.py index ca17cd39a..ca50790be 100755 --- a/tests/scm_unittest.py +++ b/tests/scm_unittest.py @@ -92,6 +92,7 @@ class GitWrapperTestCase(BaseSCMTestCase): 'IsGitSvn', 'IsValidRevision', 'MatchSvnGlob', + 'ParseGitSvnSha1', 'ShortBranchName', ] # If this test fails, you should add the relevant test. @@ -167,6 +168,16 @@ class RealGitSvnTest(fake_repos.FakeReposTestBase): self._capture(['reset', '--hard', 'HEAD^']) self.assertEquals(scm.GIT.GetGitSvnHeadRev(cwd=self.clone_dir), 1) + def testParseGitSvnSha1(self): + test_sha1 = 'a5c63ce8671922e5c59c0dea49ef4f9d4a3020c9' + expected_output = test_sha1 + '\n' + # Cygwin git-svn 1.7.9 prints extra escape sequences when run under + # TERM=xterm + cygwin_output = test_sha1 + '\n\033[?1034h' + + self.assertEquals(scm.GIT.ParseGitSvnSha1(expected_output), test_sha1) + self.assertEquals(scm.GIT.ParseGitSvnSha1(cygwin_output), test_sha1) + def testGetGetSha1ForSvnRev(self): if not self.enabled: return