Patchwork [1,of,4,topic-experiment] prev: factor out the logic to update to previous commit to a new function

login
register
mail settings
Submitter Pulkit Goyal
Date July 4, 2017, 2:29 p.m.
Message ID <143abe59c2d630461718.1499178561@workspace>
Download mbox | patch
Permalink /patch/21972/
State Not Applicable
Headers show

Comments

Pulkit Goyal - July 4, 2017, 2:29 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1499168421 -19800
#      Tue Jul 04 17:10:21 2017 +0530
# Node ID 143abe59c2d630461718ea0ea6277ab16abd9637
# Parent  b4b5c5331caec02c1e26bda42ec997fcb7b6af93
prev: factor out the logic to update to previous 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 prev` as better
support will be added in upcoming changesets.

Patch

diff --git a/hgext3rd/evolve/__init__.py b/hgext3rd/evolve/__init__.py
--- a/hgext3rd/evolve/__init__.py
+++ b/hgext3rd/evolve/__init__.py
@@ -2026,47 +2026,44 @@ 
                 raise
 
         parents = wparents[0].parents()
-        topic = getattr(repo, 'currenttopic', '')
-        if topic and not opts.get("no_topic", False):
-            parents = [ctx for ctx in parents if ctx.topic() == topic]
-        displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
-        if not parents:
-            ui.warn(_('no parent in topic "%s"\n') % topic)
-            ui.warn(_('(do you want --no-topic)\n'))
-        elif len(parents) == 1:
-            p = parents[0]
-            bm = repo._activebookmark
-            shouldmove = opts.get('move_bookmark') and bm is not None
-            if dryrunopt:
-                ui.write(('hg update %s;\n' % p.rev()))
-                if shouldmove:
-                    ui.write(('hg bookmark %s -r %s;\n' % (bm, p.rev())))
-            else:
-                ret = hg.update(repo, p.rev())
-                if not ret:
-                    tr = lock = None
-                    try:
-                        lock = repo.lock()
-                        tr = repo.transaction('previous')
-                        if shouldmove:
-                            repo._bookmarks[bm] = p.node()
-                            repo._bookmarks.recordchange(tr)
-                        else:
-                            bookmarksmod.deactivate(repo)
-                        tr.close()
-                    finally:
-                        lockmod.release(tr, lock)
-
-            displayer.show(p)
-            return 0
-        else:
-            for p in parents:
-                displayer.show(p)
-            ui.warn(_('multiple parents, explicitly update to one\n'))
-            return 1
+        return movetoprev(ui, repo, parents, opts)
     finally:
         lockmod.release(wlock)
 
+def movetoprev(ui, repo, parents, opts):
+    displayer = cmdutil.show_changeset(ui, repo, {'template': shorttemplate})
+    if len(parents) == 1:
+        p = parents[0]
+        bm = repo._activebookmark
+        shouldmove = opts.get('move_bookmark') and bm is not None
+        if opts['dry_run']:
+            ui.write(('hg update %s;\n' % p.rev()))
+            if shouldmove:
+                ui.write(('hg bookmark %s -r %s;\n' % (bm, p.rev())))
+        else:
+            ret = hg.update(repo, p.rev())
+            if not ret:
+                tr = lock = None
+                try:
+                    lock = repo.lock()
+                    tr = repo.transaction('previous')
+                    if shouldmove:
+                        repo._bookmarks[bm] = p.node()
+                        repo._bookmarks.recordchange(tr)
+                    else:
+                        bookmarksmod.deactivate(repo)
+                    tr.close()
+                finally:
+                    lockmod.release(tr, lock)
+
+        displayer.show(p)
+        return 0
+    else:
+        for p in parents:
+            displayer.show(p)
+        ui.warn(_('multiple parents, explicitly update to one\n'))
+        return 1
+
 @eh.command(
     '^next',
     [('B', 'move-bookmark', False,
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
@@ -209,9 +209,9 @@ 
   1 files updated, 0 files merged, 0 files removed, 0 files unresolved
   [16] add ggg
   $ hg prev
-  no parent in topic "bar"
-  (do you want --no-topic)
-  $ hg prev --no-topic
   switching to topic foo
   0 files updated, 0 files merged, 1 files removed, 0 files unresolved
   [15] add fff
+  $ hg prev --no-topic
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  [14] add eee