dirstate-entry: `merged_removed` and `from_p2_removed` properties
authorPierre-Yves David <pierre-yves.david@octobus.net>
Sat, 03 Jul 2021 20:34:09 +0200
changeset 47516 b8ffe85e399b
parent 47515 c94d3ff46fd5
child 47517 28632eb3ca3e
dirstate-entry: `merged_removed` and `from_p2_removed` properties Lets start to define and use more semantic property. These two might be a bit too low level and could be shaved off later, however this seems an improvement for now. Differential Revision: https://phab.mercurial-scm.org/D10958
mercurial/cext/parsers.c
mercurial/dirstate.py
mercurial/pure/parsers.py
--- a/mercurial/cext/parsers.c	Sat Jul 03 04:18:54 2021 +0200
+++ b/mercurial/cext/parsers.c	Sat Jul 03 20:34:09 2021 +0200
@@ -30,6 +30,7 @@
 static const char *const versionerrortext = "Python minor version mismatch";
 
 static const int dirstate_v1_from_p2 = -2;
+static const int dirstate_v1_nonnormal = -1;
 
 static PyObject *dict_new_presized(PyObject *self, PyObject *args)
 {
@@ -166,6 +167,15 @@
 	}
 };
 
+static PyObject *dirstatetuple_get_merged_removed(dirstateTupleObject *self)
+{
+	if (self->state == 'r' && self->size == dirstate_v1_nonnormal) {
+		Py_RETURN_TRUE;
+	} else {
+		Py_RETURN_FALSE;
+	}
+};
+
 static PyObject *dirstatetuple_get_from_p2(dirstateTupleObject *self)
 {
 	if (self->size == dirstate_v1_from_p2) {
@@ -175,6 +185,15 @@
 	}
 };
 
+static PyObject *dirstatetuple_get_from_p2_removed(dirstateTupleObject *self)
+{
+	if (self->state == 'r' && self->size == dirstate_v1_from_p2) {
+		Py_RETURN_TRUE;
+	} else {
+		Py_RETURN_FALSE;
+	}
+};
+
 static PyObject *dirstatetuple_get_removed(dirstateTupleObject *self)
 {
 	if (self->state == 'r') {
@@ -186,7 +205,11 @@
 
 static PyGetSetDef dirstatetuple_getset[] = {
     {"state", (getter)dirstatetuple_get_state, NULL, "state", NULL},
+    {"merged_removed", (getter)dirstatetuple_get_merged_removed, NULL,
+     "merged_removed", NULL},
     {"merged", (getter)dirstatetuple_get_merged, NULL, "merged", NULL},
+    {"from_p2_removed", (getter)dirstatetuple_get_from_p2_removed, NULL,
+     "from_p2_removed", NULL},
     {"from_p2", (getter)dirstatetuple_get_from_p2, NULL, "from_p2", NULL},
     {"removed", (getter)dirstatetuple_get_removed, NULL, "removed", NULL},
     {NULL} /* Sentinel */
--- a/mercurial/dirstate.py	Sat Jul 03 04:18:54 2021 +0200
+++ b/mercurial/dirstate.py	Sat Jul 03 20:34:09 2021 +0200
@@ -531,16 +531,18 @@
             # being removed, restore that state.
             entry = self._map.get(f)
             if entry is not None:
-                if entry.removed and (entry[2] == NONNORMAL or entry.from_p2):
+                # XXX this should probably be dealt with a a lower level
+                # (see `merged_removed` and `from_p2_removed`)
+                if entry.merged_removed or entry.from_p2_removed:
                     source = self._map.copymap.get(f)
-                    if entry[2] == NONNORMAL:
+                    if entry.merged_removed:
                         self.merge(f)
-                    elif entry.from_p2:
+                    elif entry.from_p2_removed:
                         self.otherparent(f)
-                    if source:
+                    if source is not None:
                         self.copy(source, f)
                     return
-                if entry.merged or entry.state == b'n' and entry.from_p2:
+                elif entry.merged or entry.state == b'n' and entry.from_p2:
                     return
         self._addpath(f, b'n', 0, possibly_dirty=True)
         self._map.copymap.pop(f, None)
--- a/mercurial/pure/parsers.py	Sat Jul 03 04:18:54 2021 +0200
+++ b/mercurial/pure/parsers.py	Sat Jul 03 20:34:09 2021 +0200
@@ -36,6 +36,9 @@
 # a special value used internally for `size` if the file come from the other parent
 FROM_P2 = -2
 
+# a special value used internally for `size` if the file is modified/merged/added
+NONNORMAL = -1
+
 
 class dirstatetuple(object):
     """represent a dirstate entry
@@ -100,10 +103,28 @@
         return self._size == FROM_P2
 
     @property
+    def from_p2_removed(self):
+        """True if the file has been removed, but was "from_p2" initially
+
+        This property seems like an abstraction leakage and should probably be
+        dealt in this class (or maybe the dirstatemap) directly.
+        """
+        return self._state == b'r' and self._size == FROM_P2
+
+    @property
     def removed(self):
         """True if the file has been removed"""
         return self._state == b'r'
 
+    @property
+    def merged_removed(self):
+        """True if the file has been removed, but was "merged" initially
+
+        This property seems like an abstraction leakage and should probably be
+        dealt in this class (or maybe the dirstatemap)  directly.
+        """
+        return self._state == b'r' and self._size == NONNORMAL
+
     def v1_state(self):
         """return a "state" suitable for v1 serialization"""
         return self._state