Skip to content

Commit 82fc828

Browse files
malon7782gitster
authored andcommitted
setup: improve error diagnosis for invalid .git files
'read_gitfile_gently()' treats any non-regular file as 'READ_GITFILE_ERR_NOT_A_FILE' and fails to discern between 'ENOENT' and other stat failures. This flawed error reporting is noted by two 'NEEDSWORK' comments. Address these comments by introducing two new error codes: 'READ_GITFILE_ERR_STAT_ENOENT' and 'READ_GITFILE_ERR_IS_A_DIR'. To preserve the original intent of the setup process: 1. Update 'read_gitfile_error_die()' to treat 'IS_A_DIR' as a no-op (like 'ENOENT'), while still calling 'die()' on true 'NOT_A_FILE' errors. 2. Unconditionally pass '&error_code' to 'read_gitfile_gently()'. This eliminates an uninitialized variable hazard that occurred when 'die_on_error' was true and 'NULL' was passed. 3. Only invoke 'is_git_directory()' when we explicitly receive 'READ_GITFILE_ERR_IS_A_DIR', avoiding redundant filesystem checks. 4. Correctly return 'GIT_DIR_INVALID_GITFILE' on unrecognized errors when 'die_on_error' is false. Additionally, audit external callers of 'read_gitfile_gently()' in 'submodule.c' and 'worktree.c' to accommodate the refined error codes. Signed-off-by: Tian Yuchen <a3205153416@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 67ad421 commit 82fc828

File tree

6 files changed

+110
-15
lines changed

6 files changed

+110
-15
lines changed

