merge: add support for tool EOL fixups
authorMatt Mackall <mpm@selenic.com>
Sun, 03 Feb 2008 19:29:05 -0600
changeset 6005 3c33032d8906
parent 6004 5af5f0f9d724
child 6006 3c9dbb743d20
merge: add support for tool EOL fixups specified with merge-tools:<tool>.fixeol=True
mercurial/filemerge.py
--- a/mercurial/filemerge.py	Sun Feb 03 19:29:05 2008 -0600
+++ b/mercurial/filemerge.py	Sun Feb 03 19:29:05 2008 -0600
@@ -59,6 +59,29 @@
         if _findtool(ui, t) and check(t, None, symlink, binary):
             return t
 
+def _eoltype(data):
+    "Guess the EOL type of a file"
+    if '\0' in data: # binary
+        return None
+    if '\r\n' in data: # Windows
+        return '\r\n'
+    if '\r' in data: # Old Mac
+        return '\r'
+    if '\n' in data: # UNIX
+        return '\n'
+    return None # unknown
+
+def _matcheol(file, origfile):
+    "Convert EOL markers in a file to match origfile"
+    tostyle = _eoltype(open(origfile, "rb").read())
+    if tostyle:
+        data = open(file, "rb").read()
+        style = _eoltype(data)
+        if style:
+            newdata = data.replace(style, tostyle)
+            if newdata != data:
+                open(file, "wb").write(newdata)
+
 def filemerge(repo, fw, fd, fo, wctx, mctx):
     """perform a 3-way merge in the working directory
 
@@ -158,6 +181,9 @@
         if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcm.data()):
             r = 1
 
+    if _toolbool(ui, tool, "fixeol"):
+        _matcheol(repo.join(fd), back)
+
     if r:
         repo.ui.warn(_("merging %s failed!\n") % fd)
     else: