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 <peff@peff.net> Helped-by: Elijah Newren <newren@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Oct 21, 2018 at 10:08 UTC
b29759d89a688bf6d198786757111d690a08cbdf
2 files changed
+73
-17
builtin/fsck.c
+38
-17
@@ -19,6 +19,7 @@
19
#include "packfile.h"
20
#include "object-store.h"
21
#include "run-command.h"
22
+#include "worktree.h"
23
24
#define REACHABLE 0x0001
25
#define SEEN 0x0002
@@ -444,7 +445,11 @@ static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid
445
static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
446
int flag, void *cb_data)
447
{
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);
453
return 0;
454
}
455
@@ -482,20 +487,34 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
487
return 0;
488
}
489
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,
492
struct object_id *head_oid);
493
494
static void get_default_heads(void)
495
{
496
+ struct worktree **worktrees, **p;
497
const char *head_points_at;
498
struct object_id head_oid;
499
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);
500
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);
518
519
/*
520
* Not having any default heads isn't really fatal, but
@@ -584,34 +603,36 @@ static void fsck_object_dir(const char *path)
603
stop_progress(&progress);
604
}
605
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,
608
struct object_id *head_oid)
609
{
610
int null_is_error = 0;
611
612
if (verbose)
593
- fprintf(stderr, "Checking HEAD link\n");
613
+ fprintf(stderr, "Checking %s link\n", head_ref_name);
614
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);
616
if (!*head_points_at) {
617
errors_found |= ERROR_REFS;
598
- return error("Invalid HEAD");
618
+ return error("Invalid %s", head_ref_name);
619
}
600
- if (!strcmp(*head_points_at, "HEAD"))
620
+ if (!strcmp(*head_points_at, head_ref_name))
621
/* detached HEAD */
622
null_is_error = 1;
623
else if (!starts_with(*head_points_at, "refs/heads/")) {
624
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);
627
}
628
if (is_null_oid(head_oid)) {
629
if (null_is_error) {
630
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);
633
}
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);
636
}
637
return 0;
638
}
t/t1450-fsck.sh
+35
@@ -101,6 +101,41 @@ test_expect_success 'HEAD link pointing at a funny place' '
101
grep "HEAD points to something strange" out
102
'
103
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
+
139
test_expect_success 'email without @ is okay' '
140
git cat-file commit HEAD >basis &&
141
sed "s/@/AT/" basis >okay &&