[PATCH 3 of 4 topic-experiment] next: factor out the logic to update to next commit to a new function

Pulkit Goyal 7895pulkit at gmail.com
Tue Jul 4 10:29:23 EDT 2017


# HG changeset patch
# User Pulkit Goyal <7895pulkit at gmail.com>
# Date 1499171076 -19800
#      Tue Jul 04 17:54:36 2017 +0530
# Node ID ce352c7b1b5880d3c8af7bda84578f42da3a1460
# Parent  3a1fb8ffae6836d11c410c94e00adb0079b764f6
next: factor out the logic to update to next commit to a new function

This will make things easy for topic extension to change the behaviour by
wrapping the new function.
While factoring, this breaks the existing topic code using `hg next` as better
support will be added in upcoming changesets.

diff --git a/hgext3rd/evolve/__init__.py b/hgext3rd/evolve/__init__.py
--- a/hgext3rd/evolve/__init__.py
+++ b/hgext3rd/evolve/__init__.py
@@ -2098,81 +2098,70 @@
                 raise
 
         children = [ctx for ctx in wparents[0].children() if not ctx.obsolete()]
-        topic = getattr(repo, 'currenttopic', '')
-        filtered = []
-        if topic and not opts.get("no_topic", False):
-            filtered = [ctx for ctx in children if ctx.topic() != topic]
-            # XXX N-square membership on children
-            children = [ctx for ctx in children if ctx not in filtered]
-        displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
-        if len(children) == 1:
-            c = children[0]
-            bm = repo._activebookmark
-            shouldmove = opts.get('move_bookmark') and bm is not None
-            if dryrunopt:
-                ui.write(('hg update %s;\n' % c.rev()))
-                if shouldmove:
-                    ui.write(('hg bookmark %s -r %s;\n' % (bm, c.rev())))
-            else:
-                ret = hg.update(repo, c.rev())
-                if not ret:
-                    lock = tr = None
-                    try:
-                        lock = repo.lock()
-                        tr = repo.transaction('next')
-                        if shouldmove:
-                            repo._bookmarks[bm] = c.node()
-                            repo._bookmarks.recordchange(tr)
-                        else:
-                            bookmarksmod.deactivate(repo)
-                        tr.close()
-                    finally:
-                        lockmod.release(tr, lock)
-            displayer.show(c)
-            result = 0
-        elif children:
-            ui.warn(_("ambigious next changeset:\n"))
-            for c in children:
-                displayer.show(c)
-            ui.warn(_('explicitly update to one of them\n'))
-            result = 1
-        else:
-            aspchildren = _aspiringchildren(repo, [repo['.'].rev()])
-            if topic:
-                filtered.extend(repo[c] for c in children
-                                if repo[c].topic() != topic)
-                # XXX N-square membership on children
-                aspchildren = [ctx for ctx in aspchildren if ctx not in filtered]
-            if not opts['evolve'] or not aspchildren:
-                if filtered:
-                    ui.warn(_('no children on topic "%s"\n') % topic)
-                    ui.warn(_('do you want --no-topic\n'))
-                else:
-                    ui.warn(_('no children\n'))
-                if aspchildren:
-                    msg = _('(%i unstable changesets to be evolved here, '
-                            'do you want --evolve?)\n')
-                    ui.warn(msg % len(aspchildren))
-                result = 1
-            elif 1 < len(aspchildren):
-                ui.warn(_("ambigious next (unstable) changeset:\n"))
-                for c in aspchildren:
-                    displayer.show(repo[c])
-                ui.warn(_("(run 'hg evolve --rev REV' on one of them)\n"))
-                return 1
-            else:
-                cmdutil.bailifchanged(repo)
-                result = _solveone(ui, repo, repo[aspchildren[0]], dryrunopt,
-                                   False, lambda: None, category='unstable')
-                if not result:
-                    ui.status(_('working directory now at %s\n')
-                              % ui.label(str(repo['.']), 'evolve.node'))
-                return result
-            return 1
-        return result
+        return movetonext(ui, repo, children, opts)
     finally:
         lockmod.release(wlock)
 
+def movetonext(ui, repo, children, opts):
+    dryrunopt = opts['dry_run']
+    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
+    if len(children) == 1:
+        c = children[0]
+        bm = repo._activebookmark
+        shouldmove = opts.get('move_bookmark') and bm is not None
+        if dryrunopt:
+            ui.write(('hg update %s;\n' % c.rev()))
+            if shouldmove:
+                ui.write(('hg bookmark %s -r %s;\n' % (bm, c.rev())))
+        else:
+            ret = hg.update(repo, c.rev())
+            if not ret:
+                lock = tr = None
+                try:
+                    lock = repo.lock()
+                    tr = repo.transaction('next')
+                    if shouldmove:
+                        repo._bookmarks[bm] = c.node()
+                        repo._bookmarks.recordchange(tr)
+                    else:
+                        bookmarksmod.deactivate(repo)
+                    tr.close()
+                finally:
+                    lockmod.release(tr, lock)
+        displayer.show(c)
+        result = 0
+    elif children:
+        ui.warn(_("ambigious next changeset:\n"))
+        for c in children:
+            displayer.show(c)
+        ui.warn(_('explicitly update to one of them\n'))
+        result = 1
+    else:
+        aspchildren = _aspiringchildren(repo, [repo['.'].rev()])
+        if not opts['evolve'] or not aspchildren:
+            ui.warn(_('no children\n'))
+            if aspchildren:
+                msg = _('(%i unstable changesets to be evolved here, '
+                        'do you want --evolve?)\n')
+                ui.warn(msg % len(aspchildren))
+            result = 1
+        elif 1 < len(aspchildren):
+            ui.warn(_("ambigious next (unstable) changeset:\n"))
+            for c in aspchildren:
+                displayer.show(repo[c])
+            ui.warn(_("(run 'hg evolve --rev REV' on one of them)\n"))
+            return 1
+        else:
+            cmdutil.bailifchanged(repo)
+            result = _solveone(ui, repo, repo[aspchildren[0]], dryrunopt,
+                               False, lambda: None, category='unstable')
+            if not result:
+                ui.status(_('working directory now at %s\n')
+                          % ui.label(str(repo['.']), 'evolve.node'))
+            return result
+        return 1
+    return result
+
 def _reachablefrombookmark(repo, revs, bookmarks):
     """filter revisions and bookmarks reachable from the given bookmark
     yoinked from mq.py
diff --git a/tests/test-evolve-topic.t b/tests/test-evolve-topic.t
--- a/tests/test-evolve-topic.t
+++ b/tests/test-evolve-topic.t
@@ -201,18 +201,16 @@
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   [15] add fff
   $ hg next
-  no children on topic "foo"
-  do you want --no-topic
-  [1]
-  $ hg next --no-topic
   switching to topic bar
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   [16] add ggg
+  $ hg next --no-topic
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  [17] add hhh
   $ hg prev
-  preserving the current topic 'bar'
   0 files updated, 0 files merged, 1 files removed, 0 files unresolved
-  [15] add fff
+  [16] add ggg
   $ hg prev --no-topic
   switching to topic foo
   0 files updated, 0 files merged, 1 files removed, 0 files unresolved
-  [14] add eee
+  [15] add fff


More information about the Mercurial-devel mailing list