[PATCH] mq: qimport cleanup on fail (issue2214)

Vishakh H vsh426 at gmail.com
Mon Jun 28 16:40:07 CDT 2010


# HG changeset patch
# User Vishakh H <vsh426 at gmail.com>
# Date 1277761182 -19800
# Branch stable
# Node ID a2ad92f45b375b97b5d2aab4ae3d4cb6d929fe95
# Parent  88abbb046e66d6b1c4e5458ffd74dc804af14e60
mq: qimport cleanup on fail (issue2214)

save state of successfully added patches and ensure
cleanup on the way out.

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -250,6 +250,7 @@
         self.ui = ui
         self.applied_dirty = 0
         self.series_dirty = 0
+        self.added = []
         self.series_path = "series"
         self.status_path = "status"
         self.guards_path = "guards"
@@ -1622,7 +1623,7 @@
         if (len(files) > 1 or len(rev) > 1) and patchname:
             raise util.Abort(_('option "-n" not valid when importing multiple '
                                'patches'))
-        added = []
+        self.added = []
         if rev:
             # If mq patches are applied, we can only import revisions
             # that form a linear path to qbase.
@@ -1672,10 +1673,11 @@
                 se = statusentry(n, patchname)
                 self.applied.insert(0, se)
 
-                added.append(patchname)
+                self.added.append(patchname)
                 patchname = None
             self.parse_series()
             self.applied_dirty = 1
+            self.series_dirty = True
 
         for i, filename in enumerate(files):
             if existing:
@@ -1709,13 +1711,10 @@
                 index = self.full_series_end() + i
                 self.full_series[index:index] = [patchname]
             self.parse_series()
+            self.series_dirty = True
             self.ui.warn(_("adding %s to series file\n") % patchname)
-            added.append(patchname)
+            self.added.append(patchname)
             patchname = None
-        self.series_dirty = 1
-        qrepo = self.qrepo()
-        if qrepo:
-            qrepo[None].add(added)
 
 def delete(ui, repo, *patches, **opts):
     """remove patches from queue
@@ -1805,10 +1804,15 @@
     using the --name flag.
     """
     q = repo.mq
-    q.qimport(repo, filename, patchname=opts['name'],
+    try:
+        q.qimport(repo, filename, patchname=opts['name'],
               existing=opts['existing'], force=opts['force'], rev=opts['rev'],
               git=opts['git'])
-    q.save_dirty()
+    finally:
+        q.save_dirty()
+        qrepo = q.qrepo()
+        if qrepo:
+            qrepo[None].add(q.added)
 
     if opts.get('push') and not opts.get('rev'):
         return q.push(repo, None)
diff --git a/tests/test-mq-qimport-fail-cleanup b/tests/test-mq-qimport-fail-cleanup
new file mode 100755
--- /dev/null
+++ b/tests/test-mq-qimport-fail-cleanup
@@ -0,0 +1,33 @@
+#!/bin/sh
+#failed qimport of patches from files should cleanup by recording successfully
+#imported patches in series file.
+
+echo "[extensions]" >> $HGRCPATH
+echo "mq=" >> $HGRCPATH
+
+hg init repo
+cd repo
+
+echo a > a
+hg ci -Am'add a'
+
+cat >b.patch<<EOF
+diff --git a/a b/a
+--- a/a
++++ b/a
+@@ -1,1 +1,2 @@
+ a
++b
+EOF
+
+echo
+echo '#empty series'
+hg qseries
+
+echo
+echo '#qimport valid patch followed by invalid patch'
+hg qimport b.patch fakepatch
+
+echo
+echo '#valid patches before fail added to series'
+hg qseries
diff --git a/tests/test-mq-qimport-fail-cleanup.out b/tests/test-mq-qimport-fail-cleanup.out
new file mode 100644
--- /dev/null
+++ b/tests/test-mq-qimport-fail-cleanup.out
@@ -0,0 +1,10 @@
+adding a
+
+#empty series
+
+#qimport valid patch followed by invalid patch
+adding b.patch to series file
+abort: unable to read fakepatch
+
+#valid patches before fail added to series
+b.patch


More information about the Mercurial-devel mailing list