From: Junio C Hamano <gitster@pobox.com>
To: "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Yoichi Nakayama <yoichi.nakayama@gmail.com>
Subject: Re: [PATCH v2] worktree repair: detect relative path in .git file correctly
Date: Thu, 20 Aug 2026 19:09:18 -0700 [thread overview]
Message-ID: <xmqq1pbsteb5.fsf@gitster.g> (raw)
In-Reply-To: <pull.2205.v2.git.1787240760069.gitgitgadget@gmail.com> (Yoichi NAKAYAMA via GitGitGadget's message of "Thu, 20 Aug 2026 15:46:00 +0000")
"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:
> This is because we wrongly use read_gitfile_gently() which always
> returns an absolute path. To fix this, introduce read_gitfile_raw()
> that is almost same as read_gitfile_gently(), but it skips existence
> check of the referenced repository and returns the unmodified path
> read from .git file.
This is more or less what I expected to see, but two function-scope
static variables are worse than one. At least let us not
proliferate the bad pattern that makes the functions non-reentrant.
The attached patch updates read_gitfile_raw() in your patch to take
a caller-prepared strbuf to store the value read from the '.git'
file, returning the error code as an integer. Ideally in the far
future, we would probably want to convert read_gitfile_gently() to
follow a similar function signature, but let us leave it as
#leftoverbits, as it has many more existing callers and all of them
would need adjusting. On the other hand, it is easier to get the API
in read_gitfile_raw() right while it still has only two callers.
setup.c | 9 +++------
setup.h | 2 +-
2 files changed, 4 insertions(+), 7 deletions(-)
diff --git c/setup.c w/setup.c
index af7601ff67..052c7d669b 100644
--- c/setup.c
+++ w/setup.c
@@ -996,7 +996,7 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
return error_code ? NULL : realpath.buf;
}
-const char *read_gitfile_raw(const char *path, int *return_error_code)
+int read_gitfile_raw(struct strbuf *contents, const char *path)
{
const int max_file_size = 1 << 20; /* 1MB */
int error_code = 0;
@@ -1004,7 +1004,6 @@ const char *read_gitfile_raw(const char *path, int *return_error_code)
struct stat st;
int fd;
ssize_t len;
- static struct strbuf contents = STRBUF_INIT;
if (stat(path, &st)) {
if (errno == ENOENT || errno == ENOTDIR)
@@ -1047,13 +1046,11 @@ const char *read_gitfile_raw(const char *path, int *return_error_code)
error_code = READ_GITFILE_ERR_NO_PATH;
goto cleanup_return;
}
- strbuf_reset(&contents);
- strbuf_add(&contents, buf+8, len-8);
+ strbuf_add(contents, buf+8, len-8);
cleanup_return:
- *return_error_code = error_code;
free(buf);
- return error_code ? NULL : contents.buf;
+ return error_code;
}
static void apply_gitdir_and_environment(struct repository *repo, const char *path)
diff --git c/setup.h w/setup.h
index 4c2fcbbeda..7394473e95 100644
--- c/setup.h
+++ w/setup.h
@@ -40,7 +40,7 @@ int is_nonbare_repository_dir(struct strbuf *path);
#define READ_GITFILE_ERR_IS_A_DIR 10
void read_gitfile_error_die(int error_code, const char *path);
const char *read_gitfile_gently(const char *path, int *return_error_code);
-const char *read_gitfile_raw(const char *path, int *return_error_code);
+int read_gitfile_raw(struct strbuf *contents, const char *path);
#define read_gitfile(path) read_gitfile_gently((path), NULL)
const char *resolve_gitdir_gently(const char *suspect, int *return_error_code);
#define resolve_gitdir(path) resolve_gitdir_gently((path), NULL)
prev parent reply other threads:[~2026-08-21 2:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-15 13:11 [PATCH] worktree repair: detect relative path in .git file correctly Yoichi NAKAYAMA via GitGitGadget
2026-08-17 17:21 ` Junio C Hamano
2026-08-17 21:27 ` Yoichi Nakayama
2026-08-20 15:46 ` [PATCH v2] " Yoichi NAKAYAMA via GitGitGadget
2026-08-21 2:09 ` Junio C Hamano [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqq1pbsteb5.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=yoichi.nakayama@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox