Patchwork D378: contrib: make simplemerge script pass context-like objects

login
register
mail settings
Submitter phabricator
Date Aug. 14, 2017, 6:15 a.m.
Message ID <differential-rev-PHID-DREV-gxmwr7df72gh7ypkijdi-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/22960/
State Superseded
Headers show

Comments

phabricator - Aug. 14, 2017, 6:15 a.m.
phillco created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  `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.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  contrib/simplemerge

CHANGE DETAILS




To: phillco, #hg-reviewers
Cc: mercurial-devel
phabricator - Aug. 19, 2017, 5:56 p.m.
phillco added a subscriber: martinvonz.
phillco added inline comments.

INLINE COMMENTS

> simplemerge:44-63
> +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:

As @martinvonz noted this could probably be combined with the `arbitraryfilectx` I made in https://bitbucket.org/phillco/hg-imm/commits/0cc65baa328bbba84629aeb4708ee2fa765a50f0.

REPOSITORY
  rHG Mercurial

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

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

Patch

diff --git a/contrib/simplemerge b/contrib/simplemerge
--- a/contrib/simplemerge
+++ b/contrib/simplemerge
@@ -41,6 +41,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 data(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)
@@ -55,7 +75,16 @@ 
         sys.exit(0)
     if len(args) != 3:
             raise ParseError(_('wrong number of arguments'))
-    sys.exit(simplemerge.simplemerge(ui.ui.load(), *args, **opts))
+    local, base, other = args
+    sys.exit(simplemerge.simplemerge(ui.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()