changeset 6206:0b6f12495276

fetch: switch the default parent used for a merge This treats newly pulled changes as authoritative, and local changes as the "satellite" changes. The prior default behaviour is still available, via the --switch-parent option.
author Bryan O'Sullivan <bos@serpentine.com>
date Wed, 05 Mar 2008 09:10:45 -0800
parents b193a6e59131
children 03b13d853dc6
files hgext/fetch.py tests/test-fetch tests/test-fetch.out
diffstat 3 files changed, 64 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/fetch.py	Mon Mar 03 21:48:43 2008 +0100
+++ b/hgext/fetch.py	Wed Mar 05 09:10:45 2008 -0800
@@ -17,7 +17,12 @@
 
     If the pulled changes add a new head, the head is automatically
     merged, and the result of the merge is committed.  Otherwise, the
-    working directory is updated.
+    working directory is updated to include the new changes.
+
+    When a merge occurs, the newly pulled changes are assumed to be
+    "authoritative".  The head of the new changes is used as the first
+    parent, with local changes as the second.  To switch the merge
+    order, use --switch-parent.
 
     See 'hg help dates' for a list of formats valid for -d/--date.
     '''
@@ -34,15 +39,27 @@
             newparent = newchildren[0]
             hg.clean(repo, newparent)
         newheads = [n for n in repo.heads() if n != newparent]
-        err = False
-        if newheads:
-            ui.status(_('merging with new head %d:%s\n') %
-                      (repo.changelog.rev(newheads[0]), short(newheads[0])))
-            err = hg.merge(repo, newheads[0], remind=False)
-        if not err and len(newheads) > 1:
+        if len(newheads) > 1:
             ui.status(_('not merging with %d other new heads '
                         '(use "hg heads" and "hg merge" to merge them)') %
                       (len(newheads) - 1))
+            return
+        err = False
+        if newheads:
+            # By default, we consider the repository we're pulling
+            # *from* as authoritative, so we merge our changes into
+            # theirs.
+            if opts['switch_parent']:
+                firstparent, secondparent = newparent, newheads[0]
+            else:
+                firstparent, secondparent = newheads[0], newparent
+                ui.status(_('updating to %d:%s\n') %
+                          (repo.changelog.rev(firstparent),
+                           short(firstparent)))
+            hg.clean(repo, firstparent)
+            ui.status(_('merging with %d:%s\n') %
+                      (repo.changelog.rev(secondparent), short(secondparent)))
+            err = hg.merge(repo, secondparent, remind=False)
         if not err:
             mod, add, rem = repo.status()[:3]
             message = (cmdutil.logmessage(opts) or
@@ -54,6 +71,7 @@
             ui.status(_('new changeset %d:%s merges remote changes '
                         'with local\n') % (repo.changelog.rev(n),
                                            short(n)))
+
     def pull():
         cmdutil.setremoteconfig(ui, opts)
 
@@ -97,6 +115,7 @@
         (fetch,
         [('r', 'rev', [], _('a specific revision you would like to pull')),
          ('f', 'force-editor', None, _('edit commit message')),
+         ('', 'switch-parent', None, _('switch parents when merging')),
         ] + commands.commitopts + commands.commitopts2 + commands.remoteopts,
         _('hg fetch [SOURCE]')),
 }
--- a/tests/test-fetch	Mon Mar 03 21:48:43 2008 +0100
+++ b/tests/test-fetch	Wed Mar 05 09:10:45 2008 -0800
@@ -24,6 +24,7 @@
 hg clone c d
 hg clone c e
 
+echo % should merge c into a
 hg --cwd c fetch -d '4 0' -m 'automated merge' ../a
 ls c
 
@@ -37,3 +38,15 @@
 echo '% fetch over http with auth (should be hidden in desc)'
 hg --cwd e fetch -d '5 0' http://user:password@localhost:$HGPORT/
 hg --cwd e tip --template '{desc}\n'
+
+hg clone a f
+hg clone a g
+
+echo f > f/f
+hg --cwd f ci -d '6 0' -Amf
+
+echo g > g/g
+hg --cwd g ci -d '6 0' -Amg
+
+echo % should merge f into g
+hg --cwd g fetch --switch ../f
--- a/tests/test-fetch.out	Mon Mar 03 21:48:43 2008 +0100
+++ b/tests/test-fetch.out	Wed Mar 05 09:10:45 2008 -0800
@@ -15,13 +15,16 @@
 adding c
 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+% should merge c into a
 pulling from ../a
 searching for changes
 adding changesets
 adding manifests
 adding file changes
 added 1 changesets with 1 changes to 1 files (+1 heads)
-merging with new head 2:97d72e5f12c7
+updating to 2:97d72e5f12c7
+1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+merging with 1:5e056962225c
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 new changeset 3:cd3a41621cf0 merges remote changes with local
 a
@@ -34,7 +37,9 @@
 adding manifests
 adding file changes
 added 1 changesets with 1 changes to 1 files (+1 heads)
-merging with new head 2:97d72e5f12c7
+updating to 2:97d72e5f12c7
+1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+merging with 1:5e056962225c
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 new changeset 3:0b6439e938f9 merges remote changes with local
 Automated merge with http://localhost:20059/
@@ -45,7 +50,24 @@
 adding manifests
 adding file changes
 added 1 changesets with 1 changes to 1 files (+1 heads)
-merging with new head 2:97d72e5f12c7
+updating to 2:97d72e5f12c7
+1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+merging with 1:5e056962225c
 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
 new changeset 3:0b6439e938f9 merges remote changes with local
 Automated merge with http://localhost:20059/
+2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+adding f
+adding g
+% should merge f into g
+pulling from ../f
+searching for changes
+adding changesets
+adding manifests
+adding file changes
+added 1 changesets with 1 changes to 1 files (+1 heads)
+0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+merging with 3:cc6a3744834d
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+new changeset 4:96d2275e70b4 merges remote changes with local