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: Idba96a0866146f05dd1773d2149db3716c2cda2d
Reviewed-on: http://gerrit.chromium.org/gerrit/2314
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 37a5673..7771e47 100644
--- a/lib/cros_build_lib.py
+++ b/lib/cros_build_lib.py
@@ -373,7 +373,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')