Patchwork D3456: revlog: extract function for getting node from known-to-exist rev

login
register
mail settings
Submitter phabricator
Date May 7, 2018, 4:55 p.m.
Message ID <differential-rev-PHID-DREV-3hn6xpp4t62e4de7erqk-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/31307/
State Superseded
Headers show

Comments

phabricator - May 7, 2018, 4:55 p.m.
martinvonz created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Many of the calls to index_node() (which converts a rev to a nodeid)
  are done with a rev that's know to exist. If the function fails,
  there's something really wrong and we should just abort. This was done
  in only one place. This patch starts by extracting that code to a
  function that we can reuse in later patches.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/cext/revlog.c

CHANGE DETAILS




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

Patch

diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -248,6 +248,18 @@ 
 	return data ? data + 32 : NULL;
 }
 
+/*
+ * Return the 20-byte SHA of the node corresponding to the given rev. The
+ * rev is assumed to be existing. If not, an exception is set.
+ */
+static const char *index_node_existing(indexObject *self, Py_ssize_t pos) {
+  const char *node = index_node(self, pos);
+  if (node == NULL) {
+    PyErr_Format(PyExc_IndexError, "could not access rev %d", (int)pos);
+  }
+  return node;
+}
+
 static int nt_insert(indexObject *self, const char *node, int rev);
 
 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
@@ -1282,10 +1294,8 @@ 
 		return PyBytes_FromStringAndSize(nullid, 20);
 	}
 
-	fullnode = index_node(self, rev);
+	fullnode = index_node_existing(self, rev);
 	if (fullnode == NULL) {
-		PyErr_Format(PyExc_IndexError,
-			     "could not access rev %d", rev);
 		return NULL;
 	}
 	return PyBytes_FromStringAndSize(fullnode, 20);