[PATCH 1 of 5 topic-experiment] topics: factor out the logic to change topic in a new function

Pulkit Goyal 7895pulkit at gmail.com
Wed Jun 21 02:09:22 UTC 2017


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1497987346 -19800
#      Wed Jun 21 01:05:46 2017 +0530
# Node ID 1261e6d3ff9534a3a0f8f703e4a490328140d794
# Parent  a788967aa800300e64b3d39d74b9ff0b562c3391
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.

diff --git a/hgext3rd/topic/__init__.py b/hgext3rd/topic/__init__.py
--- a/hgext3rd/topic/__init__.py
+++ b/hgext3rd/topic/__init__.py
@@ -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


More information about the Mercurial-devel mailing list