changeset 2642:92e882a82aaf

topics: factor out the logic to change topic in a new function It will help to refactor and fix bugs. Moreover we can re-use the logic.
author Pulkit Goyal <7895pulkit@gmail.com>
date Wed, 21 Jun 2017 01:05:46 +0530
parents c90c70d8b6de
children a9ca94defc29
files hgext3rd/topic/__init__.py
diffstat 1 files changed, 75 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Wed Jun 21 11:45:15 2017 +0200
+++ b/hgext3rd/topic/__init__.py	Wed Jun 21 01:05:46 2017 +0530
@@ -278,78 +278,7 @@
             raise error.Abort('changing topic requires a topic name or --clear')
         if any(not c.mutable() for c in repo.set('%r and public()', change)):
             raise error.Abort("can't change topic of a public change")
-        rewrote = 0
-        needevolve = False
-        l = repo.lock()
-        txn = repo.transaction('rewrite-topics')
-        try:
-            newp = None
-            oldp = None
-            p1 = None
-            p2 = None
-            for c in repo.set('%r', change):
-                def filectxfn(repo, ctx, path):
-                    try:
-                        return c[path]
-                    except error.ManifestLookupError:
-                        return None
-                fixedextra = dict(c.extra())
-                ui.debug('old node id is %s\n' % node.hex(c.node()))
-                ui.debug('origextra: %r\n' % fixedextra)
-                newtopic = None if clear else topic
-                oldtopic = fixedextra.get(constants.extrakey, None)
-                if oldtopic == newtopic:
-                    continue
-                if clear:
-                    del fixedextra[constants.extrakey]
-                else:
-                    fixedextra[constants.extrakey] = topic
-                if 'amend_source' in fixedextra:
-                    # TODO: right now the commitctx wrapper in
-                    # topicrepo overwrites the topic in extra if
-                    # amend_source is set to support 'hg commit
-                    # --amend'. Support for amend should be adjusted
-                    # to not be so invasive.
-                    del fixedextra['amend_source']
-                ui.debug('changing topic of %s from %s to %s\n' % (
-                    c, oldtopic, newtopic))
-                ui.debug('fixedextra: %r\n' % fixedextra)
-                # While changing topic of set of linear commits, make sure that
-                # we base our commits on new parent rather than old parent which
-                # was obsoleted while changing the topic
-                if newp and c.p1().node() == oldp:
-                    p1 = newp
-                    p2 = c.p2().node()
-                elif newp and c.p2().node() == oldp:
-                    p1 = c.p1().node()
-                    p2 = newp
-                else:
-                    p1 = c.p1().node()
-                    p2 = c.p2().node()
-                mc = context.memctx(
-                    repo, (p1, p2), c.description(),
-                    c.files(), filectxfn,
-                    user=c.user(), date=c.date(), extra=fixedextra)
-                newnode = repo.commitctx(mc)
-                oldp = c.node()
-                newp = newnode
-                ui.debug('new node id is %s\n' % node.hex(newnode))
-                needevolve = needevolve or (len(c.children()) > 0)
-                obsolete.createmarkers(repo, [(c, (repo[newnode],))])
-                rewrote += 1
-            txn.close()
-        except:
-            try:
-                txn.abort()
-            finally:
-                repo.invalidate()
-            raise
-        finally:
-            lock.release(txn, l)
-        ui.status('changed topic on %d changes\n' % rewrote)
-        if needevolve:
-            evolvetarget = 'topic(%s)' % topic if topic else 'not topic()'
-            ui.status('please run hg evolve --rev "%s" now\n' % evolvetarget)
+        _changetopics(ui, repo, change, topic, clear)
     if clear:
         if repo.vfs.exists('topic'):
             repo.vfs.unlink('topic')
@@ -372,6 +301,80 @@
         raise error.Abort(_('no active topic to list'))
     return stack.showstack(ui, repo, topic, opts)
 
+def _changetopics(ui, repo, revset, topic, clear):
+    rewrote = 0
+    needevolve = False
+    l = repo.lock()
+    txn = repo.transaction('rewrite-topics')
+    try:
+        newp = None
+        oldp = None
+        p1 = None
+        p2 = None
+        for c in repo.set('%r', revset):
+            def filectxfn(repo, ctx, path):
+                try:
+                    return c[path]
+                except error.ManifestLookupError:
+                    return None
+            fixedextra = dict(c.extra())
+            ui.debug('old node id is %s\n' % node.hex(c.node()))
+            ui.debug('origextra: %r\n' % fixedextra)
+            newtopic = None if clear else topic
+            oldtopic = fixedextra.get(constants.extrakey, None)
+            if oldtopic == newtopic:
+                continue
+            if clear:
+                del fixedextra[constants.extrakey]
+            else:
+                fixedextra[constants.extrakey] = topic
+            if 'amend_source' in fixedextra:
+                # TODO: right now the commitctx wrapper in
+                # topicrepo overwrites the topic in extra if
+                # amend_source is set to support 'hg commit
+                # --amend'. Support for amend should be adjusted
+                # to not be so invasive.
+                del fixedextra['amend_source']
+            ui.debug('changing topic of %s from %s to %s\n' % (
+                c, oldtopic, newtopic))
+            ui.debug('fixedextra: %r\n' % fixedextra)
+            # While changing topic of set of linear commits, make sure that
+            # we base our commits on new parent rather than old parent which
+            # was obsoleted while changing the topic
+            if newp and c.p1().node() == oldp:
+                p1 = newp
+                p2 = c.p2().node()
+            elif newp and c.p2().node() == oldp:
+                p1 = c.p1().node()
+                p2 = newp
+            else:
+                p1 = c.p1().node()
+                p2 = c.p2().node()
+            mc = context.memctx(
+                repo, (p1, p2), c.description(),
+                c.files(), filectxfn,
+                user=c.user(), date=c.date(), extra=fixedextra)
+            newnode = repo.commitctx(mc)
+            oldp = c.node()
+            newp = newnode
+            ui.debug('new node id is %s\n' % node.hex(newnode))
+            needevolve = needevolve or (len(c.children()) > 0)
+            obsolete.createmarkers(repo, [(c, (repo[newnode],))])
+            rewrote += 1
+        txn.close()
+    except:
+        try:
+            txn.abort()
+        finally:
+            repo.invalidate()
+        raise
+    finally:
+        lock.release(txn, l)
+    ui.status('changed topic on %d changes\n' % rewrote)
+    if needevolve:
+        evolvetarget = 'topic(%s)' % topic if topic else 'not topic()'
+        ui.status('please run hg evolve --rev "%s" now\n' % evolvetarget)
+
 def _listtopics(ui, repo, opts):
     fm = ui.formatter('bookmarks', opts)
     activetopic = repo.currenttopic