Re: [PATCH 1/2] worktree repair: refactor and reduce .git file reads
- From
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
- Date
- Oct 11, 2026, 16:16 UTC
- Message-ID
- <CAF5D8-vFCwDjNMRwk4Vkjz9SrRgkQKFW_yYVFLiTNM_sXnMHLA@mail.gmail.com>
- In-Reply-To
- <xmqqh5ivyks1.fsf@gitster.g>
On Fri, Oct 9, 2026 at 5:55 AM Junio C Hamano <gitster@pobox.com> wrote:
Show 108 quoted lines
>
> "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > +static const char *get_worktree_id(const char *dotgit_contents)
> > +{
> > + const char *slash = find_last_dir_sep(dotgit_contents);
> > + if (!slash)
> > + return "";
> > + return slash + 1;
> > +}
>
> This returns a pointer into dotgit_contents; it is the last
> component of a pathname, similar to what basename(3) gives us.
>
> > @@ -798,30 +806,20 @@ static int is_main_worktree_path(struct repository *repo, const char *path)
>
> The unified diff is a bit hard to follow, so let's see if we can
> compare preimage and postimage more easily.
>
> > static ssize_t infer_backlink(struct repository *repo,
> > - const char *gitfile,
> > struct strbuf *inferred)
> > {
> > - struct strbuf actual = STRBUF_INIT;
> > const char *id;
> >
> > - if (strbuf_read_file(&actual, gitfile, 0) < 0)
> > - goto error;
> > - if (!starts_with(actual.buf, "gitdir:"))
> > - goto error;
> > - if (!(id = find_last_dir_sep(actual.buf)))
> > - goto error;
> > - strbuf_trim(&actual);
> > - id++; /* advance past '/' to point at <id> */
> > if (!*id)
> > goto error;
> > repo_common_path_replace(repo, inferred, "worktrees/%s", id);
> > if (!is_directory(inferred->buf))
> > goto error;
> >
> > - strbuf_release(&actual);
> > return inferred->len;
> > error:
> > - strbuf_release(&actual);
> > strbuf_reset(inferred); /* clear invalid path */
> > return -1;
>
> We used to receive the filename of ".git", read it and made sure we
> have "gitdir:" prefix, and find the last component, but then trimmed
> the actual buffer. Which means a few things.
>
> - If the contents of the gitfile were "gitdir:foo/bar/baz \n", our
> id pointer found the slash after "foo/bar", trimmed the buffer to
> have "gitdir:foo/bar/baz", and then incremented id, which now
> points at "baz".
>
> - If the contents of the gitfile were "gitdir: foo/bar/ \n", then
> after triming, the buffer would have "gitdir: foo/bar/" and id
> would be pointing at the NUL at the end, which would have lead us
> to error.
>
> Now let's look at the new code.
>
> > @@ -798,30 +806,20 @@ static int is_main_worktree_path(struct repository *repo, const char *path)
> > * Returns -1 on failure and strbuf.len on success.
> > */
> > static ssize_t infer_backlink(struct repository *repo,
> > + const char *dotgit_contents,
> > struct strbuf *inferred)
> > {
> > const char *id;
> >
> > + id = get_worktree_id(dotgit_contents);
> > if (!*id)
> > goto error;
> > repo_common_path_replace(repo, inferred, "worktrees/%s", id);
> > if (!is_directory(inferred->buf))
> > goto error;
> >
> > return inferred->len;
> > error:
> > strbuf_reset(inferred); /* clear invalid path */
> > return -1;
>
> The caller is expected to give us the contents of gitfile read by
> setup.c:read_gitfile_raw(), which reads the file in full, validates
> that the file begins with "gitdir: " (notice the trailing space),
> removes arbitrary run of CR or LF from the end, and then returns
> the string after skipping "gitdir: " prefix (8 bytes).
>
> In the normal case, read_gitfile_raw() would see "gitdir: foo/bar/baz\n"
> in the file and returns "foo/bar/baz" to our caller. In fishy cases
> we examined for the preimage above:
>
> - If the contents of the gitfile were "gitdir:foo/bar/baz \n", our
> caller would have received an error from read_gitfile_raw() and
> wouldn't have called us.
>
> - If the contents of the gitfile were "gitdir: foo/bar/ \n", our
> caller would have given us "foo/bar/ ".
>
> get_worktree_id() will give us "baz" in the normal case, and " "
> in the last case. We fail to error out in the latter with "*id"
> check, but is_directory() check will catch us, as the inferred
> directory is "worktrees/ " in that bad case.
>
> So there are certain differences in error cases, but they behave the
> same in the most basic cases.infer_backlink() in the preimage accepts the "gitdir:" prefix without requiring a following space and uses strbuf_trim(), which does more than just remove trailing newlines. This is more lenient than `read_gitfile_gently()`, which is called when other Git commands are used. I believe the primary purpose of `worktree repair` is to fix common issues like worktree relocation, with repairing cases where the `.git` file's contents were manually modified being a secondary concern. Therefore, I have adjusted the behavior for error cases: if the file deviates from the standard `.git` file format, it is treated as ".git file broken"; if there are trailing whitespaces, they are interpreted as part of the path and treated as ".git file does not reference a repository." I would like to split the commits and provide a detailed explanation for this.
Show 7 quoted lines
> > - } else if (err == READ_GITFILE_ERR_NOT_A_FILE ||
> > - err == READ_GITFILE_ERR_IS_A_DIR) {
> > fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
> > goto done;
>
> This is inherited badness, but overly long lines like this one needs
> to be fixed.I'll fold long lines near the changes.
Show 7 quoted lines
> Ah, outside the context of this patch, before we barf for "unable to > locate repository" when we complain backlink.buf is not naming a git > directory, there is the fallback logic, and in order to reach there, > we have "if (!is_git_directory(backlink.buf) && !inferred_backlink.len)" > there. OK, so this may be doing the same thing as the original, but > it is rather hard to follow and convince readers that this is a > no-op conversion.
The first commit contained multiple changes, making it difficult to follow. I will split the commit to distinguish between the parts where the behavior is modified and those where it remains unchanged, making it easier to verify that no changes were made to the latter.
Thanks,
-- Yoichi NAKAYAMA