hgext/mq.py
changeset 8833 14639c050251
parent 8832 6e6f5b80e056
child 8875 801cacf46e62
--- a/hgext/mq.py	Fri Jun 19 12:37:29 2009 +0200
+++ b/hgext/mq.py	Fri Jun 19 13:58:22 2009 +0200
@@ -616,47 +616,51 @@
                 break
         return (err, n)
 
-    def _clean_series(self, patches):
+    def _cleanup(self, patches, numrevs, keep=False):
+        if not keep:
+            r = self.qrepo()
+            if r:
+                r.remove(patches, True)
+            else:
+                for p in patches:
+                    os.unlink(self.join(p))
+
+        if numrevs:
+            del self.applied[:numrevs]
+            self.applied_dirty = 1
+
         for i in sorted([self.find_series(p) for p in patches], reverse=True):
             del self.full_series[i]
         self.parse_series()
         self.series_dirty = 1
 
-    def finish(self, repo, revs):
+    def _revpatches(self, repo, revs):
         firstrev = repo[self.applied[0].rev].rev()
-        appliedbase = 0
         patches = []
-        for rev in sorted(revs):
+        for i, rev in enumerate(revs):
 
             if rev < firstrev:
                 raise util.Abort(_('revision %d is not managed') % rev)
-            base = bin(self.applied[appliedbase].rev)
 
             ctx = repo[rev]
+            base = bin(self.applied[i].rev)
             if ctx.node() != base:
                 msg = _('cannot delete revision %d above applied patches')
                 raise util.Abort(msg % rev)
 
-            patch = self.applied[appliedbase].name
+            patch = self.applied[i].name
             for fmt in ('[mq]: %s', 'imported patch %s'):
                 if ctx.description() == fmt % patch:
                     msg = _('patch %s finalized without changeset message\n')
                     repo.ui.status(msg % patch)
                     break
 
-            patches.append(self.applied[appliedbase].name)
-            appliedbase += 1
+            patches.append(patch)
+        return patches
 
-        r = self.qrepo()
-        if r:
-            r.remove(patches, True)
-        else:
-            for p in patches:
-                os.unlink(self.join(p))
-
-        del self.applied[:appliedbase]
-        self.applied_dirty = 1
-        self._clean_series(patches)
+    def finish(self, repo, revs):
+        patches = self._revpatches(repo, sorted(revs))
+        self._cleanup(patches, len(patches))
 
     def delete(self, repo, patches, opts):
         if not patches and not opts.get('rev'):
@@ -673,37 +677,18 @@
                 raise util.Abort(_("patch %s not in series file") % patch)
             realpatches.append(patch)
 
-        appliedbase = 0
+        numrevs = 0
         if opts.get('rev'):
             if not self.applied:
                 raise util.Abort(_('no patches applied'))
             revs = cmdutil.revrange(repo, opts['rev'])
             if len(revs) > 1 and revs[0] > revs[1]:
                 revs.reverse()
-            for rev in revs:
-                if appliedbase >= len(self.applied):
-                    raise util.Abort(_("revision %d is not managed") % rev)
-
-                base = bin(self.applied[appliedbase].rev)
-                node = repo.changelog.node(rev)
-                if node != base:
-                    raise util.Abort(_("cannot delete revision %d above "
-                                       "applied patches") % rev)
-                realpatches.append(self.applied[appliedbase].name)
-                appliedbase += 1
+            revpatches = self._revpatches(repo, revs)
+            realpatches += revpatches
+            numrevs = len(revpatches)
 
-        if not opts.get('keep'):
-            r = self.qrepo()
-            if r:
-                r.remove(realpatches, True)
-            else:
-                for p in realpatches:
-                    os.unlink(self.join(p))
-
-        if appliedbase:
-            del self.applied[:appliedbase]
-            self.applied_dirty = 1
-        self._clean_series(realpatches)
+        self._cleanup(realpatches, numrevs, opts.get('keep'))
 
     def check_toppatch(self, repo):
         if len(self.applied) > 0: