merge: use topic to pick default destination
authorPierre-Yves David <pierre-yves.david@fb.com>
Wed, 21 Oct 2015 01:09:15 +0200
changeset 1871 58ef5699fb35
parent 1870 8dd5200b4086
child 1872 4fcee38d71d6
child 1875 9eba8561870b
merge: use topic to pick default destination If the topic have multiple heads, reduce that. Otherwise, pick the branch we are on.
src/topic/__init__.py
src/topic/destination.py
tests/test-topic-dest.t
--- a/src/topic/__init__.py	Wed Oct 21 01:12:32 2015 +0200
+++ b/src/topic/__init__.py	Wed Oct 21 01:09:15 2015 +0200
@@ -28,6 +28,7 @@
 
 from . import constants
 from . import revset as topicrevset
+from . import destination
 
 cmdtable = {}
 command = cmdutil.command(cmdtable)
@@ -47,6 +48,9 @@
         return [t]
     return []
 
+def uisetup(ui):
+    destination.setupdest()
+
 def reposetup(ui, repo):
     orig = repo.__class__
     class topicrepo(repo.__class__):
--- a/src/topic/destination.py	Wed Oct 21 01:12:32 2015 +0200
+++ b/src/topic/destination.py	Wed Oct 21 01:09:15 2015 +0200
@@ -1,5 +1,48 @@
-from mercurial import revset
 from mercurial import error
+from mercurial import util
+from mercurial import destutil
+from mercurial import extensions
+from mercurial.i18n import _
+
+def _destmergebranch(orig, repo):
+    p1 = repo['.']
+    top = p1.topic()
+    if top:
+        heads = repo.revs('heads(topic(.)::topic(.))')
+        if p1.rev() not in heads:
+            raise error.Abort(_("not at topic head, update or explicit"))
+        elif 1 == len(heads):
+            # should look at all branch involved but... later
+            bhead = ngtip(repo, p1.branch(), all=True)
+            if not bhead:
+                raise error.Abort(_("nothing to merge"))
+            elif 1 == len(bhead):
+                return bhead.first()
+            else:
+                raise error.Abort(_("branch '%s' has %d heads - "
+                                   "please merge with an explicit rev")
+                                 % (p1.branch(), len(bhead)),
+                                 hint=_("run 'hg heads .' to see heads"))
+        elif 2 == len(heads):
+            heads = [r for r in heads if r != p1.rev()]
+            # XXX: bla bla bla bla bla
+            if 1 < len(heads):
+                raise error.Abort(_('working directory not at a head revision'),
+                                 hint=_("use 'hg update' or merge with an "
+                                        "explicit revision"))
+            return heads[0]
+        elif 2 < len(heads):
+            raise error.Abort(_("topic '%s' has %d heads - "
+                                "please merge with an explicit rev")
+                              % (top, len(heads)))
+        else:
+            assert False # that's impossible
+    return orig(repo)
+
+def setupdest():
+    if util.safehasattr(destutil, '_destmergebranch'):
+        extensions.wrapfunction(destutil, '_destmergebranch', _destmergebranch)
+
 def ngtip(repo, branch, all=False):
     """tip new generation"""
     ## search for untopiced heads of branch
--- a/tests/test-topic-dest.t	Wed Oct 21 01:12:32 2015 +0200
+++ b/tests/test-topic-dest.t	Wed Oct 21 01:09:15 2015 +0200
@@ -91,3 +91,66 @@
   $ hg log -r 'ngtip(.)'
   6 () c_epsilon
 
+merge destination
+=================
+
+  $ hg up 'ngtip(default)'
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ echo zeta >> zeta
+  $ hg add zeta
+  $ hg ci -m "c_zeta"
+  created new head
+  $ hg log -G
+  @  9 () c_zeta
+  |
+  | o  8 (monkey) zephir
+  |/
+  | o  7 (elephant) babar
+  |/
+  o  6 () c_epsilon
+  |
+  o  3 () c_delta
+  |
+  o  2 () c_gamma
+  |
+  o  1 () c_beta
+  |
+  o  0 () c_alpha
+  
+  $ hg up elephant
+  switching to topic elephant
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg rebase # make sure tip is elsewhere
+  rebasing 7:8d0b77140b05 "babar"
+  $ hg up monkey
+  switching to topic monkey
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ hg merge
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+  $ hg topic
+     elephant
+   * monkey
+  $ hg ci -m 'merge with default'
+  $ hg topic
+     elephant
+   * monkey
+  $ hg log -G
+  @    11 (monkey) merge with default
+  |\
+  | | o  10 (elephant) babar
+  | |/
+  | o  9 () c_zeta
+  | |
+  o |  8 (monkey) zephir
+  |/
+  o  6 () c_epsilon
+  |
+  o  3 () c_delta
+  |
+  o  2 () c_gamma
+  |
+  o  1 () c_beta
+  |
+  o  0 () c_alpha
+