Update prebuilt.py to upload correctly when tracking branch is a revision.

If the tracking branch is a revision, we shouldn't push there. We need to
look in the manifest for the default branch and use that instead.

BUG=chrome-os-partner:4169
TEST=Run prebuilt.py when tracking branch is a revision and verify it pushes
     to the right branch (i.e. the default branch defined in the manifest).

Change-Id: I3cdab81a9d8613bc30825b221ee169fb03211417
Reviewed-on: http://gerrit.chromium.org/gerrit/1749
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/lib/cros_build_lib.py b/lib/cros_build_lib.py
index e5f0972..69d501e 100644
--- a/lib/cros_build_lib.py
+++ b/lib/cros_build_lib.py
@@ -330,8 +330,12 @@
   return new_path
 
 
-def GetTrackingBranch(branch, cwd):
-  """Gets the tracking branch for the specified branch / directory.
+def GetPushBranch(branch, cwd):
+  """Gets the appropriate push branch for the specified branch / directory.
+
+  If branch has a valid tracking branch, we should push to that branch. If
+  the tracking branch is a revision, we can't push to that, so we should look
+  at the default branch from the manifest.
 
   Args:
     branch: Branch to examine for tracking branch.
@@ -341,8 +345,14 @@
   for key in ('remote', 'merge'):
     cmd = ['git', 'config', 'branch.%s.%s' % (branch, key)]
     info[key] = RunCommand(cmd, redirect_stdout=True, cwd=cwd).output.strip()
+  if not info['merge'].startswith('refs/heads/'):
+    manifest = RunCommand(['repo', 'manifest', '-o', '-'], redirect_stdout=True,
+                          cwd=cwd).output
+    m = re.search(r'<default[^>]*revision="(refs/heads/[^"]*)"', manifest)
+    assert m, "Can't find default revision in manifest"
+    info['merge'] = m.group(1)
   assert info['merge'].startswith('refs/heads/')
-  return info['merge'].replace('refs/heads', info['remote'])
+  return info['remote'], info['merge'].replace('refs/heads/', '')
 
 
 def GitPushWithRetry(branch, cwd, dryrun=False, retries=5):
@@ -359,12 +369,12 @@
     Raises:
       GitPushFailed if push was unsuccessful after retries
   """
-  tracking_branch = GetTrackingBranch(branch, cwd)
+  remote, push_branch = GetPushBranch(branch, cwd)
   for retry in range(1, retries + 1):
     try:
       RunCommand(['git', 'remote', 'update'], cwd=cwd)
-      RunCommand(['git', 'rebase', tracking_branch], cwd=cwd)
-      push_command = ['git', 'push']
+      RunCommand(['git', 'rebase', '%s/%s' % (remote, push_branch)], cwd=cwd)
+      push_command = ['git', 'push', remote, '%s:%s' % (branch, push_branch)]
       if dryrun:
         push_command.append('--dry-run')