comparison mercurial/filemerge.py @ 6003:7855b88ba838

filemerge: pull file-merging code into its own module
author Matt Mackall <mpm@selenic.com>
date Sun, 03 Feb 2008 19:29:05 -0600
parents
children 5af5f0f9d724
comparison
equal deleted inserted replaced
6002:abd66eb0889e 6003:7855b88ba838
1 # filemerge.py - file-level merge handling for Mercurial
2 #
3 # Copyright 2006, 2007, 2008 Matt Mackall <mpm@selenic.com>
4 #
5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference.
7
8 from node import *
9 from i18n import _
10 import util, os, tempfile, context
11
12 def filemerge(repo, fw, fd, fo, wctx, mctx):
13 """perform a 3-way merge in the working directory
14
15 fw = original filename in the working directory
16 fd = destination filename in the working directory
17 fo = filename in other parent
18 wctx, mctx = working and merge changecontexts
19 """
20
21 def temp(prefix, ctx):
22 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
23 (fd, name) = tempfile.mkstemp(prefix=pre)
24 data = repo.wwritedata(ctx.path(), ctx.data())
25 f = os.fdopen(fd, "wb")
26 f.write(data)
27 f.close()
28 return name
29
30 fcm = wctx.filectx(fw)
31 fcmdata = wctx.filectx(fd).data()
32 fco = mctx.filectx(fo)
33
34 if not fco.cmp(fcmdata): # files identical?
35 return None
36
37 fca = fcm.ancestor(fco)
38 if not fca:
39 fca = repo.filectx(fw, fileid=nullrev)
40 a = repo.wjoin(fd)
41 b = temp("base", fca)
42 c = temp("other", fco)
43
44 if fw != fo:
45 repo.ui.status(_("merging %s and %s\n") % (fw, fo))
46 else:
47 repo.ui.status(_("merging %s\n") % fw)
48
49 repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
50
51 cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge")
52 or "hgmerge")
53 r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root,
54 environ={'HG_FILE': fd,
55 'HG_MY_NODE': str(wctx.parents()[0]),
56 'HG_OTHER_NODE': str(mctx),
57 'HG_MY_ISLINK': fcm.islink(),
58 'HG_OTHER_ISLINK': fco.islink(),
59 'HG_BASE_ISLINK': fca.islink(),})
60 if r:
61 repo.ui.warn(_("merging %s failed!\n") % fd)
62
63 os.unlink(b)
64 os.unlink(c)
65 return r