* The tree traversal is looking at name p. If we have a matching entry, * return it. If name p is a directory in the index, do not return * anything, as we will want to match it when the traversal descends into * the directory. */
| 1275 | * the directory. |
| 1276 | */ |
| 1277 | static int find_cache_pos(struct traverse_info *info, |
| 1278 | const char *p, size_t p_len) |
| 1279 | { |
| 1280 | int pos; |
| 1281 | struct unpack_trees_options *o = info->data; |
| 1282 | struct index_state *index = o->src_index; |
| 1283 | int pfxlen = info->pathlen; |
| 1284 | |
| 1285 | for (pos = o->internal.cache_bottom; pos < index->cache_nr; pos++) { |
| 1286 | const struct cache_entry *ce = index->cache[pos]; |
| 1287 | const char *ce_name, *ce_slash; |
| 1288 | int cmp, ce_len; |
| 1289 | |
| 1290 | if (ce->ce_flags & CE_UNPACKED) { |
| 1291 | /* |
| 1292 | * cache_bottom entry is already unpacked, so |
| 1293 | * we can never match it; don't check it |
| 1294 | * again. |
| 1295 | */ |
| 1296 | if (pos == o->internal.cache_bottom) |
| 1297 | ++o->internal.cache_bottom; |
| 1298 | continue; |
| 1299 | } |
| 1300 | if (!ce_in_traverse_path(ce, info)) { |
| 1301 | /* |
| 1302 | * Check if we can skip future cache checks |
| 1303 | * (because we're already past all possible |
| 1304 | * entries in the traverse path). |
| 1305 | */ |
| 1306 | if (info->traverse_path) { |
| 1307 | if (strncmp(ce->name, info->traverse_path, |
| 1308 | info->pathlen) > 0) |
| 1309 | break; |
| 1310 | } |
| 1311 | continue; |
| 1312 | } |
| 1313 | ce_name = ce->name + pfxlen; |
| 1314 | ce_slash = strchr(ce_name, '/'); |
| 1315 | if (ce_slash) |
| 1316 | ce_len = ce_slash - ce_name; |
| 1317 | else |
| 1318 | ce_len = ce_namelen(ce) - pfxlen; |
| 1319 | cmp = name_compare(p, p_len, ce_name, ce_len); |
| 1320 | /* |
| 1321 | * Exact match; if we have a directory we need to |
| 1322 | * delay returning it. |
| 1323 | */ |
| 1324 | if (!cmp) |
| 1325 | return ce_slash ? -2 - pos : pos; |
| 1326 | if (0 < cmp) |
| 1327 | continue; /* keep looking */ |
| 1328 | /* |
| 1329 | * ce_name sorts after p->path; could it be that we |
| 1330 | * have files under p->path directory in the index? |
| 1331 | * E.g. ce_name == "t-i", and p->path == "t"; we may |
| 1332 | * have "t/a" in the index. |
| 1333 | */ |
| 1334 | if (p_len < ce_len && !memcmp(ce_name, p, p_len) && |
no test coverage detected