Allow deletion of several patches at once
authorKarl Hasselström <kha@treskal.com>
Thu, 2 Nov 2006 21:18:33 +0000 (21:18 +0000)
committerCatalin Marinas <catalin.marinas@gmail.com>
Thu, 2 Nov 2006 21:18:33 +0000 (21:18 +0000)
Signed-off-by: Karl Hasselström <kha@treskal.com>
stgit/commands/delete.py
t/t1600-delete-one.sh [new file with mode: 0755]
t/t1601-delete-many.sh [new file with mode: 0755]

index c97d8ed..6f12a15 100644 (file)
@@ -24,30 +24,58 @@ from stgit.utils import *
 from stgit import stack, git
 
 
-help = 'remove the topmost or any unapplied patch'
-usage = """%prog [options] <patch>
+help = 'delete patches'
+usage = """%prog [options] <patch1> [<patch2>] [<patch3>..<patch4>]
 
-Delete the patch passed as argument. The patch to be deleted can only
-be part of the unapplied list or be the topmost one, in the latter
-case the command also popping it from the stack. Note that the
-'delete' operation is irreversible."""
+Delete the patches passed as arguments. If an applied patch is to be
+deleted, all other patches applied on top of it must be deleted too,
+and they must be explicitly specified, since this command will not try
+to delete a patch unless you explicitly ask it to. If any applied
+patches are deleted, they are popped from the stack.
+
+Note that the 'delete' operation is irreversible."""
 
 options = [make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one')]
 
 def func(parser, options, args):
-    """Deletes a patch
-    """
-    if len(args) != 1:
-        parser.error('incorrect number of arguments')
+    """Deletes one or more patches."""
+    applied_patches = crt_series.get_applied()
+    unapplied_patches = crt_series.get_unapplied()
+    all_patches = applied_patches + unapplied_patches
+
+    if args:
+        patches = parse_patches(args, all_patches)
+    else:
+        parser.error('No patches specified')
+
+    applied = {}
+    unapplied = {}
+    for patch in patches:
+        if patch in unapplied_patches:
+            unapplied[patch] = None
+        else:
+            applied[patch] = None
 
-    if args[0] == crt_series.get_current():
+    while crt_series.get_current() in applied:
+        patch = crt_series.get_current()
         check_local_changes()
         check_conflicts()
         check_head_top_equal()
+        crt_series.delete_patch(patch)
+        del applied[patch]
+        print 'Patch "%s" successfully deleted' % patch
+
+    for patch in unapplied.iterkeys():
+        crt_series.delete_patch(patch)
+        print 'Patch "%s" successfully deleted' % patch
+
+    if applied:
+        print 'Error: failed to delete %s' % ', '.join(applied.iterkeys())
 
-    crt_series.delete_patch(args[0])
-    print 'Patch "%s" successfully deleted' % args[0]
+    failed = len(applied)
+    if failed:
+        raise CmdException, 'Failed to delete %d patches' % failed
 
     if not options.branch:
         print_crt_patch()
diff --git a/t/t1600-delete-one.sh b/t/t1600-delete-one.sh
new file mode 100755 (executable)
index 0000000..df03d79
--- /dev/null
@@ -0,0 +1,109 @@
+#!/bin/sh
+# Copyright (c) 2006 Karl Hasselström
+test_description='Test the delete command (deleting one patch at a time).'
+. ./test-lib.sh
+
+test_expect_success \
+    'Initialize the StGIT repository' \
+    'stg init'
+
+test_expect_success \
+    'Create a patch' \
+    '
+    stg new foo -m foo &&
+    echo foo > foo.txt &&
+    stg add foo.txt &&
+    stg refresh
+    '
+
+test_expect_success \
+    'Try to delete a non-existing patch' \
+    '
+    [ $(stg applied | wc -l) -eq 1 ] &&
+    ! stg delete bar &&
+    [ $(stg applied | wc -l) -eq 1 ]
+    '
+
+test_expect_success \
+    'Try to delete the topmost patch while dirty' \
+    '
+    echo dirty >> foo.txt &&
+    [ $(stg applied | wc -l) -eq 1 ] &&
+    ! stg delete foo &&
+    [ $(stg applied | wc -l) -eq 1 ] &&
+    git reset --hard
+    '
+
+test_expect_success \
+    'Delete the topmost patch' \
+    '
+    [ $(stg applied | wc -l) -eq 1 ] &&
+    stg delete foo &&
+    [ $(stg applied | wc -l) -eq 0 ]
+    '
+
+test_expect_success \
+    'Create an unapplied patch' \
+    '
+    stg new foo -m foo &&
+    echo foo > foo.txt &&
+    stg add foo.txt &&
+    stg refresh &&
+    stg pop
+    '
+
+test_expect_success \
+    'Delete an unapplied patch' \
+    '
+    [ $(stg unapplied | wc -l) -eq 1 ] &&
+    stg delete foo &&
+    [ $(stg unapplied | wc -l) -eq 0 ]
+    '
+
+test_expect_success \
+    'Create two patches' \
+    '
+    stg new foo -m foo &&
+    echo foo > foo.txt &&
+    stg add foo.txt &&
+    stg refresh &&
+    stg new bar -m bar &&
+    echo bar > bar.txt &&
+    stg add bar.txt &&
+    stg refresh
+    '
+
+test_expect_success \
+    'Try to delete a non-topmost applied patch' \
+    '
+    [ $(stg applied | wc -l) -eq 2 ] &&
+    ! stg delete foo &&
+    [ $(stg applied | wc -l) -eq 2 ]
+    '
+
+test_expect_success \
+    'Create another branch, and put one patch in each branch' \
+    '
+    stg branch --create br &&
+    stg new baz -m baz &&
+    echo baz > baz.txt &&
+    stg add baz.txt &&
+    stg refresh &&
+    stg branch master &&
+    stg new baz -m baz &&
+    echo baz > baz.txt &&
+    stg add baz.txt &&
+    stg refresh
+    '
+
+test_expect_success \
+    'Delete a patch in another branch' \
+    '
+    [ $(stg applied | wc -l) -eq 3 ] &&
+    [ $(stg applied -b br | wc -l) -eq 1 ] &&
+    stg delete -b br baz &&
+    [ $(stg applied | wc -l) -eq 3 ] &&
+    [ $(stg applied -b br | wc -l) -eq 0 ]
+    '
+
+test_done
diff --git a/t/t1601-delete-many.sh b/t/t1601-delete-many.sh
new file mode 100755 (executable)
index 0000000..8648ed1
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/sh
+# Copyright (c) 2006 Karl Hasselström
+test_description='Test the delete command (deleting many patches at once).'
+. ./test-lib.sh
+
+test_expect_success \
+    'Initialize the StGIT repository' \
+    'stg init'
+
+test_expect_success \
+    'Create five applied and five unapplied patches' \
+    '
+    stg new foo0 -m foo0 &&
+    echo foo0 > foo.txt &&
+    stg add foo.txt &&
+    stg refresh &&
+    for i in 1 2 3 4 5 6 7 8 9; do
+        stg new foo$i -m foo$i &&
+        echo foo$i >> foo.txt &&
+        stg refresh;
+    done &&
+    stg pop -n 5
+    '
+
+test_expect_success \
+    'Delete some patches' \
+    '
+    [ $(stg applied | wc -l) -eq 5 ] &&
+    [ $(stg unapplied | wc -l) -eq 5 ] &&
+    stg delete foo7 foo6 foo3 foo4 &&
+    [ $(stg applied | wc -l) -eq 3 ] &&
+    [ $(stg unapplied | wc -l) -eq 3 ]
+    '
+
+test_expect_success \
+    'Delete some more patches, some of which do not exist' \
+    '
+    [ $(stg applied | wc -l) -eq 3 ] &&
+    [ $(stg unapplied | wc -l) -eq 3 ] &&
+    ! stg delete foo7 foo8 foo2 foo0 &&
+    [ $(stg applied | wc -l) -eq 3 ] &&
+    [ $(stg unapplied | wc -l) -eq 3 ]
+    '
+
+test_expect_success \
+    'Delete a range of patches' \
+    '
+    [ $(stg applied | wc -l) -eq 3 ] &&
+    [ $(stg unapplied | wc -l) -eq 3 ] &&
+    stg delete foo1..foo8 &&
+    [ $(stg applied | wc -l) -eq 1 ] &&
+    [ $(stg unapplied | wc -l) -eq 1 ]
+    '
+
+test_done