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>
This commit is contained in:
		@ -35,6 +35,7 @@ static int show_dangling = 1;
 | 
				
			|||||||
#define ERROR_OBJECT 01
 | 
					#define ERROR_OBJECT 01
 | 
				
			||||||
#define ERROR_REACHABLE 02
 | 
					#define ERROR_REACHABLE 02
 | 
				
			||||||
#define ERROR_PACK 04
 | 
					#define ERROR_PACK 04
 | 
				
			||||||
 | 
					#define ERROR_REFS 010
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef NO_D_INO_IN_DIRENT
 | 
					#ifdef NO_D_INO_IN_DIRENT
 | 
				
			||||||
#define SORT_DIRENT 0
 | 
					#define SORT_DIRENT 0
 | 
				
			||||||
@ -495,8 +496,10 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
 | 
				
			|||||||
		/* We'll continue with the rest despite the error.. */
 | 
							/* We'll continue with the rest despite the error.. */
 | 
				
			||||||
		return 0;
 | 
							return 0;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (obj->type != OBJ_COMMIT && is_branch(refname))
 | 
						if (obj->type != OBJ_COMMIT && is_branch(refname)) {
 | 
				
			||||||
		error("%s: not a commit", refname);
 | 
							error("%s: not a commit", refname);
 | 
				
			||||||
 | 
							errors_found |= ERROR_REFS;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	default_refs++;
 | 
						default_refs++;
 | 
				
			||||||
	obj->used = 1;
 | 
						obj->used = 1;
 | 
				
			||||||
	mark_object_reachable(obj);
 | 
						mark_object_reachable(obj);
 | 
				
			||||||
@ -559,17 +562,23 @@ static int fsck_head_link(void)
 | 
				
			|||||||
		fprintf(stderr, "Checking HEAD link\n");
 | 
							fprintf(stderr, "Checking HEAD link\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
 | 
						head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
 | 
				
			||||||
	if (!head_points_at)
 | 
						if (!head_points_at) {
 | 
				
			||||||
 | 
							errors_found |= ERROR_REFS;
 | 
				
			||||||
		return error("Invalid HEAD");
 | 
							return error("Invalid HEAD");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if (!strcmp(head_points_at, "HEAD"))
 | 
						if (!strcmp(head_points_at, "HEAD"))
 | 
				
			||||||
		/* detached HEAD */
 | 
							/* detached HEAD */
 | 
				
			||||||
		null_is_error = 1;
 | 
							null_is_error = 1;
 | 
				
			||||||
	else if (!starts_with(head_points_at, "refs/heads/"))
 | 
						else if (!starts_with(head_points_at, "refs/heads/")) {
 | 
				
			||||||
 | 
							errors_found |= ERROR_REFS;
 | 
				
			||||||
		return error("HEAD points to something strange (%s)",
 | 
							return error("HEAD points to something strange (%s)",
 | 
				
			||||||
			     head_points_at);
 | 
								     head_points_at);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if (is_null_oid(&head_oid)) {
 | 
						if (is_null_oid(&head_oid)) {
 | 
				
			||||||
		if (null_is_error)
 | 
							if (null_is_error) {
 | 
				
			||||||
 | 
								errors_found |= ERROR_REFS;
 | 
				
			||||||
			return error("HEAD: detached HEAD points at nothing");
 | 
								return error("HEAD: detached HEAD points at nothing");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
 | 
							fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
 | 
				
			||||||
			head_points_at + 11);
 | 
								head_points_at + 11);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -589,6 +598,7 @@ static int fsck_cache_tree(struct cache_tree *it)
 | 
				
			|||||||
		if (!obj) {
 | 
							if (!obj) {
 | 
				
			||||||
			error("%s: invalid sha1 pointer in cache-tree",
 | 
								error("%s: invalid sha1 pointer in cache-tree",
 | 
				
			||||||
			      sha1_to_hex(it->sha1));
 | 
								      sha1_to_hex(it->sha1));
 | 
				
			||||||
 | 
								errors_found |= ERROR_REFS;
 | 
				
			||||||
			return 1;
 | 
								return 1;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		obj->used = 1;
 | 
							obj->used = 1;
 | 
				
			||||||
 | 
				
			|||||||
@ -77,11 +77,31 @@ test_expect_success 'object with bad sha1' '
 | 
				
			|||||||
test_expect_success 'branch pointing to non-commit' '
 | 
					test_expect_success 'branch pointing to non-commit' '
 | 
				
			||||||
	git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
 | 
						git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
 | 
				
			||||||
	test_when_finished "git update-ref -d refs/heads/invalid" &&
 | 
						test_when_finished "git update-ref -d refs/heads/invalid" &&
 | 
				
			||||||
	git fsck 2>out &&
 | 
						test_must_fail git fsck 2>out &&
 | 
				
			||||||
	cat out &&
 | 
						cat out &&
 | 
				
			||||||
	grep "not a commit" out
 | 
						grep "not a commit" out
 | 
				
			||||||
'
 | 
					'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_expect_success 'HEAD link pointing at a funny object' '
 | 
				
			||||||
 | 
						test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
 | 
				
			||||||
 | 
						mv .git/HEAD .git/SAVED_HEAD &&
 | 
				
			||||||
 | 
						echo 0000000000000000000000000000000000000000 >.git/HEAD &&
 | 
				
			||||||
 | 
						# avoid corrupt/broken HEAD from interfering with repo discovery
 | 
				
			||||||
 | 
						test_must_fail env GIT_DIR=.git git fsck 2>out &&
 | 
				
			||||||
 | 
						cat out &&
 | 
				
			||||||
 | 
						grep "detached HEAD points" out
 | 
				
			||||||
 | 
					'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test_expect_success 'HEAD link pointing at a funny place' '
 | 
				
			||||||
 | 
						test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
 | 
				
			||||||
 | 
						mv .git/HEAD .git/SAVED_HEAD &&
 | 
				
			||||||
 | 
						echo "ref: refs/funny/place" >.git/HEAD &&
 | 
				
			||||||
 | 
						# avoid corrupt/broken HEAD from interfering with repo discovery
 | 
				
			||||||
 | 
						test_must_fail env GIT_DIR=.git git fsck 2>out &&
 | 
				
			||||||
 | 
						cat out &&
 | 
				
			||||||
 | 
						grep "HEAD points to something strange" out
 | 
				
			||||||
 | 
					'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test_expect_success 'email without @ is okay' '
 | 
					test_expect_success 'email without @ is okay' '
 | 
				
			||||||
	git cat-file commit HEAD >basis &&
 | 
						git cat-file commit HEAD >basis &&
 | 
				
			||||||
	sed "s/@/AT/" basis >okay &&
 | 
						sed "s/@/AT/" basis >okay &&
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user