Thread View: linux.kernel.git
9 messages
9 total messages
Started by Kaartic Sivaraam
Wed, 28 Oct 2026 11:53
[PATCH v3 0/5] Improve error reporting to mention how a Git repository is discovered
Author: Kaartic Sivaraam
Date: Wed, 28 Oct 2026 11:53
Date: Wed, 28 Oct 2026 11:53
207 lines
20331 bytes
20331 bytes
At the moment, when Git gives up with "fatal: not a git repository",
it does not say which directory it actually inspected or where that
directory came from. This is especially confusing when a leftover
environment variable is steering Git somewhere the user did not
expect.
In v2 I tried to address this by explaining why the inspected
directory was not a valid repository (missing HEAD, HEAD outside
refs/, and so on). Junio pointed out that this is less useful than
saying which directory was used as the repository and where that
came from, since once the user knows that they can go look at the
directory themselves. He also pointed out that v2 was uneven, as it
named $GIT_OBJECT_DIRECTORY but did not say where the git directory
itself came from.
So this iteration takes that direction instead. The validity checks
in is_git_directory() and validate_headref() are left untouched, and
we only add information at the points where we are about to die.
With this series:
- An explicitly given git directory says where it came from:
$ cd /srv/git
$ export GIT_DIR=project.git
...
$ cd ~/src/myproject # later, same terminal
$ git status
fatal: not a git repository: 'project.git'
this path was taken from $GIT_DIR (which can also be set with --git-dir or --bare)
--git-dir and --bare both just set GIT_DIR, so by the time setup
runs we cannot tell them apart. I went with a single wording that
is true for all three rather than plumbing that information
through. Happy to revisit if it is felt to be worth it.
- The discovery walk says where it started, where it stopped and
what GIT_CEILING_DIRECTORIES is set to:
$ echo 'export GIT_CEILING_DIRECTORIES=$HOME' >> ~/.bashrc
...weeks later...
$ cd ~/.config/nvim
$ git status
fatal: not a git repository (or any of the parent directories): .git
searched from '/home/user/.config/nvim' up to '/home/user/.config'
$GIT_CEILING_DIRECTORIES is set to '/home/user'
- An unusable $GIT_OBJECT_DIRECTORY, which was the original
motivation for this series, is reported wherever we die for want
of a repository (explicit git directory, ceiling and mount point):
$ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository
fatal: not a git repository: 'repo.git'
this path was taken from $GIT_DIR (which can also be set with --git-dir or --bare)
cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY: No such file or directory
Commands that are fine running outside a repository stay silent and
do not build these messages at all.
This series does not handle the following, which I think are better
done separately:
- The gitfile case (for instance from inside a submodule or a
worktree). A gitfile pointing at an invalid repository dies
earlier in read_gitfile() with its own message.
- GIT_ALTERNATE_OBJECT_DIRECTORIES. It never leads to "not a git
repository". Its diagnostics are misleading in their own way (one
of them points at .git/objects/info/alternates even when the entry
came from the environment), but they come from the object database
rather than from setup.
- The "$GIT_CEILING_DIRECTORIES is set to" line is printed whenever
the variable is set, even if none of its entries is above the
current directory and the walk went all the way to '/'. The
"searched from ... up to '/'" line makes that visible, and getting
it exact would mean passing more state out of the discovery walk.
CI:
They seem to pass.
- GitLab: https://gitlab.com/gitlab-org/git/-/merge_requests/672
- GitHub: https://github.com/sivaraam/git/actions/runs/40676055975
Changes since v2:
- Dropped is_git_directory_verbose() and the HEAD-related reasons, as
per Junio's review. is_git_directory() and validate_headref() are
now unchanged.
- New: say where an explicitly given git directory came from.
- New: say where the discovery walk started and stopped, and what
GIT_CEILING_DIRECTORIES is set to.
- Reworked the GIT_OBJECT_DIRECTORY reporting to ask once at the
point of failure, and extended it to the discovery walk (ceiling
and mount point).
- Dropped the test for HEAD being a symlink outside refs/, as the
series no longer cares about it. This test was failing on Windows
in v2.
- Reworked the t0009 tests to check the new messages, including that
a command which does not need a repository stays silent.
- Kept each message as a single translatable string and added
TRANSLATORS comments describing the placeholders.
Range-diff versus v2:
(This may not be very helpful because I threw out 3/4 and 4/4 from v2 and have 3 new
patches in place of them.)
1: 0b062456eb = 1: b37136baa4 setup: normalize an if-else to follow our convention
2: 7e8dab0078 ! 2: 08f3584c3a t0009: add tests to cover more error reporting scenarios
@@ Metadata
Author: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
## Commit message ##
- t0009: add tests to cover more error reporting scenarios
+ t0009: add tests to cover more repository setup scenarios
- Introduce few more tests to t0009 to cover error reporting scenarios
- when --git-dir is used.
+ Introduce a few more tests to t0009 to cover repository setup
+ scenarios. We introduce these as we intend to do some change
+ in upcoming commits which would benefit from this additional
+ test coverage.
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
@@ t/t0009-git-dir-validation.sh: test_expect_success 'setup: .git as an empty dire
)
'
-+test_expect_success 'setup: custom git directory with missing HEAD is rejected' '
++test_expect_success 'setup: --git-dir pointing to an invalid directory is rejected' '
+ test_when_finished "rm -rf parent/empty-dir" &&
+ mkdir -p parent/empty-dir &&
+ test_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&
+ test_grep "not a git repository" stderr
+'
+
-+test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '
-+ test_when_finished "rm -rf parent/head-as-link-to-garbage" &&
-+ mkdir -p parent/head-as-link-to-garbage &&
++test_expect_success 'setup: discovery fails when GIT_DIR points to an invalid directory' '
++ test_must_fail env GIT_DIR="/path/to/does-not-exist" \
++ git rev-parse --is-bare-repository 2>stderr &&
++ test_grep "not a git repository" stderr
++'
++
++test_expect_success 'setup: command that does not need a repository stays silent for invalid GIT_DIR' '
++ echo x | GIT_DIR="/path/to/does-not-exist" git hash-object --stdin 2>stderr &&
++ test_must_be_empty stderr
++'
++
++test_expect_success 'setup: discovery fails when a valid git directory not found within GIT_CEILING_DIRECTORIES' '
++ test_when_finished "rm -rf parent/no-valid-git-directory-within-ceiling" &&
++ mkdir -p parent/no-valid-git-directory-within-ceiling/ceil/sub &&
+ (
-+ cd parent/head-as-link-to-garbage &&
-+ git init --bare real-repo &&
-+ touch garbage &&
-+ rm real-repo/HEAD &&
-+ ln -s ../garbage real-repo/HEAD &&
-+ test_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&
-+ test_grep "not a git repository" stderr
++ cd parent/no-valid-git-directory-within-ceiling/ceil/sub &&
++ test_must_fail env GIT_CEILING_DIRECTORIES="$(pwd)/../../" \
++ git rev-parse --is-bare-repository 2>stderr &&
++ test_grep "not a git repository (or any of the parent directories)" stderr
+ )
+'
+
-+test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTORY configuration is rejected' '
++test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY and explicit gitdir' '
+ test_when_finished "rm -rf parent/invalid-git-object-directory-config" &&
+ mkdir -p parent/invalid-git-object-directory-config &&
+ (
+ cd parent/invalid-git-object-directory-config &&
+ git init --bare real-repo &&
-+ test_must_fail env GIT_OBJECT_DIRECTORY="$(pwd)/does-not-exist" \
++ 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_done
3: 312b861c6c < -: ---------- setup: introduce new helper 'is_git_directory_verbose'
4: 29191f463e < -: ---------- setup: communicate why a directory is not a valid git directory
-: ---------- > 3: 34a110da6d setup: provide hint about the source of the explicit git directory
-: ---------- > 4: 9986739fec setup: provide hint about auto-discovery boundaries
-: ---------- > 5: 7d13445134 setup: report when GIT_OBJECT_DIRECTORY is what we could not use
Kaartic Sivaraam (5):
setup: normalize an if-else to follow our convention
t0009: add tests to cover more repository discovery scenarios
setup: provide hint about the source of the explicit git directory
setup: provide hint about auto-discovery boundaries
setup: report when GIT_OBJECT_DIRECTORY is what we could not use
setup.c | 104 ++++++++++++++++++++++++++++++----
t/t0009-git-dir-validation.sh | 77 +++++++++++++++++++++++++
2 files changed, 171 insertions(+), 10 deletions(-)
--
2.55.0.1003.g10538fe699
[PATCH v3 1/5] setup: normalize an if-else to follow our convention
Author: Kaartic Sivaraam
Date: Wed, 28 Oct 2026 11:53
Date: Wed, 28 Oct 2026 11:53
28 lines
11414 bytes
11414 bytes
The else case was not stuck with the corresponding if's
closing brace which is not in-line with our convention.
Fix the same.
This is a style-only change; no behaviour change intended.
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
setup.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/setup.c b/setup.c
index 61bcdafe82..4789b2d79d 100644
--- a/setup.c
+++ b/setup.c
@@ -1905,8 +1905,7 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
if (chdir(used_path.buf))
return NULL;
path = validated_path.buf;
- }
- else {
+ } else {
const char *gitfile = read_gitfile(path);
if (!(flags & ENTER_REPO_ANY_OWNER_OK))
die_upon_dubious_ownership(gitfile, NULL, path);
--
2.55.0.1003.g10538fe699
[PATCH v3 2/5] t0009: add tests to cover more repository discovery scenarios
Author: Kaartic Sivaraam
Date: Wed, 28 Oct 2026 11:54
Date: Wed, 28 Oct 2026 11:54
63 lines
13188 bytes
13188 bytes
Introduce a few more tests to t0009 to cover repository discovery scenarios. We introduce these as we intend to do some change in upcoming commits which would benefit from this additional test coverage. Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com> --- t/t0009-git-dir-validation.sh | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh index 4cba478e50..bc607e6c2a 100755 --- a/t/t0009-git-dir-validation.sh +++ b/t/t0009-git-dir-validation.sh @@ -74,4 +74,45 @@ test_expect_success 'setup: .git as an empty directory is ignored' ' ) ' +test_expect_success 'setup: --git-dir pointing to an invalid directory is rejected' ' + test_when_finished "rm -rf parent/empty-dir" && + mkdir -p parent/empty-dir && + test_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr && + test_grep "not a git repository" stderr +' + +test_expect_success 'setup: discovery fails when GIT_DIR points to an invalid directory' ' + test_must_fail env GIT_DIR="/path/to/does-not-exist" \ + git rev-parse --is-bare-repository 2>stderr && + test_grep "not a git repository" stderr +' + +test_expect_success 'setup: command that does not need a repository stays silent for invalid GIT_DIR' ' + echo x | GIT_DIR="/path/to/does-not-exist" git hash-object --stdin 2>stderr && + test_must_be_empty stderr +' + +test_expect_success 'setup: discovery fails when a valid git directory not found within GIT_CEILING_DIRECTORIES' ' + test_when_finished "rm -rf parent/no-valid-git-directory-within-ceiling" && + mkdir -p parent/no-valid-git-directory-within-ceiling/ceil/sub && + ( + cd parent/no-valid-git-directory-within-ceiling/ceil/sub && + test_must_fail env GIT_CEILING_DIRECTORIES="$(pwd)/../../" \ + git rev-parse --is-bare-repository 2>stderr && + test_grep "not a git repository (or any of the parent directories)" stderr + ) +' + +test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY and explicit gitdir' ' + test_when_finished "rm -rf parent/invalid-git-object-directory-config" && + mkdir -p parent/invalid-git-object-directory-config && + ( + cd parent/invalid-git-object-directory-config && + 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_done -- 2.55.0.1003.g10538fe699
[PATCH v3 3/5] setup: provide hint about the source of the explicit git directory
Author: Kaartic Sivaraam
Date: Wed, 28 Oct 2026 11:54
Date: Wed, 28 Oct 2026 11:54
77 lines
13745 bytes
13745 bytes
Git directories can be specified explicitly by setting the GIT_DIR
environment variable or by passing --git-dir / --bare before a
sub-command. When discovery fails in these cases, the error message
that we print is a bit unhelpful. This is especially more pronounced
when someone sets GIT_DIR and forgets about it.
$ cd /srv/git
$ export GIT_DIR=project.git # saves typing --git-dir on every command
$ git log --oneline -3
$ git for-each-ref --sort=-committerdate refs/heads | head
$ cd ~/src/myproject # later, same terminal
$ git status
fatal: not a git repository: 'project.git'
The blunt message does not help the user understand where 'project.git'
came from. They are on their own in figuring this out.
Improve this error reporting UX by giving an additional hint to the
user that the path comes via GIT_DIR. With this, the user would see
the more helpful hint:
...
$ cd ~/src/myproject # later, same terminal
$ git status
fatal: not a git repository: 'project.git'
this path was taken from $GIT_DIR (which can also be set with --git-dir or --bare)
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
setup.c | 8 +++++++-
t/t0009-git-dir-validation.sh | 6 ++++--
2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/setup.c b/setup.c
index 4789b2d79d..8f6b609240 100644
--- a/setup.c
+++ b/setup.c
@@ -1152,7 +1152,13 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,
*nongit_ok = 1;
goto out;
}
- die(_("not a git repository: '%s'"), gitdirenv);
+ /*
+ * 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);
}
if (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok))
diff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh
index bc607e6c2a..fed1b045bb 100755
--- a/t/t0009-git-dir-validation.sh
+++ b/t/t0009-git-dir-validation.sh
@@ -78,13 +78,15 @@ test_expect_success 'setup: --git-dir pointing to an invalid directory is reject
test_when_finished "rm -rf parent/empty-dir" &&
mkdir -p parent/empty-dir &&
test_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&
- test_grep "not a git repository" stderr
+ test_grep "not a git repository" stderr &&
+ test_grep "taken from \$GIT_DIR" stderr
'
test_expect_success 'setup: discovery fails when GIT_DIR points to an invalid directory' '
test_must_fail env GIT_DIR="/path/to/does-not-exist" \
git rev-parse --is-bare-repository 2>stderr &&
- test_grep "not a git repository" stderr
+ test_grep "not a git repository" stderr &&
+ test_grep "taken from \$GIT_DIR" stderr
'
test_expect_success 'setup: command that does not need a repository stays silent for invalid GIT_DIR' '
--
2.55.0.1003.g10538fe699
[PATCH v3 4/5] setup: provide hint about auto-discovery boundaries
Author: Kaartic Sivaraam
Date: Wed, 28 Oct 2026 11:54
Date: Wed, 28 Oct 2026 11:54
96 lines
14485 bytes
14485 bytes
In the same spirit as the previous commit, we want to improve the UX
of the Git repository discovery case when the GIT_CEILING_DIRECTORIES
environment varaible is set. For instance, consider the following
scenario:
$ cd ~ && git init # dotfiles repo at ~/.git
$ echo 'export GIT_CEILING_DIRECTORIES=$HOME' >> ~/.bashrc
# stop ~/.git swallowing every
# non-repo directory under ~
...weeks later...
$ cd ~/.config/nvim
$ vim init.lua
$ git status
fatal: not a git repository (or any of the parent directories): .git
The error message is a bit unhelpful as the gut reaction of the user
would be to wonder why their ~/.git directory was not discovered even
though the message says it checked the parent directories. We can't
really blame the user for not remembering that they had
GIT_CEILING_DIRECTORIES configured in their ~/.bashrc.
Improve this error reporting UX by giving an additional hint about the
ceiling that auto-discovery was bounded by and also mention what
GIT_CEILING_DIRECTORIES was configured to. With this, the user would
see the following:
...
$ git status
fatal: not a git repository (or any of the parent directories): .git
searched from '/home/user/.config/nvim' up to '/home/user/.config'
$GIT_CEILING_DIRECTORIES is set to '/home/user'
Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
---
setup.c | 29 ++++++++++++++++++++++++++---
t/t0009-git-dir-validation.sh | 4 +++-
2 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/setup.c b/setup.c
index 8f6b609240..87fcead87e 100644
--- a/setup.c
+++ b/setup.c
@@ -1997,9 +1997,32 @@ static void repo_discover(struct repo_discovery *discovery, int *nongit_ok)
repo_discover_bare_gitdir(discovery, &cwd, dir.len, nongit_ok);
break;
case GIT_DIR_HIT_CEILING:
- if (!nongit_ok)
- die(_("not a git repository (or any of the parent directories): %s"),
- DEFAULT_GIT_DIR_ENVIRONMENT);
+ if (!nongit_ok) {
+ struct strbuf message = STRBUF_INIT;
+ const char *ceiling = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
+
+ strbuf_addf(&message,
+ /*
+ * TRANSLATORS: The first %s is ".git", the
+ * second and third %s are the directories
+ * where the search for a repository started
+ * and where it stopped.
+ */
+ _("not a git repository (or any of the parent directories): %s\n"
+ "searched from '%s' up to '%s'"),
+ DEFAULT_GIT_DIR_ENVIRONMENT, cwd.buf, dir.buf);
+ if (ceiling && *ceiling) {
+ strbuf_addch(&message, '\n');
+ /*
+ * TRANSLATORS: The first %s is the name of
+ * the environment variable, the second %s is
+ * its value.
+ */
+ strbuf_addf(&message, _("$%s is set to '%s'"),
+ CEILING_DIRECTORIES_ENVIRONMENT, ceiling);
+ }
+ die("%s", message.buf);
+ }
*nongit_ok = 1;
break;
case GIT_DIR_HIT_MOUNT_POINT:
diff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh
index fed1b045bb..ab3d2593aa 100755
--- a/t/t0009-git-dir-validation.sh
+++ b/t/t0009-git-dir-validation.sh
@@ -101,7 +101,9 @@ test_expect_success 'setup: discovery fails when a valid git directory not found
cd parent/no-valid-git-directory-within-ceiling/ceil/sub &&
test_must_fail env GIT_CEILING_DIRECTORIES="$(pwd)/../../" \
git rev-parse --is-bare-repository 2>stderr &&
- test_grep "not a git repository (or any of the parent directories)" stderr
+ test_grep "not a git repository (or any of the parent directories)" stderr &&
+ test_grep "searched from .*ceil/sub.*up to.*ceil." stderr &&
+ test_grep "GIT_CEILING_DIRECTORIES is set to" stderr
)
'
--
2.55.0.1003.g10538fe699
[PATCH v3 5/5] setup: report when GIT_OBJECT_DIRECTORY is what we could not use
Author: Kaartic Sivaraam
Date: Wed, 28 Oct 2026 11:54
Date: Wed, 28 Oct 2026 11:54
211 lines
18760 bytes
18760 bytes
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 directory
Note 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
Re: [PATCH v3 4/5] setup: provide hint about auto-discovery boundaries
Author: Junio C Hamano
Date: Wed, 28 Oct 2026 18:57
Date: Wed, 28 Oct 2026 18:57
60 lines
15229 bytes
15229 bytes
Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
> In the same spirit as the previous commit, we want to improve the UX
> of the Git repository discovery case when the GIT_CEILING_DIRECTORIES
> environment varaible is set. For instance, consider the following
> scenario:
>
> $ cd ~ && git init # dotfiles repo at ~/.git
> $ echo 'export GIT_CEILING_DIRECTORIES=$HOME' >> ~/.bashrc
> # stop ~/.git swallowing every
> # non-repo directory under ~
> ...weeks later...
> $ cd ~/.config/nvim
> $ vim init.lua
> $ git status
> fatal: not a git repository (or any of the parent directories): .git
>
> The error message is a bit unhelpful as the gut reaction of the user
> would be to wonder why their ~/.git directory was not discovered even
> though the message says it checked the parent directories. We can't
> really blame the user for not remembering that they had
> GIT_CEILING_DIRECTORIES configured in their ~/.bashrc.
>
> Improve this error reporting UX by giving an additional hint about the
> ceiling that auto-discovery was bounded by and also mention what
> GIT_CEILING_DIRECTORIES was configured to. With this, the user would
> see the following:
>
> ...
> $ git status
> fatal: not a git repository (or any of the parent directories): .git
> searched from '/home/user/.config/nvim' up to '/home/user/.config'
> $GIT_CEILING_DIRECTORIES is set to '/home/user'
The second line of the output gives a much better information than
before. Stepping back a bit, the "(or any of the ...)" was a weak
and ineffective attempt to say that ".git" we claim is not a valid
repository is not necessarily in the current directory. But would
it be still needed with the more explicit "we looked for it from
here to there"? Can we combine both into a single statement somehow
to make it more concise and understandable?
Here is my attempt. Would this
fatal: no valid repository ".git" found in "/home/user/.config/nvim"
or any of its parent directories up to "/home/user/.config".
or something like that be easier to read?
Of course, such a change would break scripts that attempt to parse
this error message, so even if it is easier for humans, there could
be downside.
> - test_grep "not a git repository (or any of the parent directories)" stderr
> + test_grep "not a git repository (or any of the parent directories)" stderr &&
> + test_grep "searched from .*ceil/sub.*up to.*ceil." stderr &&
> + test_grep "GIT_CEILING_DIRECTORIES is set to" stderr
> )
> '
Re: [PATCH v3 5/5] setup: report when GIT_OBJECT_DIRECTORY is what we could not use
Author: Junio C Hamano
Date: Wed, 28 Oct 2026 19:27
Date: Wed, 28 Oct 2026 19:27
67 lines
15239 bytes
15239 bytes
Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:
> 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 directory
Hmph, this is only a half of the real story isn't it?
$ git status ;# to show I am in a healthy repository
on branch master
nothing to commit (use -u to show untracked files)
In such a healthy repository, if I have a bogus DB_ENVIRONMENT,
discovery does not have to fail.
$ GIT_OBJECT_DIRECTORY=/ git status
fatal: bad object HEAD
On the other hand, if I am truly in a bogus non-repository,
it does not matter where DB_ENVIRONMENT points at.
$ (cd / && GIT_OBJECT_DIRECTORY=. git status)
fatal: not a git repository (or any of the parent directories): .git
Even when DB_ENVIRONMENT points at a valid object directory
$ (cd / && GIT_OBJECT_DIRECTORY=$HOME/w/git.git/.git/objects git status)
fatal: not a git repository (or any of the parent directories): .git
the command fails because it cannot find a valid repository.
> +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;
> +}
As this is called ONLY when we cannot figure out a usable GIT_DIR,
we do not have to worry about a case where $GIT_OBJECT_DIRECTORY is
not set and $GIT_DIR/objects/ is not readable. So I do not think
this additional message hurts much, but I am not sure how much it
would help. It would make the message more confusing in the "no
valid repository, DB_ENVIRONMENT pointing at a healthy object
directory" situation in the above example.
Re: [PATCH v3 0/5] Improve error reporting to mention how a Git repository is discovered
Author: Junio C Hamano
Date: Wed, 28 Oct 2026 19:28
Date: Wed, 28 Oct 2026 19:28
12 lines
12976 bytes
12976 bytes
Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes: > At the moment, when Git gives up with "fatal: not a git repository", > it does not say which directory it actually inspected or where that > directory came from. This is especially confusing when a leftover > environment variable is steering Git somewhere the user did not > expect. I think [1/5]..[4/5] make sense but I am not sure about [5/5]. Thanks.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads