Cleanup failed rebases in cros_build_lib.py.

If a rebase fails we should be nice and git rebase --abort so that we won't
leave the tree in a busted state.

BUG=chromium-os:16098
TEST=ran prebuilt test suite

Reviewed-on: http://gerrit.chromium.org/gerrit/2302
Reviewed-by: Chris Sosa <sosa@chromium.org>
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Tested-by: David James <davidjames@chromium.org>
(cherry picked from commit cb2d970da51c6c9ae9123a4a47bad6252c608ca2)

Change-Id: Idd21c33a5b77bbc574958db4a1b94f6ed538e566
Reviewed-on: http://gerrit.chromium.org/gerrit/2316
Reviewed-by: Scott Zawalski <scottz@chromium.org>
Tested-by: David James <davidjames@chromium.org>
diff --git a/lib/cros_build_lib.py b/lib/cros_build_lib.py
index 9b5b39d..bd2743a 100644
--- a/lib/cros_build_lib.py
+++ b/lib/cros_build_lib.py
@@ -339,7 +339,13 @@
   for retry in range(1, retries + 1):
     try:
       RunCommand(['git', 'remote', 'update'], cwd=cwd)
-      RunCommand(['git', 'rebase', '%s/%s' % (remote, push_branch)], cwd=cwd)
+      try:
+        RunCommand(['git', 'rebase', '%s/%s' % (remote, push_branch)], cwd=cwd)
+      except RunCommandError:
+        # Looks like our change conflicts with upstream. Cleanup our failed
+        # rebase.
+        RunCommand(['git', 'rebase', '--abort'], error_ok=True, cwd=cwd)
+        raise
       push_command = ['git', 'push', remote, '%s:%s' % (branch, push_branch)]
       if dryrun:
         push_command.append('--dry-run')