Convert 'hide' to the lib infrastructure
[stgit] / stgit / commands / pop.py
index 246cc34..eace090 100644 (file)
@@ -16,75 +16,63 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
-import sys, os
-from optparse import OptionParser, make_option
-
-from stgit.commands.common import *
-from stgit.utils import *
-from stgit import stack, git
-
-
-help = 'pop one or more patches from the stack'
-usage = """%prog [options] [<patch1>] [<patch2>] [<patch3>..<patch4>]
-
+from stgit.commands import common
+from stgit.lib import transaction
+from stgit import argparse
+from stgit.argparse import opt
+
+help = 'Pop one or more patches from the stack'
+kind = 'stack'
+usage = ['[options] [<patch1>] [<patch2>] [<patch3>..<patch4>]']
+description = """
 Pop the topmost patch or a range of patches from the stack. The
 command fails if there are conflicts or local changes (and --keep was
 not specified).
 
 A series of pop and push operations are performed so that only the
 patches passed on the command line are popped from the stack. Some of
-the push operations may fail because of conflicts (push --undo would
+the push operations may fail because of conflicts ("stg undo" would
 revert the last push operation)."""
 
-directory = DirectoryHasRepository()
-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('-k', '--keep',
-                       help = 'keep the local changes',
-                       action = 'store_true')]
+args = [argparse.patch_range(argparse.applied_patches)]
+options = [
+    opt('-a', '--all', action = 'store_true',
+        short = 'Pop all the applied patches'),
+    opt('-n', '--number', type = 'int',
+        short = 'Pop the specified number of patches')
+    ] + argparse.keep_option()
 
+directory = common.DirectoryHasRepositoryLib()
 
 def func(parser, options, args):
-    """Pop the topmost patch from the stack
-    """
-    check_conflicts()
-    check_head_top_equal(crt_series)
-
-    if not options.keep:
-        check_local_changes()
+    """Pop the given patches or the topmost one from the stack."""
+    stack = directory.repository.current_stack
+    iw = stack.repository.default_iw
+    clean_iw = (not options.keep and iw) or None
+    trans = transaction.StackTransaction(stack, 'pop',
+                                         check_clean_iw = clean_iw)
 
-    applied = crt_series.get_applied()
-    if not applied:
-        raise CmdException, 'No patches applied'
+    if not trans.applied:
+        raise common.CmdException('No patches applied')
 
     if options.all:
-        patches = applied
+        patches = trans.applied
     elif options.number:
         # reverse it twice to also work with negative or bigger than
         # the length numbers
-        patches = applied[::-1][:options.number][::-1]
-    elif len(args) == 0:
-        patches = [applied[-1]]
+        patches = trans.applied[::-1][:options.number][::-1]
+    elif not args:
+        patches = [trans.applied[-1]]
     else:
-        patches = parse_patches(args, applied, ordered = True)
+        patches = common.parse_patches(args, trans.applied, ordered = True)
 
     if not patches:
-        raise CmdException, 'No patches to pop'
-
-    # pop to the most distant popped patch
-    topop = applied[applied.index(patches[0]):]
-    # push those not in the popped range
-    topush = [p for p in topop if p not in patches]
-
-    if options.keep and topush:
-        raise CmdException, 'Cannot pop arbitrary patches with --keep'
-
-    topop.reverse()
-    pop_patches(crt_series, topop, options.keep)
-    if topush:
-        push_patches(crt_series, topush)
-
-    print_crt_patch(crt_series)
+        raise common.CmdException('No patches to pop')
+
+    applied = [p for p in trans.applied if not p in set(patches)]
+    unapplied = patches + trans.unapplied
+    try:
+        trans.reorder_patches(applied, unapplied, iw = iw)
+    except transaction.TransactionException:
+        pass
+    return trans.run(iw)