Patchwork [3,of,7] destutil: add an 'action' layer to the destmerge message dictionnary

login
register
mail settings
Submitter Pierre-Yves David
Date Feb. 14, 2016, 2:16 a.m.
Message ID <b989484b0b89e6284683.1455416185@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/13174/
State Accepted
Headers show

Comments

Pierre-Yves David - Feb. 14, 2016, 2:16 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1454950424 -3600
#      Mon Feb 08 17:53:44 2016 +0100
# Node ID b989484b0b89e62846830c0ce20bb567b2d866df
# Parent  37f6989324369a041c717801a91f388c8750f58e
# EXP-Topic destination
# Available At http://hg.netv6.net/marmoute-wip/mercurial/
#              hg pull http://hg.netv6.net/marmoute-wip/mercurial/ -r b989484b0b89
destutil: add an 'action' layer to the destmerge message dictionnary

We'll introduce messageis for 'rebase' soon, so we introduce a way to select
a message based on the action being preformed.

Patch

diff --git a/mercurial/destutil.py b/mercurial/destutil.py
--- a/mercurial/destutil.py
+++ b/mercurial/destutil.py
@@ -135,64 +135,80 @@  def destupdate(repo, clean=False, 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")),
+        {'merge':
+            (_("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")),
+        {'merge':
+            (_("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")),
+        {'merge':
+            (_("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")),
+        {'merge':
+            (_("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")),
+        {'merge':
+            (_("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'),
+            {'merge':
+                (_('nothing to merge'),
          None),
+        },
     # repository have a single head and we are not on it
     'nootherheadsbehind':
-        (_('nothing to merge'),
-         _("use 'hg update' instead")),
+        {'merge':
+            (_('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"))
-        }
+        {'merge':
+            (_('working directory not at a head revision'),
+             _("use 'hg update' or merge with an explicit revision"))
+        },
+    }
 
-def _destmergebook(repo):
+def _destmergebook(repo, action='merge'):
     """find merge destination in the active bookmark case"""
     node = None
     bmheads = repo.bookmarkheads(repo._activebookmark)
     curhead = repo[repo._activebookmark].node()
     if len(bmheads) == 2:
         if curhead == bmheads[0]:
             node = bmheads[1]
         else:
             node = bmheads[0]
     elif len(bmheads) > 2:
-        msg, hint = msgdestmerge['toomanybookmarks']
+        msg, hint = msgdestmerge['toomanybookmarks'][action]
         raise error.Abort(msg, hint=hint)
     elif len(bmheads) <= 1:
-        msg, hint = msgdestmerge['nootherbookmarks']
+        msg, hint = msgdestmerge['nootherbookmarks'][action]
         raise error.Abort(msg, hint=hint)
     assert node is not None
     return node
 
-def _destmergebranch(repo):
+def _destmergebranch(repo, action='merge'):
     """find merge destination based on branch heads"""
     node = None
     parent = repo.dirstate.p1()
     branch = repo.dirstate.branch()
     bheads = repo.branchheads(branch)
@@ -201,48 +217,54 @@  def _destmergebranch(repo):
     if parent not in bheads:
         # Case A: working copy if not on a head.
         #
         # This is probably a user mistake We bailout pointing at 'hg update'
         if len(repo.heads()) <= 1:
-            msg, hint = msgdestmerge['nootherheadsbehind']
+            msg, hint = msgdestmerge['nootherheadsbehind'][action]
         else:
-            msg, hint = msgdestmerge['notatheads']
+            msg, hint = msgdestmerge['notatheads'][action]
         raise error.Abort(msg, hint=hint)
     elif len(nbhs) > 2:
         # Case B: There is more than 2 anonymous heads
         #
         # This means that there will be more than 1 candidate. This is
         # ambiguous. We abort asking the user to pick as explicit destination
         # instead.
-        msg, hint = msgdestmerge['toomanyheads']
+        msg, hint = msgdestmerge['toomanyheads'][action]
         msg %= (branch, len(bheads))
         raise error.Abort(msg, hint=hint)
     elif len(nbhs) <= 1:
         # Case B: There is no other anonymous head that the one we are one
         #
         # This means that there is no natural candidate to merge with.
         # We abort, with various messages for various cases.
         if len(bheads) > 1:
-            msg, hint = msgdestmerge['bookmarkedheads']
+            msg, hint = msgdestmerge['bookmarkedheads'][action]
         elif len(repo.heads()) > 1:
-            msg, hint = msgdestmerge['nootherbranchheads']
+            msg, hint = msgdestmerge['nootherbranchheads'][action]
             msg %= branch
         else:
-            msg, hint = msgdestmerge['nootherheads']
+            msg, hint = msgdestmerge['nootherheads'][action]
         raise error.Abort(msg, hint=hint)
     elif parent == nbhs[0]:
         node = nbhs[-1]
     else:
         node = nbhs[0]
     assert node is not None
     return node
 
-def destmerge(repo):
+def destmerge(repo, action='merge'):
+    """return the default destination for a merge
+
+    (or raise exception about why it can't pick one)
+
+    :action: the action being performed, controls emitted error message
+    """
     if repo._activebookmark:
-        node = _destmergebook(repo)
+        node = _destmergebook(repo, action=action)
     else:
-        node = _destmergebranch(repo)
+        node = _destmergebranch(repo, action=action)
     return repo[node].rev()
 
 histeditdefaultrevset = 'reverse(only(.) and not public() and not ::merge())'
 
 def desthistedit(ui, repo):