D604: context: add arbitraryfilectx, which can represent files outside the workdir

phillco (Phil Cohen) phabricator at mercurial-scm.org
Fri Sep 1 16:58:42 EDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rHG244e2e937b73: context: add arbitraryfilectx, which can represent files outside the workdir (authored by phillco).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D604?vs=1544&id=1559

REVISION DETAIL
  https://phab.mercurial-scm.org/D604

AFFECTED FILES
  contrib/simplemerge
  mercurial/context.py

CHANGE DETAILS

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -2385,3 +2385,34 @@
                 removed.append(f)
 
         return scmutil.status(modified, added, removed, [], [], [], [])
+
+class arbitraryfilectx(object):
+    """Allows you to use filectx-like functions on a file in an arbitrary
+    location on disk, possibly not in the working directory.
+    """
+    def __init__(self, path):
+        self._path = path
+
+    def cmp(self, otherfilectx):
+        return self.data() != otherfilectx.data()
+
+    def path(self):
+        return self._path
+
+    def flags(self):
+        return ''
+
+    def data(self):
+        return util.readfile(self._path)
+
+    def decodeddata(self):
+        with open(self._path, "rb") as f:
+            return f.read()
+
+    def remove(self):
+        util.unlink(self._path)
+
+    def write(self, data, flags):
+        assert not flags
+        with open(self._path, "w") as f:
+            f.write(data)
diff --git a/contrib/simplemerge b/contrib/simplemerge
--- a/contrib/simplemerge
+++ b/contrib/simplemerge
@@ -9,6 +9,7 @@
 
 from mercurial.i18n import _
 from mercurial import (
+    context,
     error,
     fancyopts,
     simplemerge,
@@ -49,26 +50,6 @@
     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)
@@ -85,9 +66,9 @@
             raise ParseError(_('wrong number of arguments'))
     local, base, other = args
     sys.exit(simplemerge.simplemerge(uimod.ui.load(),
-                                     filebackedctx(local),
-                                     filebackedctx(base),
-                                     filebackedctx(other),
+                                     context.arbitraryfilectx(local),
+                                     context.arbitraryfilectx(base),
+                                     context.arbitraryfilectx(other),
                                      **opts))
 except ParseError as e:
     sys.stdout.write("%s: %s\n" % (sys.argv[0], e))



To: phillco, #hg-reviewers
Cc: martinvonz, mercurial-devel


More information about the Mercurial-devel mailing list