Compare
05cf4ceb5bc28580ef30cb45c8bbd8b447431cf4..b6f17686b20647fc904e6201fa6d168731e588eb
dir: preserve pathspec prefix optimization with leading excludes
Directory walks use the common directory prefix of non-exclude pathspec items to avoid scanning unrelated portions of the working tree or index. Exclude items only remove paths from that candidate set, so they do not need to widen the traversal. When an exclude item is the first pathspec item, common_prefix_len() fails to establish a comparison base and returns a zero-length prefix. The result is correct, but Git unnecessarily traverses from a broader starting point even when all non-exclude items share a directory. Use the first non-exclude item as the comparison base and return its string together with the prefix length, allowing callers to start from the recovered directory prefix. Exclude matching continues to use full paths, so this restores the optimization without changing which paths are selected. Add a unit test covering an exclude item before two non-exclude items with a common directory. Signed-off-by: Yannik Tausch <dev@ytausch.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Yannik Tausch <dev@ytausch.de> · 2026-09-14 07:27 UTC · committed by Junio C Hamano
9 files changed,
+107
−84
- builtin/checkout.c
- builtin/worktree.c
- checkout.c
- checkout.h
- dir.c
- pathspec.c
- pathspec.h
- t/t6132-pathspec-exclude.sh
- t/unit-tests/u-dir.c
modified builtin/checkout.c
| @@ -1340,51 +1340,13 @@ enum checkout_command { | |||
| 1340 | 1340 | CHECKOUT_RESTORE = 3, | |
| 1341 | 1341 | }; | |
| 1342 | 1342 | ||
| 1343 | - | static void advise_disambiguating_remotes(enum checkout_command which_command, | |
| 1344 | - | const char *branch, | |
| 1345 | - | const struct string_list *matched_remote_names) | |
| 1346 | - | { | |
| 1347 | - | const char *cmdname; | |
| 1348 | - | struct string_list_item *item; | |
| 1349 | - | ||
| 1350 | - | switch (which_command) { | |
| 1351 | - | case CHECKOUT_CHECKOUT: | |
| 1352 | - | cmdname = "checkout"; | |
| 1353 | - | break; | |
| 1354 | - | case CHECKOUT_SWITCH: | |
| 1355 | - | cmdname = "switch"; | |
| 1356 | - | break; | |
| 1357 | - | default: | |
| 1358 | - | BUG("command <%d> should not reach advise_disambiguating_remotes", | |
| 1359 | - | which_command); | |
| 1360 | - | break; | |
| 1361 | - | } | |
| 1362 | - | ||
| 1363 | - | advise(_("Branch name '%s' appears in multiple remotes:"), branch); | |
| 1364 | - | for_each_string_list_item(item, matched_remote_names) { | |
| 1365 | - | advise(_(" %s"), item->string); | |
| 1366 | - | } | |
| 1367 | - | advise(_("If you meant to check out a remote tracking branch on <remote>,\n" | |
| 1368 | - | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1369 | - | "\n" | |
| 1370 | - | " git %s --track <remote>/%s\n" | |
| 1371 | - | "\n" | |
| 1372 | - | "If you'd like to always have checkouts of an ambiguous name prefer\n" | |
| 1373 | - | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1374 | - | "checkout.defaultRemote=origin in your config."), | |
| 1375 | - | cmdname, branch); | |
| 1376 | - | } | |
| 1377 | - | ||
| 1378 | 1343 | static char *parse_remote_branch(const char *arg, | |
| 1379 | 1344 | struct object_id *rev, | |
| 1380 | 1345 | int could_be_checkout_paths, | |
| 1381 | 1346 | enum checkout_command which_command) | |
| 1382 | 1347 | { | |
| 1383 | 1348 | int num_matches = 0; | |
| 1384 | - | struct string_list matched_remote_names = STRING_LIST_INIT_DUP; | |
| 1385 | - | ||
| 1386 | - | char *remote = unique_tracking_name(arg, rev, &num_matches, | |
| 1387 | - | &matched_remote_names); | |
| 1349 | + | char *remote = unique_tracking_name(arg, rev, &num_matches); | |
| 1388 | 1350 | ||
| 1389 | 1351 | if (remote && could_be_checkout_paths) { | |
| 1390 | 1352 | die(_("'%s' could be both a local file and a tracking branch.\n" | |
| @@ -1393,15 +1355,37 @@ static char *parse_remote_branch(const char *arg, | |||
| 1393 | 1355 | } | |
| 1394 | 1356 | ||
| 1395 | 1357 | if (!remote && num_matches > 1) { | |
| 1396 | - | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 1397 | - | advise_disambiguating_remotes(which_command, arg, | |
| 1398 | - | &matched_remote_names); | |
| 1399 | - | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1400 | - | arg, num_matches); | |
| 1358 | + | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
| 1359 | + | const char *cmdname; | |
| 1360 | + | ||
| 1361 | + | switch (which_command) { | |
| 1362 | + | case CHECKOUT_CHECKOUT: | |
| 1363 | + | cmdname = "checkout"; | |
| 1364 | + | break; | |
| 1365 | + | case CHECKOUT_SWITCH: | |
| 1366 | + | cmdname = "switch"; | |
| 1367 | + | break; | |
| 1368 | + | default: | |
| 1369 | + | BUG("command <%d> should not reach parse_remote_branch", | |
| 1370 | + | which_command); | |
| 1371 | + | break; | |
| 1372 | + | } | |
| 1373 | + | ||
| 1374 | + | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1375 | + | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1376 | + | "\n" | |
| 1377 | + | " git %s --track origin/<name>\n" | |
| 1378 | + | "\n" | |
| 1379 | + | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1380 | + | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1381 | + | "checkout.defaultRemote=origin in your config."), | |
| 1382 | + | cmdname); | |
| 1383 | + | } | |
| 1384 | + | ||
| 1385 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1386 | + | arg, num_matches); | |
| 1401 | 1387 | } | |
| 1402 | 1388 | ||
| 1403 | - | string_list_clear(&matched_remote_names, 0); | |
| 1404 | - | ||
| 1405 | 1389 | return remote; | |
| 1406 | 1390 | } | |
| 1407 | 1391 | ||
modified builtin/worktree.c
| @@ -781,7 +781,7 @@ static char *dwim_branch(const char *path, char **new_branch) | |||
| 781 | 781 | *new_branch = branchname; | |
| 782 | 782 | if (guess_remote) { | |
| 783 | 783 | struct object_id oid; | |
| 784 | - | char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL); | |
| 784 | + | char *remote = unique_tracking_name(*new_branch, &oid, NULL); | |
| 785 | 785 | return remote; | |
| 786 | 786 | } | |
| 787 | 787 | return NULL; | |
| @@ -903,7 +903,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 903 | 903 | ||
| 904 | 904 | commit = lookup_commit_reference_by_name(branch); | |
| 905 | 905 | if (!commit) { | |
| 906 | - | remote = unique_tracking_name(branch, &oid, NULL, NULL); | |
| 906 | + | remote = unique_tracking_name(branch, &oid, NULL); | |
| 907 | 907 | if (remote) { | |
| 908 | 908 | new_branch = branch; | |
| 909 | 909 | branch = new_branch_to_free = remote; | |
modified checkout.c
| @@ -8,7 +8,6 @@ | |||
| 8 | 8 | #include "checkout.h" | |
| 9 | 9 | #include "config.h" | |
| 10 | 10 | #include "strbuf.h" | |
| 11 | - | #include "string-list.h" | |
| 12 | 11 | ||
| 13 | 12 | struct tracking_name_data { | |
| 14 | 13 | /* const */ char *src_ref; | |
| @@ -18,7 +17,6 @@ struct tracking_name_data { | |||
| 18 | 17 | const char *default_remote; | |
| 19 | 18 | char *default_dst_ref; | |
| 20 | 19 | struct object_id *default_dst_oid; | |
| 21 | - | struct string_list *remote_names; | |
| 22 | 20 | }; | |
| 23 | 21 | ||
| 24 | 22 | #define TRACKING_NAME_DATA_INIT { 0 } | |
| @@ -41,8 +39,6 @@ static int check_tracking_name(struct remote *remote, void *cb_data) | |||
| 41 | 39 | oidcpy(dst, cb->dst_oid); | |
| 42 | 40 | cb->default_dst_oid = dst; | |
| 43 | 41 | } | |
| 44 | - | if (cb->remote_names) | |
| 45 | - | string_list_append(cb->remote_names, remote->name); | |
| 46 | 42 | if (cb->dst_ref) { | |
| 47 | 43 | free(query.dst); | |
| 48 | 44 | return 0; | |
| @@ -52,19 +48,14 @@ static int check_tracking_name(struct remote *remote, void *cb_data) | |||
| 52 | 48 | } | |
| 53 | 49 | ||
| 54 | 50 | char *unique_tracking_name(const char *name, struct object_id *oid, | |
| 55 | - | int *dwim_remotes_matched, | |
| 56 | - | struct string_list *dwim_remote_names) | |
| 51 | + | int *dwim_remotes_matched) | |
| 57 | 52 | { | |
| 58 | 53 | struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT; | |
| 59 | 54 | const char *default_remote = NULL; | |
| 60 | - | ||
| 61 | - | if (!repo_config_get_string_tmp(the_repository, | |
| 62 | - | "checkout.defaultremote", | |
| 63 | - | &default_remote)) | |
| 55 | + | if (!repo_config_get_string_tmp(the_repository, "checkout.defaultremote", &default_remote)) | |
| 64 | 56 | cb_data.default_remote = default_remote; | |
| 65 | 57 | cb_data.src_ref = xstrfmt("refs/heads/%s", name); | |
| 66 | 58 | cb_data.dst_oid = oid; | |
| 67 | - | cb_data.remote_names = dwim_remote_names; | |
| 68 | 59 | for_each_remote(check_tracking_name, &cb_data); | |
| 69 | 60 | if (dwim_remotes_matched) | |
| 70 | 61 | *dwim_remotes_matched = cb_data.num_matches; | |
modified checkout.h
| @@ -3,8 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | 4 | #include "hash.h" | |
| 5 | 5 | ||
| 6 | - | struct string_list; | |
| 7 | - | ||
| 8 | 6 | /* | |
| 9 | 7 | * Check if the branch name uniquely matches a branch name on a remote | |
| 10 | 8 | * tracking branch. Return the name of the remote if such a branch | |
| @@ -12,7 +10,6 @@ struct string_list; | |||
| 12 | 10 | */ | |
| 13 | 11 | char *unique_tracking_name(const char *name, | |
| 14 | 12 | struct object_id *oid, | |
| 15 | - | int *dwim_remotes_matched, | |
| 16 | - | struct string_list *dwim_remote_names); | |
| 13 | + | int *dwim_remotes_matched); | |
| 17 | 14 | ||
| 18 | 15 | #endif /* CHECKOUT_H */ | |
modified dir.c
| @@ -212,9 +212,10 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen, | |||
| 212 | 212 | return match_status; | |
| 213 | 213 | } | |
| 214 | 214 | ||
| 215 | - | static size_t common_prefix_len(const struct pathspec *pathspec) | |
| 215 | + | static size_t common_prefix_len(const struct pathspec *pathspec, | |
| 216 | + | const char **matched_prefix) | |
| 216 | 217 | { | |
| 217 | - | int n; | |
| 218 | + | int n, first = -1; | |
| 218 | 219 | size_t max = 0; | |
| 219 | 220 | ||
| 220 | 221 | /* | |
| @@ -237,43 +238,47 @@ static size_t common_prefix_len(const struct pathspec *pathspec) | |||
| 237 | 238 | size_t i = 0, len = 0, item_len; | |
| 238 | 239 | if (pathspec->items[n].magic & PATHSPEC_EXCLUDE) | |
| 239 | 240 | continue; | |
| 241 | + | if (first < 0) | |
| 242 | + | first = n; | |
| 240 | 243 | if (pathspec->items[n].magic & PATHSPEC_ICASE) | |
| 241 | 244 | item_len = pathspec->items[n].prefix; | |
| 242 | 245 | else | |
| 243 | 246 | item_len = pathspec->items[n].nowildcard_len; | |
| 244 | - | while (i < item_len && (n == 0 || i < max)) { | |
| 247 | + | while (i < item_len && (n == first || i < max)) { | |
| 245 | 248 | char c = pathspec->items[n].match[i]; | |
| 246 | - | if (c != pathspec->items[0].match[i]) | |
| 249 | + | if (c != pathspec->items[first].match[i]) | |
| 247 | 250 | break; | |
| 248 | 251 | if (c == '/') | |
| 249 | 252 | len = i + 1; | |
| 250 | 253 | i++; | |
| 251 | 254 | } | |
| 252 | - | if (n == 0 || len < max) { | |
| 255 | + | if (n == first || len < max) { | |
| 253 | 256 | max = len; | |
| 254 | 257 | if (!max) | |
| 255 | 258 | break; | |
| 256 | 259 | } | |
| 257 | 260 | } | |
| 261 | + | *matched_prefix = first < 0 ? NULL : pathspec->items[first].match; | |
| 258 | 262 | return max; | |
| 259 | 263 | } | |
| 260 | 264 | ||
| 261 | 265 | /* | |
| 262 | - | * Returns a copy of the longest leading path common among all | |
| 263 | - | * pathspecs. | |
| 266 | + | * Returns a copy of the longest leading path common among all pathspec | |
| 267 | + | * items that are not excluded. | |
| 264 | 268 | */ | |
| 265 | 269 | char *common_prefix(const struct pathspec *pathspec) | |
| 266 | 270 | { | |
| 267 | - | unsigned long len = common_prefix_len(pathspec); | |
| 271 | + | const char *matched_prefix; | |
| 272 | + | size_t len = common_prefix_len(pathspec, &matched_prefix); | |
| 268 | 273 | ||
| 269 | - | return len ? xmemdupz(pathspec->items[0].match, len) : NULL; | |
| 274 | + | return len ? xmemdupz(matched_prefix, len) : NULL; | |
| 270 | 275 | } | |
| 271 | 276 | ||
| 272 | 277 | int fill_directory(struct dir_struct *dir, | |
| 273 | 278 | struct index_state *istate, | |
| 274 | 279 | const struct pathspec *pathspec) | |
| 275 | 280 | { | |
| 276 | - | const char *prefix; | |
| 281 | + | const char *matched_prefix; | |
| 277 | 282 | size_t prefix_len; | |
| 278 | 283 | ||
| 279 | 284 | unsigned exclusive_flags = DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO; | |
| @@ -284,11 +289,11 @@ int fill_directory(struct dir_struct *dir, | |||
| 284 | 289 | * Calculate common prefix for the pathspec, and | |
| 285 | 290 | * use that to optimize the directory walk | |
| 286 | 291 | */ | |
| 287 | - | prefix_len = common_prefix_len(pathspec); | |
| 288 | - | prefix = prefix_len ? pathspec->items[0].match : ""; | |
| 292 | + | prefix_len = common_prefix_len(pathspec, &matched_prefix); | |
| 289 | 293 | ||
| 290 | 294 | /* Read the directory and prune it */ | |
| 291 | - | read_directory(dir, istate, prefix, prefix_len, pathspec); | |
| 295 | + | read_directory(dir, istate, prefix_len ? matched_prefix : "", | |
| 296 | + | prefix_len, pathspec); | |
| 292 | 297 | ||
| 293 | 298 | return prefix_len; | |
| 294 | 299 | } | |
| @@ -394,7 +399,7 @@ static int match_pathspec_item(struct index_state *istate, | |||
| 394 | 399 | ||
| 395 | 400 | /* | |
| 396 | 401 | * The normal call pattern is: | |
| 397 | - | * 1. prefix = common_prefix_len(ps); | |
| 402 | + | * 1. prefix = common_prefix_len(ps, &matched_prefix); | |
| 398 | 403 | * 2. prune something, or fill_directory | |
| 399 | 404 | * 3. match_pathspec() | |
| 400 | 405 | * | |
| @@ -414,8 +419,8 @@ static int match_pathspec_item(struct index_state *istate, | |||
| 414 | 419 | * Normally the caller (common_prefix_len() in fact) does | |
| 415 | 420 | * _exact_ matching on name[-prefix+1..-1] and we do not need | |
| 416 | 421 | * to check that part. Be defensive and check it anyway, in | |
| 417 | - | * case common_prefix_len is changed, or a new caller is | |
| 418 | - | * introduced that does not use common_prefix_len. | |
| 422 | + | * case common_prefix_len() is changed, or a new caller is | |
| 423 | + | * introduced that does not use common_prefix_len(). | |
| 419 | 424 | * | |
| 420 | 425 | * If the penalty turns out too high when prefix is really | |
| 421 | 426 | * long, maybe change it to | |
| @@ -593,7 +598,7 @@ static int match_pathspec_with_flags(struct index_state *istate, | |||
| 593 | 598 | if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive) | |
| 594 | 599 | return positive; | |
| 595 | 600 | negative = do_match_pathspec(istate, ps, name, namelen, | |
| 596 | - | prefix, seen, | |
| 601 | + | 0, seen, | |
| 597 | 602 | flags | DO_MATCH_EXCLUDE); | |
| 598 | 603 | return negative ? 0 : positive; | |
| 599 | 604 | } | |
modified pathspec.c
| @@ -749,8 +749,8 @@ void clear_pathspec(struct pathspec *pathspec) | |||
| 749 | 749 | int i, j; | |
| 750 | 750 | ||
| 751 | 751 | for (i = 0; i < pathspec->nr; i++) { | |
| 752 | - | free(pathspec->items[i].match); | |
| 753 | - | free(pathspec->items[i].original); | |
| 752 | + | free((void *)pathspec->items[i].match); | |
| 753 | + | free((void *)pathspec->items[i].original); | |
| 754 | 754 | ||
| 755 | 755 | for (j = 0; j < pathspec->items[i].attr_match_nr; j++) | |
| 756 | 756 | free(pathspec->items[i].attr_match[j].value); | |
modified pathspec.h
| @@ -35,8 +35,8 @@ struct pathspec { | |||
| 35 | 35 | unsigned magic; | |
| 36 | 36 | int max_depth; | |
| 37 | 37 | struct pathspec_item { | |
| 38 | - | char *match; | |
| 39 | - | char *original; | |
| 38 | + | const char *match; | |
| 39 | + | const char *original; | |
| 40 | 40 | unsigned magic; | |
| 41 | 41 | int len, prefix; | |
| 42 | 42 | int nowildcard_len; | |
modified t/t6132-pathspec-exclude.sh
| @@ -183,6 +183,24 @@ EOF | |||
| 183 | 183 | test_cmp expect actual | |
| 184 | 184 | ' | |
| 185 | 185 | ||
| 186 | + | test_expect_success 'negative pathspec shorter than positive pathspec prefix' ' | |
| 187 | + | git ls-files -- sub/sub/ ":(exclude)sub2" >actual && | |
| 188 | + | cat <<-\EOF >expect && | |
| 189 | + | sub/sub/file | |
| 190 | + | sub/sub/sub/file | |
| 191 | + | EOF | |
| 192 | + | test_cmp expect actual | |
| 193 | + | ' | |
| 194 | + | ||
| 195 | + | test_expect_success 'exclude is matched against the full path' ' | |
| 196 | + | git ls-files -- sub/sub/ ":(exclude)zzzzzzz" >actual && | |
| 197 | + | cat <<-\EOF >expect && | |
| 198 | + | sub/sub/file | |
| 199 | + | sub/sub/sub/file | |
| 200 | + | EOF | |
| 201 | + | test_cmp expect actual | |
| 202 | + | ' | |
| 203 | + | ||
| 186 | 204 | test_expect_success 'multiple exclusions' ' | |
| 187 | 205 | git ls-files -- ":^*/file2" ":^sub2" >actual && | |
| 188 | 206 | cat <<-\EOF >expect && | |
modified t/unit-tests/u-dir.c
| @@ -45,3 +45,31 @@ void test_dir__within_depth(void) | |||
| 45 | 45 | ||
| 46 | 46 | ||
| 47 | 47 | } | |
| 48 | + | ||
| 49 | + | void test_dir__common_prefix_skips_excluded_pathspec_items(void) | |
| 50 | + | { | |
| 51 | + | struct pathspec_item items[] = { | |
| 52 | + | { | |
| 53 | + | .match = "unrelated/path", | |
| 54 | + | .magic = PATHSPEC_EXCLUDE, | |
| 55 | + | .nowildcard_len = 14, | |
| 56 | + | }, | |
| 57 | + | { | |
| 58 | + | .match = "foo/bar", | |
| 59 | + | .nowildcard_len = 7, | |
| 60 | + | }, | |
| 61 | + | { | |
| 62 | + | .match = "foo/baz", | |
| 63 | + | .nowildcard_len = 7, | |
| 64 | + | }, | |
| 65 | + | }; | |
| 66 | + | struct pathspec pathspec = { | |
| 67 | + | .nr = ARRAY_SIZE(items), | |
| 68 | + | .magic = PATHSPEC_EXCLUDE, | |
| 69 | + | .items = items, | |
| 70 | + | }; | |
| 71 | + | char *prefix = common_prefix(&pathspec); | |
| 72 | + | ||
| 73 | + | cl_assert_equal_s(prefix, "foo/"); | |
| 74 | + | free(prefix); | |
| 75 | + | } | |