Compare

d59500321923d2f21db38edfb8490f17a1ff1b83..52e324c8d60c30b8ea3bac654f183bfb3467dfae

Merge branch 'rs/worktree-add-basename-fixes'

The string extraction logic for the branch name and worktree name
from the given path in 'git worktree add' has been corrected and
simplified to avoid out-of-bounds reads and improper handling of
trailing slashes.

* rs/worktree-add-basename-fixes:
  worktree add: let worktree_basename() return string copy
  worktree add: trim slashes when deriving branch name from path
  worktree add: reject separator-only path
  worktree add: don't read out of bounds in worktree_basename()

Junio C Hamano <gitster@pobox.com> · 2026-09-10 12:36 UTC

Browse files
6 files changed, +91 −31
modified .github/workflows/main.yml
@@ -5,18 +5,20 @@ on: [push, pull_request]
5 5 env:
6 6 DEVELOPER: 1
7 7
8 - # If more than one workflow run is triggered for the very same commit hash
9 - # (which happens when multiple branches pointing to the same commit), only
10 - # the first one is allowed to run, the second will be kept in the "queued"
11 - # state. This allows a successful completion of the first run to be reused
12 - # in the second run via the `skip-if-redundant` logic in the `config` job.
8 + # For pull requests, only the latest workflow run is allowed to proceed.
9 + # Older runs are canceled when a new revision is pushed.
13 10 #
14 - # The only caveat is that if a workflow run is triggered for the same commit
15 - # hash that another run is already being held, that latter run will be
16 - # canceled. For more details about the `concurrency` attribute, see:
11 + # For pushes, if more than one workflow run is triggered for the very same
12 + # commit hash (which happens when multiple branches point to the same commit),
13 + # only the first one is allowed to run. This allows a successful completion of
14 + # the first run to be reused in the second run via the `skip-if-redundant`
15 + # logic in the `config` job.
16 + #
17 + # For more details about the `concurrency` attribute, see:
17 18 # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency
18 19 concurrency:
19 - group: ${{ github.sha }}
20 + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
21 + cancel-in-progress: ${{ github.event_name == 'pull_request' }}
20 22
21 23 jobs:
22 24 ci-config:
modified Documentation/RelNotes/2.56.0.adoc
@@ -139,6 +139,11 @@ UI, Workflows & Features
139 139 * The command line completion (in contrib/) has been taught to handle
140 140 the experimental 'git history' command.
141 141
142 + * 'git checkout' and 'git worktree add' makes guesses based on a name
143 + of a remote-tracking branch, but does not give an error when such a
144 + remote-tracking branch cannot be uniquely identified, which has
145 + been corrected.
146 +
142 147
143 148 Performance, Internal Implementation, Development Support etc.
144 149 --------------------------------------------------------------
@@ -484,6 +489,15 @@ Performance, Internal Implementation, Development Support etc.
484 489 'struct repository', continuing the libification process and
485 490 allowing per-repository control (such as for submodules).
486 491
492 + * The reftable code has been optimized to avoid an unnecessary
493 + stat/reload of the stack when an addition already holds the
494 + list_file lock, reducing the number of newfstatat syscalls from
495 + linear to constant when writing refs.
496 +
497 + * The application of the edited patch in 'git add -e' has been
498 + refactored to use the internal apply API directly, avoiding the need
499 + to spawn a 'git apply' subprocess.
500 +
487 501
488 502 Fixes since v2.55
489 503 -----------------
@@ -748,6 +762,23 @@ Fixes since v2.55
748 762 This prevents intended textual URLs from being mangled or mistakenly
749 763 treated as metadata keys.
750 764
765 + * The instructions for deprecated commands emitted by
766 + you_still_use_that() have been reworded to clarify that the removal
767 + decision is final and to provide more assertive guidance on finding
768 + a replacement.
769 + (merge 8ace32221c jc/you-still-use-that later to maint).
770 +
771 + * The zsh completion script (in 'contrib/') has been updated to
772 + correctly locate the Git command after global options like '-C' by
773 + properly skipping them, similar to how the bash completion does.
774 +
775 + * The git worktree repair command failed to rewrite the .git file of
776 + a working tree from a relative path to an absolute path when the
777 + command was run in the working tree itself. The
778 + read_gitfile_gently() function was modified to also return whether
779 + the path originally recorded in the file was absolute, and this new
780 + capability is used to correctly detect such mismatches.
781 +
751 782 * Other code cleanup, docfix, build fix, etc.
752 783 (merge 026636128f ss/submittingpatches-typofix later to maint).
753 784 (merge d2af22cc21 jc/rerere-doc-typofix later to maint).
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;
@@ -630,6 +628,7 @@ done:
630 628 strbuf_release(&sb_git);
631 629 strbuf_release(&sb_name);
632 630 free_worktree(wt);
631 + free(name_to_free);
633 632 return ret;
634 633 }
635 634
@@ -785,10 +784,8 @@ static void advise_disambiguating_remotes(const char *path, const char *branch,
785 784
786 785 static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch)
787 786 {
788 - int n;
789 787 int branch_exists;
790 - const char *s = worktree_basename(path, &n);
791 - char *branchname = xstrndup(s, n);
788 + char *branchname = worktree_basename_dup(path);
792 789 struct strbuf ref = STRBUF_INIT;
793 790
794 791 branch_exists = !check_branch_ref(the_repository, &ref, branchname) &&
@@ -909,9 +906,7 @@ static int add(int ac, const char **av, const char *prefix,
909 906 }
910 907
911 908 if (opts.orphan && !new_branch) {
912 - int n;
913 - const char *s = worktree_basename(path, &n);
914 - new_branch = new_branch_to_free = xstrndup(s, n);
909 + new_branch = new_branch_to_free = worktree_basename_dup(path);
915 910 } else if (opts.orphan) {
916 911 ; /* no-op */
917 912 } else if (opts.detach) {
modified t/t1401-symbolic-ref.sh
@@ -38,6 +38,18 @@ test_expect_success 'symbolic-ref refuses bare sha1' '
38 38
39 39 reset_to_sane
40 40
41 + test_expect_success 'symbolic-ref reports a non-symbolic ref' '
42 + test_must_fail git symbolic-ref refs/heads/foo >out 2>err &&
43 + test_must_be_empty out &&
44 + test_grep "is not a symbolic ref" err
45 + '
46 +
47 + test_expect_success 'symbolic-ref -q is silent on a non-symbolic ref' '
48 + test_must_fail git symbolic-ref -q refs/heads/foo >out 2>err &&
49 + test_must_be_empty out &&
50 + test_must_be_empty err
51 + '
52 +
41 53 test_expect_success 'HEAD cannot be removed' '
42 54 test_must_fail git symbolic-ref -d HEAD
43 55 '
modified t/t1402-check-ref-format.sh
@@ -49,16 +49,19 @@ invalid_ref 'foo/./bar'
49 49 invalid_ref 'foo/bar/.'
50 50 invalid_ref '.refs/foo'
51 51 invalid_ref 'refs/heads/foo.'
52 - invalid_ref 'heads/foo..bar'
53 - invalid_ref 'heads/foo?bar'
52 + for c in '?' '~' '^' ':' '*' '[' ' ' '\' '..'
53 + do
54 + invalid_ref "heads/foo${c}bar"
55 + done
54 56 valid_ref 'foo./bar'
55 57 invalid_ref 'heads/foo.lock'
56 58 invalid_ref 'heads///foo.lock'
57 59 invalid_ref 'foo.lock/bar'
58 60 invalid_ref 'foo.lock///bar'
59 61 valid_ref 'heads/foo@bar'
62 + valid_ref 'refs/@'
63 + invalid_ref '@' --allow-onelevel
60 64 invalid_ref 'heads/v@{ation'
61 - invalid_ref 'heads/foo\bar'
62 65 invalid_ref "$(printf 'heads/foo\t')"
63 66 invalid_ref "$(printf 'heads/foo\177')"
64 67 valid_ref "$(printf 'heads/fu\303\237')"
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 &&