setup.c

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,14 @@ int verify_repository_format(const struct repository_format *format,
895895
void read_gitfile_error_die(int error_code, const char *path, const char *dir)
896896
{
897897
switch (error_code) {
898-
case READ_GITFILE_ERR_STAT_FAILED:
899-
case READ_GITFILE_ERR_NOT_A_FILE:
898+
case READ_GITFILE_ERR_STAT_ENOENT:
899+
case READ_GITFILE_ERR_IS_A_DIR:
900900
/* non-fatal; follow return path */
901901
break;
902+
case READ_GITFILE_ERR_STAT_FAILED:
903+
die(_("error reading %s"), path);
904+
case READ_GITFILE_ERR_NOT_A_FILE:
905+
die(_("not a regular file: %s"), path);
902906
case READ_GITFILE_ERR_OPEN_FAILED:
903907
die_errno(_("error opening '%s'"), path);
904908
case READ_GITFILE_ERR_TOO_LARGE:
@@ -939,8 +943,14 @@ const char *read_gitfile_gently(const char *path, int *return_error_code)
939943
static struct strbuf realpath = STRBUF_INIT;
940944

941945
if (stat(path, &st)) {
942-
/* NEEDSWORK: discern between ENOENT vs other errors */
943-
error_code = READ_GITFILE_ERR_STAT_FAILED;
946+
if (errno == ENOENT)
947+
error_code = READ_GITFILE_ERR_STAT_ENOENT;
948+
else
949+
error_code = READ_GITFILE_ERR_STAT_FAILED;
950+
goto cleanup_return;
951+
}
952+
if (S_ISDIR(st.st_mode)) {
953+
error_code = READ_GITFILE_ERR_IS_A_DIR;
944954
goto cleanup_return;
945955
}
946956
if (!S_ISREG(st.st_mode)) {
@@ -1576,20 +1586,28 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
15761586
if (offset > min_offset)
15771587
strbuf_addch(dir, '/');
15781588
strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT);
1579-
gitdirenv = read_gitfile_gently(dir->buf, die_on_error ?
1580-
NULL : &error_code);
1589+
gitdirenv = read_gitfile_gently(dir->buf, &error_code);
15811590
if (!gitdirenv) {
1582-
if (die_on_error ||
1583-
error_code == READ_GITFILE_ERR_NOT_A_FILE) {
1584-
/* NEEDSWORK: fail if .git is not file nor dir */
1591+
switch (error_code) {
1592+
case READ_GITFILE_ERR_STAT_ENOENT:
1593+
/* no .git in this directory, move on */
1594+
break;
1595+
case READ_GITFILE_ERR_IS_A_DIR:
15851596
if (is_git_directory(dir->buf)) {
15861597
gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
15871598
gitdir_path = xstrdup(dir->buf);
15881599
}
1589-
} else if (error_code != READ_GITFILE_ERR_STAT_FAILED)
1590-
return GIT_DIR_INVALID_GITFILE;
1591-
} else
1600+
/* NEEDSWORK: should we catch a directory .git that is not a git directory here? */
1601+
break;
1602+
default:
1603+
if (die_on_error || error_code == READ_GITFILE_ERR_NOT_A_FILE)
1604+
read_gitfile_error_die(error_code, dir->buf, NULL);
1605+
else
1606+
return GIT_DIR_INVALID_GITFILE;
1607+
}
1608+
} else {
15921609
gitfile = xstrdup(dir->buf);
1610+
}
15931611
/*
15941612
* Earlier, we tentatively added DEFAULT_GIT_DIR_ENVIRONMENT
15951613
* to check that directory for a repository.

setup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ int is_nonbare_repository_dir(struct strbuf *path);
3636
#define READ_GITFILE_ERR_NO_PATH 6
3737
#define READ_GITFILE_ERR_NOT_A_REPO 7
3838
#define READ_GITFILE_ERR_TOO_LARGE 8
39+
#define READ_GITFILE_ERR_STAT_ENOENT 9
40+
#define READ_GITFILE_ERR_IS_A_DIR 10
3941
void read_gitfile_error_die(int error_code, const char *path, const char *dir);
4042
const char *read_gitfile_gently(const char *path, int *return_error_code);
4143
#define read_gitfile(path) read_gitfile_gently((path), NULL)

submodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2413,7 +2413,7 @@ void absorb_git_dir_into_superproject(const char *path,
24132413
const struct submodule *sub;
24142414
struct strbuf sub_gitdir = STRBUF_INIT;
24152415

2416-
if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2416+
if (err_code == READ_GITFILE_ERR_STAT_ENOENT) {
24172417
/* unpopulated as expected */
24182418
strbuf_release(&gitdir);
24192419
return;

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ integration_tests = [
8080
't0006-date.sh',
8181
't0007-git-var.sh',
8282
't0008-ignores.sh',
83+
't0009-git-dir-validation.sh',
8384
't0010-racy-git.sh',
8485
't0012-help.sh',
8586
't0013-sha1dc.sh',

t/t0009-git-dir-validation.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/sh
2+
3+
test_description='setup: validation of .git file/directory types
4+
5+
Verify that setup_git_directory() correctly handles:
6+
1. Valid .git directories (including symlinks to them).
7+
2. Invalid .git files (FIFOs, sockets) by erroring out.
8+
3. Invalid .git files (garbage) by erroring out.
9+
'
10+
11+
. ./test-lib.sh
12+
13+
test_expect_success 'setup: create parent git repository' '
14+
git init parent &&
15+
test_commit -C parent "root-commit"
16+
'
17+
18+
test_expect_success SYMLINKS 'setup: .git as a symlink to a directory is valid' '
19+
mkdir -p parent/link-to-dir &&
20+
(
21+
cd parent/link-to-dir &&
22+
git init real-repo &&
23+
ln -s real-repo/.git .git &&
24+
git rev-parse --git-dir >actual &&
25+
echo .git >expect &&
26+
test_cmp expect actual
27+
)
28+
'
29+
30+
test_expect_success PIPE 'setup: .git as a FIFO (named pipe) is rejected' '
31+
mkdir -p parent/fifo-trap &&
32+
(
33+
cd parent/fifo-trap &&
34+
mkfifo .git &&
35+
test_must_fail git rev-parse --git-dir 2>stderr &&
36+
grep "not a regular file" stderr
37+
)
38+
'
39+
40+
test_expect_success SYMLINKS,PIPE 'setup: .git as a symlink to a FIFO is rejected' '
41+
mkdir -p parent/symlink-fifo-trap &&
42+
(
43+
cd parent/symlink-fifo-trap &&
44+
mkfifo target-fifo &&
45+
ln -s target-fifo .git &&
46+
test_must_fail git rev-parse --git-dir 2>stderr &&
47+
grep "not a regular file" stderr
48+
)
49+
'
50+
51+
test_expect_success 'setup: .git with garbage content is rejected' '
52+
mkdir -p parent/garbage-trap &&
53+
(
54+
cd parent/garbage-trap &&
55+
echo "garbage" >.git &&
56+
test_must_fail git rev-parse --git-dir 2>stderr &&
57+
grep "invalid gitfile format" stderr
58+
)
59+
'
60+
61+
test_expect_success 'setup: .git as an empty directory is ignored' '
62+
mkdir -p parent/empty-dir &&
63+
(
64+
cd parent/empty-dir &&
65+
mkdir .git &&
66+
git rev-parse --git-dir >actual &&
67+
echo "$TRASH_DIRECTORY/parent/.git" >expect &&
68+
test_cmp expect actual
69+
)
70+
'
71+
72+
test_done

worktree.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,8 @@ static void repair_gitfile(struct worktree *wt,
653653
}
654654
}
655655

656-
if (err == READ_GITFILE_ERR_NOT_A_FILE)
656+
if (err == READ_GITFILE_ERR_NOT_A_FILE ||
657+
err == READ_GITFILE_ERR_IS_A_DIR)
657658
fn(1, wt->path, _(".git is not a file"), cb_data);
658659
else if (err)
659660
repair = _(".git file broken");
@@ -833,7 +834,8 @@ void repair_worktree_at_path(const char *path,
833834
strbuf_addstr(&backlink, dotgit_contents);
834835
strbuf_realpath_forgiving(&backlink, backlink.buf, 0);
835836
}
836-
} else if (err == READ_GITFILE_ERR_NOT_A_FILE) {
837+
} else if (err == READ_GITFILE_ERR_NOT_A_FILE ||
838+
err == READ_GITFILE_ERR_IS_A_DIR) {
837839
fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
838840
goto done;
839841
} else if (err == READ_GITFILE_ERR_NOT_A_REPO) {

0 commit comments

Comments
 (0)