Compare
05cf4ceb5bc28580ef30cb45c8bbd8b447431cf4..2e8a9d94b009c69628e29bc7c50d9a3fc12e14ba
worktree add: let worktree_basename() return string copy
worktree_basename() requires callers to do pointer arithmetic to get the actual basename. Simplify them by doing the calculations in the function and returning a copy of the basename directly. Remind programmers to free the result by renaming the function to worktree_basename_dup(). Among the three callers of the original function, two immediately make copies of the returned string before using and freeing it, which makes for an easy conversion. Convert the other one from resetting a shared strbuf to freeing the allocated string, which requires the same number of lines, but no arithmetic. The added allocation is negligible because it's small and there's only one per run of "git worktree add". Signed-off-by: René Scharfe <l.s.r@web.de> [jc: rephrased the second paragraph a bit.] Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe <l.s.r@web.de> · 2026-08-25 18:03 UTC · committed by Junio C Hamano
5 files changed,
+66
−82
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
| @@ -294,7 +294,7 @@ static void remove_junk_on_signal(int signo) | |||
| 294 | 294 | raise(signo); | |
| 295 | 295 | } | |
| 296 | 296 | ||
| 297 | - | static const char *worktree_basename(const char *path, int *olen) | |
| 297 | + | static char *worktree_basename_dup(const char *path) | |
| 298 | 298 | { | |
| 299 | 299 | const char *name; | |
| 300 | 300 | int len; | |
| @@ -303,14 +303,11 @@ static const char *worktree_basename(const char *path, int *olen) | |||
| 303 | 303 | while (len && is_dir_sep(path[len - 1])) | |
| 304 | 304 | len--; | |
| 305 | 305 | ||
| 306 | - | for (name = path + len - 1; name > path; name--) | |
| 307 | - | if (is_dir_sep(*name)) { | |
| 308 | - | name++; | |
| 309 | - | break; | |
| 310 | - | } | |
| 306 | + | name = path + len; | |
| 307 | + | while (name > path && !is_dir_sep(name[-1])) | |
| 308 | + | name--; | |
| 311 | 309 | ||
| 312 | - | *olen = len; | |
| 313 | - | return name; | |
| 310 | + | return xmemdupz(name, path + len - name); | |
| 314 | 311 | } | |
| 315 | 312 | ||
| 316 | 313 | /* check that path is viable location for worktree */ | |
| @@ -464,6 +461,7 @@ static int add_worktree(const char *path, const char *refname, | |||
| 464 | 461 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; | |
| 465 | 462 | struct strbuf sb = STRBUF_INIT; | |
| 466 | 463 | const char *name; | |
| 464 | + | char *name_to_free = NULL; | |
| 467 | 465 | struct strvec child_env = STRVEC_INIT; | |
| 468 | 466 | unsigned int counter = 0; | |
| 469 | 467 | int len, ret; | |
| @@ -491,12 +489,12 @@ static int add_worktree(const char *path, const char *refname, | |||
| 491 | 489 | if (!commit && !opts->orphan) | |
| 492 | 490 | die(_("invalid reference: %s"), refname); | |
| 493 | 491 | ||
| 494 | - | name = worktree_basename(path, &len); | |
| 495 | - | strbuf_add(&sb, name, path + len - name); | |
| 496 | - | sanitize_refname_component(sb.buf, &sb_name); | |
| 492 | + | name = name_to_free = worktree_basename_dup(path); | |
| 493 | + | if (!*name) | |
| 494 | + | die(_("invalid path '%s'"), path); | |
| 495 | + | sanitize_refname_component(name, &sb_name); | |
| 497 | 496 | if (!sb_name.len) | |
| 498 | - | BUG("How come '%s' becomes empty after sanitization?", sb.buf); | |
| 499 | - | strbuf_reset(&sb); | |
| 497 | + | BUG("How come '%s' becomes empty after sanitization?", name); | |
| 500 | 498 | name = sb_name.buf; | |
| 501 | 499 | repo_git_path_replace(the_repository, &sb_repo, "worktrees/%s", name); | |
| 502 | 500 | len = sb_repo.len; | |
| @@ -629,6 +627,7 @@ done: | |||
| 629 | 627 | strbuf_release(&sb_git); | |
| 630 | 628 | strbuf_release(&sb_name); | |
| 631 | 629 | free_worktree(wt); | |
| 630 | + | free(name_to_free); | |
| 632 | 631 | return ret; | |
| 633 | 632 | } | |
| 634 | 633 | ||
| @@ -765,10 +764,8 @@ static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote) | |||
| 765 | 764 | ||
| 766 | 765 | static char *dwim_branch(const char *path, char **new_branch) | |
| 767 | 766 | { | |
| 768 | - | int n; | |
| 769 | 767 | int branch_exists; | |
| 770 | - | const char *s = worktree_basename(path, &n); | |
| 771 | - | char *branchname = xstrndup(s, n); | |
| 768 | + | char *branchname = worktree_basename_dup(path); | |
| 772 | 769 | struct strbuf ref = STRBUF_INIT; | |
| 773 | 770 | ||
| 774 | 771 | branch_exists = !check_branch_ref(&ref, branchname) && | |
| @@ -781,7 +778,7 @@ static char *dwim_branch(const char *path, char **new_branch) | |||
| 781 | 778 | *new_branch = branchname; | |
| 782 | 779 | if (guess_remote) { | |
| 783 | 780 | struct object_id oid; | |
| 784 | - | char *remote = unique_tracking_name(*new_branch, &oid, NULL, NULL); | |
| 781 | + | char *remote = unique_tracking_name(*new_branch, &oid, NULL); | |
| 785 | 782 | return remote; | |
| 786 | 783 | } | |
| 787 | 784 | return NULL; | |
| @@ -875,9 +872,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 875 | 872 | } | |
| 876 | 873 | ||
| 877 | 874 | if (opts.orphan && !new_branch) { | |
| 878 | - | int n; | |
| 879 | - | const char *s = worktree_basename(path, &n); | |
| 880 | - | new_branch = new_branch_to_free = xstrndup(s, n); | |
| 875 | + | new_branch = new_branch_to_free = worktree_basename_dup(path); | |
| 881 | 876 | } else if (opts.orphan) { | |
| 882 | 877 | ; /* no-op */ | |
| 883 | 878 | } else if (opts.detach) { | |
| @@ -903,7 +898,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 903 | 898 | ||
| 904 | 899 | commit = lookup_commit_reference_by_name(branch); | |
| 905 | 900 | if (!commit) { | |
| 906 | - | remote = unique_tracking_name(branch, &oid, NULL, NULL); | |
| 901 | + | remote = unique_tracking_name(branch, &oid, NULL); | |
| 907 | 902 | if (remote) { | |
| 908 | 903 | new_branch = branch; | |
| 909 | 904 | 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 t/t2400-worktree-add.sh
| @@ -46,6 +46,10 @@ test_expect_success '"add" refuses to checkout locked branch' ' | |||
| 46 | 46 | test_path_is_missing .git/worktrees/zere | |
| 47 | 47 | ' | |
| 48 | 48 | ||
| 49 | + | test_expect_success '"add" rejects an empty path' ' | |
| 50 | + | test_must_fail git worktree add "" HEAD | |
| 51 | + | ' | |
| 52 | + | ||
| 49 | 53 | test_expect_success 'checking out paths not complaining about linked checkouts' ' | |
| 50 | 54 | ( | |
| 51 | 55 | cd existing_empty && | |
| @@ -294,6 +298,11 @@ test_expect_success '"add" with <branch> omitted' ' | |||
| 294 | 298 | test_cmp_rev HEAD bat | |
| 295 | 299 | ' | |
| 296 | 300 | ||
| 301 | + | test_expect_success '"add" with trailing slash and <branch> omitted' ' | |
| 302 | + | git worktree add waffle/bit/ && | |
| 303 | + | test_cmp_rev HEAD bit | |
| 304 | + | ' | |
| 305 | + | ||
| 297 | 306 | test_expect_success '"add" checks out existing branch of dwimd name' ' | |
| 298 | 307 | git branch dwim HEAD~1 && | |
| 299 | 308 | git worktree add dwim && | |
| @@ -384,6 +393,14 @@ test_expect_success '"add --orphan (no -b)"' ' | |||
| 384 | 393 | test_cmp expected actual | |
| 385 | 394 | ' | |
| 386 | 395 | ||
| 396 | + | test_expect_success '"add --orphan with trailing slash (no -b)"' ' | |
| 397 | + | test_when_finished "git worktree remove -f -f neworphan" && | |
| 398 | + | git worktree add --orphan ./neworphan/ && | |
| 399 | + | echo refs/heads/neworphan >expected && | |
| 400 | + | git -C neworphan symbolic-ref HEAD >actual && | |
| 401 | + | test_cmp expected actual | |
| 402 | + | ' | |
| 403 | + | ||
| 387 | 404 | test_expect_success '"add --orphan --quiet"' ' | |
| 388 | 405 | test_when_finished "git worktree remove -f -f orphandir" && | |
| 389 | 406 | git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual && | |