fsck: exit with non-zero status upon errors
git-fsck always exited with status 0, which was a bit sloppy. This makes it exit with a non-zero status when errors are found. The error code is an OR'ed result of: 1 if corrupted objects are found. 2 if objects that are ought to be reachable are missing or corrupt. For example, it would exit with 1 in a repository with an unreachable corrupt object. If a tree object of the HEAD commit is corrupt, you would get 3. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Mar 5, 2007 at 00:22 UTC
e2b4f63512357d83f1f11bafadb22063e5d56621
1 file changed
+21
-5
builtin-fsck.c
+21
-5
@@ -18,6 +18,9 @@ static int check_full;
18
static int check_strict;
19
static int keep_cache_objects;
20
static unsigned char head_sha1[20];
21
+static int errors_found;
22
+#define ERROR_OBJECT 01
23
+#define ERROR_REACHABLE 02
24
25
#ifdef NO_D_INO_IN_DIRENT
26
#define SORT_DIRENT 0
@@ -40,6 +43,7 @@ static int objerror(struct object *obj, const char *err, ...)
43
{
44
va_list params;
45
va_start(params, err);
46
+ errors_found |= ERROR_OBJECT;
47
objreport(obj, "error", err, params);
48
va_end(params);
49
return -1;
@@ -70,6 +74,7 @@ static void check_reachable_object(struct object *obj)
74
if (has_sha1_pack(obj->sha1, NULL))
75
return; /* it is in pack - forget about it */
76
printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
77
+ errors_found |= ERROR_REACHABLE;
78
return;
79
}
80
@@ -88,6 +93,7 @@ static void check_reachable_object(struct object *obj)
93
typename(obj->type), sha1_to_hex(obj->sha1));
94
printf(" to %7s %s\n",
95
typename(ref->type), sha1_to_hex(ref->sha1));
96
+ errors_found |= ERROR_REACHABLE;
97
}
98
}
99
}
@@ -346,8 +352,11 @@ static int fsck_tag(struct tag *tag)
352
static int fsck_sha1(unsigned char *sha1)
353
{
354
struct object *obj = parse_object(sha1);
349
- if (!obj)
350
- return error("%s: object corrupt or missing", sha1_to_hex(sha1));
355
+ if (!obj) {
356
+ errors_found |= ERROR_OBJECT;
357
+ return error("%s: object corrupt or missing",
358
+ sha1_to_hex(sha1));
359
+ }
360
if (obj->flags & SEEN)
361
return 0;
362
obj->flags |= SEEN;
@@ -359,8 +368,10 @@ static int fsck_sha1(unsigned char *sha1)
368
return fsck_commit((struct commit *) obj);
369
if (obj->type == OBJ_TAG)
370
return fsck_tag((struct tag *) obj);
371
+
372
/* By now, parse_object() would've returned NULL instead. */
363
- return objerror(obj, "unknown type '%d' (internal fsck error)", obj->type);
373
+ return objerror(obj, "unknown type '%d' (internal fsck error)",
374
+ obj->type);
375
}
376
377
/*
@@ -576,11 +587,16 @@ static int fsck_cache_tree(struct cache_tree *it)
587
return err;
588
}
589
590
+static const char fsck_usage[] =
591
+"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
592
+"[--strict] <head-sha1>*]";
593
+
594
int cmd_fsck(int argc, char **argv, const char *prefix)
595
{
596
int i, heads;
597
598
track_object_refs = 1;
599
+ errors_found = 0;
600
601
for (i = 1; i < argc; i++) {
602
const char *arg = argv[i];
@@ -610,7 +626,7 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
626
continue;
627
}
628
if (*arg == '-')
613
- usage("git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]");
629
+ usage(fsck_usage);
630
}
631
632
fsck_head_link();
@@ -690,5 +706,5 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
706
}
707
708
check_connectivity();
693
- return 0;
709
+ return errors_found;
710
}