Fix the preserving of the local changes during pop
[stgit] / stgit / commands / pop.py
index 9bee9f1..645eebd 100644 (file)
@@ -24,54 +24,61 @@ from stgit.utils import *
 from stgit import stack, git
 
 
-help = 'pop the top of the series'
-usage = """%prog [options]
+help = 'pop one or more patches from the stack'
+usage = """%prog [options] [<patch>]
 
 Pop the topmost patch or a range of patches starting with the topmost
 one from the stack. The command fails if there are local changes or
-conflicts."""
+conflicts. If a patch name is given as argument, the command will pop
+all the patches up to the given one."""
 
 options = [make_option('-a', '--all',
                        help = 'pop all the applied patches',
                        action = 'store_true'),
            make_option('-n', '--number', type = 'int',
                        help = 'pop the specified number of patches'),
-           make_option('-t', '--to', metavar = 'PATCH',
-                       help = 'pop all patches up to PATCH')]
+           make_option('-k', '--keep',
+                       help = 'keep the local changes',
+                       action = 'store_true')]
 
 
 def func(parser, options, args):
     """Pop the topmost patch from the stack
     """
-    if len(args) != 0:
+    if len(args) > 1:
         parser.error('incorrect number of arguments')
 
-    check_local_changes()
     check_conflicts()
     check_head_top_equal()
 
+    if not options.keep:
+        check_local_changes()
+
     applied = crt_series.get_applied()
     if not applied:
         raise CmdException, 'No patches applied'
+    # the popping is done in reverse order
     applied.reverse()
 
-    if options.to:
-        if options.to not in applied:
-            if options.to in crt_series.get_unapplied():
-                raise CmdException, 'Patch "%s" is not currently applied.' % options.to
-            else:
-                raise CmdException, 'Patch "%s" does not exist.' % options.to
-        patches = applied[:applied.index(options.to)]
+    if options.all:
+        patches = applied
     elif options.number:
         patches = applied[:options.number]
-    elif options.all:
-        patches = applied
+    elif len(args) == 1:
+        upto_patch = args[0]
+        if upto_patch not in applied:
+            if upto_patch in crt_series.get_unapplied():
+                raise CmdException, 'Patch "%s" is not currently applied' \
+                      % upto_patch
+            else:
+                raise CmdException, 'Patch "%s" does not exist' % upto_patch
+        patches = applied[:applied.index(upto_patch)]
     else:
         patches = [applied[0]]
 
     if patches == []:
         raise CmdException, 'No patches to pop'
 
-    pop_patches(patches)
+    pop_patches(patches, options.keep)
 
     print_crt_patch()