Skip to content

Commit 1c7dc12

Browse files
committed
Merge branch 'jc/fsck-dropped-errors' into maint
There were some classes of errors that "git fsck" diagnosed to its standard error that did not cause it to exit with non-zero status. * jc/fsck-dropped-errors: fsck: exit with non-zero when problems are found
2 parents 8f6f177 + 122f76f commit 1c7dc12

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

builtin/fsck.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static int show_dangling = 1;
3838
#define ERROR_OBJECT 01
3939
#define ERROR_REACHABLE 02
4040
#define ERROR_PACK 04
41+
#define ERROR_REFS 010
4142

4243
#ifdef NO_D_INO_IN_DIRENT
4344
#define SORT_DIRENT 0
@@ -521,8 +522,10 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
521522
/* We'll continue with the rest despite the error.. */
522523
return 0;
523524
}
524-
if (obj->type != OBJ_COMMIT && is_branch(refname))
525+
if (obj->type != OBJ_COMMIT && is_branch(refname)) {
525526
error("%s: not a commit", refname);
527+
errors_found |= ERROR_REFS;
528+
}
526529
default_refs++;
527530
obj->used = 1;
528531
mark_object_reachable(obj);
@@ -585,17 +588,23 @@ static int fsck_head_link(void)
585588
fprintf(stderr, "Checking HEAD link\n");
586589

587590
head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
588-
if (!head_points_at)
591+
if (!head_points_at) {
592+
errors_found |= ERROR_REFS;
589593
return error("Invalid HEAD");
594+
}
590595
if (!strcmp(head_points_at, "HEAD"))
591596
/* detached HEAD */
592597
null_is_error = 1;
593-
else if (!starts_with(head_points_at, "refs/heads/"))
598+
else if (!starts_with(head_points_at, "refs/heads/")) {
599+
errors_found |= ERROR_REFS;
594600
return error("HEAD points to something strange (%s)",
595601
head_points_at);
602+
}
596603
if (is_null_oid(&head_oid)) {
597-
if (null_is_error)
604+
if (null_is_error) {
605+
errors_found |= ERROR_REFS;
598606
return error("HEAD: detached HEAD points at nothing");
607+
}
599608
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
600609
head_points_at + 11);
601610
}
@@ -615,6 +624,7 @@ static int fsck_cache_tree(struct cache_tree *it)
615624
if (!obj) {
616625
error("%s: invalid sha1 pointer in cache-tree",
617626
sha1_to_hex(it->sha1));
627+
errors_found |= ERROR_REFS;
618628
return 1;
619629
}
620630
obj->used = 1;

t/t1450-fsck.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,31 @@ test_expect_success 'object with bad sha1' '
7777
test_expect_success 'branch pointing to non-commit' '
7878
git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
7979
test_when_finished "git update-ref -d refs/heads/invalid" &&
80-
git fsck 2>out &&
80+
test_must_fail git fsck 2>out &&
8181
cat out &&
8282
grep "not a commit" out
8383
'
8484

85+
test_expect_success 'HEAD link pointing at a funny object' '
86+
test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
87+
mv .git/HEAD .git/SAVED_HEAD &&
88+
echo 0000000000000000000000000000000000000000 >.git/HEAD &&
89+
# avoid corrupt/broken HEAD from interfering with repo discovery
90+
test_must_fail env GIT_DIR=.git git fsck 2>out &&
91+
cat out &&
92+
grep "detached HEAD points" out
93+
'
94+
95+
test_expect_success 'HEAD link pointing at a funny place' '
96+
test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
97+
mv .git/HEAD .git/SAVED_HEAD &&
98+
echo "ref: refs/funny/place" >.git/HEAD &&
99+
# avoid corrupt/broken HEAD from interfering with repo discovery
100+
test_must_fail env GIT_DIR=.git git fsck 2>out &&
101+
cat out &&
102+
grep "HEAD points to something strange" out
103+
'
104+
85105
test_expect_success 'email without @ is okay' '
86106
git cat-file commit HEAD >basis &&
87107
sed "s/@/AT/" basis >okay &&

0 commit comments

Comments
 (0)