From: Yoichi NAKAYAMA Date: Mon, 12 Oct 2026 19:57:06 GMT Subject: Re: [PATCH 1/2] worktree repair: refactor and reduce .git file reads Message-ID: In-Reply-To: On Mon, Oct 12, 2026 at 1:16 AM Yoichi NAKAYAMA wrote: > > On Fri, Oct 9, 2026 at 5:55 AM Junio C Hamano wrote: > > > > "Yoichi NAKAYAMA via GitGitGadget" 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 */ > > > 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