Skip to content

Commit a49481d

Browse files
authored
Merge pull request DeusData#2602 from bline/fix/doc-link-moved-directory
fix(doc-links): report references into a moved directory as missing (DeusData#2588)
2 parents f05c221 + 6530142 commit a49481d

2 files changed

Lines changed: 94 additions & 7 deletions

File tree

‎src/pipeline/doc_links_md.c‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
* document wrote the path.
1717
*
1818
* Unresolved:
19-
* missing the path is not in the graph, but its directory is: the
20-
* document names something that is not there (doc rot)
19+
* missing the path is not in the graph, but its directory is, or a
20+
* directory above it that the reference wrote is: the document
21+
* names something that is not there (doc rot; a moved or
22+
* renamed directory)
2123
* ambiguous a file name alone that is not next to the document but exists
2224
* elsewhere in the repository: the document does not say which
2325
* Not a reference to this repository at all (LOCAL: no edge, no row): a link
2426
* back into the same document; a path that is neither indexed nor of a type
2527
* the indexer reads (an image, an archive, an extensionless page); a path
26-
* whose directory is not in the repository either (a file a tutorial asks
27-
* the reader to create, a system path, a path above the root); and, in a
28-
* code span, a rooted single segment without a file extension, which is a
28+
* none of whose directories is in the repository either (a file a tutorial
29+
* asks the reader to create, a system path, a path above the root); and, in
30+
* a code span, a rooted single segment without a file extension, which is a
2931
* URL route (`/docs`, `/health`), not the directory of that name.
3032
*/
3133
#include "pipeline/doc_links.h"
@@ -408,6 +410,25 @@ static const cbm_gbuf_node_t *mdr_folder(const mdr_index_t *x, const cbm_gbuf_t
408410
return path[0] ? (const cbm_gbuf_node_t *)cbm_ht_get(x->folders, path) : NULL;
409411
}
410412

413+
/* Whether a directory above `path` that the reference itself wrote, one
414+
* longer than the `base_len` bytes it was read from, is a Folder. */
415+
static bool mdr_written_ancestor(const mdr_index_t *x, const cbm_gbuf_t *graph, const char *path,
416+
size_t base_len) {
417+
char dir[MDR_PATH_CAP];
418+
size_t n = strlen(path);
419+
if (n >= sizeof(dir)) {
420+
return false;
421+
}
422+
memcpy(dir, path, n + SKIP_ONE);
423+
for (char *s = strrchr(dir, '/'); s && (size_t)(s - dir) > base_len; s = strrchr(dir, '/')) {
424+
*s = '\0';
425+
if (mdr_folder(x, graph, dir)) {
426+
return true;
427+
}
428+
}
429+
return false;
430+
}
431+
411432
static bool mdr_code_label(const char *label) {
412433
for (size_t i = 0; label && MDR_CODE_LABELS[i]; i++) {
413434
if (strcmp(label, MDR_CODE_LABELS[i]) == 0) {
@@ -1126,8 +1147,19 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11261147
char tries[MDR_TRIES][MDR_PATH_CAP];
11271148
int nt = 0;
11281149
int attempted = 0;
1150+
size_t base_len[MDR_TRIES]; /* the directory each try reads from, before what p wrote */
11291151
if (!rooted) {
11301152
attempted++;
1153+
char lead[MDR_PATH_CAP]; /* p's leading `./` and `../` segments */
1154+
char base[MDR_PATH_CAP];
1155+
size_t ll = 0;
1156+
while (ll < sizeof(lead) - PAIR_LEN - 1 && (strncmp(p + ll, "./", PAIR_LEN) == 0 ||
1157+
strncmp(p + ll, "../", PAIR_LEN + 1) == 0)) {
1158+
ll += p[ll + 1] == '/' ? PAIR_LEN : PAIR_LEN + 1;
1159+
}
1160+
memcpy(lead, p, ll);
1161+
lead[ll] = '\0';
1162+
base_len[nt] = mdr_join(doc_dir, lead, base, sizeof(base)) ? strlen(base) : 0;
11311163
nt += mdr_join(doc_dir, p, tries[nt], MDR_PATH_CAP);
11321164
}
11331165
if (!relative && !r->name_only) {
@@ -1137,7 +1169,7 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11371169
q++;
11381170
}
11391171
if (mdr_join("", q, tries[nt], MDR_PATH_CAP) && (nt == 0 || strcmp(tries[0], tries[nt]))) {
1140-
nt++;
1172+
base_len[nt++] = 0;
11411173
}
11421174
}
11431175
for (int i = 0; i < nt; i++) {
@@ -1174,7 +1206,8 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11741206
return;
11751207
}
11761208
/* doc rot when the reference points into the repository: its directory is
1177-
* there (a bare name in a link: the document's own directory) */
1209+
* there (a bare name in a link: the document's own directory), or a
1210+
* directory above it that the reference wrote is (one moved or renamed) */
11781211
for (int i = 0; i < nt; i++) {
11791212
char parent[MDR_PATH_CAP];
11801213
const char *ps = strrchr(tries[i], '/');
@@ -1185,6 +1218,10 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11851218
out->reason = CBM_DOCLINK_REASON_MISSING;
11861219
return;
11871220
}
1221+
if (pl > 0 && strchr(p, '/') && mdr_written_ancestor(x, graph, tries[i], base_len[i])) {
1222+
out->reason = CBM_DOCLINK_REASON_MISSING;
1223+
return;
1224+
}
11881225
if (pl == 0 && r->link && !rooted) {
11891226
out->reason = CBM_DOCLINK_REASON_MISSING;
11901227
return;

‎tests/test_doc_links_md.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,55 @@ TEST(doc_links_md_incremental) {
444444
PASS();
445445
}
446446

447+
/* #2588: a reference into a directory that was moved or renamed named a
448+
* path whose directory is gone, so it counted as no reference to this
449+
* repository: no edge and no row. A directory above it that the reference
450+
* wrote is still there, so it is doc rot. A path none of whose directories
451+
* the repository has stays no reference. */
452+
TEST(doc_links_md_moved_directory_issue2588) {
453+
char tmp[256];
454+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_dlmd_mv_XXXXXX");
455+
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
456+
char repo[512];
457+
snprintf(repo, sizeof(repo), "%s/repo", tmp);
458+
th_write_file(TH_PATH(repo, "docs/notes.md"), "# Notes\n"
459+
"\n"
460+
"See `src/util/rounding.py::clamp` and "
461+
"[rounding](../src/util/rounding.py).\n"
462+
"Create `scratch/new/thing.py` first.\n");
463+
th_write_file(TH_PATH(repo, "src/ledger.py"), "def balance(ledger):\n"
464+
" return sum(ledger)\n");
465+
th_write_file(TH_PATH(repo, "src/util/rounding.py"), "def clamp(x, lo, hi):\n"
466+
" return max(lo, min(hi, x))\n");
467+
char db[512];
468+
char full_db[512];
469+
snprintf(db, sizeof(db), "%s/inc.db", tmp);
470+
snprintf(full_db, sizeof(full_db), "%s/full.db", tmp);
471+
ASSERT_EQ(dm_index(repo, db, NULL), 0);
472+
char props[512];
473+
char reason[64];
474+
ASSERT_EQ(md_edge(db, "Notes", "rounding.clamp", props, sizeof(props)), 1);
475+
476+
/* src/util moves to src/helpers: both references are missing, on the
477+
* incremental route as in a full index */
478+
th_write_file(TH_PATH(repo, "src/helpers/rounding.py"), "def clamp(x, lo, hi):\n"
479+
" return max(lo, min(hi, x))\n");
480+
ASSERT_EQ(th_rmtree(TH_PATH(repo, "src/util")), 0);
481+
ASSERT_EQ(dm_step(repo, db, full_db, "directory moved", CBM_INCREMENTAL_ROUTE_FORCED_FULL), 0);
482+
ASSERT_EQ(md_edge(db, "Notes", "rounding.clamp", props, sizeof(props)), 0);
483+
dm_row(db, "docs/notes.md", "src/util/rounding.py::clamp", reason, sizeof(reason), NULL, 0);
484+
ASSERT_STR_EQ(reason, "missing");
485+
dm_row(db, "docs/notes.md", "../src/util/rounding.py", reason, sizeof(reason), NULL, 0);
486+
ASSERT_STR_EQ(reason, "missing");
487+
488+
/* no directory of it in the repository: still no reference */
489+
dm_row(db, "docs/notes.md", "scratch/new/thing.py", reason, sizeof(reason), NULL, 0);
490+
ASSERT_STR_EQ(reason, "");
491+
492+
th_cleanup(tmp);
493+
PASS();
494+
}
495+
447496
/* ── ADRs ────────────────────────────────────────────────────────── */
448497

449498
static void md_write_adrs(const char *tmp, const char *first_status) {
@@ -866,6 +915,7 @@ SUITE(doc_links_md) {
866915
RUN_TEST(doc_links_md_classify_span);
867916
RUN_TEST(doc_links_md_resolve);
868917
RUN_TEST(doc_links_md_incremental);
918+
RUN_TEST(doc_links_md_moved_directory_issue2588);
869919
RUN_TEST(doc_links_md_adr);
870920
RUN_TEST(doc_links_md_adr_label_choice);
871921
RUN_TEST(doc_links_md_adr_reported);

0 commit comments

Comments
 (0)