fsck: HEAD is part of refs
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>
Junio C Hamano committed
Jan 30, 2009 at 00:33 UTC
469e2ebf63cbf05455365aa3ed6d43acf6f4e496
2 files changed
+27
-5
builtin-fsck.c
+8
-5
@@ -23,6 +23,7 @@ static int check_full;
23
static int check_strict;
24
static int keep_cache_objects;
25
static unsigned char head_sha1[20];
26
+static const char *head_points_at;
27
static int errors_found;
28
static int write_lost_and_found;
29
static int verbose;
@@ -473,6 +474,8 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
474
475
static void get_default_heads(void)
476
{
477
+ if (head_points_at && !is_null_sha1(head_sha1))
478
+ fsck_handle_ref("HEAD", head_sha1, 0, NULL);
479
for_each_ref(fsck_handle_ref, NULL);
480
if (include_reflogs)
481
for_each_reflog(fsck_handle_reflog, NULL);
@@ -512,14 +515,13 @@ static void fsck_object_dir(const char *path)
515
516
static int fsck_head_link(void)
517
{
515
- unsigned char sha1[20];
518
int flag;
519
int null_is_error = 0;
518
- const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
520
521
if (verbose)
522
fprintf(stderr, "Checking HEAD link\n");
523
524
+ head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
525
if (!head_points_at)
526
return error("Invalid HEAD");
527
if (!strcmp(head_points_at, "HEAD"))
@@ -528,7 +530,7 @@ static int fsck_head_link(void)
530
else if (prefixcmp(head_points_at, "refs/heads/"))
531
return error("HEAD points to something strange (%s)",
532
head_points_at);
531
- if (is_null_sha1(sha1)) {
533
+ if (is_null_sha1(head_sha1)) {
534
if (null_is_error)
535
return error("HEAD: detached HEAD points at nothing");
536
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
@@ -624,8 +626,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
626
heads = 0;
627
for (i = 0; i < argc; i++) {
628
const char *arg = argv[i];
627
- if (!get_sha1(arg, head_sha1)) {
628
- struct object *obj = lookup_object(head_sha1);
629
+ unsigned char sha1[20];
630
+ if (!get_sha1(arg, sha1)) {
631
+ struct object *obj = lookup_object(sha1);
632
633
/* Error is printed by lookup_object(). */
634
if (!obj)
t/t1450-fsck.sh
new
+19
@@ -0,0 +1,19 @@
1
+#!/bin/sh
2
+
3
+test_description='git fsck random collection of tests'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success setup '
8
+ test_commit A fileA one &&
9
+ git checkout HEAD^0 &&
10
+ test_commit B fileB two &&
11
+ git tag -d A B &&
12
+ git reflog expire --expire=now --all
13
+'
14
+
15
+test_expect_success 'HEAD is part of refs' '
16
+ test 0 = $(git fsck | wc -l)
17
+'
18
+
19
+test_done