@@ -1885,6 +1885,68 @@ void initialize_repository_version(int hash_algo, int reinit)
1885
1885
git_config_set_gently ("extensions.objectformat" , NULL );
1886
1886
}
1887
1887
1888
+ static int is_reinit (void )
1889
+ {
1890
+ struct strbuf buf = STRBUF_INIT ;
1891
+ char junk [2 ];
1892
+ int ret ;
1893
+
1894
+ git_path_buf (& buf , "HEAD" );
1895
+ ret = !access (buf .buf , R_OK ) || readlink (buf .buf , junk , sizeof (junk ) - 1 ) != -1 ;
1896
+ strbuf_release (& buf );
1897
+ return ret ;
1898
+ }
1899
+
1900
+ static void create_reference_database (const char * initial_branch , int quiet )
1901
+ {
1902
+ struct strbuf err = STRBUF_INIT ;
1903
+ int reinit = is_reinit ();
1904
+
1905
+ /*
1906
+ * We need to create a "refs" dir in any case so that older versions of
1907
+ * Git can tell that this is a repository. This serves two main purposes:
1908
+ *
1909
+ * - Clients will know to stop walking the parent-directory chain when
1910
+ * detecting the Git repository. Otherwise they may end up detecting
1911
+ * a Git repository in a parent directory instead.
1912
+ *
1913
+ * - Instead of failing to detect a repository with unknown reference
1914
+ * format altogether, old clients will print an error saying that
1915
+ * they do not understand the reference format extension.
1916
+ */
1917
+ safe_create_dir (git_path ("refs" ), 1 );
1918
+ adjust_shared_perm (git_path ("refs" ));
1919
+
1920
+ if (refs_init_db (& err ))
1921
+ die ("failed to set up refs db: %s" , err .buf );
1922
+
1923
+ /*
1924
+ * Point the HEAD symref to the initial branch with if HEAD does
1925
+ * not yet exist.
1926
+ */
1927
+ if (!reinit ) {
1928
+ char * ref ;
1929
+
1930
+ if (!initial_branch )
1931
+ initial_branch = git_default_branch_name (quiet );
1932
+
1933
+ ref = xstrfmt ("refs/heads/%s" , initial_branch );
1934
+ if (check_refname_format (ref , 0 ) < 0 )
1935
+ die (_ ("invalid initial branch name: '%s'" ),
1936
+ initial_branch );
1937
+
1938
+ if (create_symref ("HEAD" , ref , NULL ) < 0 )
1939
+ exit (1 );
1940
+ free (ref );
1941
+ }
1942
+
1943
+ if (reinit && initial_branch )
1944
+ warning (_ ("re-init: ignored --initial-branch=%s" ),
1945
+ initial_branch );
1946
+
1947
+ strbuf_release (& err );
1948
+ }
1949
+
1888
1950
static int create_default_files (const char * template_path ,
1889
1951
const char * original_git_dir ,
1890
1952
const char * initial_branch ,
@@ -1896,10 +1958,8 @@ static int create_default_files(const char *template_path,
1896
1958
struct stat st1 ;
1897
1959
struct strbuf buf = STRBUF_INIT ;
1898
1960
char * path ;
1899
- char junk [2 ];
1900
1961
int reinit ;
1901
1962
int filemode ;
1902
- struct strbuf err = STRBUF_INIT ;
1903
1963
const char * init_template_dir = NULL ;
1904
1964
const char * work_tree = get_git_work_tree ();
1905
1965
@@ -1919,6 +1979,8 @@ static int create_default_files(const char *template_path,
1919
1979
reset_shared_repository ();
1920
1980
git_config (git_default_config , NULL );
1921
1981
1982
+ reinit = is_reinit ();
1983
+
1922
1984
/*
1923
1985
* We must make sure command-line options continue to override any
1924
1986
* values we might have just re-read from the config.
@@ -1962,39 +2024,7 @@ static int create_default_files(const char *template_path,
1962
2024
adjust_shared_perm (get_git_dir ());
1963
2025
}
1964
2026
1965
- /*
1966
- * We need to create a "refs" dir in any case so that older
1967
- * versions of git can tell that this is a repository.
1968
- */
1969
- safe_create_dir (git_path ("refs" ), 1 );
1970
- adjust_shared_perm (git_path ("refs" ));
1971
-
1972
- if (refs_init_db (& err ))
1973
- die ("failed to set up refs db: %s" , err .buf );
1974
-
1975
- /*
1976
- * Point the HEAD symref to the initial branch with if HEAD does
1977
- * not yet exist.
1978
- */
1979
- path = git_path_buf (& buf , "HEAD" );
1980
- reinit = (!access (path , R_OK )
1981
- || readlink (path , junk , sizeof (junk )- 1 ) != -1 );
1982
- if (!reinit ) {
1983
- char * ref ;
1984
-
1985
- if (!initial_branch )
1986
- initial_branch = git_default_branch_name (quiet );
1987
-
1988
- ref = xstrfmt ("refs/heads/%s" , initial_branch );
1989
- if (check_refname_format (ref , 0 ) < 0 )
1990
- die (_ ("invalid initial branch name: '%s'" ),
1991
- initial_branch );
1992
-
1993
- if (create_symref ("HEAD" , ref , NULL ) < 0 )
1994
- exit (1 );
1995
- free (ref );
1996
- }
1997
-
2027
+ create_reference_database (initial_branch , quiet );
1998
2028
initialize_repository_version (fmt -> hash_algo , 0 );
1999
2029
2000
2030
/* Check filemode trustability */
@@ -2158,9 +2188,6 @@ int init_db(const char *git_dir, const char *real_git_dir,
2158
2188
prev_bare_repository ,
2159
2189
init_shared_repository ,
2160
2190
flags & INIT_DB_QUIET );
2161
- if (reinit && initial_branch )
2162
- warning (_ ("re-init: ignored --initial-branch=%s" ),
2163
- initial_branch );
2164
2191
2165
2192
create_object_directory ();
2166
2193
0 commit comments