Skip to content

Commit 122f76f

Browse files
committed
fsck: exit with non-zero when problems are found
After finding some problems (e.g. a ref refs/heads/X points at an object that is not a commit) and issuing an error message, the program failed to signal the fact that it found an error by a non-zero exit status. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5455ee0 commit 122f76f

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
@@ -35,6 +35,7 @@ static int show_dangling = 1;
3535
#define ERROR_OBJECT 01
3636
#define ERROR_REACHABLE 02
3737
#define ERROR_PACK 04
38+
#define ERROR_REFS 010
3839

3940
#ifdef NO_D_INO_IN_DIRENT
4041
#define SORT_DIRENT 0
@@ -495,8 +496,10 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
495496
/* We'll continue with the rest despite the error.. */
496497
return 0;
497498
}
498-
if (obj->type != OBJ_COMMIT && is_branch(refname))
499+
if (obj->type != OBJ_COMMIT && is_branch(refname)) {
499500
error("%s: not a commit", refname);
501+
errors_found |= ERROR_REFS;
502+
}
500503
default_refs++;
501504
obj->used = 1;
502505
mark_object_reachable(obj);
@@ -559,17 +562,23 @@ static int fsck_head_link(void)
559562
fprintf(stderr, "Checking HEAD link\n");
560563

561564
head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
562-
if (!head_points_at)
565+
if (!head_points_at) {
566+
errors_found |= ERROR_REFS;
563567
return error("Invalid HEAD");
568+
}
564569
if (!strcmp(head_points_at, "HEAD"))
565570
/* detached HEAD */
566571
null_is_error = 1;
567-
else if (!starts_with(head_points_at, "refs/heads/"))
572+
else if (!starts_with(head_points_at, "refs/heads/")) {
573+
errors_found |= ERROR_REFS;
568574
return error("HEAD points to something strange (%s)",
569575
head_points_at);
576+
}
570577
if (is_null_oid(&head_oid)) {
571-
if (null_is_error)
578+
if (null_is_error) {
579+
errors_found |= ERROR_REFS;
572580
return error("HEAD: detached HEAD points at nothing");
581+
}
573582
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
574583
head_points_at + 11);
575584
}
@@ -589,6 +598,7 @@ static int fsck_cache_tree(struct cache_tree *it)
589598
if (!obj) {
590599
error("%s: invalid sha1 pointer in cache-tree",
591600
sha1_to_hex(it->sha1));
601+
errors_found |= ERROR_REFS;
592602
return 1;
593603
}
594604
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)