+ prevbuf[0] = '\0';
+ tf = triewalk_next(tw, buf);
+ assert(tf);
+ while (1) {
+ int i;
+
+ /*
+ * Get the next file from the index. So we are
+ * currently holding, and have not yet
+ * indexed, prevtf (with pathname prevbuf) and
+ * tf (with pathname buf).
+ */
+ prevtf = tf;
+ memcpy(prevbuf, buf, maxpathlen);
+ tf = triewalk_next(tw, buf);
+
+ if (!tf)
+ buf[0] = '\0';
+
+ /*
+ * Find the first differing character position
+ * between our two pathnames.
+ */
+ for (i = 0; prevbuf[i] && prevbuf[i] == buf[i]; i++);
+
+ /*
+ * If prevbuf was a directory name and buf is
+ * something inside that directory, then
+ * trie_before() will be called on prevbuf
+ * itself. Hence we must drop a tag before it,
+ * so that the resulting index is usable.
+ */
+ if ((!prevbuf[i] && (buf[i] == pathsep ||
+ (i > 0 && buf[i-1] == pathsep))))
+ indexbuild_tag(ib);
+
+ /*
+ * Add prevtf to the index.
+ */
+ indexbuild_add(ib, prevtf);
+
+ if (!tf) {
+ /*
+ * Drop an unconditional final tag, and
+ * get out of this loop.
+ */
+ indexbuild_tag(ib);
+ break;
+ }
+
+ /*
+ * In full-index mode, index everything.
+ */
+ if (fullindex)
+ indexbuild_tag(ib);
+
+ /*
+ * If prevbuf was a filename inside some
+ * directory which buf is outside, then
+ * trie_before() will be called on some
+ * pathname either equal to buf or epsilon
+ * less than it. Either way, we're going to
+ * need to drop a tag after prevtf.
+ */
+ if (strchr(prevbuf+i, pathsep) || !tf)
+ indexbuild_tag(ib);
+ }
+