@@ -411,6 +411,7 @@ static int read_worktree_config(const char *var, const char *value, void *vdata)
411411 } else if (strcmp (var , "core.worktree" ) == 0 ) {
412412 if (!value )
413413 return config_error_nonbool (var );
414+ free (data -> work_tree );
414415 data -> work_tree = xstrdup (value );
415416 }
416417 return 0 ;
@@ -476,7 +477,7 @@ static int check_repository_format_gently(const char *gitdir, struct repository_
476477 }
477478
478479 repository_format_precious_objects = candidate -> precious_objects ;
479- repository_format_partial_clone = candidate -> partial_clone ;
480+ repository_format_partial_clone = xstrdup_or_null ( candidate -> partial_clone ) ;
480481 repository_format_worktree_config = candidate -> worktree_config ;
481482 string_list_clear (& candidate -> unknown_extensions , 0 );
482483
@@ -499,27 +500,38 @@ static int check_repository_format_gently(const char *gitdir, struct repository_
499500 }
500501 if (candidate -> work_tree ) {
501502 free (git_work_tree_cfg );
502- git_work_tree_cfg = candidate -> work_tree ;
503+ git_work_tree_cfg = xstrdup ( candidate -> work_tree ) ;
503504 inside_work_tree = -1 ;
504505 }
505- } else {
506- free (candidate -> work_tree );
507506 }
508507
509508 return 0 ;
510509}
511510
511+ static void init_repository_format (struct repository_format * format )
512+ {
513+ const struct repository_format fresh = REPOSITORY_FORMAT_INIT ;
514+
515+ memcpy (format , & fresh , sizeof (fresh ));
516+ }
517+
512518int read_repository_format (struct repository_format * format , const char * path )
513519{
514- memset (format , 0 , sizeof (* format ));
515- format -> version = -1 ;
516- format -> is_bare = -1 ;
517- format -> hash_algo = GIT_HASH_SHA1 ;
518- string_list_init (& format -> unknown_extensions , 1 );
520+ clear_repository_format (format );
519521 git_config_from_file (check_repo_format , path , format );
522+ if (format -> version == -1 )
523+ clear_repository_format (format );
520524 return format -> version ;
521525}
522526
527+ void clear_repository_format (struct repository_format * format )
528+ {
529+ string_list_clear (& format -> unknown_extensions , 0 );
530+ free (format -> work_tree );
531+ free (format -> partial_clone );
532+ init_repository_format (format );
533+ }
534+
523535int verify_repository_format (const struct repository_format * format ,
524536 struct strbuf * err )
525537{
@@ -997,7 +1009,7 @@ int discover_git_directory(struct strbuf *commondir,
9971009 struct strbuf dir = STRBUF_INIT , err = STRBUF_INIT ;
9981010 size_t gitdir_offset = gitdir -> len , cwd_len ;
9991011 size_t commondir_offset = commondir -> len ;
1000- struct repository_format candidate ;
1012+ struct repository_format candidate = REPOSITORY_FORMAT_INIT ;
10011013
10021014 if (strbuf_getcwd (& dir ))
10031015 return -1 ;
@@ -1034,9 +1046,11 @@ int discover_git_directory(struct strbuf *commondir,
10341046 strbuf_release (& err );
10351047 strbuf_setlen (commondir , commondir_offset );
10361048 strbuf_setlen (gitdir , gitdir_offset );
1049+ clear_repository_format (& candidate );
10371050 return -1 ;
10381051 }
10391052
1053+ clear_repository_format (& candidate );
10401054 return 0 ;
10411055}
10421056
@@ -1045,7 +1059,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
10451059 static struct strbuf cwd = STRBUF_INIT ;
10461060 struct strbuf dir = STRBUF_INIT , gitdir = STRBUF_INIT ;
10471061 const char * prefix = NULL ;
1048- struct repository_format repo_fmt ;
1062+ struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT ;
10491063
10501064 /*
10511065 * We may have read an incomplete configuration before
@@ -1157,6 +1171,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
11571171
11581172 strbuf_release (& dir );
11591173 strbuf_release (& gitdir );
1174+ clear_repository_format (& repo_fmt );
11601175
11611176 return prefix ;
11621177}
@@ -1214,9 +1229,10 @@ int git_config_perm(const char *var, const char *value)
12141229
12151230void check_repository_format (void )
12161231{
1217- struct repository_format repo_fmt ;
1232+ struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT ;
12181233 check_repository_format_gently (get_git_dir (), & repo_fmt , NULL );
12191234 startup_info -> have_repository = 1 ;
1235+ clear_repository_format (& repo_fmt );
12201236}
12211237
12221238/*
0 commit comments