git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] worktree repair: refactor and reduce .git file reads

From
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Date
Oct 12, 2026, 19:57 UTC
Message-ID
<CAF5D8-siz_USPuW=GEc3LNvSGD_8HXjVcFgr_zUNdYkeRu7Fzg@mail.gmail.com>
In-Reply-To
<CAF5D8-vFCwDjNMRwk4Vkjz9SrRgkQKFW_yYVFLiTNM_sXnMHLA@mail.gmail.com>

On Mon, Oct 12, 2026 at 1:16 AM Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> wrote:

Show 123 quoted lines
>
> On Fri, Oct 9, 2026 at 5:55 AM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > "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."

The former case (where there is no space after "gitdir:") behaved this way even before the change. repair_worktree_at_path() calls read_gitfile_gently() and returns `err = READ_GITFILE_ERR_INVALID_FORMAT`; this behavior remains unchanged.

Thanks,
-- 
Yoichi NAKAYAMA
Previous: Yoichi NAKAYAMANext: Yoichi NAKAYAMA via GitGitGadget
Message 5 of 23 in “worktree repair: avoid breaking unrelated .git file and gitdir”
  1. 0/2 worktree repair: avoid breaking unrelated .git file and gitdirYoichi NAKAYAMA via GitGitGadget, Sep 13, 2026
  2. 1/2 worktree repair: refactor and reduce .git file readsYoichi NAKAYAMA via GitGitGadget, Sep 13, 2026
  3. Junio C HamanoOct 8, 2026
  4. Yoichi NAKAYAMAOct 11, 2026
  5. Yoichi NAKAYAMAOct 12, 2026
  6. 2/2 worktree repair: avoid breaking unrelated .git file and gitdirYoichi NAKAYAMA via GitGitGadget, Sep 13, 2026
  7. Yoichi NAKAYAMAOct 8, 2026
  8. 0/5 worktree repair: avoid breaking unrelated .git file and gitdirYoichi NAKAYAMA via GitGitGadget, Oct 14, 2026
  9. 1/5 worktree repair: align .git file handling with repair_gitfile()Yoichi NAKAYAMA via GitGitGadget, Oct 14, 2026
  10. 2/5 worktree repair: centralize inferred backlink selectionYoichi NAKAYAMA via GitGitGadget, Oct 14, 2026
  11. 3/5 worktree repair: use read_gitfile_raw() in infer_backlink()Yoichi NAKAYAMA via GitGitGadget, Oct 14, 2026
  12. 4/5 worktree repair: reduce .git file readsYoichi NAKAYAMA via GitGitGadget, Oct 14, 2026
  13. 5/5 worktree repair: avoid breaking unrelated .git file and gitdirYoichi NAKAYAMA via GitGitGadget, Oct 14, 2026
  14. Junio C HamanoOct 15, 2026
  15. Yoichi NAKAYAMAOct 17, 2026
  16. Junio C HamanoOct 14, 2026
  17. Yoichi NAKAYAMAOct 15, 2026
  18. 0/5 worktree repair: avoid breaking unrelated .git file and gitdirYoichi NAKAYAMA via GitGitGadget, Oct 17, 2026
  19. 1/5 worktree repair: align .git file handling with repair_gitfile()Yoichi NAKAYAMA via GitGitGadget, Oct 17, 2026
  20. 2/5 worktree repair: centralize inferred backlink selectionYoichi NAKAYAMA via GitGitGadget, Oct 17, 2026
  21. 3/5 worktree repair: use read_gitfile_raw() in infer_backlink()Yoichi NAKAYAMA via GitGitGadget, Oct 17, 2026
  22. 4/5 worktree repair: reduce .git file readsYoichi NAKAYAMA via GitGitGadget, Oct 17, 2026
  23. 5/5 worktree repair: avoid breaking unrelated .git file and gitdirYoichi NAKAYAMA via GitGitGadget, Oct 17, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.