{"thread":{"id":"30207","subject":"[PATCH] stg delete --empty","startedAt":"2012-04-11T14:12:36Z","lastAt":"2012-04-18T11:23:26Z","messageCount":3,"participants":["Stepan Koltsov","Catalin Marinas"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"188962","messageId":"B940726E-2CFA-45CB-8B81-A8F4268D51A6@jetbrains.com","threadId":"30207","inReplyTo":null,"subject":"[PATCH] stg delete --empty","fromName":"Stepan Koltsov","fromEmail":"stepan.koltsov@jetbrains.com","sentAt":"2012-04-11T14:12:36Z","receivedAt":"2012-04-11T14:12:36Z","isPatch":true,"sender":{"key":"stepan.koltsov@jetbrains.com","avatar":null},"body":"`stg delete --empty` deletes all empty patches in series. Command\ndoes nothing and exits with zero code if all patches are not empty.\n\n--empty option can be useful in this workflow:\n\n* you work on the patches\n* you send patches to upstream (using stg export or stg mail)\n* patches are committed to upstream after a while\n* you do `git fetch && stg rebase origin`\n* `stg rebase origin` makes applied to upstrem local patches empty\n* now you can use stg delete --empty to cleanup\n\nSigned-off-by: Stepan Koltsov <stepan.koltsov@jetbrains.com>\n---\nstgit/commands/delete.py |   30 ++++++++++++++++++++++++--\nt/t1603-delete-empty.sh  |   51 ++++++++++++++++++++++++++++++++++++++++++++++\n2 files changed, 78 insertions(+), 3 deletions(-)\ncreate mode 100755 t/t1603-delete-empty.sh\n\ndiff --git a/stgit/commands/delete.py b/stgit/commands/delete.py\nindex 84a057e..414f456 100644\n--- a/stgit/commands/delete.py\n+++ b/stgit/commands/delete.py\n@@ -1,3 +1,4 @@\n+from stgit.out import MessagePrinter\n\n__copyright__ = \"\"\"\nCopyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>\n@@ -40,7 +41,9 @@ options = [\n    opt('-b', '--branch', args = [argparse.stg_branches],\n        short = 'Use BRANCH instead of the default branch'),\n    opt('-t', '--top', action = 'store_true',\n-        short = 'Delete top patch'),]\n+        short = 'Delete top patch'),\n+    opt('-e', '--empty', action = 'store_true',\n+        short = 'Delete empty patches')]\n\ndirectory = common.DirectoryHasRepositoryLib()\n\n@@ -51,8 +54,17 @@ def func(parser, options, args):\n        iw = None # can't use index/workdir to manipulate another branch\n    else:\n        iw = stack.repository.default_iw\n-    if args and options.top:\n-        parser.error('Either --top or patches must be specified')\n+        \n+    mode_count = 0\n+    if args:\n+        mode_count += 1\n+    if options.top:\n+        mode_count += 1\n+    if options.empty:\n+        mode_count += 1\n+        \n+    if mode_count > 1:\n+        parser.error('Either --top or --empty or patches must be specified')\n    elif args:\n        patches = set(common.parse_patches(args, list(stack.patchorder.all),\n                                           len(stack.patchorder.applied)))\n@@ -62,6 +74,16 @@ def func(parser, options, args):\n            patches = set([applied[-1]])\n        else:\n            raise common.CmdException, 'No patches applied'\n+\n+    elif options.empty:\n+        patches_list = []\n+        for p in stack.patchorder.all:\n+            if stack.patches.get(p).is_empty():\n+                patches_list.append(p)\n+        if not patches_list:\n+            out.info('No empty patches')\n+            return 0\n+        patches = set(patches_list)\n    else:\n        parser.error('No patches specified')\n\n@@ -86,3 +108,5 @@ def func(parser, options, args):\n    except transaction.TransactionHalted:\n        pass\n    return trans.run(iw)\n+\n+out = MessagePrinter()\ndiff --git a/t/t1603-delete-empty.sh b/t/t1603-delete-empty.sh\nnew file mode 100755\nindex 0000000..8a5630a\n--- /dev/null\n+++ b/t/t1603-delete-empty.sh\n@@ -0,0 +1,51 @@\n+#!/bin/sh -e\n+# Copyright (c) 2012 Stepan Koltsov\n+test_description='Test the delete --empty command.'\n+\n+. ./test-lib.sh\n+\n+test_expect_success \\\n+    'Initialize the StGIT repository' \\\n+    'stg init'\n+\n+test_expect_success \\\n+    'Create first real patch' \\\n+    '\n+    stg new foo -m foo &&\n+    echo foo > foo.txt &&\n+    stg add foo.txt &&\n+    stg refresh\n+    '\n+\n+test_expect_success \\\n+    'Create second empty patch' \\\n+    'stg new bar -m bar'\n+\n+test_expect_success \\\n+    'Create third real patch' \\\n+    '\n+    stg new baz -m foo &&\n+    echo baz > baz.txt &&\n+    stg add baz.txt &&\n+    stg refresh\n+    '\n+\n+test_expect_success \\\n+    'Create forth empty patch' \\\n+    'stg new qux -m qux'\n+\n+test_expect_success \\\n+    'Delete empty patches' \\\n+    '\n+    stg delete --empty &&\n+    [ \"$(echo $(stg series --noprefix))\" = \"foo baz\" ]\n+    '\n+\n+test_expect_success \\\n+    'Delete empty patches again (i. e. delete nothing)' \\\n+    '\n+    stg delete --empty &&\n+    [ \"$(echo $(stg series --noprefix))\" = \"foo baz\" ]\n+    '\n+\n+test_done\n-- \n1.7.9.1\n"},{"id":"189618","messageId":"CAHkRjk7EH8m09Qx-SH2E1NHARL5yZq9M=mOKrG56LiTUY1ArhQ@mail.gmail.com","threadId":"30207","inReplyTo":"B940726E-2CFA-45CB-8B81-A8F4268D51A6@jetbrains.com","subject":"Re: [PATCH] stg delete --empty","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@gmail.com","sentAt":"2012-04-18T11:03:15Z","receivedAt":"2012-04-18T11:03:15Z","isPatch":true,"sender":{"key":"catalin.marinas@gmail.com","avatar":null},"body":"On 11 April 2012 15:12, Stepan Koltsov <Stepan.Koltsov@jetbrains.com> wrote:\n> `stg delete --empty` deletes all empty patches in series. Command\n> does nothing and exits with zero code if all patches are not empty.\n\nThanks for the patch but there is already 'stg clean' with the same\nfunctionality.\n\n-- \nCatalin\n"},{"id":"189620","messageId":"37A023AD-BCA9-41B8-A9DA-23D1FA5B3827@mx1.ru","threadId":"30207","inReplyTo":"CAHkRjk7EH8m09Qx-SH2E1NHARL5yZq9M=mOKrG56LiTUY1ArhQ@mail.gmail.com","subject":"Re: [PATCH] stg delete --empty","fromName":"Stepan Koltsov","fromEmail":"stepancheg@mx1.ru","sentAt":"2012-04-18T11:23:26Z","receivedAt":"2012-04-18T11:23:26Z","isPatch":true,"sender":{"key":"stepancheg@mx1.ru","avatar":null},"body":"On 18.04.2012, at 15:03, Catalin Marinas <catalin.marinas@gmail.com> wrote:\n\n> On 11 April 2012 15:12, Stepan Koltsov <Stepan.Koltsov@jetbrains.com> wrote:\n>> `stg delete --empty` deletes all empty patches in series. Command\n>> does nothing and exits with zero code if all patches are not empty.\n> \n> Thanks for the patch but there is already 'stg clean' with the same\n> functionality.\n\nMy bad. Thank you.\n\n-- \nStepan\n"}]}