Skip to content

Commit b29759d

Browse files
pcloudsgitster
authored andcommitted
fsck: check HEAD and reflog from other worktrees
fsck is a repo-wide operation and should check all references no matter which worktree they are associated to. Reported-by: Jeff King <[email protected]> Helped-by: Elijah Newren <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a8c754d commit b29759d

File tree

2 files changed

+73
-17
lines changed

2 files changed

+73
-17
lines changed

builtin/fsck.c

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "packfile.h"
2020
#include "object-store.h"
2121
#include "run-command.h"
22+
#include "worktree.h"
2223

2324
#define REACHABLE 0x0001
2425
#define SEEN 0x0002
@@ -444,7 +445,11 @@ static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid
444445
static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
445446
int flag, void *cb_data)
446447
{
447-
for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
448+
struct strbuf refname = STRBUF_INIT;
449+
450+
strbuf_worktree_ref(cb_data, &refname, logname);
451+
for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf);
452+
strbuf_release(&refname);
448453
return 0;
449454
}
450455

@@ -482,20 +487,34 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
482487
return 0;
483488
}
484489

485-
static int fsck_head_link(const char **head_points_at,
490+
static int fsck_head_link(const char *head_ref_name,
491+
const char **head_points_at,
486492
struct object_id *head_oid);
487493

488494
static void get_default_heads(void)
489495
{
496+
struct worktree **worktrees, **p;
490497
const char *head_points_at;
491498
struct object_id head_oid;
492499

493-
fsck_head_link(&head_points_at, &head_oid);
494-
if (head_points_at && !is_null_oid(&head_oid))
495-
fsck_handle_ref("HEAD", &head_oid, 0, NULL);
496500
for_each_rawref(fsck_handle_ref, NULL);
497-
if (include_reflogs)
498-
for_each_reflog(fsck_handle_reflog, NULL);
501+
502+
worktrees = get_worktrees(0);
503+
for (p = worktrees; *p; p++) {
504+
struct worktree *wt = *p;
505+
struct strbuf ref = STRBUF_INIT;
506+
507+
strbuf_worktree_ref(wt, &ref, "HEAD");
508+
fsck_head_link(ref.buf, &head_points_at, &head_oid);
509+
if (head_points_at && !is_null_oid(&head_oid))
510+
fsck_handle_ref(ref.buf, &head_oid, 0, NULL);
511+
strbuf_release(&ref);
512+
513+
if (include_reflogs)
514+
refs_for_each_reflog(get_worktree_ref_store(wt),
515+
fsck_handle_reflog, wt);
516+
}
517+
free_worktrees(worktrees);
499518

500519
/*
501520
* Not having any default heads isn't really fatal, but
@@ -584,34 +603,36 @@ static void fsck_object_dir(const char *path)
584603
stop_progress(&progress);
585604
}
586605

587-
static int fsck_head_link(const char **head_points_at,
606+
static int fsck_head_link(const char *head_ref_name,
607+
const char **head_points_at,
588608
struct object_id *head_oid)
589609
{
590610
int null_is_error = 0;
591611

592612
if (verbose)
593-
fprintf(stderr, "Checking HEAD link\n");
613+
fprintf(stderr, "Checking %s link\n", head_ref_name);
594614

595-
*head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid, NULL);
615+
*head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL);
596616
if (!*head_points_at) {
597617
errors_found |= ERROR_REFS;
598-
return error("Invalid HEAD");
618+
return error("Invalid %s", head_ref_name);
599619
}
600-
if (!strcmp(*head_points_at, "HEAD"))
620+
if (!strcmp(*head_points_at, head_ref_name))
601621
/* detached HEAD */
602622
null_is_error = 1;
603623
else if (!starts_with(*head_points_at, "refs/heads/")) {
604624
errors_found |= ERROR_REFS;
605-
return error("HEAD points to something strange (%s)",
606-
*head_points_at);
625+
return error("%s points to something strange (%s)",
626+
head_ref_name, *head_points_at);
607627
}
608628
if (is_null_oid(head_oid)) {
609629
if (null_is_error) {
610630
errors_found |= ERROR_REFS;
611-
return error("HEAD: detached HEAD points at nothing");
631+
return error("%s: detached HEAD points at nothing",
632+
head_ref_name);
612633
}
613-
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
614-
*head_points_at + 11);
634+
fprintf(stderr, "notice: %s points to an unborn branch (%s)\n",
635+
head_ref_name, *head_points_at + 11);
615636
}
616637
return 0;
617638
}

t/t1450-fsck.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,41 @@ test_expect_success 'HEAD link pointing at a funny place' '
101101
grep "HEAD points to something strange" out
102102
'
103103

104+
test_expect_success 'HEAD link pointing at a funny object (from different wt)' '
105+
test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
106+
test_when_finished "rm -rf .git/worktrees wt" &&
107+
git worktree add wt &&
108+
mv .git/HEAD .git/SAVED_HEAD &&
109+
echo $ZERO_OID >.git/HEAD &&
110+
# avoid corrupt/broken HEAD from interfering with repo discovery
111+
test_must_fail git -C wt fsck 2>out &&
112+
grep "main-worktree/HEAD: detached HEAD points" out
113+
'
114+
115+
test_expect_success 'other worktree HEAD link pointing at a funny object' '
116+
test_when_finished "rm -rf .git/worktrees other" &&
117+
git worktree add other &&
118+
echo $ZERO_OID >.git/worktrees/other/HEAD &&
119+
test_must_fail git fsck 2>out &&
120+
grep "worktrees/other/HEAD: detached HEAD points" out
121+
'
122+
123+
test_expect_success 'other worktree HEAD link pointing at missing object' '
124+
test_when_finished "rm -rf .git/worktrees other" &&
125+
git worktree add other &&
126+
echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
127+
test_must_fail git fsck 2>out &&
128+
grep "worktrees/other/HEAD: invalid sha1 pointer" out
129+
'
130+
131+
test_expect_success 'other worktree HEAD link pointing at a funny place' '
132+
test_when_finished "rm -rf .git/worktrees other" &&
133+
git worktree add other &&
134+
echo "ref: refs/funny/place" >.git/worktrees/other/HEAD &&
135+
test_must_fail git fsck 2>out &&
136+
grep "worktrees/other/HEAD points to something strange" out
137+
'
138+
104139
test_expect_success 'email without @ is okay' '
105140
git cat-file commit HEAD >basis &&
106141
sed "s/@/AT/" basis >okay &&

0 commit comments

Comments
 (0)