Compare

d0ee845a718a447cacabecf9610d2e1da6d83330..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

Browse files
4 files changed, +38 −41
modified builtin/add.c
@@ -13,6 +13,7 @@
13 13 #include "dir.h"
14 14 #include "gettext.h"
15 15 #include "pathspec.h"
16 + #include "run-command.h"
16 17 #include "object-file.h"
17 18 #include "odb.h"
18 19 #include "odb/transaction.h"
@@ -22,9 +23,9 @@
22 23 #include "diff.h"
23 24 #include "read-cache.h"
24 25 #include "revision.h"
26 + #include "strvec.h"
25 27 #include "submodule.h"
26 28 #include "add-interactive.h"
27 - #include "apply.h"
28 29
29 30 static const char * const builtin_add_usage[] = {
30 31 N_("git add [<options>] [--] <pathspec>..."),
@@ -186,8 +187,7 @@ static int edit_patch(struct repository *repo,
186 187 const char *prefix)
187 188 {
188 189 char *file = repo_git_path(repo, "ADD_EDIT.patch");
189 - struct apply_state state;
190 - const char *apply_argv[2];
190 + struct child_process child = CHILD_PROCESS_INIT;
191 191 struct rev_info rev;
192 192 int out;
193 193 struct stat st;
@@ -217,16 +217,11 @@ static int edit_patch(struct repository *repo,
217 217 if (!st.st_size)
218 218 die(_("empty patch. aborted"));
219 219
220 - apply_argv[0] = file;
221 - apply_argv[1] = NULL;
222 - if (init_apply_state(&state, repo, NULL))
223 - die(_("could not initialize apply state"));
224 - state.cached = 1;
225 - if (check_apply_state(&state, 0))
226 - die(_("could not check apply state"));
227 - if (apply_all_patches(&state, 1, apply_argv, APPLY_OPT_RECOUNT))
220 + child.git_cmd = 1;
221 + strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
222 + NULL);
223 + if (run_command(&child))
228 224 die(_("could not apply '%s'"), file);
229 - clear_apply_state(&state);
230 225
231 226 unlink(file);
232 227 free(file);
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) &&
@@ -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) {
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 &&
modified t/t3702-add-edit.sh
@@ -124,15 +124,5 @@ test_expect_success 'add -e notices editor failure' '
124 124 test_must_fail env GIT_EDITOR=false git add -e &&
125 125 test_expect_code 1 git diff --exit-code
126 126 '
127 - test_expect_success 'add -e works from a subdirectory' '
128 - git reset --hard &&
129 - echo change >>file &&
130 - mkdir -p subdir &&
131 - (
132 - cd subdir &&
133 - GIT_EDITOR=cat git add -e ../file
134 - ) &&
135 - git diff --cached | grep -q "^+change"
136 - '
137 127
138 128 test_done