Skip to content

Commit

Permalink
fsck: HEAD is part of refs
Browse files Browse the repository at this point in the history
By default we looked at all refs but not HEAD.  The only thing that made
fsck not lose sight of commits that are only reachable from a detached
HEAD was the reflog for the HEAD.

This fixes it, with a new test.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Jan 31, 2009
1 parent a34a9db commit 469e2eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 8 additions & 5 deletions builtin-fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static int check_full;
static int check_strict;
static int keep_cache_objects;
static unsigned char head_sha1[20];
static const char *head_points_at;
static int errors_found;
static int write_lost_and_found;
static int verbose;
Expand Down Expand Up @@ -473,6 +474,8 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f

static void get_default_heads(void)
{
if (head_points_at && !is_null_sha1(head_sha1))
fsck_handle_ref("HEAD", head_sha1, 0, NULL);
for_each_ref(fsck_handle_ref, NULL);
if (include_reflogs)
for_each_reflog(fsck_handle_reflog, NULL);
Expand Down Expand Up @@ -512,14 +515,13 @@ static void fsck_object_dir(const char *path)

static int fsck_head_link(void)
{
unsigned char sha1[20];
int flag;
int null_is_error = 0;
const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);

if (verbose)
fprintf(stderr, "Checking HEAD link\n");

head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
if (!head_points_at)
return error("Invalid HEAD");
if (!strcmp(head_points_at, "HEAD"))
Expand All @@ -528,7 +530,7 @@ static int fsck_head_link(void)
else if (prefixcmp(head_points_at, "refs/heads/"))
return error("HEAD points to something strange (%s)",
head_points_at);
if (is_null_sha1(sha1)) {
if (is_null_sha1(head_sha1)) {
if (null_is_error)
return error("HEAD: detached HEAD points at nothing");
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
Expand Down Expand Up @@ -624,8 +626,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
heads = 0;
for (i = 0; i < argc; i++) {
const char *arg = argv[i];
if (!get_sha1(arg, head_sha1)) {
struct object *obj = lookup_object(head_sha1);
unsigned char sha1[20];
if (!get_sha1(arg, sha1)) {
struct object *obj = lookup_object(sha1);

/* Error is printed by lookup_object(). */
if (!obj)
Expand Down
19 changes: 19 additions & 0 deletions t/t1450-fsck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

test_description='git fsck random collection of tests'

. ./test-lib.sh

test_expect_success setup '
test_commit A fileA one &&
git checkout HEAD^0 &&
test_commit B fileB two &&
git tag -d A B &&
git reflog expire --expire=now --all
'

test_expect_success 'HEAD is part of refs' '
test 0 = $(git fsck | wc -l)
'

test_done

0 comments on commit 469e2eb

Please sign in to comment.