Re: [PATCH v2 5/5] worktree repair: avoid breaking unrelated .git file and gitdir
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 15, 2026, 18:03 UTC
- Message-ID
- <xmqq8q3yon7p.fsf@gitster.g>
- In-Reply-To
- <015602168c8532c07b94c9f50925bf5d5a2ef7c8.1792012836.git.gitgitgadget@gmail.com>
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 16 quoted lines
> diff --git a/worktree.c b/worktree.c
> index 879941a977..86094307b1 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -640,7 +640,11 @@ int other_head_refs(struct repository *repo,
> static const char *get_worktree_id(const char *dotgit_contents)
> {
> const char *slash = find_last_dir_sep(dotgit_contents);
> - if (!slash)
> + const char *prefix = "/worktrees";
> + int prefixlen = strlen(prefix);
> + if (!slash ||
> + slash - dotgit_contents < prefixlen ||
> + strncmp(slash - prefixlen, prefix, prefixlen))
> return "";
> return slash + 1;Just being curious, but slash is found by scanning dotgit_contents for either '/' or '\'. We make sure what comes before the slash is "/worktree".
Could there be situations where this code needs to accept
"...\worktrees\my-worktree"
where slash points at the backslash before "my-worktree"? Because somebody wrote the pathname with backslash before the worktree name, it would be natural to expect another backslash before the "worktrees" constant, no? For that matter, would this code need to also accept
".../worktrees//my-worktree"
where three are extra/duplicated slashes (or backslashes)? The normal code paths to create these directories and records the path to these directories should hopefully be using the canonical '/' without unnecessary duplicate slashes, but (1) existing use of find_last_dir_sep() might be a sign that we may have to deal with backslashes, and (2) since this is about reparing a broken set-up, it does not seem too far-fetched to attempt dealing with the above two sample input, hence this question.
Thanks.