git: add GetCurrentBranchOrId
Android CQ testing of 9999 ebuild changes needs to work in a detached
state.
Also adjust the call in cros_mark_android_as_stable.
BUG=b:175901991
TEST=Unit tests, Dry-run
Change-Id: I416d657be73873c18ed61d266143e39837fe1f82
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2618818
Tested-by: LaMont Jones <lamontjones@chromium.org>
Commit-Queue: Alex Klein <saklein@chromium.org>
Auto-Submit: LaMont Jones <lamontjones@chromium.org>
Reviewed-by: Alex Klein <saklein@chromium.org>
diff --git a/lib/git.py b/lib/git.py
index 0f8f939..c4660ce 100644
--- a/lib/git.py
+++ b/lib/git.py
@@ -213,6 +213,12 @@
return True
+def GetCurrentBranchOrId(cwd):
+ """Returns current branch of a repo, commit ID if repo is on detached HEAD."""
+ return GetCurrentBranch(cwd) or RunGit(cwd,
+ ['rev-parse', 'HEAD']).output.strip()
+
+
def GetCurrentBranch(cwd):
"""Returns current branch of a repo, and None if repo is on detached HEAD."""
try:
diff --git a/lib/git_unittest.py b/lib/git_unittest.py
index d868595..afb3383 100644
--- a/lib/git_unittest.py
+++ b/lib/git_unittest.py
@@ -109,6 +109,20 @@
git.FindGitTopLevel(self.fake_path)
self.assertCommandContains(['--show-toplevel'])
+ def testGetCurrentBranchOrId_NoBranch(self):
+ test_hash = '5' * 40
+ self.rc.AddCmdResult(partial_mock.In('symbolic-ref'), returncode=1)
+ self.rc.AddCmdResult(
+ partial_mock.In('rev-parse'), output='%s\n' % test_hash)
+ self.assertEqual(git.GetCurrentBranchOrId(self.fake_path), test_hash)
+ self.assertCommandContains(['rev-parse', 'HEAD'])
+
+ def testGetCurrentBranchOrId_OnBranch(self):
+ self.rc.AddCmdResult(
+ partial_mock.In('symbolic-ref'), output='refs/heads/branch\n')
+ self.assertEqual(git.GetCurrentBranchOrId(self.fake_path), 'branch')
+ self.assertCommandContains(['symbolic-ref', '-q', 'HEAD'])
+
def testAddPath(self):
git.AddPath(self.fake_path)
self.assertCommandContains(['add'])
diff --git a/scripts/cros_mark_android_as_stable.py b/scripts/cros_mark_android_as_stable.py
index 3b33407..ca2e1ef 100644
--- a/scripts/cros_mark_android_as_stable.py
+++ b/scripts/cros_mark_android_as_stable.py
@@ -670,7 +670,7 @@
logging.info('No stable candidate found.')
tracking_branch = 'remotes/m/%s' % os.path.basename(options.tracking_branch)
- existing_branch = git.GetCurrentBranch(android_package_dir)
+ existing_branch = git.GetCurrentBranchOrId(android_package_dir)
work_branch = cros_mark_as_stable.GitBranch(constants.STABLE_EBUILD_BRANCH,
tracking_branch,
android_package_dir)