Patchwork [3,of,6] destutil: extract all 'mergedest' abort messages into a dictionnary

login
register
mail settings
Submitter Pierre-Yves David
Date Feb. 12, 2016, 2:29 p.m.
Message ID <b28f7171ac9e1a35153b.1455287346@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/13134/
State Accepted
Headers show

Comments

Pierre-Yves David - Feb. 12, 2016, 2:29 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1455052477 0
#      Tue Feb 09 21:14:37 2016 +0000
# Node ID b28f7171ac9e1a35153b7bf7f9d448a6d1c08270
# Parent  9ed356ea821fafd6c200e62e9e465f09f97a09d9
# EXP-Topic destination
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r b28f7171ac9e
destutil: extract all 'mergedest' abort messages into a dictionnary

We plan to be able to reuse this function for rebase. The error message
explicitly refer to "merge" in multiple places. So we'll need to be able to use
different messages. The first step of that is to extract all messages in a
dedicated dictionary and use them indirectly.

As a side effect it clarify the actual function and open the way to various
cleanup and fixes in future changesets.

Patch

diff --git a/mercurial/destutil.py b/mercurial/destutil.py
--- a/mercurial/destutil.py
+++ b/mercurial/destutil.py
@@ -132,10 +132,47 @@  def destupdate(repo, clean=False, check=
 
     _destupdatevalidate(repo, rev, clean, check)
 
     return rev, movemark, activemark
 
+msgdestmerge = {
+    # too many matching divergent bookmark
+    'toomanybookmarks':
+        (_("multiple matching bookmarks to merge -"
+           " please merge with an explicit rev or bookmark"),
+         _("run 'hg heads' to see all heads")),
+    # no other matching divergent bookmark
+    'nootherbookmarks':
+        (_("no matching bookmark to merge - "
+           "please merge with an explicit rev or bookmark"),
+         _("run 'hg heads' to see all heads")),
+    # branch have too many unbookmarked heads, no obvious destination
+    'toomanyheads':
+        (_("branch '%s' has %d heads - please merge with an explicit rev"),
+         _("run 'hg heads .' to see heads")),
+    # branch have no other unbookmarked heads
+    'bookmarkedheads':
+        (_("heads are bookmarked - please merge with an explicit rev"),
+         _("run 'hg heads' to see all heads")),
+    # branch have just a single heads, but there is other branches
+    'nootherbranchheads':
+        (_("branch '%s' has one head - please merge with an explicit rev"),
+         _("run 'hg heads' to see all heads")),
+    # repository have a single head
+    'nootherheads':
+        (_('nothing to merge'),
+         None),
+    # repository have a single head and we are not on it
+    'nootherheadsbehind':
+        (_('nothing to merge'),
+         _("use 'hg update' instead")),
+    # We are not on a head
+    'notatheads':
+        (_('working directory not at a head revision'),
+         _("use 'hg update' or merge with an explicit revision"))
+        }
+
 def _destmergebook(repo):
     """find merge destination in the active bookmark case"""
     node = None
     bmheads = repo.bookmarkheads(repo._activebookmark)
     curhead = repo[repo._activebookmark].node()
@@ -143,17 +180,15 @@  def _destmergebook(repo):
         if curhead == bmheads[0]:
             node = bmheads[1]
         else:
             node = bmheads[0]
     elif len(bmheads) > 2:
-        raise error.Abort(_("multiple matching bookmarks to merge - "
-            "please merge with an explicit rev or bookmark"),
-            hint=_("run 'hg heads' to see all heads"))
+        msg, hint = msgdestmerge['toomanybookmarks']
+        raise error.Abort(msg, hint=hint)
     elif len(bmheads) <= 1:
-        raise error.Abort(_("no matching bookmark to merge - "
-            "please merge with an explicit rev or bookmark"),
-            hint=_("run 'hg heads' to see all heads"))
+        msg, hint = msgdestmerge['nootherbookmarks']
+        raise error.Abort(msg, hint=hint)
     assert node is not None
     return node
 
 def _destmergebranch(repo):
     """find merge destination based on branch heads"""
@@ -161,35 +196,30 @@  def _destmergebranch(repo):
     branch = repo[None].branch()
     bheads = repo.branchheads(branch)
     nbhs = [bh for bh in bheads if not repo[bh].bookmarks()]
 
     if len(nbhs) > 2:
-        raise error.Abort(_("branch '%s' has %d heads - "
-                           "please merge with an explicit rev")
-                         % (branch, len(bheads)),
-                         hint=_("run 'hg heads .' to see heads"))
+        msg, hint = msgdestmerge['toomanyheads']
+        msg %= (branch, len(bheads))
+        raise error.Abort(msg, hint=hint)
 
     parent = repo.dirstate.p1()
     if len(nbhs) <= 1:
         if len(bheads) > 1:
-            raise error.Abort(_("heads are bookmarked - "
-                               "please merge with an explicit rev"),
-                             hint=_("run 'hg heads' to see all heads"))
-        if len(repo.heads()) > 1:
-            raise error.Abort(_("branch '%s' has one head - "
-                               "please merge with an explicit rev")
-                             % branch,
-                             hint=_("run 'hg heads' to see all heads"))
-        msg, hint = _('nothing to merge'), None
-        if parent != repo.lookup(branch):
-            hint = _("use 'hg update' instead")
+            msg, hint = msgdestmerge['bookmarkedheads']
+        elif len(repo.heads()) > 1:
+            msg, hint = msgdestmerge['nootherbranchheads']
+            msg %= branch
+        elif parent != repo.lookup(branch):
+            msg, hint = msgdestmerge['nootherheadsbehind']
+        else:
+            msg, hint = msgdestmerge['nootherheads']
         raise error.Abort(msg, hint=hint)
 
     if parent not in bheads:
-        raise error.Abort(_('working directory not at a head revision'),
-                         hint=_("use 'hg update' or merge with an "
-                                "explicit revision"))
+        msg, hint = msgdestmerge['notatheads']
+        raise error.Abort(msg, hint=hint)
     if parent == nbhs[0]:
         node = nbhs[-1]
     else:
         node = nbhs[0]
     assert node is not None