Patchwork [5,of,7,mergedriver] filemerge: introduce class whose objects represent files not in a context

login
register
mail settings
Submitter Siddharth Agarwal
Date Nov. 16, 2015, 11:46 p.m.
Message ID <91b518d4c3f5854f7443.1447717569@dev666.prn1.facebook.com>
Download mbox | patch
Permalink /patch/11415/
State Accepted
Headers show

Comments

Siddharth Agarwal - Nov. 16, 2015, 11:46 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1447703135 28800
#      Mon Nov 16 11:45:35 2015 -0800
# Node ID 91b518d4c3f5854f7443bf73467e5d0d83cec01f
# Parent  7040a88af48337c463947171ed16e067ba50ffe9
filemerge: introduce class whose objects represent files not in a context

Most code is going to barf at the return values here (particularly from data
and size), so we restrict it to the filemerge code.

This is already somewhat supported via:

  ctx.filectx(f, fileid=nullid)

Indeed, for add/add conflicts (ancestor doesn't have the file) we use precisely
that. However, that is broken in subtle ways:
- The cmp() function in filectx returns False (identical) for such a filectx
  when compared to a zero-length file.
- size() returns 0 rather than some sort of value indicating that the file isn't
  present.
- data() returns '' rather than some sort of value indicating that the file isn't
  present.

Given the relatively niche use of such filectxes, this seems to be the simplest
way to fix all these issues.

Patch

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -13,7 +13,7 @@  import re
 import tempfile
 
 from .i18n import _
-from .node import short
+from .node import nullid, short
 
 from . import (
     cmdutil,
@@ -44,6 +44,50 @@  nomerge = None
 mergeonly = 'mergeonly'  # just the full merge, no premerge
 fullmerge = 'fullmerge'  # both premerge and merge
 
+class absentfilectx(object):
+    """Represents a file that's ostensibly in a context but is actually not
+    present in it.
+
+    This is here because it's very specific to the filemerge code for now --
+    other code is likely going to break with the values this returns."""
+    def __init__(self, ctx, f):
+        self._ctx = ctx
+        self._f = f
+
+    def path(self):
+        return self._f
+
+    def size(self):
+        return None
+
+    def data(self):
+        return None
+
+    def filenode(self):
+        return nullid
+
+    _customcmp = True
+    def cmp(self, fctx):
+        """compare with other file context
+
+        returns True if different from fctx.
+        """
+        return not (fctx.isabsent() and
+                    fctx.ctx() == self.ctx() and
+                    fctx.path() == self.path())
+
+    def flags(self):
+        return ''
+
+    def changectx(self):
+        return self._ctx
+
+    def isbinary(self):
+        return False
+
+    def isabsent(self):
+        return True
+
 def internaltool(name, mergetype, onfailure=None, precheck=None):
     '''return a decorator for populating internal merge tool table'''
     def decorator(func):