Mercurial > public > mercurial-scm > hg
diff mercurial/context.py @ 34051:d2fc88426d21
context: add arbitraryfilectx, which can represent files outside the workdir
Move it from contrib/simplemerge so it can be re-used in the future.
Differential Revision: https://phab.mercurial-scm.org/D604
author | Phil Cohen <phillco@fb.com> |
---|---|
date | Fri, 01 Sep 2017 11:52:20 -0700 |
parents | 65ae54582713 |
children | f698bb31bdfb |
line wrap: on
line diff
--- a/mercurial/context.py Fri Sep 01 10:35:43 2017 -0700 +++ b/mercurial/context.py Fri Sep 01 11:52:20 2017 -0700 @@ -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)