| 1261 | } |
| 1262 | |
| 1263 | static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_discover_opts_t *opts, |
| 1264 | const cbm_gitignore_t *gitignore, const gitignore_link_t *ancestors, |
| 1265 | const cbm_gitignore_t *global_gi, const cbm_gitignore_t *cbmignore, |
| 1266 | file_list_t *out) { |
| 1267 | walk_stack_t ws = { |
| 1268 | .frames = calloc(WALK_STACK_CAP, sizeof(walk_frame_t)), .top = 0, .cap = WALK_STACK_CAP}; |
| 1269 | if (!ws.frames) { |
| 1270 | out->failed = true; |
| 1271 | return; |
| 1272 | } |
| 1273 | /* Collect all owned gitignores and chain links — freed at the end because |
| 1274 | * child frames on the stack hold borrowed pointers to them. */ |
| 1275 | cbm_gitignore_t **owned_gis = NULL; |
| 1276 | size_t owned_count = 0; |
| 1277 | size_t owned_capacity = 0; |
| 1278 | gitignore_link_t *owned_links = NULL; |
| 1279 | |
| 1280 | int initial_directory_length = snprintf(ws.frames[0].dir, CBM_SZ_4K, "%s", dir_path); |
| 1281 | int initial_prefix_length = snprintf(ws.frames[0].prefix, CBM_SZ_4K, "%s", rel_prefix); |
| 1282 | if (initial_directory_length <= 0 || initial_directory_length >= CBM_SZ_4K || |
| 1283 | initial_prefix_length < 0 || initial_prefix_length >= CBM_SZ_4K) { |
| 1284 | out->failed = true; |
| 1285 | free(ws.frames); |
| 1286 | return; |
| 1287 | } |
| 1288 | /* `ancestors` is the deepest link of the enclosing repository's chain (or |
| 1289 | * NULL); the walk root's own matcher sits below it so it still wins. */ |
| 1290 | ws.frames[0].ignore_chain = ancestors; |
| 1291 | if (gitignore) { |
| 1292 | ws.frames[0].ignore_chain = |
| 1293 | gitignore_link_new(gitignore, rel_prefix, "", ancestors, &owned_links); |
| 1294 | if (!ws.frames[0].ignore_chain) { |
| 1295 | out->failed = true; |
| 1296 | free(ws.frames); |
| 1297 | return; |
| 1298 | } |
| 1299 | } |
| 1300 | ws.top++; |
| 1301 | |
| 1302 | while (ws.top > 0 && !file_list_should_stop(out)) { |
| 1303 | walk_frame_t frame = ws.frames[--ws.top]; |
| 1304 | |
| 1305 | cbm_gitignore_t *loaded = try_load_nested_gitignore(&frame); |
| 1306 | if (loaded) { |
| 1307 | if (!walk_owned_gitignore_append(&owned_gis, &owned_count, &owned_capacity, loaded)) { |
| 1308 | cbm_gitignore_free(loaded); |
| 1309 | out->failed = true; |
| 1310 | break; |
| 1311 | } |
| 1312 | /* owned_gis owns `loaded` from here on, even if the link fails. */ |
| 1313 | const gitignore_link_t *link = |
| 1314 | gitignore_link_new(loaded, frame.prefix, "", frame.ignore_chain, &owned_links); |
| 1315 | if (!link) { |
| 1316 | out->failed = true; |
| 1317 | break; |
| 1318 | } |
| 1319 | frame.ignore_chain = link; |
| 1320 | } |
no test coverage detected