Patchwork D1242: overlayworkingctx: add ``tomemctx()`` and ``commit()``

login
register
mail settings
Submitter phabricator
Date Dec. 7, 2017, 9:52 p.m.
Message ID <19a71a91d4a608eedff55fd006cc4f11@localhost.localdomain>
Download mbox | patch
Permalink /patch/26049/
State Not Applicable
Headers show

Comments

phabricator - Dec. 7, 2017, 9:52 p.m.
phillco updated this revision to Diff 4215.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1242?vs=4207&id=4215

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

AFFECTED FILES
  mercurial/context.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -2177,6 +2177,57 @@ 
                                              self._path)
         return self._wrappedctx[path].size()
 
+    def tomemctx(self, text, branch=None, extra=None, date=None, parents=None, user=None, editor=None):
+        """Converts this ``overlayworkingctx`` into a ``memctx`` ready to be
+        committed.
+        
+        ``text`` is the commit message.
+        ``parents`` (optional) are rev numbers.
+        """
+        # Default parents to the wrapped contexts' if not passed.
+        if parents == None:
+            parents = self._wrappedctx.parents()
+            if len(parents) == 1:
+                parents = (parents[0], None)
+
+        # ``parents`` is passed as rev numbers; convert to ``commitctxs``.
+        if parents[1] is None:
+            parents = (self._repo[parents[0]], None)
+        else:
+            parents = (self._repo[parents[0]], self._repo[parents[1]])
+
+        files = self._cache.keys()
+        def getfile(repo, memctx, path):
+            if self._cache[path]['exists']:
+                return memfilectx(repo, path,
+                                  self._cache[path]['data'],
+                                  'l' in self._cache[path]['flags'],
+                                  'x' in self._cache[path]['flags'],
+                                  self._cache[path]['copied'],
+                                  memctx)
+            else:
+                # Returning None, but including the path in `files`, is
+                # necessary for memctx to register a deletion.
+                return None
+        return memctx(self._repo, parents, text, files, getfile, date=date,
+                      extra=extra, user=user, branch=branch, editor=editor)
+
+    def commit(self, text, parents=None, date=None, extra=None, editor=None,
+               user=None, branch=None):
+        allowemptycommit = (len(self._cache) or
+                            self._repo.ui.configbool('ui', 'allowemptycommit'))
+        if not allowemptycommit:
+            return None
+
+        # By convention, ``extra['branch']`` clobbers ``branch`` (used when
+        # passing ``--keepbranches``).
+        if 'branch' in extra:
+            branch = extra['branch']
+
+        memctx = self.tomemctx(text, parents=parents, date=date, extra=extra,
+                               user=user, branch=branch, editor=editor)
+        return self._repo.commitctx(memctx)
+
     def isdirty(self, path):
         return path in self._cache