Commit 85153289 authored by Edward Lemur's avatar Edward Lemur Committed by LUCI CQ

git-cl: Use scm.GIT.{Get,Set}BranchConfig and {Get,Set}BranchRef instead of git calls.

scm.GIT functions are tested, and using them makes it easier to mock
git calls on git-cl tests.

Bug: 1051631
Change-Id: If067aa3f71b7478cafd7985d3020dacc0c6a41c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2055464Reviewed-by: 's avatarAnthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
parent c4243361
This diff is collapsed.
......@@ -187,12 +187,18 @@ class GIT(object):
@staticmethod
def GetBranchRef(cwd):
"""Returns the full branch reference, e.g. 'refs/heads/master'."""
try:
return GIT.Capture(['symbolic-ref', 'HEAD'], cwd=cwd)
except subprocess2.CalledProcessError:
return None
@staticmethod
def GetBranch(cwd):
"""Returns the short branch name, e.g. 'master'."""
return GIT.ShortBranchName(GIT.GetBranchRef(cwd))
branchref = GIT.GetBranchRef(cwd)
if branchref:
return GIT.ShortBranchName(branchref)
return None
@staticmethod
def GetRemoteBranches(cwd):
......
This diff is collapsed.
......@@ -198,6 +198,20 @@ class RealGitTest(fake_repos.FakeReposTestBase):
scm.GIT.SetBranchConfig(self.cwd, branch, 'merge')
scm.GIT.SetBranchConfig(self.cwd, branch, 'remote')
def testGetBranchRef(self):
self.assertEqual('refs/heads/master', scm.GIT.GetBranchRef(self.cwd))
HEAD = scm.GIT.Capture(['rev-parse', 'HEAD'], cwd=self.cwd)
scm.GIT.Capture(['checkout', HEAD], cwd=self.cwd)
self.assertIsNone(scm.GIT.GetBranchRef(self.cwd))
scm.GIT.Capture(['checkout', 'master'], cwd=self.cwd)
def testGetBranch(self):
self.assertEqual('master', scm.GIT.GetBranch(self.cwd))
HEAD = scm.GIT.Capture(['rev-parse', 'HEAD'], cwd=self.cwd)
scm.GIT.Capture(['checkout', HEAD], cwd=self.cwd)
self.assertIsNone(scm.GIT.GetBranchRef(self.cwd))
scm.GIT.Capture(['checkout', 'master'], cwd=self.cwd)
if __name__ == '__main__':
if '-v' in sys.argv:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment