@@ -201,6 +201,26 @@ void initialize_repository_version(int hash_algo)
201201 hash_algos [hash_algo ].name );
202202}
203203
204+ static char * get_default_branch_name (void )
205+ {
206+ const char * branch_name ;
207+ char * prefixed ;
208+
209+ /* get the default branch name from config, or failing that, env */
210+ if (git_config_get_string_const ("init.defaultbranchname" , & branch_name ))
211+ branch_name = getenv ("GIT_DEFAULT_BRANCH_NAME" );
212+
213+ if (!branch_name )
214+ branch_name = "master" ;
215+
216+ /* prepend "refs/heads/" to the branch name */
217+ prefixed = xstrfmt ("refs/heads/%s" , branch_name );
218+ if (check_refname_format (prefixed , 0 ))
219+ die (_ ("Invalid value for default branch name %s" ), branch_name );
220+
221+ return prefixed ;
222+ }
223+
204224static int create_default_files (const char * template_path ,
205225const char * original_git_dir ,
206226const struct repository_format * fmt )
@@ -265,27 +285,10 @@ static int create_default_files(const char *template_path,
265285reinit = (!access (path , R_OK )
266286 || readlink (path , junk , sizeof (junk )- 1 ) != -1 );
267287if (!reinit ) {
268- const char * branch_name ;
269- char * prefixed ;
270-
271- /* get the default branch name from config, or failing that, env */
272- if (git_config_get_string_const ("init.defaultbranchname" , & branch_name ))
273- branch_name = getenv ("GIT_DEFAULT_BRANCH_NAME" );
274-
275- if (!branch_name ) {
276- branch_name = "master" ;
277- }
278-
279- /* prepend "refs/heads/" to the branch name */
280- prefixed = xstrfmt ("refs/heads/%s" , branch_name );
281- if (check_refname_format (prefixed , 0 )) {
282- die (_ ("Invalid value for default branch name %s" ), branch_name );
283- }
284-
285- if (create_symref ("HEAD" , prefixed , NULL ) < 0 )
288+ char * default_ref = get_default_branch_name ();
289+ if (create_symref ("HEAD" , default_ref , NULL ) < 0 )
286290exit (1 );
287-
288- free (prefixed );
291+ free (default_ref );
289292}
290293
291294initialize_repository_version (fmt -> hash_algo );
0 commit comments