Skip to content

Commit a8c754d

Browse files
newrengitster
authored andcommitted
fsck: move fsck_head_link() to get_default_heads() to avoid some globals
This will make it easier to check the HEAD of other worktrees from fsck. Signed-off-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 ab3e1f7 commit a8c754d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

builtin/fsck.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ static int check_strict;
3636
static int keep_cache_objects;
3737
static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
3838
static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
39-
static struct object_id head_oid;
40-
static const char *head_points_at;
4139
static int errors_found;
4240
static int write_lost_and_found;
4341
static int verbose;
@@ -484,8 +482,15 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
484482
return 0;
485483
}
486484

485+
static int fsck_head_link(const char **head_points_at,
486+
struct object_id *head_oid);
487+
487488
static void get_default_heads(void)
488489
{
490+
const char *head_points_at;
491+
struct object_id head_oid;
492+
493+
fsck_head_link(&head_points_at, &head_oid);
489494
if (head_points_at && !is_null_oid(&head_oid))
490495
fsck_handle_ref("HEAD", &head_oid, 0, NULL);
491496
for_each_rawref(fsck_handle_ref, NULL);
@@ -579,33 +584,34 @@ static void fsck_object_dir(const char *path)
579584
stop_progress(&progress);
580585
}
581586

582-
static int fsck_head_link(void)
587+
static int fsck_head_link(const char **head_points_at,
588+
struct object_id *head_oid)
583589
{
584590
int null_is_error = 0;
585591

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

589-
head_points_at = resolve_ref_unsafe("HEAD", 0, &head_oid, NULL);
590-
if (!head_points_at) {
595+
*head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid, NULL);
596+
if (!*head_points_at) {
591597
errors_found |= ERROR_REFS;
592598
return error("Invalid HEAD");
593599
}
594-
if (!strcmp(head_points_at, "HEAD"))
600+
if (!strcmp(*head_points_at, "HEAD"))
595601
/* detached HEAD */
596602
null_is_error = 1;
597-
else if (!starts_with(head_points_at, "refs/heads/")) {
603+
else if (!starts_with(*head_points_at, "refs/heads/")) {
598604
errors_found |= ERROR_REFS;
599605
return error("HEAD points to something strange (%s)",
600-
head_points_at);
606+
*head_points_at);
601607
}
602-
if (is_null_oid(&head_oid)) {
608+
if (is_null_oid(head_oid)) {
603609
if (null_is_error) {
604610
errors_found |= ERROR_REFS;
605611
return error("HEAD: detached HEAD points at nothing");
606612
}
607613
fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
608-
head_points_at + 11);
614+
*head_points_at + 11);
609615
}
610616
return 0;
611617
}
@@ -720,7 +726,6 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
720726

721727
git_config(fsck_config, NULL);
722728

723-
fsck_head_link();
724729
if (connectivity_only) {
725730
for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
726731
for_each_packed_object(mark_packed_for_connectivity, NULL, 0);

0 commit comments

Comments
 (0)