contrib: make simplemerge script pass context-like objects
authorPhil Cohen <phillco@fb.com>
Thu, 24 Aug 2017 21:30:37 -0700
changeset 33903 ed6f64173121
parent 33902 f39ba8237ed6
child 33904 1915a5e809ca
contrib: make simplemerge script pass context-like objects `simplemerge()` will soon require context-like objects to work. Create a simple context-like object that wraps the requested files and can be passed to the new API. Differential Revision: https://phab.mercurial-scm.org/D378
contrib/simplemerge
--- a/contrib/simplemerge	Thu Aug 24 21:26:40 2017 -0700
+++ b/contrib/simplemerge	Thu Aug 24 21:30:37 2017 -0700
@@ -49,6 +49,26 @@
     for first, second in out_opts:
         sys.stdout.write(' %-*s  %s\n' % (opts_len, first, second))
 
+class filebackedctx(object):
+    """simplemerge requires context-like objects"""
+    def __init__(self, path):
+        self._path = path
+
+    def decodeddata(self):
+        with open(self._path, "rb") as f:
+            return f.read()
+
+    def flags(self):
+        return ''
+
+    def path(self):
+        return self._path
+
+    def write(self, data, flags):
+        assert not flags
+        with open(self._path, "w") as f:
+            f.write(data)
+
 try:
     for fp in (sys.stdin, sys.stdout, sys.stderr):
         util.setbinary(fp)
@@ -63,7 +83,16 @@
         sys.exit(0)
     if len(args) != 3:
             raise ParseError(_('wrong number of arguments'))
-    sys.exit(simplemerge.simplemerge(uimod.ui.load(), *args, **opts))
+    local, base, other = args
+    sys.exit(simplemerge.simplemerge(uimod.ui.load(),
+                                     local,
+                                     base,
+                                     other,
+                                     filebackedctx(local),
+                                     filebackedctx(base),
+                                     filebackedctx(other),
+                                     filtereddata=True,
+                                     **opts))
 except ParseError as e:
     sys.stdout.write("%s: %s\n" % (sys.argv[0], e))
     showhelp()