[PATCH v3 5/5] setup: report when GIT_OBJECT_DIRECTORY is what we could not use
- From
Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
- Date
- Oct 28, 2026, 11:54 UTC
- Message-ID
- <20261028115412.169748-6-kaartic.sivaraam@gmail.com>
- In-Reply-To
- <20261028115412.169748-1-kaartic.sivaraam@gmail.com>
In the same spirit as the last two commits, we want to improve the UX of the Git repository discovery case when the GIT_OBJECT_DIRECTORY environment variable is set to an invalid directory. For instance, consider the following scenario:
$ echo 'export GIT_OBJECT_DIRECTORY=/tmp/git-objects' >> ~/.bashrc
# shared object store across clones
...after a reboot clears /tmp...
$ cd ~/src/myproject
$ git status
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).We get an error here because when GIT_OBJECT_DIRECTORY points at a directory that cannot be used, Git refuses to work with any repository at all, but says so in a way that blames the repository.
Improve this error reporting UX by explaining the real cause wherever we are about to die. Because the check does not depend on the candidate that is given to is_git_directory(), it is enough to ask once, at the point of failure, whether the configured object directory is to blame. With this, the user would see the following:
...
$ git status
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
cannot access object directory '/tmp/git-objects' set via
$GIT_OBJECT_DIRECTORY: No such file or directoryNote that pointing GIT_OBJECT_DIRECTORY at a directory that does not exist yet is legitimate for "git init", which goes on to create it. Diagnosing only where we would have died anyway leaves that case alone.
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com> --- setup.c | 80 +++++++++++++++++++++++++++++------ t/t0009-git-dir-validation.sh | 36 +++++++++++++++- 2 files changed, 102 insertions(+), 14 deletions(-)
diff --git a/setup.c b/setup.c index 87fcead87e..7b1e67e0dc 100644 --- a/setup.c +++ b/setup.c @@ -450,6 +450,52 @@ int is_git_directory(const char *suspect) return ret; } +/* + * If $GIT_OBJECT_DIRECTORY names a directory we cannot use, describe why in + * `out` and return 1, otherwise return 0. + * + * Note that the check is not specific to any one candidate directory: when the + * variable is set but unusable, is_git_directory() rejects every directory it + * is given, so repository discovery cannot succeed anywhere. + */ +static int describe_unusable_object_dir(struct strbuf *out) +{ + const char *objdir = getenv(DB_ENVIRONMENT); + int saved_errno; + + if (!objdir || !access(objdir, X_OK)) + return 0; + saved_errno = errno; + + /* + * TRANSLATORS: The first %s is a path, the second %s is the + * name of the environment variable that set it, and the third + * %s is the system error message. + */ + strbuf_addf(out, _("cannot access object directory '%s' set via $%s: %s"), + objdir, DB_ENVIRONMENT, strerror(saved_errno)); + return 1; +} + +/* + * Die with `message`, which reports that no repository could be used. When an + * unusable $GIT_OBJECT_DIRECTORY is what stood in the way, say so as well: on + * its own the message blames the repository we were pointed at, which may be + * perfectly fine. + * + * `message` is not released, as we are about to exit anyway. + */ +static void NORETURN die_no_repository(struct strbuf *message) +{ + struct strbuf objdir = STRBUF_INIT; + + if (describe_unusable_object_dir(&objdir)) + strbuf_addf(message, "\n%s", objdir.buf); + strbuf_release(&objdir); + + die("%s", message->buf); +} + int is_nonbare_repository_dir(struct strbuf *path) { int ret = 0; @@ -1148,17 +1194,22 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery, } if (!is_git_directory(gitdirenv)) { + struct strbuf message = STRBUF_INIT; + if (nongit_ok) { *nongit_ok = 1; goto out; } - /* - * TRANSLATORS: The first %s is a path, the second %s is the - * name of the environment variable it was taken from. - */ - die(_("not a git repository: '%s'\n" - "this path was taken from $%s (which can also be set with --git-dir or --bare)"), - gitdirenv, GIT_DIR_ENVIRONMENT); + strbuf_addf(&message, + /* + * TRANSLATORS: The first %s is a path, the second + * %s is the name of the environment variable it + * was taken from. + */ + _("not a git repository: '%s'\n" + "this path was taken from $%s (which can also be set with --git-dir or --bare)"), + gitdirenv, GIT_DIR_ENVIRONMENT); + die_no_repository(&message); } if (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok)) @@ -2021,15 +2072,20 @@ static void repo_discover(struct repo_discovery *discovery, int *nongit_ok) strbuf_addf(&message, _("$%s is set to '%s'"), CEILING_DIRECTORIES_ENVIRONMENT, ceiling); } - die("%s", message.buf); + die_no_repository(&message); } *nongit_ok = 1; break; case GIT_DIR_HIT_MOUNT_POINT: - if (!nongit_ok) - die(_("not a git repository (or any parent up to mount point %s)\n" - "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), - dir.buf); + if (!nongit_ok) { + struct strbuf message = STRBUF_INIT; + + strbuf_addf(&message, + _("not a git repository (or any parent up to mount point %s)\n" + "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."), + dir.buf); + die_no_repository(&message); + } *nongit_ok = 1; break; case GIT_DIR_INVALID_OWNERSHIP: diff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh index ab3d2593aa..94ef10edcd 100755 --- a/t/t0009-git-dir-validation.sh +++ b/t/t0009-git-dir-validation.sh @@ -107,7 +107,7 @@ test_expect_success 'setup: discovery fails when a valid git directory not found ) ' -test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY and explicit gitdir' ' +test_expect_success 'setup: unusable $GIT_OBJECT_DIRECTORY is reported with explicit gitdir' ' test_when_finished "rm -rf parent/invalid-git-object-directory-config" && mkdir -p parent/invalid-git-object-directory-config && ( @@ -115,8 +115,40 @@ test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY git init --bare real-repo && test_must_fail env GIT_OBJECT_DIRECTORY="/path/to/does-not-exist" \ git --git-dir real-repo rev-parse --is-bare-repository 2>stderr && - test_grep "not a git repository" stderr + test_grep "not a git repository" stderr && + test_grep "cannot access object directory .*does-not-exist" stderr && + test_grep "GIT_OBJECT_DIRECTORY" stderr ) ' +test_expect_success 'setup: unusable $GIT_OBJECT_DIRECTORY is reported during discovery' ' + test_when_finished "rm -rf objdir-discovery" && + git init objdir-discovery && + ( + cd objdir-discovery && + test_must_fail env GIT_OBJECT_DIRECTORY="$(pwd)/does-not-exist" \ + git rev-parse --git-dir 2>stderr && + test_grep "not a git repository" stderr && + test_grep "cannot access object directory .*does-not-exist" stderr && + test_grep "GIT_OBJECT_DIRECTORY" stderr + ) +' + +test_expect_success 'setup: usable $GIT_OBJECT_DIRECTORY is not reported' ' + test_when_finished "rm -rf objdir-usable.git objdir-usable-odb" && + git init --bare objdir-usable.git && + mkdir objdir-usable-odb && + GIT_OBJECT_DIRECTORY="$(pwd)/objdir-usable-odb" \ + git --git-dir objdir-usable.git rev-parse --is-bare-repository >actual 2>stderr && + echo true >expect && + test_cmp expect actual && + test_grep ! "not a git repository" stderr && + test_grep ! "cannot access object directory" stderr && + + test_must_fail env GIT_OBJECT_DIRECTORY="$(pwd)/objdir-usable-odb" \ + git --git-dir does-not-exist.git rev-parse --is-bare-repository 2>stderr && + test_grep "not a git repository" stderr && + test_grep ! "cannot access object directory" stderr +' + test_done
-- 2.55.0.1003.g10538fe699