mq: qimport cleanup on fail (issue2214) stable
authorVishakh H <vsh426@gmail.com>
Tue, 29 Jun 2010 03:09:42 +0530
branchstable
changeset 11462 1b82a26635d7
parent 11461 2b83c26b29f3
child 11463 f0ea93557133
mq: qimport cleanup on fail (issue2214) save state of successfully added patches and ensure cleanup on the way out.
hgext/mq.py
tests/test-mq-qimport-fail-cleanup
tests/test-mq-qimport-fail-cleanup.out
--- a/hgext/mq.py	Mon Jun 28 23:23:55 2010 +0200
+++ b/hgext/mq.py	Tue Jun 29 03:09:42 2010 +0530
@@ -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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-mq-qimport-fail-cleanup	Tue Jun 29 03:09:42 2010 +0530
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-mq-qimport-fail-cleanup.out	Tue Jun 29 03:09:42 2010 +0530
@@ -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