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 <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>

Elijah Newren committed Oct 21, 2018 at 10:08 UTC a8c754d4e2ced5439c23e8cf4636c24749ea1ddc
1 file changed +16 -11
builtin/fsck.c
+16 -11
@@ -36,8 +36,6 @@ static int check_strict;
36 static int keep_cache_objects;
37 static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT;
38 static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT;
39 -static struct object_id head_oid;
40 -static const char *head_points_at;
39 static int errors_found;
40 static int write_lost_and_found;
41 static int verbose;
@@ -484,8 +482,15 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
482 return 0;
483 }
484
485 +static int fsck_head_link(const char **head_points_at,
486 + struct object_id *head_oid);
487 +
488 static void get_default_heads(void)
489 {
490 + const char *head_points_at;
491 + struct object_id head_oid;
492 +
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);
496 for_each_rawref(fsck_handle_ref, NULL);
@@ -579,33 +584,34 @@ static void fsck_object_dir(const char *path)
584 stop_progress(&progress);
585 }
586
582 -static int fsck_head_link(void)
587 +static int fsck_head_link(const char **head_points_at,
588 + struct object_id *head_oid)
589 {
590 int null_is_error = 0;
591
592 if (verbose)
593 fprintf(stderr, "Checking HEAD link\n");
594
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) {
597 errors_found |= ERROR_REFS;
598 return error("Invalid HEAD");
599 }
594 - if (!strcmp(head_points_at, "HEAD"))
600 + if (!strcmp(*head_points_at, "HEAD"))
601 /* detached HEAD */
602 null_is_error = 1;
597 - else if (!starts_with(head_points_at, "refs/heads/")) {
603 + else if (!starts_with(*head_points_at, "refs/heads/")) {
604 errors_found |= ERROR_REFS;
605 return error("HEAD points to something strange (%s)",
600 - head_points_at);
606 + *head_points_at);
607 }
602 - if (is_null_oid(&head_oid)) {
608 + if (is_null_oid(head_oid)) {
609 if (null_is_error) {
610 errors_found |= ERROR_REFS;
611 return error("HEAD: detached HEAD points at nothing");
612 }
613 fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",
608 - head_points_at + 11);
614 + *head_points_at + 11);
615 }
616 return 0;
617 }
@@ -720,7 +726,6 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
726
727 git_config(fsck_config, NULL);
728
723 - fsck_head_link();
729 if (connectivity_only) {
730 for_each_loose_object(mark_loose_for_connectivity, NULL, 0);
731 for_each_packed_object(mark_packed_for_connectivity, NULL, 0);