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
@@ -36,8 +37,6 @@ static int check_strict;
3637static int keep_cache_objects ;
3738static struct fsck_options fsck_walk_options = FSCK_OPTIONS_DEFAULT ;
3839static struct fsck_options fsck_obj_options = FSCK_OPTIONS_DEFAULT ;
39- static struct object_id head_oid ;
40- static const char * head_points_at ;
4140static int errors_found ;
4241static int write_lost_and_found ;
4342static int verbose ;
@@ -446,7 +445,11 @@ static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid
446445static int fsck_handle_reflog (const char * logname , const struct object_id * oid ,
447446 int flag , void * cb_data )
448447{
449- 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 );
450453 return 0 ;
451454}
452455
@@ -484,13 +487,34 @@ static int fsck_handle_ref(const char *refname, const struct object_id *oid,
484487 return 0 ;
485488}
486489
490+ static int fsck_head_link (const char * head_ref_name ,
491+ const char * * head_points_at ,
492+ struct object_id * head_oid );
493+
487494static void get_default_heads (void )
488495{
489- if (head_points_at && !is_null_oid (& head_oid ))
490- fsck_handle_ref ("HEAD" , & head_oid , 0 , NULL );
496+ struct worktree * * worktrees , * * p ;
497+ const char * head_points_at ;
498+ struct object_id head_oid ;
499+
491500 for_each_rawref (fsck_handle_ref , NULL );
492- if (include_reflogs )
493- 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 );
494518
495519 /*
496520 * Not having any default heads isn't really fatal, but
@@ -579,33 +603,36 @@ static void fsck_object_dir(const char *path)
579603 stop_progress (& progress );
580604}
581605
582- static int fsck_head_link (void )
606+ static int fsck_head_link (const char * head_ref_name ,
607+ const char * * head_points_at ,
608+ struct object_id * head_oid )
583609{
584610 int null_is_error = 0 ;
585611
586612 if (verbose )
587- fprintf (stderr , "Checking HEAD link\n" );
613+ fprintf (stderr , "Checking %s link\n" , head_ref_name );
588614
589- head_points_at = resolve_ref_unsafe ("HEAD" , 0 , & head_oid , NULL );
590- if (!head_points_at ) {
615+ * head_points_at = resolve_ref_unsafe (head_ref_name , 0 , head_oid , NULL );
616+ if (!* head_points_at ) {
591617 errors_found |= ERROR_REFS ;
592- return error ("Invalid HEAD" );
618+ return error ("Invalid %s" , head_ref_name );
593619 }
594- if (!strcmp (head_points_at , "HEAD" ))
620+ if (!strcmp (* head_points_at , head_ref_name ))
595621 /* detached HEAD */
596622 null_is_error = 1 ;
597- else if (!starts_with (head_points_at , "refs/heads/" )) {
623+ else if (!starts_with (* head_points_at , "refs/heads/" )) {
598624 errors_found |= ERROR_REFS ;
599- return error ("HEAD points to something strange (%s)" ,
600- head_points_at );
625+ return error ("%s points to something strange (%s)" ,
626+ head_ref_name , * head_points_at );
601627 }
602- if (is_null_oid (& head_oid )) {
628+ if (is_null_oid (head_oid )) {
603629 if (null_is_error ) {
604630 errors_found |= ERROR_REFS ;
605- return error ("HEAD: detached HEAD points at nothing" );
631+ return error ("%s: detached HEAD points at nothing" ,
632+ head_ref_name );
606633 }
607- fprintf (stderr , "notice: HEAD points to an unborn branch (%s)\n" ,
608- head_points_at + 11 );
634+ fprintf (stderr , "notice: %s points to an unborn branch (%s)\n" ,
635+ head_ref_name , * head_points_at + 11 );
609636 }
610637 return 0 ;
611638}
@@ -720,7 +747,6 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
720747
721748 git_config (fsck_config , NULL );
722749
723- fsck_head_link ();
724750 if (connectivity_only ) {
725751 for_each_loose_object (mark_loose_for_connectivity , NULL , 0 );
726752 for_each_packed_object (mark_packed_for_connectivity , NULL , 0 );
0 commit comments