{"thread":{"id":"66383","subject":"[RFC PATCH 0/3] Improve error reporting to mention \"why\" a directory is not a repository","startedAt":"2026-09-24T12:05:13Z","lastAt":"2026-10-28T19:28:38Z","messageCount":30,"participants":["Kaartic Sivaraam","Junio C Hamano","Patrick Steinhardt"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"553170","messageId":"20260924120502.2642141-1-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":null,"subject":"[RFC PATCH 0/3] Improve error reporting to mention \"why\" a directory is not a repository","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-24T12:02:18Z","receivedAt":"2026-09-24T12:05:13Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"At the moment, there are a few scenarios where the error message for an\ninvalid Git repository is a bit blunt. For instance, when we point\nGIT_OBJECT_DIRECTORY at a directory that does not exist, we get this:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n\nEven though repo.git itself is a valid repository, we get this rather\npuzzling error saying that it is not. The actual problem is the invalid\nvalue given to GIT_OBJECT_DIRECTORY, and the user is left on their\nto figure that out. This series aims to make such issues easier to\ndiagnose by saying why the specified repository was not considered\nvalid.\n\nWith this series, the same command outputs:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n  reason: cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY\n\nThe following scenarios are covered when --git-dir is given explicitly:\n\n  - HEAD is missing, or its path is not traversable\n  - HEAD is a symlink that cannot be read\n  - HEAD is a symlink whose target lives outside refs/\n  - HEAD cannot be opened, or cannot be read\n  - HEAD contains neither a ref under refs/ nor an object ID\n  - $GIT_OBJECT_DIRECTORY is set to something we cannot access\n  - the object directory in the common directory is inaccessible\n  - the refs directory in the common directory is inaccessible\n\nThis series does not yet report a reason in the following cases.\n\nThe discovery walk, where we iterate up to the ceiling or the mount\npoint looking for a repository:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git rev-parse --is-bare-repository\n  fatal: not a git repository (or any parent up to mount point /)\n  Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)\n\nThe gitfile case, for instance when run from inside a submodule /\nworktree:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git rev-parse --is-bare-repository\n  fatal: gitfile does not point to a valid repository: /path/to/super/sub/.git\n\nGIT_ALTERNATE_OBJECT_DIRECTORIES is also left alone. Its diagnostic\nare misleading in their own way, but they are emitted much later, from\nthe object database rather than from setup, so they need a separate\ntreatment.\n\nI would be very interested in hearing what others think about this\ndirection. If it looks reasonable, I'm happy to cover the remaining\ncases too, either in this series or separately.\n\nKaartic Sivaraam (3):\n  t0009: add tests to cover more error reporting scenarios\n  setup: introduce new helper 'is_git_directory_verbose'\n  setup: communicate why a directory is not a valid git directory\n\n setup.c                       | 165 +++++++++++++++++++++++++---------\n t/t0009-git-dir-validation.sh |  38 ++++++++\n 2 files changed, 161 insertions(+), 42 deletions(-)\n\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553171","messageId":"20260924120502.2642141-2-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260924120502.2642141-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH 1/3] t0009: add tests to cover more error reporting scenarios","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-24T12:02:19Z","receivedAt":"2026-09-24T12:05:15Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Introduce few more tests to t0009 to cover error reporting scenarios\nwhen --git-dir is used.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n t/t0009-git-dir-validation.sh | 36 +++++++++++++++++++++++++++++++++++\n 1 file changed, 36 insertions(+)\n\ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex 4cba478e50..244dc07c0e 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -74,4 +74,40 @@ test_expect_success 'setup: .git as an empty directory is ignored' '\n \t)\n '\n \n+test_expect_success 'setup: custom git directory with missing HEAD is rejected' '\n+\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n+\tmkdir -p parent/empty-dir &&\n+\t(\n+\t\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr\n+\t)\n+'\n+\n+test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n+\ttest_when_finished \"rm -rf parent/head-as-link-to-garbage\" &&\n+\tmkdir -p parent/head-as-link-to-garbage &&\n+\t(\n+\t\tcd parent/head-as-link-to-garbage &&\n+\t\tgit init --bare real-repo &&\n+\t\ttouch garbage &&\n+\t\trm real-repo/HEAD &&\n+\t\tln -s ../garbage real-repo/HEAD &&\n+\t\ttest_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr\n+\t)\n+'\n+\n+test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTORY configuration is rejected' '\n+\ttest_when_finished \"rm -rf parent/invalid-git-object-directory-config\" &&\n+\tmkdir -p parent/invalid-git-object-directory-config &&\n+\t(\n+\t\tcd parent/invalid-git-object-directory-config &&\n+\t\tgit init --bare real-repo &&\n+\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n+\t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr\n+\t)\n+'\n+\n+\n test_done\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553172","messageId":"20260924120502.2642141-3-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260924120502.2642141-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH 2/3] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-24T12:02:20Z","receivedAt":"2026-09-24T12:05:18Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Introduce a new helper is_git_directory_verbose() as a\ncounterpart to the existing is_git_directory().\n\nis_git_directory_verbose() also populates an optional\nstring strbuf with reasoning around why the given suspect\nis not a valid git directory. This strbuf in turn can be\nused to improve the error reporting which is currently blunt:\n\n  fatal: not a git repository\n\nThis is not helpful as the user does not get any hint about \"why\"\nthe repository is not considered valid.\n\nCall-site(s) will be made to use this helper in a follow-up commit.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c | 152 +++++++++++++++++++++++++++++++++++++++++---------------\n 1 file changed, 112 insertions(+), 40 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 0d157ac254..b3b53a1cfc 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -347,7 +347,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)\n \treturn ret;\n }\n \n-static int validate_headref(const char *path)\n+static int validate_headref(const char *path, struct strbuf *err)\n {\n \tstruct stat st;\n \tchar buffer[256];\n@@ -356,14 +356,32 @@ static int validate_headref(const char *path)\n \tint fd;\n \tssize_t len;\n \n-\tif (lstat(path, &st) < 0)\n+\tif (lstat(path, &st) < 0) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(\n+\t\t\t\terr, _(\"could not stat HEAD at '%s'\"), path\n+\t\t\t);\n \t\treturn -1;\n+\t}\n \n \t/* Make sure it is a \"refs/..\" symlink */\n \tif (S_ISLNK(st.st_mode)) {\n \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n \t\t\treturn 0;\n+\t\tif (len == -1 && err)\n+\t\t\tstrbuf_addf(\n+\t\t\t\terr,\n+\t\t\t\t_(\"could not read the symlink HEAD at '%s'\"),\n+\t\t\t\tpath\n+\t\t\t);\n+\t\telse if (err)\n+\t\t\tstrbuf_addf(\n+\t\t\t\terr,\n+\t\t\t\t_(\"HEAD is a symlink ('%s') but target\"\n+\t\t\t\t  \" lives outside refs/\"),\n+\t\t\t\tpath\n+\t\t\t);\n \t\treturn -1;\n \t}\n \n@@ -371,13 +389,23 @@ static int validate_headref(const char *path)\n \t * Anything else, just open it and try to see if it is a symbolic ref.\n \t */\n \tfd = open(path, O_RDONLY);\n-\tif (fd < 0)\n+\tif (fd < 0) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(\n+\t\t\t\terr, _(\"could not open HEAD at '%s'\"), path\n+\t\t\t);\n \t\treturn -1;\n+\t}\n \tlen = read_in_full(fd, buffer, sizeof(buffer)-1);\n \tclose(fd);\n \n-\tif (len < 0)\n+\tif (len < 0) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(\n+\t\t\t\terr, _(\"could not read HEAD at '%s'\"), path\n+\t\t\t);\n \t\treturn -1;\n+\t}\n \tbuffer[len] = '\\0';\n \n \t/*\n@@ -396,9 +424,88 @@ static int validate_headref(const char *path)\n \tif (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN)\n \t\treturn 0;\n \n+\tif (err)\n+\t\tstrbuf_addf(\n+\t\t\terr,\n+\t\t\t_(\"HEAD at '%s' does not point to a valid\"\n+\t\t\t  \" symbolic link or an object ID\"),\n+\t\t\tpath\n+\t\t);\n+\n \treturn -1;\n }\n \n+/*\n+ * A variant of is_git_directory that gives additional\n+ * context via 'err' about why a given suspect is not\n+ * a valid git repository.\n+ */\n+static int is_git_directory_verbose(const char *suspect, struct strbuf *err)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tchar *objdir;\n+\tint ret = 0;\n+\tsize_t len;\n+\n+\t/* Check worktree-related signatures */\n+\tstrbuf_addstr(&path, suspect);\n+\tstrbuf_complete(&path, '/');\n+\tstrbuf_addstr(&path, \"HEAD\");\n+\tif (validate_headref(path.buf, err))\n+\t\tgoto done;\n+\n+\tstrbuf_reset(&path);\n+\tget_common_dir(&path, suspect);\n+\tlen = path.len;\n+\n+\t/* Check non-worktree-related signatures */\n+\tobjdir = getenv(DB_ENVIRONMENT);\n+\tif (objdir) {\n+\t\tif (access(objdir, X_OK)) {\n+\t\t\tif (err)\n+\t\t\t\tstrbuf_addf(\n+\t\t\t\t\terr,\n+\t\t\t\t\t_(\"cannot access object directory '%s'\"\n+\t\t\t\t\t  \" set via $%s\\n\"),\n+\t\t\t\tobjdir,\n+\t\t\t\tDB_ENVIRONMENT\n+\t\t\t);\n+\t\t\tgoto done;\n+\t\t}\n+\t}\n+\telse {\n+\t\tstrbuf_setlen(&path, len);\n+\t\tstrbuf_addstr(&path, \"/objects\");\n+\t\tif (access(path.buf, X_OK)) {\n+\t\t\tif (err)\n+\t\t\t\tstrbuf_addf(\n+\t\t\t\t\terr,\n+\t\t\t\t\t_(\"cannot access object directory '%s'\"),\n+\t\t\t\t\tpath.buf\n+\t\t\t\t);\n+\t\t\tgoto done;\n+\t\t}\n+\t}\n+\n+\tstrbuf_setlen(&path, len);\n+\tstrbuf_addstr(&path, \"/refs\");\n+\tif (access(path.buf, X_OK)) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(\n+\t\t\t\terr,\n+\t\t\t\t_(\"cannot access refs directory '%s'\"),\n+\t\t\t\tpath.buf\n+\t\t\t);\n+\t\tgoto done;\n+\t}\n+\n+\tret = 1;\n+done:\n+\tstrbuf_release(&path);\n+\treturn ret;\n+\n+}\n+\n /*\n  * Test if it looks like we're at a git directory.\n  * We want to see:\n@@ -412,42 +519,7 @@ static int validate_headref(const char *path)\n  */\n int is_git_directory(const char *suspect)\n {\n-\tstruct strbuf path = STRBUF_INIT;\n-\tint ret = 0;\n-\tsize_t len;\n-\n-\t/* Check worktree-related signatures */\n-\tstrbuf_addstr(&path, suspect);\n-\tstrbuf_complete(&path, '/');\n-\tstrbuf_addstr(&path, \"HEAD\");\n-\tif (validate_headref(path.buf))\n-\t\tgoto done;\n-\n-\tstrbuf_reset(&path);\n-\tget_common_dir(&path, suspect);\n-\tlen = path.len;\n-\n-\t/* Check non-worktree-related signatures */\n-\tif (getenv(DB_ENVIRONMENT)) {\n-\t\tif (access(getenv(DB_ENVIRONMENT), X_OK))\n-\t\t\tgoto done;\n-\t}\n-\telse {\n-\t\tstrbuf_setlen(&path, len);\n-\t\tstrbuf_addstr(&path, \"/objects\");\n-\t\tif (access(path.buf, X_OK))\n-\t\t\tgoto done;\n-\t}\n-\n-\tstrbuf_setlen(&path, len);\n-\tstrbuf_addstr(&path, \"/refs\");\n-\tif (access(path.buf, X_OK))\n-\t\tgoto done;\n-\n-\tret = 1;\n-done:\n-\tstrbuf_release(&path);\n-\treturn ret;\n+\treturn is_git_directory_verbose(suspect, NULL);\n }\n \n int is_nonbare_repository_dir(struct strbuf *path)\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553173","messageId":"20260924120502.2642141-4-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260924120502.2642141-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH 3/3] setup: communicate why a directory is not a valid git directory","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-24T12:02:21Z","receivedAt":"2026-09-24T12:05:20Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"At the moment, there are a few scenarios in which the error message\nsurrounding an invalid Git repository is a bit blunt:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n\nIn this case, even though repo.git is a valid Git repository,\nwe get an output saying it is not since the GIT_OBJECT_DIRECTORY\ndoes not point to a valid object directory. At the moment, the\nuser is on their own in figuring this out.\n\nInstead, make it more easy for users to figure such issues\nparticularly in cases where they have explicitly specified\na Git directory. This intends to improve the error reporting UX\nby clarifying why the specified repository is not considered valid.\n\nWe achieve this by means of using the new helper\nis_git_directory_verbose() that has been introduced. With the\nsame, we get a more helpful error message as follows:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n  reason: cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c                       | 13 +++++++++++--\n t/t0009-git-dir-validation.sh | 10 ++++++----\n 2 files changed, 17 insertions(+), 6 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex b3b53a1cfc..3e99141474 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1225,6 +1225,7 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \t\t\t\t\t  int *nongit_ok)\n {\n \tconst char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);\n+\tstruct strbuf invalid_gitdir_reason = STRBUF_INIT;\n \tchar *gitfile;\n \tint offset;\n \n@@ -1237,12 +1238,19 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \t\tgitdirenv = gitfile;\n \t}\n \n-\tif (!is_git_directory(gitdirenv)) {\n+\tif (!is_git_directory_verbose(gitdirenv, &invalid_gitdir_reason)) {\n+\t\tstruct strbuf die_msg = STRBUF_INIT;\n \t\tif (nongit_ok) {\n \t\t\t*nongit_ok = 1;\n \t\t\tgoto out;\n \t\t}\n-\t\tdie(_(\"not a git repository: '%s'\"), gitdirenv);\n+\n+\t\tstrbuf_addf(&die_msg, _(\"not a git repository: '%s'\"), gitdirenv);\n+\t\tstrbuf_addch(&die_msg, '\\n');\n+\t\tstrbuf_addf(&die_msg, _(\"reason: %s\"), invalid_gitdir_reason.buf);\n+\t\tdie(\"%s\", die_msg.buf);\n+\n+\t\tstrbuf_release(&die_msg);\n \t}\n \n \tif (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok))\n@@ -1304,6 +1312,7 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \trepo_discovery_set_gitdir(discovery, gitdirenv, 0);\n \n out:\n+\tstrbuf_release(&invalid_gitdir_reason);\n \tfree(gitfile);\n }\n \ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex 244dc07c0e..411aac1d9e 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -79,7 +79,8 @@ test_expect_success 'setup: custom git directory with missing HEAD is rejected'\n \tmkdir -p parent/empty-dir &&\n \t(\n \t\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository\" stderr\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"reason: could not stat HEAD at\" stderr\n \t)\n '\n \n@@ -93,7 +94,8 @@ test_expect_success 'setup: custom git directory with HEAD as a symlink outside\n \t\trm real-repo/HEAD &&\n \t\tln -s ../garbage real-repo/HEAD &&\n \t\ttest_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository\" stderr\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"reason: HEAD is a symlink .* but target lives outside refs\" stderr\n \t)\n '\n \n@@ -105,9 +107,9 @@ test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTO\n \t\tgit init --bare real-repo &&\n \t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n \t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository\" stderr\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"reason: cannot access object directory .* set via \\$GIT_OBJECT_DIRECTORY\"   stderr\n \t)\n '\n \n-\n test_done\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553244","messageId":"xmqqjyoaz4ig.fsf@gitster.g","threadId":"66383","inReplyTo":"20260924120502.2642141-2-kaartic.sivaraam@gmail.com","subject":"Re: [RFC PATCH 1/3] t0009: add tests to cover more error reporting scenarios","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-24T22:08:39Z","receivedAt":"2026-09-24T22:08:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> Introduce few more tests to t0009 to cover error reporting scenarios\n> when --git-dir is used.\n>\n> Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n> ---\n>  t/t0009-git-dir-validation.sh | 36 +++++++++++++++++++++++++++++++++++\n>  1 file changed, 36 insertions(+)\n>\n> diff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\n> index 4cba478e50..244dc07c0e 100755\n> --- a/t/t0009-git-dir-validation.sh\n> +++ b/t/t0009-git-dir-validation.sh\n> @@ -74,4 +74,40 @@ test_expect_success 'setup: .git as an empty directory is ignored' '\n>  \t)\n>  '\n>  \n> +test_expect_success 'setup: custom git directory with missing HEAD is rejected' '\n> +\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n> +\tmkdir -p parent/empty-dir &&\n> +\t(\n> +\t\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n> +\t\ttest_grep \"not a git repository\" stderr\n> +\t)\n> +'\n\nWhy subshell?\n\n> +\n> +test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n> +\ttest_when_finished \"rm -rf parent/head-as-link-to-garbage\" &&\n> +\tmkdir -p parent/head-as-link-to-garbage &&\n> +\t(\n> +\t\tcd parent/head-as-link-to-garbage &&\n> +\t\tgit init --bare real-repo &&\n> +\t\ttouch garbage &&\n> +\t\trm real-repo/HEAD &&\n> +\t\tln -s ../garbage real-repo/HEAD &&\n> +\t\ttest_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n> +\t\ttest_grep \"not a git repository\" stderr\n> +\t)\n> +'\n> +\n> +test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTORY configuration is rejected' '\n> +\ttest_when_finished \"rm -rf parent/invalid-git-object-directory-config\" &&\n> +\tmkdir -p parent/invalid-git-object-directory-config &&\n> +\t(\n> +\t\tcd parent/invalid-git-object-directory-config &&\n> +\t\tgit init --bare real-repo &&\n> +\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n> +\t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n> +\t\ttest_grep \"not a git repository\" stderr\n> +\t)\n> +'\n> +\n> +\n>  test_done\n"},{"id":"553245","messageId":"xmqqcxu2z4cy.fsf@gitster.g","threadId":"66383","inReplyTo":"20260924120502.2642141-3-kaartic.sivaraam@gmail.com","subject":"Re: [RFC PATCH 2/3] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-24T22:11:57Z","receivedAt":"2026-09-24T22:11:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> Introduce a new helper is_git_directory_verbose() as a\n> counterpart to the existing is_git_directory().\n>\n> is_git_directory_verbose() also populates an optional\n> string strbuf with reasoning around why the given suspect\n> is not a valid git directory. This strbuf in turn can be\n> used to improve the error reporting which is currently blunt:\n>\n>   fatal: not a git repository\n>\n> This is not helpful as the user does not get any hint about \"why\"\n> the repository is not considered valid.\n>\n> Call-site(s) will be made to use this helper in a follow-up commit.\n>\n> Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n> ---\n>  setup.c | 152 +++++++++++++++++++++++++++++++++++++++++---------------\n>  1 file changed, 112 insertions(+), 40 deletions(-)\n>\n> diff --git a/setup.c b/setup.c\n> index 0d157ac254..b3b53a1cfc 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -347,7 +347,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)\n>  \treturn ret;\n>  }\n>  \n> -static int validate_headref(const char *path)\n> +static int validate_headref(const char *path, struct strbuf *err)\n>  {\n>  \tstruct stat st;\n>  \tchar buffer[256];\n> @@ -356,14 +356,32 @@ static int validate_headref(const char *path)\n>  \tint fd;\n>  \tssize_t len;\n>  \n> -\tif (lstat(path, &st) < 0)\n> +\tif (lstat(path, &st) < 0) {\n> +\t\tif (err)\n> +\t\t\tstrbuf_addf(\n> +\t\t\t\terr, _(\"could not stat HEAD at '%s'\"), path\n> +\t\t\t);\n>  \t\treturn -1;\n> +\t}\n>  \n>  \t/* Make sure it is a \"refs/..\" symlink */\n>  \tif (S_ISLNK(st.st_mode)) {\n>  \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n>  \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n>  \t\t\treturn 0;\n> +\t\tif (len == -1 && err)\n> +\t\t\tstrbuf_addf(\n> +\t\t\t\terr,\n> +\t\t\t\t_(\"could not read the symlink HEAD at '%s'\"),\n> +\t\t\t\tpath\n> +\t\t\t);\n> +\t\telse if (err)\n> +\t\t\tstrbuf_addf(\n> +\t\t\t\terr,\n> +\t\t\t\t_(\"HEAD is a symlink ('%s') but target\"\n> +\t\t\t\t  \" lives outside refs/\"),\n> +\t\t\t\tpath\n> +\t\t\t);\n>  \t\treturn -1;\n>  \t}\n\nAll of the above (and below---ellided) look fairly funny way to\nindent them.  If you are trying ot match the style used in the\nexisting code around the same area, I wouldn't complain, but I\ndidn't look beyond what is visible in the patch.\n\n> +\t}\n> +\telse {\n\nStyle: \"} else {\" go on a single line.\n"},{"id":"553246","messageId":"xmqq7bkaz45e.fsf@gitster.g","threadId":"66383","inReplyTo":"20260924120502.2642141-4-kaartic.sivaraam@gmail.com","subject":"Re: [RFC PATCH 3/3] setup: communicate why a directory is not a valid git directory","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-24T22:16:29Z","receivedAt":"2026-09-24T22:16:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> +\t\tstrbuf_addf(&die_msg, _(\"not a git repository: '%s'\"), gitdirenv);\n> +\t\tstrbuf_addch(&die_msg, '\\n');\n> +\t\tstrbuf_addf(&die_msg, _(\"reason: %s\"), invalid_gitdir_reason.buf);\n> +\t\tdie(\"%s\", die_msg.buf);\n> +\n> +\t\tstrbuf_release(&die_msg);\n\nYou just called die(); nobody will execute this strbuf_release() for\nyou, and because die() is marked with NORETURN, smart enough compilers\nwould scold you for introducing dead code.\n\nWhy are you lego-assembling localized message yourself, instead of\ndoing something like ...\n\n\tdie(_(\"not a git repository: '%s'\\nreason: %s\"),\n\t    gitdirenv, invalid_gitdir_reason.buf);\n\n... which is what is usually done?\n\n"},{"id":"553282","messageId":"b024b447-4c63-494c-8ffc-f700fb3c7c46@gmail.com","threadId":"66383","inReplyTo":"xmqqjyoaz4ig.fsf@gitster.g","subject":"Re: [RFC PATCH 1/3] t0009: add tests to cover more error reporting scenarios","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-25T13:46:18Z","receivedAt":"2026-09-25T13:46:22Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On 9/25/26 03:38, Junio C Hamano wrote:\n> Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n> \n>> Introduce few more tests to t0009 to cover error reporting scenarios\n>> when --git-dir is used.\n>>\n>> Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n>> ---\n>>   t/t0009-git-dir-validation.sh | 36 +++++++++++++++++++++++++++++++++++\n>>   1 file changed, 36 insertions(+)\n>>\n>> diff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\n>> index 4cba478e50..244dc07c0e 100755\n>> --- a/t/t0009-git-dir-validation.sh\n>> +++ b/t/t0009-git-dir-validation.sh\n>> @@ -74,4 +74,40 @@ test_expect_success 'setup: .git as an empty directory is ignored' '\n>>   \t)\n>>   '\n>>   \n>> +test_expect_success 'setup: custom git directory with missing HEAD is rejected' '\n>> +\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n>> +\tmkdir -p parent/empty-dir &&\n>> +\t(\n>> +\t\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n>> +\t\ttest_grep \"not a git repository\" stderr\n>> +\t)\n>> +'\n> \n> Why subshell?\n>\n\nGood catch. It is unnecessary. An earlier iteration used to cd into \nparent/empty-dir. This is no longer the case. So, I'll avoid the \nsub-shell in this test.\n\n-- \nSivaraam\n\n"},{"id":"553284","messageId":"b27162ed-0c62-4ef0-b125-4340517e0d50@gmail.com","threadId":"66383","inReplyTo":"xmqq7bkaz45e.fsf@gitster.g","subject":"Re: [RFC PATCH 3/3] setup: communicate why a directory is not a valid git directory","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-25T14:33:24Z","receivedAt":"2026-09-25T14:33:32Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On 9/25/26 03:46, Junio C Hamano wrote:\n> Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n> \n>> +\t\tstrbuf_addf(&die_msg, _(\"not a git repository: '%s'\"), gitdirenv);\n>> +\t\tstrbuf_addch(&die_msg, '\\n');\n>> +\t\tstrbuf_addf(&die_msg, _(\"reason: %s\"), invalid_gitdir_reason.buf);\n>> +\t\tdie(\"%s\", die_msg.buf);\n>> +\n>> +\t\tstrbuf_release(&die_msg);\n> \n> You just called die(); nobody will execute this strbuf_release() for\n> you, and because die() is marked with NORETURN, smart enough compilers\n> would scold you for introducing dead code.\n>\n\nOops. It should indeed warn me. I did a quick check and seems gcc (which \nI was using) lost its ability to report this long ago[1]. Sad. Clang \nclearly reports this.\n\n[1]: https://gcc.gnu.org/legacy-ml/gcc-help/2011-05/msg00360.html\n\n> Why are you lego-assembling localized message yourself, instead of\n> doing something like ...\n> \n> \tdie(_(\"not a git repository: '%s'\\nreason: %s\"),\n> \t    gitdirenv, invalid_gitdir_reason.buf);\n> \n> ... which is what is usually done?\n> \n\nThat was my attempt at trying to save translators some work of \ntranslating the \"not a git repository\" part. I was thinking this lego \nwas not a problem as it was not so complex. If we just want to avoid \nlegos totally, I'm cool with doing it the usual way.\n\n-- \nSivaraam\n\n"},{"id":"553306","messageId":"c7808d4b-36f8-4583-8836-7b7d8bc24905@gmail.com","threadId":"66383","inReplyTo":"xmqqcxu2z4cy.fsf@gitster.g","subject":"Re: [RFC PATCH 2/3] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-25T17:51:08Z","receivedAt":"2026-09-25T17:51:14Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On 9/25/26 03:41, Junio C Hamano wrote:\n> Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n> \n>> diff --git a/setup.c b/setup.c\n>> index 0d157ac254..b3b53a1cfc 100644\n>>   \n>>   \t/* Make sure it is a \"refs/..\" symlink */\n>>   \tif (S_ISLNK(st.st_mode)) {\n>>   \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n>>   \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n>>   \t\t\treturn 0;\n>> +\t\tif (len == -1 && err)\n>> +\t\t\tstrbuf_addf(\n>> +\t\t\t\terr,\n>> +\t\t\t\t_(\"could not read the symlink HEAD at '%s'\"),\n>> +\t\t\t\tpath\n>> +\t\t\t);\n>> +\t\telse if (err)\n>> +\t\t\tstrbuf_addf(\n>> +\t\t\t\terr,\n>> +\t\t\t\t_(\"HEAD is a symlink ('%s') but target\"\n>> +\t\t\t\t  \" lives outside refs/\"),\n>> +\t\t\t\tpath\n>> +\t\t\t);\n>>   \t\treturn -1;\n>>   \t}\n> \n> All of the above (and below---ellided) look fairly funny way to\n> indent them.  If you are trying ot match the style used in the\n> existing code around the same area, I wouldn't complain, but I\n> didn't look beyond what is visible in the patch.\n> \n\nIndeed. My bad. Does the following look like a good indentation style \nfor shorter messages?\n\n\tif (lstat(path, &st) < 0) {\n\t\tif (err)\n\t\t\tstrbuf_addf(err, _(\"could not stat HEAD at '%s'\"), path);\n\t\treturn -1;\n          }\n\n... and this for messages that are a bit longer:\n\n\tif (len == -1 && err)\n\t\tstrbuf_addf(err, _(\"could not read the symlink HEAD at '%s'\"),\n\t\t\t    path);\n\telse if (err)\n\t\tstrbuf_addf(err, _(\"HEAD is a symlink ('%s') but target lives\"\n\t\t\t\t   \" outside refs/\"), path);\n\n\n-- \nSivaraam\n\nPS: I'm not yet very sure if my MUA will send this as intended. Will \nresend if it doesn't. Excuse the noise in advance.\n\n"},{"id":"553574","messageId":"20260929102513.712181-2-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260929102513.712181-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH v2 1/4] setup: normalize an if-else to follow our convention","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-29T10:25:07Z","receivedAt":"2026-09-29T10:25:24Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"The else case was not stuck with the corresponding if's\nclosing brace which is not in-line with our convention.\nFix the same.\n\nThis is a style-only change; no behaviour change intended.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c | 3 +--\n 1 file changed, 1 insertion(+), 2 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 0d157ac254..e9a9ecda19 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1879,8 +1879,7 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags\n \t\tif (chdir(used_path.buf))\n \t\t\treturn NULL;\n \t\tpath = validated_path.buf;\n-\t}\n-\telse {\n+\t} else {\n \t\tconst char *gitfile = read_gitfile(path);\n \t\tif (!(flags & ENTER_REPO_ANY_OWNER_OK))\n \t\t\tdie_upon_dubious_ownership(gitfile, NULL, path);\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553576","messageId":"20260929102513.712181-1-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260924120502.2642141-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH v2 0/4] Improve error reporting to mention \"why\" a directory is not a repository","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-29T10:25:06Z","receivedAt":"2026-09-29T10:25:24Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"At the moment, there are a few scenarios where the error message for an\ninvalid Git repository is a bit blunt. For instance, when we point\nGIT_OBJECT_DIRECTORY at a directory that does not exist, we get this:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n\nEven though repo.git itself is a valid repository, we get this rather\npuzzling error saying that it is not. The actual problem is the invalid\nvalue given to GIT_OBJECT_DIRECTORY, and the user is left on their\nto figure that out. This series aims to make such issues easier to\ndiagnose by saying why the specified repository was not considered\nvalid.\n\nWith this series, the same command outputs:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n  reason: cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY\n\nThe following scenarios are covered when --git-dir is given explicitly:\n\n  - HEAD is missing, or its path is not traversable\n  - HEAD is a symlink that cannot be read\n  - HEAD is a symlink whose target lives outside refs/\n  - HEAD cannot be opened, or cannot be read\n  - HEAD contains neither a ref under refs/ nor an object ID\n  - $GIT_OBJECT_DIRECTORY is set to something we cannot access\n  - the object directory in the common directory is inaccessible\n  - the refs directory in the common directory is inaccessible\n\nThis series does not yet report a reason in the following cases.\n\nThe discovery walk, where we iterate up to the ceiling or the mount\npoint looking for a repository:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git rev-parse --is-bare-repository\n  fatal: not a git repository (or any parent up to mount point /)\n  Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)\n\nThe gitfile case, for instance when run from inside a submodule /\nworktree:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git rev-parse --is-bare-repository\n  fatal: gitfile does not point to a valid repository: /path/to/super/sub/.git\n\nGIT_ALTERNATE_OBJECT_DIRECTORIES is also left alone. Its diagnostic\nare misleading in their own way, but they are emitted much later, from\nthe object database rather than from setup, so they need a separate\ntreatment.\n\nI would be very interested in hearing what others think about this\ndirection. If it looks reasonable, I'm happy to cover the remaining\ncases too, either in this series or separately.\n\nCI:\n\nOne symlink related test appears to be failing in Windows. I'll check the same.\nAll others pass.\n\nSee here: https://github.com/sivaraam/git/actions/runs/36548531586\n\nChanges since v2:\n\n- Included 1/4 which fixes a style nit\n- Dropped the sub-shell from the test that was unnecessary\n- Fixed some style issue and tried to improve the code intentation corresponding\n  to the strbuf construction code\n- Replaced sentence lego with a single string even though that means\n  additional work for translators. On the plus side, avoiding sentence\n  lego provides them more context about the message.\n\nRange-diff between v1 and v2\n\n-:  ---------- > 1:  17b72331d0 setup: normalize an if-else to follow our convention\n1:  b4d7a4efa2 ! 2:  962418c9f3 t0009: add tests to cover more error reporting scenarios\n    @@ t/t0009-git-dir-validation.sh: test_expect_success 'setup: .git as an empty dire\n     +test_expect_success 'setup: custom git directory with missing HEAD is rejected' '\n     +\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n     +\tmkdir -p parent/empty-dir &&\n    -+\t(\n    -+\t\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n    -+\t\ttest_grep \"not a git repository\" stderr\n    -+\t)\n    ++\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n    ++\ttest_grep \"not a git repository\" stderr\n     +'\n     +\n     +test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n2:  8591dc4162 ! 3:  4a1503a6dc setup: introduce new helper 'is_git_directory_verbose'\n    @@ setup.c: static int validate_headref(const char *path)\n     -\tif (lstat(path, &st) < 0)\n     +\tif (lstat(path, &st) < 0) {\n     +\t\tif (err)\n    -+\t\t\tstrbuf_addf(\n    -+\t\t\t\terr, _(\"could not stat HEAD at '%s'\"), path\n    -+\t\t\t);\n    ++\t\t\tstrbuf_addf(err, _(\"could not stat HEAD at '%s'\"), path);\n      \t\treturn -1;\n     +\t}\n      \n    @@ setup.c: static int validate_headref(const char *path)\n      \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n      \t\t\treturn 0;\n     +\t\tif (len == -1 && err)\n    -+\t\t\tstrbuf_addf(\n    -+\t\t\t\terr,\n    -+\t\t\t\t_(\"could not read the symlink HEAD at '%s'\"),\n    -+\t\t\t\tpath\n    -+\t\t\t);\n    ++\t\t\tstrbuf_addf(err, _(\"could not read the symlink HEAD at '%s'\"),\n    ++\t\t\t\t    path);\n     +\t\telse if (err)\n    -+\t\t\tstrbuf_addf(\n    -+\t\t\t\terr,\n    -+\t\t\t\t_(\"HEAD is a symlink ('%s') but target\"\n    -+\t\t\t\t  \" lives outside refs/\"),\n    -+\t\t\t\tpath\n    -+\t\t\t);\n    ++\t\t\tstrbuf_addf(err, _(\"HEAD is a symlink ('%s') but target lives\"\n    ++\t\t\t\t\t   \" outside refs/\"), path);\n      \t\treturn -1;\n      \t}\n      \n    @@ setup.c: static int validate_headref(const char *path)\n     -\tif (fd < 0)\n     +\tif (fd < 0) {\n     +\t\tif (err)\n    -+\t\t\tstrbuf_addf(\n    -+\t\t\t\terr, _(\"could not open HEAD at '%s'\"), path\n    -+\t\t\t);\n    ++\t\t\tstrbuf_addf(err, _(\"could not open HEAD at '%s'\"), path);\n      \t\treturn -1;\n     +\t}\n      \tlen = read_in_full(fd, buffer, sizeof(buffer)-1);\n    @@ setup.c: static int validate_headref(const char *path)\n     -\tif (len < 0)\n     +\tif (len < 0) {\n     +\t\tif (err)\n    -+\t\t\tstrbuf_addf(\n    -+\t\t\t\terr, _(\"could not read HEAD at '%s'\"), path\n    -+\t\t\t);\n    ++\t\t\tstrbuf_addf(err, _(\"could not read HEAD at '%s'\"), path);\n      \t\treturn -1;\n     +\t}\n      \tbuffer[len] = '\\0';\n    @@ setup.c: static int validate_headref(const char *path)\n      \t\treturn 0;\n      \n     +\tif (err)\n    -+\t\tstrbuf_addf(\n    -+\t\t\terr,\n    -+\t\t\t_(\"HEAD at '%s' does not point to a valid\"\n    -+\t\t\t  \" symbolic link or an object ID\"),\n    -+\t\t\tpath\n    -+\t\t);\n    ++\t\tstrbuf_addf(err, _(\"HEAD at '%s' does not point to a valid symbolic\"\n    ++\t\t\t\t   \" link or an object ID\"), path);\n     +\n      \treturn -1;\n      }\n    @@ setup.c: static int validate_headref(const char *path)\n     +\tif (objdir) {\n     +\t\tif (access(objdir, X_OK)) {\n     +\t\t\tif (err)\n    -+\t\t\t\tstrbuf_addf(\n    -+\t\t\t\t\terr,\n    -+\t\t\t\t\t_(\"cannot access object directory '%s'\"\n    -+\t\t\t\t\t  \" set via $%s\\n\"),\n    -+\t\t\t\tobjdir,\n    -+\t\t\t\tDB_ENVIRONMENT\n    -+\t\t\t);\n    ++\t\t\t\tstrbuf_addf(err, _(\"cannot access object directory '%s'\"\n    ++\t\t\t\t\t\t   \"set via $%s\\n\"), objdir, DB_ENVIRONMENT);\n     +\t\t\tgoto done;\n     +\t\t}\n    -+\t}\n    -+\telse {\n    ++\t} else {\n     +\t\tstrbuf_setlen(&path, len);\n     +\t\tstrbuf_addstr(&path, \"/objects\");\n     +\t\tif (access(path.buf, X_OK)) {\n     +\t\t\tif (err)\n    -+\t\t\t\tstrbuf_addf(\n    -+\t\t\t\t\terr,\n    -+\t\t\t\t\t_(\"cannot access object directory '%s'\"),\n    -+\t\t\t\t\tpath.buf\n    -+\t\t\t\t);\n    ++\t\t\t\tstrbuf_addf(err, _(\"cannot access object directory '%s'\"),\n    ++\t\t\t\t\t    path.buf);\n     +\t\t\tgoto done;\n     +\t\t}\n     +\t}\n    @@ setup.c: static int validate_headref(const char *path)\n     +\tstrbuf_addstr(&path, \"/refs\");\n     +\tif (access(path.buf, X_OK)) {\n     +\t\tif (err)\n    -+\t\t\tstrbuf_addf(\n    -+\t\t\t\terr,\n    -+\t\t\t\t_(\"cannot access refs directory '%s'\"),\n    -+\t\t\t\tpath.buf\n    -+\t\t\t);\n    ++\t\t\tstrbuf_addf(err, _(\"cannot access refs directory '%s'\"), path.buf);\n     +\t\tgoto done;\n     +\t}\n     +\n3:  2c9c8d64bb ! 4:  27a9f668ab setup: communicate why a directory is not a valid git directory\n    @@ setup.c: static void repo_discover_explicit_gitdir(struct repo_discovery *discov\n      \t\t}\n     -\t\tdie(_(\"not a git repository: '%s'\"), gitdirenv);\n     +\n    -+\t\tstrbuf_addf(&die_msg, _(\"not a git repository: '%s'\"), gitdirenv);\n    -+\t\tstrbuf_addch(&die_msg, '\\n');\n    -+\t\tstrbuf_addf(&die_msg, _(\"reason: %s\"), invalid_gitdir_reason.buf);\n    ++\t\tstrbuf_addf(&die_msg, _(\"not a git repository: '%s'\\nreason: %s\"),\n    ++\t\t\t    gitdirenv, invalid_gitdir_reason.buf);\n     +\t\tdie(\"%s\", die_msg.buf);\n    -+\n    -+\t\tstrbuf_release(&die_msg);\n      \t}\n      \n      \tif (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok))\n    @@ setup.c: static void repo_discover_explicit_gitdir(struct repo_discovery *discov\n     \n      ## t/t0009-git-dir-validation.sh ##\n     @@ t/t0009-git-dir-validation.sh: test_expect_success 'setup: custom git directory with missing HEAD is rejected'\n    + \ttest_when_finished \"rm -rf parent/empty-dir\" &&\n      \tmkdir -p parent/empty-dir &&\n    - \t(\n    - \t\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n    --\t\ttest_grep \"not a git repository\" stderr\n    -+\t\ttest_grep \"not a git repository\" stderr &&\n    -+\t\ttest_grep \"reason: could not stat HEAD at\" stderr\n    - \t)\n    + \ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n    +-\ttest_grep \"not a git repository\" stderr\n    ++\ttest_grep \"not a git repository\" stderr &&\n    ++\ttest_grep \"reason: could not stat HEAD at\" stderr\n      '\n      \n    + test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n     @@ t/t0009-git-dir-validation.sh: test_expect_success 'setup: custom git directory with HEAD as a symlink outside\n      \t\trm real-repo/HEAD &&\n      \t\tln -s ../garbage real-repo/HEAD &&\n\nKaartic Sivaraam (4):\n  setup: normalize an if-else to follow our convention\n  t0009: add tests to cover more error reporting scenarios\n  setup: introduce new helper 'is_git_directory_verbose'\n  setup: communicate why a directory is not a valid git directory\n\n setup.c                       | 135 +++++++++++++++++++++++-----------\n t/t0009-git-dir-validation.sh |  36 +++++++++\n 2 files changed, 127 insertions(+), 44 deletions(-)\n\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553575","messageId":"20260929102513.712181-3-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260929102513.712181-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH v2 2/4] t0009: add tests to cover more error reporting scenarios","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-29T10:25:08Z","receivedAt":"2026-09-29T10:25:27Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Introduce few more tests to t0009 to cover error reporting scenarios\nwhen --git-dir is used.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n t/t0009-git-dir-validation.sh | 34 ++++++++++++++++++++++++++++++++++\n 1 file changed, 34 insertions(+)\n\ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex 4cba478e50..6c40925aa4 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -74,4 +74,38 @@ test_expect_success 'setup: .git as an empty directory is ignored' '\n \t)\n '\n \n+test_expect_success 'setup: custom git directory with missing HEAD is rejected' '\n+\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n+\tmkdir -p parent/empty-dir &&\n+\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n+\ttest_grep \"not a git repository\" stderr\n+'\n+\n+test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n+\ttest_when_finished \"rm -rf parent/head-as-link-to-garbage\" &&\n+\tmkdir -p parent/head-as-link-to-garbage &&\n+\t(\n+\t\tcd parent/head-as-link-to-garbage &&\n+\t\tgit init --bare real-repo &&\n+\t\ttouch garbage &&\n+\t\trm real-repo/HEAD &&\n+\t\tln -s ../garbage real-repo/HEAD &&\n+\t\ttest_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr\n+\t)\n+'\n+\n+test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTORY configuration is rejected' '\n+\ttest_when_finished \"rm -rf parent/invalid-git-object-directory-config\" &&\n+\tmkdir -p parent/invalid-git-object-directory-config &&\n+\t(\n+\t\tcd parent/invalid-git-object-directory-config &&\n+\t\tgit init --bare real-repo &&\n+\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n+\t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr\n+\t)\n+'\n+\n+\n test_done\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553577","messageId":"20260929102513.712181-4-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260929102513.712181-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH v2 3/4] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-29T10:25:09Z","receivedAt":"2026-09-29T10:25:27Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Introduce a new helper is_git_directory_verbose() as a\ncounterpart to the existing is_git_directory().\n\nis_git_directory_verbose() also populates an optional\nstring strbuf with reasoning around why the given suspect\nis not a valid git directory. This strbuf in turn can be\nused to improve the error reporting which is currently blunt:\n\n  fatal: not a git repository\n\nThis is not helpful as the user does not get any hint about \"why\"\nthe repository is not considered valid.\n\nCall-site(s) will be made to use this helper in a follow-up commit.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c | 122 +++++++++++++++++++++++++++++++++++++-------------------\n 1 file changed, 82 insertions(+), 40 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex e9a9ecda19..a0fb68f7f6 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -347,7 +347,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)\n \treturn ret;\n }\n \n-static int validate_headref(const char *path)\n+static int validate_headref(const char *path, struct strbuf *err)\n {\n \tstruct stat st;\n \tchar buffer[256];\n@@ -356,14 +356,23 @@ static int validate_headref(const char *path)\n \tint fd;\n \tssize_t len;\n \n-\tif (lstat(path, &st) < 0)\n+\tif (lstat(path, &st) < 0) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(err, _(\"could not stat HEAD at '%s'\"), path);\n \t\treturn -1;\n+\t}\n \n \t/* Make sure it is a \"refs/..\" symlink */\n \tif (S_ISLNK(st.st_mode)) {\n \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n \t\t\treturn 0;\n+\t\tif (len == -1 && err)\n+\t\t\tstrbuf_addf(err, _(\"could not read the symlink HEAD at '%s'\"),\n+\t\t\t\t    path);\n+\t\telse if (err)\n+\t\t\tstrbuf_addf(err, _(\"HEAD is a symlink ('%s') but target lives\"\n+\t\t\t\t\t   \" outside refs/\"), path);\n \t\treturn -1;\n \t}\n \n@@ -371,13 +380,19 @@ static int validate_headref(const char *path)\n \t * Anything else, just open it and try to see if it is a symbolic ref.\n \t */\n \tfd = open(path, O_RDONLY);\n-\tif (fd < 0)\n+\tif (fd < 0) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(err, _(\"could not open HEAD at '%s'\"), path);\n \t\treturn -1;\n+\t}\n \tlen = read_in_full(fd, buffer, sizeof(buffer)-1);\n \tclose(fd);\n \n-\tif (len < 0)\n+\tif (len < 0) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(err, _(\"could not read HEAD at '%s'\"), path);\n \t\treturn -1;\n+\t}\n \tbuffer[len] = '\\0';\n \n \t/*\n@@ -396,9 +411,71 @@ static int validate_headref(const char *path)\n \tif (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN)\n \t\treturn 0;\n \n+\tif (err)\n+\t\tstrbuf_addf(err, _(\"HEAD at '%s' does not point to a valid symbolic\"\n+\t\t\t\t   \" link or an object ID\"), path);\n+\n \treturn -1;\n }\n \n+/*\n+ * A variant of is_git_directory that gives additional\n+ * context via 'err' about why a given suspect is not\n+ * a valid git repository.\n+ */\n+static int is_git_directory_verbose(const char *suspect, struct strbuf *err)\n+{\n+\tstruct strbuf path = STRBUF_INIT;\n+\tchar *objdir;\n+\tint ret = 0;\n+\tsize_t len;\n+\n+\t/* Check worktree-related signatures */\n+\tstrbuf_addstr(&path, suspect);\n+\tstrbuf_complete(&path, '/');\n+\tstrbuf_addstr(&path, \"HEAD\");\n+\tif (validate_headref(path.buf, err))\n+\t\tgoto done;\n+\n+\tstrbuf_reset(&path);\n+\tget_common_dir(&path, suspect);\n+\tlen = path.len;\n+\n+\t/* Check non-worktree-related signatures */\n+\tobjdir = getenv(DB_ENVIRONMENT);\n+\tif (objdir) {\n+\t\tif (access(objdir, X_OK)) {\n+\t\t\tif (err)\n+\t\t\t\tstrbuf_addf(err, _(\"cannot access object directory '%s'\"\n+\t\t\t\t\t\t   \" set via $%s\\n\"), objdir, DB_ENVIRONMENT);\n+\t\t\tgoto done;\n+\t\t}\n+\t} else {\n+\t\tstrbuf_setlen(&path, len);\n+\t\tstrbuf_addstr(&path, \"/objects\");\n+\t\tif (access(path.buf, X_OK)) {\n+\t\t\tif (err)\n+\t\t\t\tstrbuf_addf(err, _(\"cannot access object directory '%s'\"),\n+\t\t\t\t\t    path.buf);\n+\t\t\tgoto done;\n+\t\t}\n+\t}\n+\n+\tstrbuf_setlen(&path, len);\n+\tstrbuf_addstr(&path, \"/refs\");\n+\tif (access(path.buf, X_OK)) {\n+\t\tif (err)\n+\t\t\tstrbuf_addf(err, _(\"cannot access refs directory '%s'\"), path.buf);\n+\t\tgoto done;\n+\t}\n+\n+\tret = 1;\n+done:\n+\tstrbuf_release(&path);\n+\treturn ret;\n+\n+}\n+\n /*\n  * Test if it looks like we're at a git directory.\n  * We want to see:\n@@ -412,42 +489,7 @@ static int validate_headref(const char *path)\n  */\n int is_git_directory(const char *suspect)\n {\n-\tstruct strbuf path = STRBUF_INIT;\n-\tint ret = 0;\n-\tsize_t len;\n-\n-\t/* Check worktree-related signatures */\n-\tstrbuf_addstr(&path, suspect);\n-\tstrbuf_complete(&path, '/');\n-\tstrbuf_addstr(&path, \"HEAD\");\n-\tif (validate_headref(path.buf))\n-\t\tgoto done;\n-\n-\tstrbuf_reset(&path);\n-\tget_common_dir(&path, suspect);\n-\tlen = path.len;\n-\n-\t/* Check non-worktree-related signatures */\n-\tif (getenv(DB_ENVIRONMENT)) {\n-\t\tif (access(getenv(DB_ENVIRONMENT), X_OK))\n-\t\t\tgoto done;\n-\t}\n-\telse {\n-\t\tstrbuf_setlen(&path, len);\n-\t\tstrbuf_addstr(&path, \"/objects\");\n-\t\tif (access(path.buf, X_OK))\n-\t\t\tgoto done;\n-\t}\n-\n-\tstrbuf_setlen(&path, len);\n-\tstrbuf_addstr(&path, \"/refs\");\n-\tif (access(path.buf, X_OK))\n-\t\tgoto done;\n-\n-\tret = 1;\n-done:\n-\tstrbuf_release(&path);\n-\treturn ret;\n+\treturn is_git_directory_verbose(suspect, NULL);\n }\n \n int is_nonbare_repository_dir(struct strbuf *path)\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553578","messageId":"20260929102513.712181-5-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260929102513.712181-1-kaartic.sivaraam@gmail.com","subject":"[RFC PATCH v2 4/4] setup: communicate why a directory is not a valid git directory","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-09-29T10:25:10Z","receivedAt":"2026-09-29T10:25:31Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"At the moment, there are a few scenarios in which the error message\nsurrounding an invalid Git repository is a bit blunt:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n\nIn this case, even though repo.git is a valid Git repository,\nwe get an output saying it is not since the GIT_OBJECT_DIRECTORY\ndoes not point to a valid object directory. At the moment, the\nuser is on their own in figuring this out.\n\nInstead, make it more easy for users to figure such issues\nparticularly in cases where they have explicitly specified\na Git directory. This intends to improve the error reporting UX\nby clarifying why the specified repository is not considered valid.\n\nWe achieve this by means of using the new helper\nis_git_directory_verbose() that has been introduced. With the\nsame, we get a more helpful error message as follows:\n\n  $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n  fatal: not a git repository: 'repo.git'\n  reason: cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c                       | 10 ++++++++--\n t/t0009-git-dir-validation.sh | 10 ++++++----\n 2 files changed, 14 insertions(+), 6 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex a0fb68f7f6..a0d3c0c5bb 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1195,6 +1195,7 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \t\t\t\t\t  int *nongit_ok)\n {\n \tconst char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);\n+\tstruct strbuf invalid_gitdir_reason = STRBUF_INIT;\n \tchar *gitfile;\n \tint offset;\n \n@@ -1207,12 +1208,16 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \t\tgitdirenv = gitfile;\n \t}\n \n-\tif (!is_git_directory(gitdirenv)) {\n+\tif (!is_git_directory_verbose(gitdirenv, &invalid_gitdir_reason)) {\n+\t\tstruct strbuf die_msg = STRBUF_INIT;\n \t\tif (nongit_ok) {\n \t\t\t*nongit_ok = 1;\n \t\t\tgoto out;\n \t\t}\n-\t\tdie(_(\"not a git repository: '%s'\"), gitdirenv);\n+\n+\t\tstrbuf_addf(&die_msg, _(\"not a git repository: '%s'\\nreason: %s\"),\n+\t\t\t    gitdirenv, invalid_gitdir_reason.buf);\n+\t\tdie(\"%s\", die_msg.buf);\n \t}\n \n \tif (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok))\n@@ -1274,6 +1279,7 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \trepo_discovery_set_gitdir(discovery, gitdirenv, 0);\n \n out:\n+\tstrbuf_release(&invalid_gitdir_reason);\n \tfree(gitfile);\n }\n \ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex 6c40925aa4..1f8ac3fad5 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -78,7 +78,8 @@ test_expect_success 'setup: custom git directory with missing HEAD is rejected'\n \ttest_when_finished \"rm -rf parent/empty-dir\" &&\n \tmkdir -p parent/empty-dir &&\n \ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n-\ttest_grep \"not a git repository\" stderr\n+\ttest_grep \"not a git repository\" stderr &&\n+\ttest_grep \"reason: could not stat HEAD at\" stderr\n '\n \n test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n@@ -91,7 +92,8 @@ test_expect_success 'setup: custom git directory with HEAD as a symlink outside\n \t\trm real-repo/HEAD &&\n \t\tln -s ../garbage real-repo/HEAD &&\n \t\ttest_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository\" stderr\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"reason: HEAD is a symlink .* but target lives outside refs\" stderr\n \t)\n '\n \n@@ -103,9 +105,9 @@ test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTO\n \t\tgit init --bare real-repo &&\n \t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n \t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository\" stderr\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"reason: cannot access object directory .* set via \\$GIT_OBJECT_DIRECTORY\"   stderr\n \t)\n '\n \n-\n test_done\n-- \n2.56.0.rc1.12.g2c9c8d64bb\n\n"},{"id":"553724","messageId":"ar0yutZ9ksSvaVmM@pks.im","threadId":"66383","inReplyTo":"20260929102513.712181-4-kaartic.sivaraam@gmail.com","subject":"Re: [RFC PATCH v2 3/4] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-09-30T16:03:06Z","receivedAt":"2026-09-30T16:03:13Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Sep 29, 2026 at 03:55:09PM +0530, Kaartic Sivaraam wrote:\n> diff --git a/setup.c b/setup.c\n> index e9a9ecda19..a0fb68f7f6 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -347,7 +347,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)\n>  \treturn ret;\n>  }\n>  \n> -static int validate_headref(const char *path)\n> +static int validate_headref(const char *path, struct strbuf *err)\n>  {\n>  \tstruct stat st;\n>  \tchar buffer[256];\n\nIf only we had structured errors.\n\n> @@ -356,14 +356,23 @@ static int validate_headref(const char *path)\n>  \tint fd;\n>  \tssize_t len;\n>  \n> -\tif (lstat(path, &st) < 0)\n> +\tif (lstat(path, &st) < 0) {\n> +\t\tif (err)\n> +\t\t\tstrbuf_addf(err, _(\"could not stat HEAD at '%s'\"), path);\n\nShouldn't this also include `strerror(errno)`? Otherwise you're still\nnot that much wiser what the root cause of this is.\n\n>  \t\treturn -1;\n> +\t}\n>  \n>  \t/* Make sure it is a \"refs/..\" symlink */\n>  \tif (S_ISLNK(st.st_mode)) {\n>  \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n>  \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n>  \t\t\treturn 0;\n> +\t\tif (len == -1 && err)\n> +\t\t\tstrbuf_addf(err, _(\"could not read the symlink HEAD at '%s'\"),\n> +\t\t\t\t    path);\n\nSame here, we should include `errno`. Other sites should probably be\nupdated, too.\n\n> @@ -396,9 +411,71 @@ static int validate_headref(const char *path)\n>  \tif (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN)\n>  \t\treturn 0;\n>  \n> +\tif (err)\n> +\t\tstrbuf_addf(err, _(\"HEAD at '%s' does not point to a valid symbolic\"\n> +\t\t\t\t   \" link or an object ID\"), path);\n> +\n>  \treturn -1;\n>  }\n>  \n> +/*\n> + * A variant of is_git_directory that gives additional\n> + * context via 'err' about why a given suspect is not\n> + * a valid git repository.\n> + */\n> +static int is_git_directory_verbose(const char *suspect, struct strbuf *err)\n> +{\n> +\tstruct strbuf path = STRBUF_INIT;\n> +\tchar *objdir;\n> +\tint ret = 0;\n> +\tsize_t len;\n> +\n> +\t/* Check worktree-related signatures */\n> +\tstrbuf_addstr(&path, suspect);\n> +\tstrbuf_complete(&path, '/');\n> +\tstrbuf_addstr(&path, \"HEAD\");\n> +\tif (validate_headref(path.buf, err))\n> +\t\tgoto done;\n> +\n> +\tstrbuf_reset(&path);\n> +\tget_common_dir(&path, suspect);\n> +\tlen = path.len;\n> +\n> +\t/* Check non-worktree-related signatures */\n> +\tobjdir = getenv(DB_ENVIRONMENT);\n> +\tif (objdir) {\n> +\t\tif (access(objdir, X_OK)) {\n> +\t\t\tif (err)\n> +\t\t\t\tstrbuf_addf(err, _(\"cannot access object directory '%s'\"\n> +\t\t\t\t\t\t   \" set via $%s\\n\"), objdir, DB_ENVIRONMENT);\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t} else {\n> +\t\tstrbuf_setlen(&path, len);\n> +\t\tstrbuf_addstr(&path, \"/objects\");\n> +\t\tif (access(path.buf, X_OK)) {\n> +\t\t\tif (err)\n> +\t\t\t\tstrbuf_addf(err, _(\"cannot access object directory '%s'\"),\n> +\t\t\t\t\t    path.buf);\n> +\t\t\tgoto done;\n> +\t\t}\n> +\t}\n> +\n> +\tstrbuf_setlen(&path, len);\n> +\tstrbuf_addstr(&path, \"/refs\");\n> +\tif (access(path.buf, X_OK)) {\n> +\t\tif (err)\n> +\t\t\tstrbuf_addf(err, _(\"cannot access refs directory '%s'\"), path.buf);\n> +\t\tgoto done;\n> +\t}\n> +\n> +\tret = 1;\n> +done:\n> +\tstrbuf_release(&path);\n> +\treturn ret;\n> +\n> +}\n> +\n>  /*\n>   * Test if it looks like we're at a git directory.\n>   * We want to see:\n\nIt would've been helpful to move the function up in a separate commit.\nLike this it's hard to see what exactly has changed.\n\nPatrick\n"},{"id":"553725","messageId":"ar0ywAlMDuzK1Hvb@pks.im","threadId":"66383","inReplyTo":"20260929102513.712181-5-kaartic.sivaraam@gmail.com","subject":"Re: [RFC PATCH v2 4/4] setup: communicate why a directory is not a valid git directory","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-09-30T16:03:12Z","receivedAt":"2026-09-30T16:03:18Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Tue, Sep 29, 2026 at 03:55:10PM +0530, Kaartic Sivaraam wrote:\n> At the moment, there are a few scenarios in which the error message\n> surrounding an invalid Git repository is a bit blunt:\n> \n>   $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n>   fatal: not a git repository: 'repo.git'\n> \n> In this case, even though repo.git is a valid Git repository,\n> we get an output saying it is not since the GIT_OBJECT_DIRECTORY\n> does not point to a valid object directory. At the moment, the\n> user is on their own in figuring this out.\n> \n> Instead, make it more easy for users to figure such issues\n\ns/more easy/easier/\n\n> particularly in cases where they have explicitly specified\n> a Git directory. This intends to improve the error reporting UX\n> by clarifying why the specified repository is not considered valid.\n\nWhich I think is a good motivation.\n\n> We achieve this by means of using the new helper\n> is_git_directory_verbose() that has been introduced. With the\n> same, we get a more helpful error message as follows:\n> \n>   $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n>   fatal: not a git repository: 'repo.git'\n>   reason: cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY\n\nHaving a separate \"reason:\" line feels a bit off to me, but that may be\nsubjective. I'd have preferred to have it on the same line, or maybe\nfirst have \"error:\" followed by \"fatal:\".\n\n> diff --git a/setup.c b/setup.c\n> index a0fb68f7f6..a0d3c0c5bb 100644\n> --- a/setup.c\n> +++ b/setup.c\n> @@ -1195,6 +1195,7 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n>  \t\t\t\t\t  int *nongit_ok)\n>  {\n>  \tconst char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);\n> +\tstruct strbuf invalid_gitdir_reason = STRBUF_INIT;\n\nNit, please feel free to ignore: I'd just have called this `errbuf`.\n\nPatrick\n"},{"id":"553740","messageId":"xmqqv77ma8ty.fsf@gitster.g","threadId":"66383","inReplyTo":"20260929102513.712181-4-kaartic.sivaraam@gmail.com","subject":"Re: [RFC PATCH v2 3/4] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-09-30T18:32:41Z","receivedAt":"2026-09-30T18:32:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> Introduce a new helper is_git_directory_verbose() as a\n> counterpart to the existing is_git_directory().\n>\n> is_git_directory_verbose() also populates an optional\n> string strbuf with reasoning around why the given suspect\n> is not a valid git directory. This strbuf in turn can be\n> used to improve the error reporting which is currently blunt:\n>\n>   fatal: not a git repository\n>\n> This is not helpful as the user does not get any hint about \"why\"\n> the repository is not considered valid.\n\nI suspect that it is much less the failure to report the reason that\nmay confuse users than the failure to report which directory was\ninspected and why we picked that directory.  Once we make it clear\nwhich directory was used as the repository to run the Git operation\nthe user specified, they can visit the directory themselves and see\nthat there is no HEAD, etc.  The user may have a leftover GIT_DIR in\nthe environment that is interfering with their operation without\ntheir remembering they still had it, for example.  For this reason,\nI am not so enthused to see this much code churn to tell the user\nthe subtle differences among a dangling symlink HEAD, a HEAD\npointing outside refs, and a missing HEAD.\n\nWhat would be preferable is a much less invasive patch that reports\nwhich path was assumed to be the repository and where it came from\n(among GIT_DIR, auto-discovery stopped at GIT_CEILING_DIRECTORIES,\netc.).  It would help the user figure out why the directory they\nthought was the Git directory is not what the git binary inspected.\n\nThis patch does report which path we thought HEAD should be at in its\nmessages, but does not explain where that assumption came from,\nunlike the verification of the objects/ directory, which mentions the\nenvironment variable if it is involved.  This feels uneven.\n\nThanks.\n"},{"id":"554166","messageId":"168ac5aa-a05b-43df-9cf4-78c4295e4faa@gmail.com","threadId":"66383","inReplyTo":"ar0yutZ9ksSvaVmM@pks.im","subject":"Re: [RFC PATCH v2 3/4] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-05T12:14:56Z","receivedAt":"2026-10-05T12:15:01Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On 9/30/26 21:33, Patrick Steinhardt wrote:\n> On Tue, Sep 29, 2026 at 03:55:09PM +0530, Kaartic Sivaraam wrote:\n>> diff --git a/setup.c b/setup.c\n>> index e9a9ecda19..a0fb68f7f6 100644\n>> --- a/setup.c\n>> +++ b/setup.c\n>> @@ -347,7 +347,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)\n>>   \treturn ret;\n>>   }\n>>   \n>> -static int validate_headref(const char *path)\n>> +static int validate_headref(const char *path, struct strbuf *err)\n>>   {\n>>   \tstruct stat st;\n>>   \tchar buffer[256];\n> \n> If only we had structured errors.\n>\n\nIndeed.\n>> @@ -356,14 +356,23 @@ static int validate_headref(const char *path)\n>>   \tint fd;\n>>   \tssize_t len;\n>>   \n>> -\tif (lstat(path, &st) < 0)\n>> +\tif (lstat(path, &st) < 0) {\n>> +\t\tif (err)\n>> +\t\t\tstrbuf_addf(err, _(\"could not stat HEAD at '%s'\"), path);\n> \n> Shouldn't this also include `strerror(errno)`? Otherwise you're still\n> not that much wiser what the root cause of this is.\n> \n\nThat would of course be an improvement as it helps provide more context. \nWill check on it.\n  >>   \t\treturn -1;\n>> +\t}\n>>   \n>>   \t/* Make sure it is a \"refs/..\" symlink */\n>>   \tif (S_ISLNK(st.st_mode)) {\n>>   \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n>>   \t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n>>   \t\t\treturn 0;\n>> +\t\tif (len == -1 && err)\n>> +\t\t\tstrbuf_addf(err, _(\"could not read the symlink HEAD at '%s'\"),\n>> +\t\t\t\t    path);\n> \n> Same here, we should include `errno`. Other sites should probably be\n> updated, too.\n> \n\nNoted.\n\n> \n> It would've been helpful to move the function up in a separate commit.\n> Like this it's hard to see what exactly has changed.\n> \n\nIndeed. I will improve it in the next iteration.\n\n-- \nSivaraam\n\n"},{"id":"554168","messageId":"b829d416-f36e-454e-a141-3fc93fd62cb3@gmail.com","threadId":"66383","inReplyTo":"ar0ywAlMDuzK1Hvb@pks.im","subject":"Re: [RFC PATCH v2 4/4] setup: communicate why a directory is not a valid git directory","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-05T12:29:23Z","receivedAt":"2026-10-05T12:29:28Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On 9/30/26 21:33, Patrick Steinhardt wrote:\n> On Tue, Sep 29, 2026 at 03:55:10PM +0530, Kaartic Sivaraam wrote:\n>> At the moment, there are a few scenarios in which the error message\n>> surrounding an invalid Git repository is a bit blunt:\n>>\n>>    $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n>>    fatal: not a git repository: 'repo.git'\n>>\n>> In this case, even though repo.git is a valid Git repository,\n>> we get an output saying it is not since the GIT_OBJECT_DIRECTORY\n>> does not point to a valid object directory. At the moment, the\n>> user is on their own in figuring this out.\n>>\n>> Instead, make it more easy for users to figure such issues\n> \n> s/more easy/easier/\n>\n\nAh. Will correct.\n\n>> particularly in cases where they have explicitly specified\n>> a Git directory. This intends to improve the error reporting UX\n>> by clarifying why the specified repository is not considered valid.\n> \n> Which I think is a good motivation.\n> \n>> We achieve this by means of using the new helper\n>> is_git_directory_verbose() that has been introduced. With the\n>> same, we get a more helpful error message as follows:\n>>\n>>    $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n>>    fatal: not a git repository: 'repo.git'\n>>    reason: cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY\n> \n> Having a separate \"reason:\" line feels a bit off to me, but that may be\n> subjective. I'd have preferred to have it on the same line, or maybe\n> first have \"error:\" followed by \"fatal:\".\n> \n\nHmm. Let me think which of these I could adopt. Thank you for the \nsuggestion.\n\n>> diff --git a/setup.c b/setup.c\n>> index a0fb68f7f6..a0d3c0c5bb 100644\n>> --- a/setup.c\n>> +++ b/setup.c\n>> @@ -1195,6 +1195,7 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n>>   \t\t\t\t\t  int *nongit_ok)\n>>   {\n>>   \tconst char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);\n>> +\tstruct strbuf invalid_gitdir_reason = STRBUF_INIT;\n> \n> Nit, please feel free to ignore: I'd just have called this `errbuf`.\n> \n\nNoted.\n\n-- \nSivaraam\n\n"},{"id":"556641","messageId":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20260924120502.2642141-1-kaartic.sivaraam@gmail.com","subject":"[PATCH v3 0/5] Improve error reporting to mention how a Git repository is discovered","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:53:58Z","receivedAt":"2026-10-28T11:53:58Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"At the moment, when Git gives up with \"fatal: not a git repository\",\nit does not say which directory it actually inspected or where that\ndirectory came from. This is especially confusing when a leftover\nenvironment variable is steering Git somewhere the user did not\nexpect.\n\nIn v2 I tried to address this by explaining why the inspected\ndirectory was not a valid repository (missing HEAD, HEAD outside\nrefs/, and so on). Junio pointed out that this is less useful than\nsaying which directory was used as the repository and where that\ncame from, since once the user knows that they can go look at the\ndirectory themselves. He also pointed out that v2 was uneven, as it\nnamed $GIT_OBJECT_DIRECTORY but did not say where the git directory\nitself came from.\n\nSo this iteration takes that direction instead. The validity checks\nin is_git_directory() and validate_headref() are left untouched, and\nwe only add information at the points where we are about to die.\n\nWith this series:\n\n - An explicitly given git directory says where it came from:\n\n     $ cd /srv/git\n     $ export GIT_DIR=project.git\n     ...\n     $ cd ~/src/myproject            # later, same terminal\n     $ git status\n     fatal: not a git repository: 'project.git'\n     this path was taken from $GIT_DIR (which can also be set with --git-dir or --bare)\n\n   --git-dir and --bare both just set GIT_DIR, so by the time setup\n   runs we cannot tell them apart. I went with a single wording that\n   is true for all three rather than plumbing that information\n   through. Happy to revisit if it is felt to be worth it.\n\n - The discovery walk says where it started, where it stopped and\n   what GIT_CEILING_DIRECTORIES is set to:\n\n     $ echo 'export GIT_CEILING_DIRECTORIES=$HOME' >> ~/.bashrc\n     ...weeks later...\n     $ cd ~/.config/nvim\n     $ git status\n     fatal: not a git repository (or any of the parent directories): .git\n     searched from '/home/user/.config/nvim' up to '/home/user/.config'\n     $GIT_CEILING_DIRECTORIES is set to '/home/user'\n\n - An unusable $GIT_OBJECT_DIRECTORY, which was the original\n   motivation for this series, is reported wherever we die for want\n   of a repository (explicit git directory, ceiling and mount point):\n\n     $ GIT_OBJECT_DIRECTORY=/does/not/exist git --git-dir repo.git rev-parse --is-bare-repository\n     fatal: not a git repository: 'repo.git'\n     this path was taken from $GIT_DIR (which can also be set with --git-dir or --bare)\n     cannot access object directory '/does/not/exist' set via $GIT_OBJECT_DIRECTORY: No such file or directory\n\nCommands that are fine running outside a repository stay silent and\ndo not build these messages at all.\n\nThis series does not handle the following, which I think are better\ndone separately:\n\n - The gitfile case (for instance from inside a submodule or a\n   worktree). A gitfile pointing at an invalid repository dies\n   earlier in read_gitfile() with its own message.\n\n - GIT_ALTERNATE_OBJECT_DIRECTORIES. It never leads to \"not a git\n   repository\". Its diagnostics are misleading in their own way (one\n   of them points at .git/objects/info/alternates even when the entry\n   came from the environment), but they come from the object database\n   rather than from setup.\n\n - The \"$GIT_CEILING_DIRECTORIES is set to\" line is printed whenever\n   the variable is set, even if none of its entries is above the\n   current directory and the walk went all the way to '/'. The\n   \"searched from ... up to '/'\" line makes that visible, and getting\n   it exact would mean passing more state out of the discovery walk.\n\nCI:\n\nThey seem to pass.\n\n- GitLab: https://gitlab.com/gitlab-org/git/-/merge_requests/672\n\n- GitHub: https://github.com/sivaraam/git/actions/runs/40676055975\n\nChanges since v2:\n\n- Dropped is_git_directory_verbose() and the HEAD-related reasons, as\n  per Junio's review. is_git_directory() and validate_headref() are\n  now unchanged.\n- New: say where an explicitly given git directory came from.\n- New: say where the discovery walk started and stopped, and what\n  GIT_CEILING_DIRECTORIES is set to.\n- Reworked the GIT_OBJECT_DIRECTORY reporting to ask once at the\n  point of failure, and extended it to the discovery walk (ceiling\n  and mount point).\n- Dropped the test for HEAD being a symlink outside refs/, as the\n  series no longer cares about it. This test was failing on Windows\n  in v2.\n- Reworked the t0009 tests to check the new messages, including that\n  a command which does not need a repository stays silent.\n- Kept each message as a single translatable string and added\n  TRANSLATORS comments describing the placeholders.\n\nRange-diff versus v2:\n(This may not be very helpful because I threw out 3/4 and 4/4 from v2 and have 3 new\n patches in place of them.)\n\n1:  0b062456eb = 1:  b37136baa4 setup: normalize an if-else to follow our convention\n2:  7e8dab0078 ! 2:  08f3584c3a t0009: add tests to cover more error reporting scenarios\n    @@ Metadata\n     Author: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n\n      ## Commit message ##\n    -    t0009: add tests to cover more error reporting scenarios\n    +    t0009: add tests to cover more repository setup scenarios\n\n    -    Introduce few more tests to t0009 to cover error reporting scenarios\n    -    when --git-dir is used.\n    +    Introduce a few more tests to t0009 to cover repository setup\n    +    scenarios. We introduce these as we intend to do some change\n    +    in upcoming commits which would benefit from this additional\n    +    test coverage.\n\n         Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n\n    @@ t/t0009-git-dir-validation.sh: test_expect_success 'setup: .git as an empty dire\n      \t)\n      '\n\n    -+test_expect_success 'setup: custom git directory with missing HEAD is rejected' '\n    ++test_expect_success 'setup: --git-dir pointing to an invalid directory is rejected' '\n     +\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n     +\tmkdir -p parent/empty-dir &&\n     +\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n     +\ttest_grep \"not a git repository\" stderr\n     +'\n     +\n    -+test_expect_success 'setup: custom git directory with HEAD as a symlink outside refs/ is rejected' '\n    -+\ttest_when_finished \"rm -rf parent/head-as-link-to-garbage\" &&\n    -+\tmkdir -p parent/head-as-link-to-garbage &&\n    ++test_expect_success 'setup: discovery fails when GIT_DIR points to an invalid directory' '\n    ++\ttest_must_fail env GIT_DIR=\"/path/to/does-not-exist\" \\\n    ++\t\tgit rev-parse --is-bare-repository 2>stderr &&\n    ++\ttest_grep \"not a git repository\" stderr\n    ++'\n    ++\n    ++test_expect_success 'setup: command that does not need a repository stays silent for invalid GIT_DIR' '\n    ++\techo x | GIT_DIR=\"/path/to/does-not-exist\" git hash-object --stdin 2>stderr &&\n    ++\ttest_must_be_empty stderr\n    ++'\n    ++\n    ++test_expect_success 'setup: discovery fails when a valid git directory not found within GIT_CEILING_DIRECTORIES' '\n    ++\ttest_when_finished \"rm -rf parent/no-valid-git-directory-within-ceiling\" &&\n    ++\tmkdir -p parent/no-valid-git-directory-within-ceiling/ceil/sub &&\n     +\t(\n    -+\t\tcd parent/head-as-link-to-garbage &&\n    -+\t\tgit init --bare real-repo &&\n    -+\t\ttouch garbage &&\n    -+\t\trm real-repo/HEAD &&\n    -+\t\tln -s ../garbage real-repo/HEAD &&\n    -+\t\ttest_must_fail git --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n    -+\t\ttest_grep \"not a git repository\" stderr\n    ++\t\tcd parent/no-valid-git-directory-within-ceiling/ceil/sub &&\n    ++\t\ttest_must_fail env GIT_CEILING_DIRECTORIES=\"$(pwd)/../../\" \\\n    ++\t\t\tgit rev-parse --is-bare-repository 2>stderr &&\n    ++\t\ttest_grep \"not a git repository (or any of the parent directories)\" stderr\n     +\t)\n     +'\n     +\n    -+test_expect_success 'setup: custom git directory with invalid GIT_OBJECT_DIRECTORY configuration is rejected' '\n    ++test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY and explicit gitdir' '\n     +\ttest_when_finished \"rm -rf parent/invalid-git-object-directory-config\" &&\n     +\tmkdir -p parent/invalid-git-object-directory-config &&\n     +\t(\n     +\t\tcd parent/invalid-git-object-directory-config &&\n     +\t\tgit init --bare real-repo &&\n    -+\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n    ++\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"/path/to/does-not-exist\" \\\n     +\t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n     +\t\ttest_grep \"not a git repository\" stderr\n     +\t)\n     +'\n    -+\n     +\n      test_done\n3:  312b861c6c < -:  ---------- setup: introduce new helper 'is_git_directory_verbose'\n4:  29191f463e < -:  ---------- setup: communicate why a directory is not a valid git directory\n-:  ---------- > 3:  34a110da6d setup: provide hint about the source of the explicit git directory\n-:  ---------- > 4:  9986739fec setup: provide hint about auto-discovery boundaries\n-:  ---------- > 5:  7d13445134 setup: report when GIT_OBJECT_DIRECTORY is what we could not use\n\nKaartic Sivaraam (5):\n  setup: normalize an if-else to follow our convention\n  t0009: add tests to cover more repository discovery scenarios\n  setup: provide hint about the source of the explicit git directory\n  setup: provide hint about auto-discovery boundaries\n  setup: report when GIT_OBJECT_DIRECTORY is what we could not use\n\n setup.c                       | 104 ++++++++++++++++++++++++++++++----\n t/t0009-git-dir-validation.sh |  77 +++++++++++++++++++++++++\n 2 files changed, 171 insertions(+), 10 deletions(-)\n\n-- \n2.55.0.1003.g10538fe699\n\n\n"},{"id":"556642","messageId":"20261028115412.169748-2-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","subject":"[PATCH v3 1/5] setup: normalize an if-else to follow our convention","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:53:59Z","receivedAt":"2026-10-28T11:53:59Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"The else case was not stuck with the corresponding if's\nclosing brace which is not in-line with our convention.\nFix the same.\n\nThis is a style-only change; no behaviour change intended.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c | 3 +--\n 1 file changed, 1 insertion(+), 2 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 61bcdafe82..4789b2d79d 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1905,8 +1905,7 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags\n \t\tif (chdir(used_path.buf))\n \t\t\treturn NULL;\n \t\tpath = validated_path.buf;\n-\t}\n-\telse {\n+\t} else {\n \t\tconst char *gitfile = read_gitfile(path);\n \t\tif (!(flags & ENTER_REPO_ANY_OWNER_OK))\n \t\t\tdie_upon_dubious_ownership(gitfile, NULL, path);\n-- \n2.55.0.1003.g10538fe699\n\n\n"},{"id":"556643","messageId":"20261028115412.169748-3-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","subject":"[PATCH v3 2/5] t0009: add tests to cover more repository discovery scenarios","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:54:00Z","receivedAt":"2026-10-28T11:54:00Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Introduce a few more tests to t0009 to cover repository discovery\nscenarios. We introduce these as we intend to do some change\nin upcoming commits which would benefit from this additional\ntest coverage.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n t/t0009-git-dir-validation.sh | 41 +++++++++++++++++++++++++++++++++++\n 1 file changed, 41 insertions(+)\n\ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex 4cba478e50..bc607e6c2a 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -74,4 +74,45 @@ test_expect_success 'setup: .git as an empty directory is ignored' '\n \t)\n '\n \n+test_expect_success 'setup: --git-dir pointing to an invalid directory is rejected' '\n+\ttest_when_finished \"rm -rf parent/empty-dir\" &&\n+\tmkdir -p parent/empty-dir &&\n+\ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n+\ttest_grep \"not a git repository\" stderr\n+'\n+\n+test_expect_success 'setup: discovery fails when GIT_DIR points to an invalid directory' '\n+\ttest_must_fail env GIT_DIR=\"/path/to/does-not-exist\" \\\n+\t\tgit rev-parse --is-bare-repository 2>stderr &&\n+\ttest_grep \"not a git repository\" stderr\n+'\n+\n+test_expect_success 'setup: command that does not need a repository stays silent for invalid GIT_DIR' '\n+\techo x | GIT_DIR=\"/path/to/does-not-exist\" git hash-object --stdin 2>stderr &&\n+\ttest_must_be_empty stderr\n+'\n+\n+test_expect_success 'setup: discovery fails when a valid git directory not found within GIT_CEILING_DIRECTORIES' '\n+\ttest_when_finished \"rm -rf parent/no-valid-git-directory-within-ceiling\" &&\n+\tmkdir -p parent/no-valid-git-directory-within-ceiling/ceil/sub &&\n+\t(\n+\t\tcd parent/no-valid-git-directory-within-ceiling/ceil/sub &&\n+\t\ttest_must_fail env GIT_CEILING_DIRECTORIES=\"$(pwd)/../../\" \\\n+\t\t\tgit rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository (or any of the parent directories)\" stderr\n+\t)\n+'\n+\n+test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY and explicit gitdir' '\n+\ttest_when_finished \"rm -rf parent/invalid-git-object-directory-config\" &&\n+\tmkdir -p parent/invalid-git-object-directory-config &&\n+\t(\n+\t\tcd parent/invalid-git-object-directory-config &&\n+\t\tgit init --bare real-repo &&\n+\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"/path/to/does-not-exist\" \\\n+\t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr\n+\t)\n+'\n+\n test_done\n-- \n2.55.0.1003.g10538fe699\n\n\n"},{"id":"556644","messageId":"20261028115412.169748-4-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","subject":"[PATCH v3 3/5] setup: provide hint about the source of the explicit git directory","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:54:01Z","receivedAt":"2026-10-28T11:54:01Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"Git directories can be specified explicitly by setting the GIT_DIR\nenvironment variable or by passing --git-dir / --bare before a\nsub-command. When discovery fails in these cases, the error message\nthat we print is a bit unhelpful. This is especially more pronounced\nwhen someone sets GIT_DIR and forgets about it.\n\n  $ cd /srv/git\n  $ export GIT_DIR=project.git    # saves typing --git-dir on every command\n  $ git log --oneline -3\n  $ git for-each-ref --sort=-committerdate refs/heads | head\n  $ cd ~/src/myproject            # later, same terminal\n  $ git status\n  fatal: not a git repository: 'project.git'\n\nThe blunt message does not help the user understand where 'project.git'\ncame from. They are on their own in figuring this out.\n\nImprove this error reporting UX by giving an additional hint to the\nuser that the path comes via GIT_DIR. With this, the user would see\nthe more helpful hint:\n\n  ...\n  $ cd ~/src/myproject            # later, same terminal\n  $ git status\n  fatal: not a git repository: 'project.git'\n  this path was taken from $GIT_DIR (which can also be set with --git-dir or --bare)\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c                       | 8 +++++++-\n t/t0009-git-dir-validation.sh | 6 ++++--\n 2 files changed, 11 insertions(+), 3 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 4789b2d79d..8f6b609240 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1152,7 +1152,13 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \t\t\t*nongit_ok = 1;\n \t\t\tgoto out;\n \t\t}\n-\t\tdie(_(\"not a git repository: '%s'\"), gitdirenv);\n+\t\t/*\n+\t\t * TRANSLATORS: The first %s is a path, the second %s is the\n+\t\t * name of the environment variable it was taken from.\n+\t\t */\n+\t\tdie(_(\"not a git repository: '%s'\\n\"\n+\t\t      \"this path was taken from $%s (which can also be set with --git-dir or --bare)\"),\n+\t\t    gitdirenv, GIT_DIR_ENVIRONMENT);\n \t}\n \n \tif (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok))\ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex bc607e6c2a..fed1b045bb 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -78,13 +78,15 @@ test_expect_success 'setup: --git-dir pointing to an invalid directory is reject\n \ttest_when_finished \"rm -rf parent/empty-dir\" &&\n \tmkdir -p parent/empty-dir &&\n \ttest_must_fail git --git-dir parent/empty-dir rev-parse --is-bare-repository 2>stderr &&\n-\ttest_grep \"not a git repository\" stderr\n+\ttest_grep \"not a git repository\" stderr &&\n+\ttest_grep \"taken from \\$GIT_DIR\" stderr\n '\n \n test_expect_success 'setup: discovery fails when GIT_DIR points to an invalid directory' '\n \ttest_must_fail env GIT_DIR=\"/path/to/does-not-exist\" \\\n \t\tgit rev-parse --is-bare-repository 2>stderr &&\n-\ttest_grep \"not a git repository\" stderr\n+\ttest_grep \"not a git repository\" stderr &&\n+\ttest_grep \"taken from \\$GIT_DIR\" stderr\n '\n \n test_expect_success 'setup: command that does not need a repository stays silent for invalid GIT_DIR' '\n-- \n2.55.0.1003.g10538fe699\n\n\n"},{"id":"556645","messageId":"20261028115412.169748-5-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","subject":"[PATCH v3 4/5] setup: provide hint about auto-discovery boundaries","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:54:02Z","receivedAt":"2026-10-28T11:54:02Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"In the same spirit as the previous commit, we want to improve the UX\nof the Git repository discovery case when the GIT_CEILING_DIRECTORIES\nenvironment varaible is set. For instance, consider the following\nscenario:\n\n  $ cd ~ && git init                    # dotfiles repo at ~/.git\n  $ echo 'export GIT_CEILING_DIRECTORIES=$HOME' >> ~/.bashrc\n                                        # stop ~/.git swallowing every\n                                        # non-repo directory under ~\n  ...weeks later...\n  $ cd ~/.config/nvim\n  $ vim init.lua\n  $ git status\n  fatal: not a git repository (or any of the parent directories): .git\n\nThe error message is a bit unhelpful as the gut reaction of the user\nwould be to wonder why their ~/.git directory was not discovered even\nthough the message says it checked the parent directories. We can't\nreally blame the user for not remembering that they had\nGIT_CEILING_DIRECTORIES configured in their ~/.bashrc.\n\nImprove this error reporting UX by giving an additional hint about the\nceiling that auto-discovery was bounded by and also mention what\nGIT_CEILING_DIRECTORIES was configured to. With this, the user would\nsee the following:\n\n  ...\n  $ git status\n  fatal: not a git repository (or any of the parent directories): .git\n  searched from '/home/user/.config/nvim' up to '/home/user/.config'\n  $GIT_CEILING_DIRECTORIES is set to '/home/user'\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c                       | 29 ++++++++++++++++++++++++++---\n t/t0009-git-dir-validation.sh |  4 +++-\n 2 files changed, 29 insertions(+), 4 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 8f6b609240..87fcead87e 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1997,9 +1997,32 @@ static void repo_discover(struct repo_discovery *discovery, int *nongit_ok)\n \t\trepo_discover_bare_gitdir(discovery, &cwd, dir.len, nongit_ok);\n \t\tbreak;\n \tcase GIT_DIR_HIT_CEILING:\n-\t\tif (!nongit_ok)\n-\t\t\tdie(_(\"not a git repository (or any of the parent directories): %s\"),\n-\t\t\t    DEFAULT_GIT_DIR_ENVIRONMENT);\n+\t\tif (!nongit_ok) {\n+\t\t\tstruct strbuf message = STRBUF_INIT;\n+\t\t\tconst char *ceiling = getenv(CEILING_DIRECTORIES_ENVIRONMENT);\n+\n+\t\t\tstrbuf_addf(&message,\n+\t\t\t\t    /*\n+\t\t\t\t     * TRANSLATORS: The first %s is \".git\", the\n+\t\t\t\t     * second and third %s are the directories\n+\t\t\t\t     * where the search for a repository started\n+\t\t\t\t     * and where it stopped.\n+\t\t\t\t     */\n+\t\t\t\t    _(\"not a git repository (or any of the parent directories): %s\\n\"\n+\t\t\t\t      \"searched from '%s' up to '%s'\"),\n+\t\t\t\t    DEFAULT_GIT_DIR_ENVIRONMENT, cwd.buf, dir.buf);\n+\t\t\tif (ceiling && *ceiling) {\n+\t\t\t\tstrbuf_addch(&message, '\\n');\n+\t\t\t\t/*\n+\t\t\t\t * TRANSLATORS: The first %s is the name of\n+\t\t\t\t * the environment variable, the second %s is\n+\t\t\t\t * its value.\n+\t\t\t\t */\n+\t\t\t\tstrbuf_addf(&message, _(\"$%s is set to '%s'\"),\n+\t\t\t\t\t    CEILING_DIRECTORIES_ENVIRONMENT, ceiling);\n+\t\t\t}\n+\t\t\tdie(\"%s\", message.buf);\n+\t\t}\n \t\t*nongit_ok = 1;\n \t\tbreak;\n \tcase GIT_DIR_HIT_MOUNT_POINT:\ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex fed1b045bb..ab3d2593aa 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -101,7 +101,9 @@ test_expect_success 'setup: discovery fails when a valid git directory not found\n \t\tcd parent/no-valid-git-directory-within-ceiling/ceil/sub &&\n \t\ttest_must_fail env GIT_CEILING_DIRECTORIES=\"$(pwd)/../../\" \\\n \t\t\tgit rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository (or any of the parent directories)\" stderr\n+\t\ttest_grep \"not a git repository (or any of the parent directories)\" stderr &&\n+\t\ttest_grep \"searched from .*ceil/sub.*up to.*ceil.\" stderr &&\n+\t\ttest_grep \"GIT_CEILING_DIRECTORIES is set to\" stderr\n \t)\n '\n \n-- \n2.55.0.1003.g10538fe699\n\n\n"},{"id":"556646","messageId":"20261028115412.169748-6-kaartic.sivaraam@gmail.com","threadId":"66383","inReplyTo":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","subject":"[PATCH v3 5/5] setup: report when GIT_OBJECT_DIRECTORY is what we could not use","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:54:03Z","receivedAt":"2026-10-28T11:54:03Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"In the same spirit as the last two commits, we want to improve the UX\nof the Git repository discovery case when the GIT_OBJECT_DIRECTORY\nenvironment variable is set to an invalid directory. For instance,\nconsider the following scenario:\n\n  $ echo 'export GIT_OBJECT_DIRECTORY=/tmp/git-objects' >> ~/.bashrc\n                                     # shared object store across clones\n  ...after a reboot clears /tmp...\n  $ cd ~/src/myproject\n  $ git status\n  fatal: not a git repository (or any parent up to mount point /)\n  Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\n\nWe get an error here because when GIT_OBJECT_DIRECTORY points at a\ndirectory that cannot be used, Git refuses to work with any repository\nat all, but says so in a way that blames the repository.\n\nImprove this error reporting UX by explaining the real cause wherever\nwe are about to die. Because the check does not depend on the candidate\nthat is given to is_git_directory(), it is enough to ask once, at the\npoint of failure, whether the configured object directory is to blame.\nWith this, the user would see the following:\n\n  ...\n  $ git status\n  fatal: not a git repository (or any parent up to mount point /)\n  Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\n  cannot access object directory '/tmp/git-objects' set via\n    $GIT_OBJECT_DIRECTORY: No such file or directory\n\nNote that pointing GIT_OBJECT_DIRECTORY at a directory that does not\nexist yet is legitimate for \"git init\", which goes on to create it.\nDiagnosing only where we would have died anyway leaves that case alone.\n\nSigned-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>\n---\n setup.c                       | 80 +++++++++++++++++++++++++++++------\n t/t0009-git-dir-validation.sh | 36 +++++++++++++++-\n 2 files changed, 102 insertions(+), 14 deletions(-)\n\ndiff --git a/setup.c b/setup.c\nindex 87fcead87e..7b1e67e0dc 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -450,6 +450,52 @@ int is_git_directory(const char *suspect)\n \treturn ret;\n }\n \n+/*\n+ * If $GIT_OBJECT_DIRECTORY names a directory we cannot use, describe why in\n+ * `out` and return 1, otherwise return 0.\n+ *\n+ * Note that the check is not specific to any one candidate directory: when the\n+ * variable is set but unusable, is_git_directory() rejects every directory it\n+ * is given, so repository discovery cannot succeed anywhere.\n+ */\n+static int describe_unusable_object_dir(struct strbuf *out)\n+{\n+\tconst char *objdir = getenv(DB_ENVIRONMENT);\n+\tint saved_errno;\n+\n+\tif (!objdir || !access(objdir, X_OK))\n+\t\treturn 0;\n+\tsaved_errno = errno;\n+\n+\t/*\n+\t * TRANSLATORS: The first %s is a path, the second %s is the\n+\t * name of the environment variable that set it, and the third\n+\t * %s is the system error message.\n+\t */\n+\tstrbuf_addf(out, _(\"cannot access object directory '%s' set via $%s: %s\"),\n+\t\t    objdir, DB_ENVIRONMENT, strerror(saved_errno));\n+\treturn 1;\n+}\n+\n+/*\n+ * Die with `message`, which reports that no repository could be used. When an\n+ * unusable $GIT_OBJECT_DIRECTORY is what stood in the way, say so as well: on\n+ * its own the message blames the repository we were pointed at, which may be\n+ * perfectly fine.\n+ *\n+ * `message` is not released, as we are about to exit anyway.\n+ */\n+static void NORETURN die_no_repository(struct strbuf *message)\n+{\n+\tstruct strbuf objdir = STRBUF_INIT;\n+\n+\tif (describe_unusable_object_dir(&objdir))\n+\t\tstrbuf_addf(message, \"\\n%s\", objdir.buf);\n+\tstrbuf_release(&objdir);\n+\n+\tdie(\"%s\", message->buf);\n+}\n+\n int is_nonbare_repository_dir(struct strbuf *path)\n {\n \tint ret = 0;\n@@ -1148,17 +1194,22 @@ static void repo_discover_explicit_gitdir(struct repo_discovery *discovery,\n \t}\n \n \tif (!is_git_directory(gitdirenv)) {\n+\t\tstruct strbuf message = STRBUF_INIT;\n+\n \t\tif (nongit_ok) {\n \t\t\t*nongit_ok = 1;\n \t\t\tgoto out;\n \t\t}\n-\t\t/*\n-\t\t * TRANSLATORS: The first %s is a path, the second %s is the\n-\t\t * name of the environment variable it was taken from.\n-\t\t */\n-\t\tdie(_(\"not a git repository: '%s'\\n\"\n-\t\t      \"this path was taken from $%s (which can also be set with --git-dir or --bare)\"),\n-\t\t    gitdirenv, GIT_DIR_ENVIRONMENT);\n+\t\tstrbuf_addf(&message,\n+\t\t\t    /*\n+\t\t\t     * TRANSLATORS: The first %s is a path, the second\n+\t\t\t     * %s is the name of the environment variable it\n+\t\t\t     * was taken from.\n+\t\t\t     */\n+\t\t\t    _(\"not a git repository: '%s'\\n\"\n+\t\t\t      \"this path was taken from $%s (which can also be set with --git-dir or --bare)\"),\n+\t\t\t    gitdirenv, GIT_DIR_ENVIRONMENT);\n+\t\tdie_no_repository(&message);\n \t}\n \n \tif (read_and_verify_repository_format(&discovery->format, gitdirenv, nongit_ok))\n@@ -2021,15 +2072,20 @@ static void repo_discover(struct repo_discovery *discovery, int *nongit_ok)\n \t\t\t\tstrbuf_addf(&message, _(\"$%s is set to '%s'\"),\n \t\t\t\t\t    CEILING_DIRECTORIES_ENVIRONMENT, ceiling);\n \t\t\t}\n-\t\t\tdie(\"%s\", message.buf);\n+\t\t\tdie_no_repository(&message);\n \t\t}\n \t\t*nongit_ok = 1;\n \t\tbreak;\n \tcase GIT_DIR_HIT_MOUNT_POINT:\n-\t\tif (!nongit_ok)\n-\t\t\tdie(_(\"not a git repository (or any parent up to mount point %s)\\n\"\n-\t\t\t      \"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\"),\n-\t\t\t    dir.buf);\n+\t\tif (!nongit_ok) {\n+\t\t\tstruct strbuf message = STRBUF_INIT;\n+\n+\t\t\tstrbuf_addf(&message,\n+\t\t\t\t    _(\"not a git repository (or any parent up to mount point %s)\\n\"\n+\t\t\t\t      \"Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\"),\n+\t\t\t\t    dir.buf);\n+\t\t\tdie_no_repository(&message);\n+\t\t}\n \t\t*nongit_ok = 1;\n \t\tbreak;\n \tcase GIT_DIR_INVALID_OWNERSHIP:\ndiff --git a/t/t0009-git-dir-validation.sh b/t/t0009-git-dir-validation.sh\nindex ab3d2593aa..94ef10edcd 100755\n--- a/t/t0009-git-dir-validation.sh\n+++ b/t/t0009-git-dir-validation.sh\n@@ -107,7 +107,7 @@ test_expect_success 'setup: discovery fails when a valid git directory not found\n \t)\n '\n \n-test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY and explicit gitdir' '\n+test_expect_success 'setup: unusable $GIT_OBJECT_DIRECTORY is reported with explicit gitdir' '\n \ttest_when_finished \"rm -rf parent/invalid-git-object-directory-config\" &&\n \tmkdir -p parent/invalid-git-object-directory-config &&\n \t(\n@@ -115,8 +115,40 @@ test_expect_success 'setup: discovery fails with unusable $GIT_OBJECT_DIRECTORY\n \t\tgit init --bare real-repo &&\n \t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"/path/to/does-not-exist\" \\\n \t\t\tgit --git-dir real-repo rev-parse --is-bare-repository 2>stderr &&\n-\t\ttest_grep \"not a git repository\" stderr\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"cannot access object directory .*does-not-exist\" stderr &&\n+\t\ttest_grep \"GIT_OBJECT_DIRECTORY\" stderr\n \t)\n '\n \n+test_expect_success 'setup: unusable $GIT_OBJECT_DIRECTORY is reported during discovery' '\n+\ttest_when_finished \"rm -rf objdir-discovery\" &&\n+\tgit init objdir-discovery &&\n+\t(\n+\t\tcd objdir-discovery &&\n+\t\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/does-not-exist\" \\\n+\t\t\tgit rev-parse --git-dir 2>stderr &&\n+\t\ttest_grep \"not a git repository\" stderr &&\n+\t\ttest_grep \"cannot access object directory .*does-not-exist\" stderr &&\n+\t\ttest_grep \"GIT_OBJECT_DIRECTORY\" stderr\n+\t)\n+'\n+\n+test_expect_success 'setup: usable $GIT_OBJECT_DIRECTORY is not reported' '\n+\ttest_when_finished \"rm -rf objdir-usable.git objdir-usable-odb\" &&\n+\tgit init --bare objdir-usable.git &&\n+\tmkdir objdir-usable-odb &&\n+\tGIT_OBJECT_DIRECTORY=\"$(pwd)/objdir-usable-odb\" \\\n+\t\tgit --git-dir objdir-usable.git rev-parse --is-bare-repository >actual 2>stderr &&\n+\techo true >expect &&\n+\ttest_cmp expect actual &&\n+\ttest_grep ! \"not a git repository\" stderr &&\n+\ttest_grep ! \"cannot access object directory\" stderr &&\n+\n+\ttest_must_fail env GIT_OBJECT_DIRECTORY=\"$(pwd)/objdir-usable-odb\" \\\n+\t\tgit --git-dir does-not-exist.git rev-parse --is-bare-repository 2>stderr &&\n+\ttest_grep \"not a git repository\" stderr &&\n+\ttest_grep ! \"cannot access object directory\" stderr\n+'\n+\n test_done\n-- \n2.55.0.1003.g10538fe699\n\n\n"},{"id":"556647","messageId":"38881eb1-d97c-43f0-94b2-d6957221fe1e@gmail.com","threadId":"66383","inReplyTo":"xmqqv77ma8ty.fsf@gitster.g","subject":"Re: [RFC PATCH v2 3/4] setup: introduce new helper 'is_git_directory_verbose'","fromName":"Kaartic Sivaraam","fromEmail":"kaartic.sivaraam@gmail.com","sentAt":"2026-10-28T11:57:03Z","receivedAt":"2026-10-28T11:57:03Z","isPatch":true,"sender":{"key":"kaartic.sivaraam@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12448084?v=4"},"body":"On 10/1/26 00:02, Junio C Hamano wrote:\n> Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n> >> Introduce a new helper is_git_directory_verbose() as a\n>> counterpart to the existing is_git_directory().\n>>\n>> is_git_directory_verbose() also populates an optional\n>> string strbuf with reasoning around why the given suspect\n>> is not a valid git directory. This strbuf in turn can be\n>> used to improve the error reporting which is currently blunt:\n>>\n>>    fatal: not a git repository\n>>\n>> This is not helpful as the user does not get any hint about \"why\"\n>> the repository is not considered valid.\n> > I suspect that it is much less the failure to report the reason that\n> may confuse users than the failure to report which directory was\n> inspected and why we picked that directory.  Once we make it clear\n> which directory was used as the repository to run the Git operation\n> the user specified, they can visit the directory themselves and see\n> that there is no HEAD, etc.  The user may have a leftover GIT_DIR in\n> the environment that is interfering with their operation without\n> their remembering they still had it, for example.  For this reason,\n> I am not so enthused to see this much code churn to tell the user\n> the subtle differences among a dangling symlink HEAD, a HEAD\n> pointing outside refs, and a missing HEAD.\n> > What would be preferable is a much less invasive patch that reports\n> which path was assumed to be the repository and where it came from\n> (among GIT_DIR, auto-discovery stopped at GIT_CEILING_DIRECTORIES,\n> etc.).  It would help the user figure out why the directory they\n> thought was the Git directory is not what the git binary inspected.\n> > This patch does report which path we thought HEAD should be at in its\n> messages, but does not explain where that assumption came from,\n> unlike the verification of the objects/ directory, which mentions the\n> environment variable if it is involved.  This feels uneven.\n>\n\nThank you for your insights and excuse the delay. I have tried to take this into consideration and just sent a v3. Kindly check the same and let me know your thoughts.\n\n  v3: <20261028115412.169748-1-kaartic.sivaraam@gmail.com/>\n\n-- \nSivaraam\n\n\n"},{"id":"556693","messageId":"xmqqa4nxac14.fsf@gitster.g","threadId":"66383","inReplyTo":"20261028115412.169748-5-kaartic.sivaraam@gmail.com","subject":"Re: [PATCH v3 4/5] setup: provide hint about auto-discovery boundaries","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-28T18:57:27Z","receivedAt":"2026-10-28T18:57:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> In the same spirit as the previous commit, we want to improve the UX\n> of the Git repository discovery case when the GIT_CEILING_DIRECTORIES\n> environment varaible is set. For instance, consider the following\n> scenario:\n>\n>   $ cd ~ && git init                    # dotfiles repo at ~/.git\n>   $ echo 'export GIT_CEILING_DIRECTORIES=$HOME' >> ~/.bashrc\n>                                         # stop ~/.git swallowing every\n>                                         # non-repo directory under ~\n>   ...weeks later...\n>   $ cd ~/.config/nvim\n>   $ vim init.lua\n>   $ git status\n>   fatal: not a git repository (or any of the parent directories): .git\n>\n> The error message is a bit unhelpful as the gut reaction of the user\n> would be to wonder why their ~/.git directory was not discovered even\n> though the message says it checked the parent directories. We can't\n> really blame the user for not remembering that they had\n> GIT_CEILING_DIRECTORIES configured in their ~/.bashrc.\n>\n> Improve this error reporting UX by giving an additional hint about the\n> ceiling that auto-discovery was bounded by and also mention what\n> GIT_CEILING_DIRECTORIES was configured to. With this, the user would\n> see the following:\n>\n>   ...\n>   $ git status\n>   fatal: not a git repository (or any of the parent directories): .git\n>   searched from '/home/user/.config/nvim' up to '/home/user/.config'\n>   $GIT_CEILING_DIRECTORIES is set to '/home/user'\n\nThe second line of the output gives a much better information than\nbefore.  Stepping back a bit, the \"(or any of the ...)\" was a weak\nand ineffective attempt to say that \".git\" we claim is not a valid\nrepository is not necessarily in the current directory.  But would\nit be still needed with the more explicit \"we looked for it from\nhere to there\"?  Can we combine both into a single statement somehow\nto make it more concise and understandable?\n\nHere is my attempt.  Would this\n\n    fatal: no valid repository \".git\" found in \"/home/user/.config/nvim\"\n    or any of its parent directories up to \"/home/user/.config\".\n\nor something like that be easier to read?\n\nOf course, such a change would break scripts that attempt to parse\nthis error message, so even if it is easier for humans, there could\nbe downside.\n\n> -\t\ttest_grep \"not a git repository (or any of the parent directories)\" stderr\n> +\t\ttest_grep \"not a git repository (or any of the parent directories)\" stderr &&\n> +\t\ttest_grep \"searched from .*ceil/sub.*up to.*ceil.\" stderr &&\n> +\t\ttest_grep \"GIT_CEILING_DIRECTORIES is set to\" stderr\n>  \t)\n>  '\n\n"},{"id":"556696","messageId":"xmqq4ie5aamt.fsf@gitster.g","threadId":"66383","inReplyTo":"20261028115412.169748-6-kaartic.sivaraam@gmail.com","subject":"Re: [PATCH v3 5/5] setup: report when GIT_OBJECT_DIRECTORY is what we could not use","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-28T19:27:38Z","receivedAt":"2026-10-28T19:27:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> Improve this error reporting UX by explaining the real cause wherever\n> we are about to die. Because the check does not depend on the candidate\n> that is given to is_git_directory(), it is enough to ask once, at the\n> point of failure, whether the configured object directory is to blame.\n> With this, the user would see the following:\n>\n>   ...\n>   $ git status\n>   fatal: not a git repository (or any parent up to mount point /)\n>   Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).\n>   cannot access object directory '/tmp/git-objects' set via\n>     $GIT_OBJECT_DIRECTORY: No such file or directory\n\nHmph, this is only a half of the real story isn't it?\n\n    $ git status ;# to show I am in a healthy repository\n    on branch master\n    nothing to commit (use -u to show untracked files)\n\nIn such a healthy repository, if I have a bogus DB_ENVIRONMENT,\ndiscovery does not have to fail.\n\n    $ GIT_OBJECT_DIRECTORY=/ git status\n    fatal: bad object HEAD\n\nOn the other hand, if I am truly in a bogus non-repository,\nit does not matter where DB_ENVIRONMENT points at.\n\n    $ (cd / && GIT_OBJECT_DIRECTORY=. git status)\n    fatal: not a git repository (or any of the parent directories): .git\n\nEven when DB_ENVIRONMENT points at a valid object directory\n\n    $ (cd / && GIT_OBJECT_DIRECTORY=$HOME/w/git.git/.git/objects git status)\n    fatal: not a git repository (or any of the parent directories): .git\n\nthe command fails because it cannot find a valid repository.\n\n> +static int describe_unusable_object_dir(struct strbuf *out)\n> +{\n> +\tconst char *objdir = getenv(DB_ENVIRONMENT);\n> +\tint saved_errno;\n> +\n> +\tif (!objdir || !access(objdir, X_OK))\n> +\t\treturn 0;\n> +\tsaved_errno = errno;\n> +\n> +\t/*\n> +\t * TRANSLATORS: The first %s is a path, the second %s is the\n> +\t * name of the environment variable that set it, and the third\n> +\t * %s is the system error message.\n> +\t */\n> +\tstrbuf_addf(out, _(\"cannot access object directory '%s' set via $%s: %s\"),\n> +\t\t    objdir, DB_ENVIRONMENT, strerror(saved_errno));\n> +\treturn 1;\n> +}\n\nAs this is called ONLY when we cannot figure out a usable GIT_DIR,\nwe do not have to worry about a case where $GIT_OBJECT_DIRECTORY is\nnot set and $GIT_DIR/objects/ is not readable.  So I do not think\nthis additional message hurts much, but I am not sure how much it\nwould help.  It would make the message more confusing in the \"no\nvalid repository, DB_ENVIRONMENT pointing at a healthy object\ndirectory\" situation in the above example.\n\n\n"},{"id":"556697","messageId":"xmqqwlr18w0p.fsf@gitster.g","threadId":"66383","inReplyTo":"20261028115412.169748-1-kaartic.sivaraam@gmail.com","subject":"Re: [PATCH v3 0/5] Improve error reporting to mention how a Git repository is discovered","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-28T19:28:38Z","receivedAt":"2026-10-28T19:28:38Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Kaartic Sivaraam <kaartic.sivaraam@gmail.com> writes:\n\n> At the moment, when Git gives up with \"fatal: not a git repository\",\n> it does not say which directory it actually inspected or where that\n> directory came from. This is especially confusing when a leftover\n> environment variable is steering Git somewhere the user did not\n> expect.\n\nI think [1/5]..[4/5] make sense but I am not sure about [5/5].\n\nThanks.\n\n"}]}