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 <gitster@pobox.com>

Junio C Hamano committed Sep 23, 2015 at 13:46 UTC 122f76f574ce260429bfbd11251eed15039e3469
2 files changed +35 -5
builtin/fsck.c
+14 -4
@@ -35,6 +35,7 @@ static int show_dangling = 1;
35 #define ERROR_OBJECT 01
36 #define ERROR_REACHABLE 02
37 #define ERROR_PACK 04
38 +#define ERROR_REFS 010
39
40 #ifdef NO_D_INO_IN_DIRENT
41 #define SORT_DIRENT 0
@@ -495,8 +496,10 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
496 /* We'll continue with the rest despite the error.. */
497 return 0;
498 }
498 - if (obj->type != OBJ_COMMIT && is_branch(refname))
499 + if (obj->type != OBJ_COMMIT && is_branch(refname)) {
500 error("%s: not a commit", refname);
501 + errors_found |= ERROR_REFS;
502 + }
503 default_refs++;
504 obj->used = 1;
505 mark_object_reachable(obj);
@@ -559,17 +562,23 @@ static int fsck_head_link(void)
562 fprintf(stderr, "Checking HEAD link\n");
563
564 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;
567 return error("Invalid HEAD");
568 + }
569 if (!strcmp(head_points_at, "HEAD"))
570 /* detached HEAD */
571 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;
574 return error("HEAD points to something strange (%s)",
575 head_points_at);
576 + }
577 if (is_null_oid(&head_oid)) {
571 - if (null_is_error)
578 + if (null_is_error) {
579 + errors_found |= ERROR_REFS;
580 return error("HEAD: detached HEAD points at nothing");
581 + }
582 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
583 head_points_at + 11);
584 }
@@ -589,6 +598,7 @@ static int fsck_cache_tree(struct cache_tree *it)
598 if (!obj) {
599 error("%s: invalid sha1 pointer in cache-tree",
600 sha1_to_hex(it->sha1));
601 + errors_found |= ERROR_REFS;
602 return 1;
603 }
604 obj->used = 1;
t/t1450-fsck.sh
+21 -1
@@ -77,11 +77,31 @@ test_expect_success 'object with bad sha1' '
77 test_expect_success 'branch pointing to non-commit' '
78 git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
79 test_when_finished "git update-ref -d refs/heads/invalid" &&
80 - git fsck 2>out &&
80 + test_must_fail git fsck 2>out &&
81 cat out &&
82 grep "not a commit" out
83 '
84
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 +
105 test_expect_success 'email without @ is okay' '
106 git cat-file commit HEAD >basis &&
107 sed "s/@/AT/" basis >okay &&