@@ -70,7 +70,7 @@ static int check_extensions(git_config *config, int version);
7070
7171#define GIT_FILE_CONTENT_PREFIX "gitdir:"
7272
73- #define GIT_BRANCH_MASTER "master"
73+ #define GIT_BRANCH_DEFAULT "master"
7474
7575#define GIT_REPO_VERSION 0
7676#define GIT_REPO_MAX_VERSION 1
@@ -1408,9 +1408,6 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
14081408 (error = git_filebuf_open (& ref , ref_path .ptr , 0 , GIT_REFS_FILE_MODE )) < 0 )
14091409 goto out ;
14101410
1411- if (!ref_name )
1412- ref_name = GIT_BRANCH_MASTER ;
1413-
14141411 if (git__prefixcmp (ref_name , GIT_REFS_DIR ) == 0 )
14151412 fmt = "ref: %s\n" ;
14161413 else
@@ -2061,6 +2058,43 @@ static int repo_init_directories(
20612058 return error ;
20622059}
20632060
2061+ static int repo_init_head (const char * repo_dir , const char * given )
2062+ {
2063+ git_config * cfg = NULL ;
2064+ git_buf head_path = GIT_BUF_INIT , cfg_branch = GIT_BUF_INIT ;
2065+ const char * initial_head = NULL ;
2066+ int error ;
2067+
2068+ if ((error = git_buf_joinpath (& head_path , repo_dir , GIT_HEAD_FILE )) < 0 )
2069+ goto out ;
2070+
2071+ /*
2072+ * A template may have set a HEAD; use that unless it's been
2073+ * overridden by the caller's given initial head setting.
2074+ */
2075+ if (git_path_exists (head_path .ptr ) && !given )
2076+ goto out ;
2077+
2078+ if (given ) {
2079+ initial_head = given ;
2080+ } else if ((error = git_config_open_default (& cfg )) >= 0 &&
2081+ (error = git_config_get_string_buf (& cfg_branch , cfg , "init.defaultbranch" )) >= 0 ) {
2082+ initial_head = cfg_branch .ptr ;
2083+ }
2084+
2085+ if (!initial_head )
2086+ initial_head = GIT_BRANCH_DEFAULT ;
2087+
2088+ error = git_repository_create_head (repo_dir , initial_head );
2089+
2090+ out :
2091+ git_config_free (cfg );
2092+ git_buf_dispose (& head_path );
2093+ git_buf_dispose (& cfg_branch );
2094+
2095+ return error ;
2096+ }
2097+
20642098static int repo_init_create_origin (git_repository * repo , const char * url )
20652099{
20662100 int error ;
@@ -2091,7 +2125,7 @@ int git_repository_init_ext(
20912125 git_repository_init_options * opts )
20922126{
20932127 git_buf repo_path = GIT_BUF_INIT , wd_path = GIT_BUF_INIT ,
2094- common_path = GIT_BUF_INIT , head_path = GIT_BUF_INIT ;
2128+ common_path = GIT_BUF_INIT ;
20952129 const char * wd ;
20962130 bool is_valid ;
20972131 int error ;
@@ -2125,16 +2159,7 @@ int git_repository_init_ext(
21252159 } else {
21262160 if ((error = repo_init_structure (repo_path .ptr , wd , opts )) < 0 ||
21272161 (error = repo_init_config (repo_path .ptr , wd , opts -> flags , opts -> mode )) < 0 ||
2128- (error = git_buf_joinpath (& head_path , repo_path .ptr , GIT_HEAD_FILE )) < 0 )
2129- goto out ;
2130-
2131- /*
2132- * Only set the new HEAD if the file does not exist already via
2133- * a template or if the caller has explicitly supplied an
2134- * initial HEAD value.
2135- */
2136- if ((!git_path_exists (head_path .ptr ) || opts -> initial_head ) &&
2137- (error = git_repository_create_head (repo_path .ptr , opts -> initial_head )) < 0 )
2162+ (error = repo_init_head (repo_path .ptr , opts -> initial_head )) < 0 )
21382163 goto out ;
21392164 }
21402165
@@ -2146,7 +2171,6 @@ int git_repository_init_ext(
21462171 goto out ;
21472172
21482173out :
2149- git_buf_dispose (& head_path );
21502174 git_buf_dispose (& common_path );
21512175 git_buf_dispose (& repo_path );
21522176 git_buf_dispose (& wd_path );
@@ -2330,23 +2354,59 @@ static int repo_contains_no_reference(git_repository *repo)
23302354 return error ;
23312355}
23322356
2357+ int git_repository_initialbranch (git_buf * out , git_repository * repo )
2358+ {
2359+ git_config * config ;
2360+ git_config_entry * entry = NULL ;
2361+ const char * branch ;
2362+ int error ;
2363+
2364+ if ((error = git_repository_config__weakptr (& config , repo )) < 0 )
2365+ return error ;
2366+
2367+ if ((error = git_config_get_entry (& entry , config , "init.defaultbranch" )) == 0 ) {
2368+ branch = entry -> value ;
2369+ }
2370+ else if (error == GIT_ENOTFOUND ) {
2371+ branch = GIT_BRANCH_DEFAULT ;
2372+ }
2373+ else {
2374+ goto done ;
2375+ }
2376+
2377+ if ((error = git_buf_puts (out , GIT_REFS_HEADS_DIR )) < 0 ||
2378+ (error = git_buf_puts (out , branch )) < 0 )
2379+ goto done ;
2380+
2381+ if (!git_reference_is_valid_name (out -> ptr )) {
2382+ git_error_set (GIT_ERROR_INVALID , "the value of init.defaultBranch is not a valid reference name" );
2383+ error = -1 ;
2384+ }
2385+
2386+ done :
2387+ git_config_entry_free (entry );
2388+ return error ;
2389+ }
2390+
23332391int git_repository_is_empty (git_repository * repo )
23342392{
23352393 git_reference * head = NULL ;
2336- int is_empty = 0 ;
2394+ git_buf initialbranch = GIT_BUF_INIT ;
2395+ int result = 0 ;
23372396
2338- if (git_reference_lookup (& head , repo , GIT_HEAD_FILE ) < 0 )
2339- return -1 ;
2397+ if ((result = git_reference_lookup (& head , repo , GIT_HEAD_FILE )) < 0 ||
2398+ (result = git_repository_initialbranch (& initialbranch , repo )) < 0 )
2399+ goto done ;
23402400
2341- if (git_reference_type (head ) == GIT_REFERENCE_SYMBOLIC )
2342- is_empty =
2343- (strcmp (git_reference_symbolic_target (head ),
2344- GIT_REFS_HEADS_DIR "master" ) == 0 ) &&
2345- repo_contains_no_reference (repo );
2401+ result = (git_reference_type (head ) == GIT_REFERENCE_SYMBOLIC &&
2402+ strcmp (git_reference_symbolic_target (head ), initialbranch .ptr ) == 0 &&
2403+ repo_contains_no_reference (repo ));
23462404
2405+ done :
23472406 git_reference_free (head );
2407+ git_buf_dispose (& initialbranch );
23482408
2349- return is_empty ;
2409+ return result ;
23502410}
23512411
23522412static const char * resolved_parent_path (const git_repository * repo , git_repository_item_t item , git_repository_item_t fallback )
0 commit comments