Skip to content

Commit 88ccb9f

Browse files
committed
Merge branch 'jc/fsck' (early part)
* 'jc/fsck' (early part): fsck: check loose objects from alternate object stores by default fsck: HEAD is part of refs
2 parents 8aa7eeb + e15ef66 commit 88ccb9f

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

builtin-fsck.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ static int check_full;
2323
static int check_strict;
2424
static int keep_cache_objects;
2525
static unsigned char head_sha1[20];
26+
static const char *head_points_at;
2627
static int errors_found;
2728
static int write_lost_and_found;
2829
static int verbose;
@@ -473,6 +474,8 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
473474

474475
static void get_default_heads(void)
475476
{
477+
if (head_points_at && !is_null_sha1(head_sha1))
478+
fsck_handle_ref("HEAD", head_sha1, 0, NULL);
476479
for_each_ref(fsck_handle_ref, NULL);
477480
if (include_reflogs)
478481
for_each_reflog(fsck_handle_reflog, NULL);
@@ -512,14 +515,13 @@ static void fsck_object_dir(const char *path)
512515

513516
static int fsck_head_link(void)
514517
{
515-
unsigned char sha1[20];
516518
int flag;
517519
int null_is_error = 0;
518-
const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
519520

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

524+
head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
523525
if (!head_points_at)
524526
return error("Invalid HEAD");
525527
if (!strcmp(head_points_at, "HEAD"))
@@ -528,7 +530,7 @@ static int fsck_head_link(void)
528530
else if (prefixcmp(head_points_at, "refs/heads/"))
529531
return error("HEAD points to something strange (%s)",
530532
head_points_at);
531-
if (is_null_sha1(sha1)) {
533+
if (is_null_sha1(head_sha1)) {
532534
if (null_is_error)
533535
return error("HEAD: detached HEAD points at nothing");
534536
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
@@ -584,6 +586,7 @@ static struct option fsck_opts[] = {
584586
int cmd_fsck(int argc, const char **argv, const char *prefix)
585587
{
586588
int i, heads;
589+
struct alternate_object_database *alt;
587590

588591
errors_found = 0;
589592

@@ -595,17 +598,19 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
595598

596599
fsck_head_link();
597600
fsck_object_dir(get_object_directory());
601+
602+
prepare_alt_odb();
603+
for (alt = alt_odb_list; alt; alt = alt->next) {
604+
char namebuf[PATH_MAX];
605+
int namelen = alt->name - alt->base;
606+
memcpy(namebuf, alt->base, namelen);
607+
namebuf[namelen - 1] = 0;
608+
fsck_object_dir(namebuf);
609+
}
610+
598611
if (check_full) {
599-
struct alternate_object_database *alt;
600612
struct packed_git *p;
601-
prepare_alt_odb();
602-
for (alt = alt_odb_list; alt; alt = alt->next) {
603-
char namebuf[PATH_MAX];
604-
int namelen = alt->name - alt->base;
605-
memcpy(namebuf, alt->base, namelen);
606-
namebuf[namelen - 1] = 0;
607-
fsck_object_dir(namebuf);
608-
}
613+
609614
prepare_packed_git();
610615
for (p = packed_git; p; p = p->next)
611616
/* verify gives error messages itself */
@@ -624,8 +629,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
624629
heads = 0;
625630
for (i = 0; i < argc; i++) {
626631
const char *arg = argv[i];
627-
if (!get_sha1(arg, head_sha1)) {
628-
struct object *obj = lookup_object(head_sha1);
632+
unsigned char sha1[20];
633+
if (!get_sha1(arg, sha1)) {
634+
struct object *obj = lookup_object(sha1);
629635

630636
/* Error is printed by lookup_object(). */
631637
if (!obj)

t/t1450-fsck.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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_expect_success 'loose objects borrowed from alternate are not missing' '
20+
mkdir another &&
21+
(
22+
cd another &&
23+
git init &&
24+
echo ../../../.git/objects >.git/objects/info/alternates &&
25+
test_commit C fileC one &&
26+
git fsck >out &&
27+
! grep "missing blob" out
28+
)
29+
'
30+
31+
test_done

0 commit comments

Comments
 (0)