chiark / gitweb /
Generate command lists automatically
[stgit] / stgit / commands / clean.py
index 8f5c60645678de801b1d21aed3daa62f738637f3..27a77168eefad1e1f8d139c0e78f9499261b55d4 100644 (file)
@@ -15,44 +15,44 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
 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 = 'delete the empty patches in the series'
-usage = """%prog [options]
-
+from stgit.argparse import opt
+from stgit.out import *
+from stgit.commands import common
+from stgit.lib import transaction
+
+help = 'Delete the empty patches in the series'
+kind = 'stack'
+usage = ['']
+description = """
 Delete the empty patches in the whole series or only those applied or
 unapplied. A patch is considered empty if the two commit objects
 representing its boundaries refer to the same tree object."""
 
 Delete the empty patches in the whole series or only those applied or
 unapplied. A patch is considered empty if the two commit objects
 representing its boundaries refer to the same tree object."""
 
-options = [make_option('-a', '--applied',
-                       help = 'delete the empty applied patches',
-                       action = 'store_true'),
-           make_option('-u', '--unapplied',
-                       help = 'delete the empty unapplied patches',
-                       action = 'store_true')]
-
-
-def __delete_empty(patches, applied):
-    """Delete the empty patches
-    """
-    for p in patches:
-        if crt_series.empty_patch(p):
-            print 'Deleting patch "%s"...' % p,
-            sys.stdout.flush()
-
-            if applied and crt_series.patch_applied(p):
-                crt_series.pop_patch(p)
-            crt_series.delete_patch(p)
-
-            print 'done'
-        elif applied and crt_series.patch_unapplied(p):
-            crt_series.push_patch(p)
+options = [
+    opt('-a', '--applied', action = 'store_true',
+        short = 'Delete the empty applied patches'),
+    opt('-u', '--unapplied', action = 'store_true',
+        short = 'Delete the empty unapplied patches')]
+
+directory = common.DirectoryHasRepositoryLib()
+
+def _clean(stack, clean_applied, clean_unapplied):
+    trans = transaction.StackTransaction(stack, 'clean', allow_conflicts = True)
+    def del_patch(pn):
+        if pn in stack.patchorder.applied:
+            if pn == stack.patchorder.applied[-1]:
+                # We're about to clean away the topmost patch. Don't
+                # do that if we have conflicts, since that means the
+                # patch is only empty because the conflicts have made
+                # us dump its contents into the index and worktree.
+                if stack.repository.default_index.conflicts():
+                    return False
+            return clean_applied and trans.patches[pn].data.is_nochange()
+        elif pn in stack.patchorder.unapplied:
+            return clean_unapplied and trans.patches[pn].data.is_nochange()
+    for pn in trans.delete_patches(del_patch):
+        trans.push_patch(pn)
+    trans.run()
 
 def func(parser, options, args):
     """Delete the empty patches in the series
 
 def func(parser, options, args):
     """Delete the empty patches in the series
@@ -60,19 +60,8 @@ def func(parser, options, args):
     if len(args) != 0:
         parser.error('incorrect number of arguments')
 
     if len(args) != 0:
         parser.error('incorrect number of arguments')
 
-    check_local_changes()
-    check_conflicts()
-    check_head_top_equal()
-
     if not (options.applied or options.unapplied):
         options.applied = options.unapplied = True
 
     if not (options.applied or options.unapplied):
         options.applied = options.unapplied = True
 
-    if options.applied:
-        applied = crt_series.get_applied()
-        __delete_empty(applied, True)
-
-    if options.unapplied:
-        unapplied = crt_series.get_unapplied()
-        __delete_empty(unapplied, False)
-
-    print_crt_patch()
+    _clean(directory.repository.current_stack,
+           options.applied, options.unapplied)