merge: extend file merge function for renames
authorMatt Mackall <mpm@selenic.com>
Mon, 02 Oct 2006 12:04:48 -0500
changeset 3211 3fd098e0902d
parent 3210 7240f9e47144
child 3212 cac7be0b9b99
merge: extend file merge function for renames
mercurial/merge.py
tests/test-merge7.out
tests/test-up-local-change.out
--- a/mercurial/merge.py	Sat Sep 30 15:15:59 2006 +0200
+++ b/mercurial/merge.py	Mon Oct 02 12:04:48 2006 -0500
@@ -10,43 +10,59 @@
 from demandload import *
 demandload(globals(), "errno util os tempfile")
 
-def merge3(repo, fn, my, other, p1, p2):
-    """perform a 3-way merge in the working directory"""
+def filemerge(repo, fw, fo, fd, my, other, p1, p2, move):
+    """perform a 3-way merge in the working directory
 
-    def temp(prefix, node):
-        pre = "%s~%s." % (os.path.basename(fn), prefix)
+    fw = filename in the working directory and first parent
+    fo = filename in other parent
+    fd = destination filename
+    my = fileid in first parent
+    other = fileid in second parent
+    p1, p2 = hex changeset ids for merge command
+    move = whether to move or copy the file to the destination
+
+    TODO:
+      if fw is copied in the working directory, we get confused
+      implement move and fd
+    """
+
+    def temp(prefix, ctx):
+        pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
         (fd, name) = tempfile.mkstemp(prefix=pre)
         f = os.fdopen(fd, "wb")
-        repo.wwrite(fn, fl.read(node), f)
+        repo.wwrite(ctx.path(), ctx.data(), f)
         f.close()
         return name
 
-    fl = repo.file(fn)
-    base = fl.ancestor(my, other)
-    a = repo.wjoin(fn)
-    b = temp("base", base)
-    c = temp("other", other)
+    fcm = repo.filectx(fw, fileid=my)
+    fco = repo.filectx(fo, fileid=other)
+    fca = fcm.ancestor(fco)
+    if not fca:
+        fca = repo.filectx(fw, fileid=-1)
+    a = repo.wjoin(fw)
+    b = temp("base", fca)
+    c = temp("other", fco)
 
-    repo.ui.note(_("resolving %s\n") % fn)
-    repo.ui.debug(_("file %s: my %s other %s ancestor %s\n") %
-                          (fn, short(my), short(other), short(base)))
+    repo.ui.note(_("resolving %s\n") % fw)
+    repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
 
     cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge")
            or "hgmerge")
     r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root,
-                    environ={'HG_FILE': fn,
+                    environ={'HG_FILE': fw,
                              'HG_MY_NODE': p1,
-                             'HG_OTHER_NODE': p2,
-                             'HG_FILE_MY_NODE': hex(my),
-                             'HG_FILE_OTHER_NODE': hex(other),
-                             'HG_FILE_BASE_NODE': hex(base)})
+                             'HG_OTHER_NODE': p2})
     if r:
-        repo.ui.warn(_("merging %s failed!\n") % fn)
+        repo.ui.warn(_("merging %s failed!\n") % fw)
 
     os.unlink(b)
     os.unlink(c)
     return r
 
+def merge3(repo, fn, my, other, p1, p2):
+    """perform a 3-way merge in the working directory"""
+    return filemerge(repo, fn, fn, fn, my, other, p1, p2, False)
+
 def checkunknown(repo, m2, status):
     """
     check for collisions between unknown files and files in m2
--- a/tests/test-merge7.out	Sat Sep 30 15:15:59 2006 +0200
+++ b/tests/test-merge7.out	Mon Oct 02 12:04:48 2006 -0500
@@ -27,7 +27,7 @@
  test.txt: versions differ -> m
 merging test.txt
 resolving test.txt
-file test.txt: my fc3148072371 other d40249267ae3 ancestor 8fe46a3eb557
+my test.txt@451c744aabcc other test.txt@a070d41e8360 ancestor test.txt@faaea63e63a9
 merging test.txt failed!
 0 files updated, 0 files merged, 0 files removed, 1 files unresolved
 There are unresolved merges, you can redo the full merge using:
--- a/tests/test-up-local-change.out	Sat Sep 30 15:15:59 2006 +0200
+++ b/tests/test-up-local-change.out	Mon Oct 02 12:04:48 2006 -0500
@@ -21,7 +21,7 @@
  b: remote created -> g
 merging a
 resolving a
-file a: my b789fdd96dc2 other d730145abbf9 ancestor b789fdd96dc2
+my a@33aaa84a386b other a@802f095af299 ancestor a@33aaa84a386b
 getting b
 1 files updated, 1 files merged, 0 files removed, 0 files unresolved
 changeset:   1:802f095af299
@@ -55,7 +55,7 @@
  b: remote created -> g
 merging a
 resolving a
-file a: my b789fdd96dc2 other d730145abbf9 ancestor b789fdd96dc2
+my a@33aaa84a386b other a@802f095af299 ancestor a@33aaa84a386b
 getting b
 1 files updated, 1 files merged, 0 files removed, 0 files unresolved
 changeset:   1:802f095af299
@@ -106,10 +106,10 @@
  b: versions differ -> m
 merging a
 resolving a
-file a: my d730145abbf9 other 13e0d5f949fa ancestor b789fdd96dc2
+my a@802f095af299 other a@030602aee63d ancestor a@33aaa84a386b
 merging b
 resolving b
-file b: my 1e88685f5dde other 61de8c7723ca ancestor 000000000000
+my b@802f095af299 other b@030602aee63d ancestor b@000000000000
 0 files updated, 2 files merged, 0 files removed, 0 files unresolved
 (branch merge, don't forget to commit)
 changeset:   1:802f095af299