Commit

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
1 file changed, +10 −15
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;
@@ -307,8 +307,7 @@ static const char *worktree_basename(const char *path, int *olen)
307 307 while (name > path && !is_dir_sep(name[-1]))
308 308 name--;
309 309
310 - *olen = len;
311 - return name;
310 + return xmemdupz(name, path + len - name);
312 311 }
313 312
314 313 /* check that path is viable location for worktree */
@@ -462,6 +461,7 @@ static int add_worktree(const char *path, const char *refname,
462 461 struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
463 462 struct strbuf sb = STRBUF_INIT;
464 463 const char *name;
464 + char *name_to_free = NULL;
465 465 struct strvec child_env = STRVEC_INIT;
466 466 unsigned int counter = 0;
467 467 int len, ret;
@@ -489,14 +489,12 @@ static int add_worktree(const char *path, const char *refname,
489 489 if (!commit && !opts->orphan)
490 490 die(_("invalid reference: %s"), refname);
491 491
492 - name = worktree_basename(path, &len);
493 - strbuf_add(&sb, name, path + len - name);
494 - if (!sb.len)
492 + name = name_to_free = worktree_basename_dup(path);
493 + if (!*name)
495 494 die(_("invalid path '%s'"), path);
496 - sanitize_refname_component(sb.buf, &sb_name);
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 = xmemdupz(s, path + n - s);
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 = xmemdupz(s, path + n - s);
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) {