Support automatically stashing local modifications during repo-rebase.

Currently repo-rebase requires that all modifications be committed
locally before it will allow the rebase. In high-velocity environments,
you may want to just pull in newer code without explicitly creating
local commits, which is typically achieved using git-stash.

If called with the --auto-stash command line argument, and it is
determined that the current index is dirty, the local modifications
are stashed, and the rebase continues.  If a stash was performed, that
stash is popped once the rebase completes.

Note that there is still a possibility that the git-stash pop will
result in a merge conflict.

Change-Id: Ibe3da96f0b4486cb7ce8d040639187e26501f6af
diff --git a/subcmds/rebase.py b/subcmds/rebase.py
index 7c8e938..20662b1 100644
--- a/subcmds/rebase.py
+++ b/subcmds/rebase.py
@@ -52,6 +52,9 @@
     p.add_option('--whitespace',
                  dest='whitespace', action='store', metavar='WS',
                  help='Pass --whitespace to git rebase')
+    p.add_option('--auto-stash',
+                 dest='auto_stash', action='store_true',
+                 help='Stash local modifications before starting')
 
   def Execute(self, opt, args):
     all = self.GetProjects(args)
@@ -103,5 +106,23 @@
       print >>sys.stderr, '# %s: rebasing %s -> %s' % \
         (project.relpath, cb, upbranch.LocalMerge)
 
+      needs_stash = False
+      if opt.auto_stash:
+        stash_args = ["update-index", "--refresh", "-q"]
+
+        if GitCommand(project, stash_args).Wait() != 0:
+          needs_stash = True
+          # Dirty index, requires stash...
+          stash_args = ["stash"]
+
+          if GitCommand(project, stash_args).Wait() != 0:
+            return -1
+
       if GitCommand(project, args).Wait() != 0:
         return -1
+
+      if needs_stash:
+        stash_args.append('pop')
+        stash_args.append('--quiet')
+        if GitCommand(project, stash_args).Wait() != 0:
+          return -1