@@ -258,15 +258,34 @@ static int create_default_files(const char *template_path,
258
258
die ("failed to set up refs db: %s" , err .buf );
259
259
260
260
/*
261
- * Create the default symlink from ".git/HEAD" to the "master"
262
- * branch, if it does not exist yet.
261
+ * Create the default symlink from ".git/HEAD" to the default
262
+ * branch name , if it does not exist yet.
263
263
*/
264
264
path = git_path_buf (& buf , "HEAD" );
265
265
reinit = (!access (path , R_OK )
266
266
|| readlink (path , junk , sizeof (junk )- 1 ) != -1 );
267
267
if (!reinit ) {
268
- if (create_symref ("HEAD" , "refs/heads/master" , NULL ) < 0 )
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 )
269
286
exit (1 );
287
+
288
+ free (prefixed );
270
289
}
271
290
272
291
initialize_repository_version (fmt -> hash_algo );
0 commit comments