@@ -1808,32 +1808,6 @@ int apply_repository_format(struct repository *repo,
18081808 return 0 ;
18091809}
18101810
1811- /*
1812- * Check the repository format version in the path found in repo_get_git_dir(repo),
1813- * and die if it is a version we don't understand. Generally one would
1814- * set_git_dir() before calling this, and use it only for "are we in a valid
1815- * repo?".
1816- *
1817- * If successful and fmt is not NULL, fill fmt with data.
1818- */
1819- static void check_and_apply_repository_format (struct repository * repo ,
1820- struct repository_format * fmt ,
1821- enum apply_repository_format_flags flags )
1822- {
1823- struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT ;
1824- struct strbuf err = STRBUF_INIT ;
1825-
1826- if (!fmt )
1827- fmt = & repo_fmt ;
1828-
1829- check_repository_format_gently (repo_get_git_dir (repo ), fmt , NULL );
1830- if (apply_repository_format (repo , fmt , flags , & err ) < 0 )
1831- die ("%s" , err .buf );
1832- startup_info -> have_repository = 1 ;
1833-
1834- clear_repository_format (& repo_fmt );
1835- }
1836-
18371811const char * enter_repo (struct repository * repo , const char * path , unsigned flags )
18381812{
18391813 static struct strbuf validated_path = STRBUF_INIT ;
@@ -1907,9 +1881,17 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
19071881 }
19081882
19091883 if (is_git_directory ("." )) {
1884+ struct repository_format fmt = REPOSITORY_FORMAT_INIT ;
1885+ struct strbuf err = STRBUF_INIT ;
1886+
19101887 set_git_dir (repo , "." , 0 );
1911- check_and_apply_repository_format (repo , NULL ,
1912- APPLY_REPOSITORY_FORMAT_HONOR_ENV );
1888+ check_repository_format_gently ("." , & fmt , NULL );
1889+ if (apply_repository_format (repo , & fmt , APPLY_REPOSITORY_FORMAT_HONOR_ENV , & err ) < 0 )
1890+ die ("%s" , err .buf );
1891+ startup_info -> have_repository = 1 ;
1892+
1893+ clear_repository_format (& fmt );
1894+ strbuf_release (& err );
19131895 return path ;
19141896 }
19151897
@@ -1944,7 +1926,6 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
19441926 static struct strbuf cwd = STRBUF_INIT ;
19451927 struct strbuf dir = STRBUF_INIT , gitdir = STRBUF_INIT , report = STRBUF_INIT ;
19461928 const char * prefix = NULL ;
1947- const char * ref_backend_uri ;
19481929 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT ;
19491930
19501931 /*
@@ -2061,13 +2042,33 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
20612042 startup_info -> have_repository ||
20622043 /* GIT_DIR_EXPLICIT */
20632044 getenv (GIT_DIR_ENVIRONMENT )) {
2045+ const char * ref_backend_uri ;
2046+
20642047 if (!repo -> gitdir ) {
20652048 const char * gitdir = getenv (GIT_DIR_ENVIRONMENT );
20662049 if (!gitdir )
20672050 gitdir = DEFAULT_GIT_DIR_ENVIRONMENT ;
20682051 setup_git_env_internal (repo , gitdir );
20692052 }
20702053
2054+ /*
2055+ * The env variable should override the repository config
2056+ * for 'extensions.refStorage'.
2057+ */
2058+ ref_backend_uri = getenv (GIT_REFERENCE_BACKEND_ENVIRONMENT );
2059+ if (ref_backend_uri ) {
2060+ char * format ;
2061+
2062+ free (repo_fmt .ref_storage_payload );
2063+
2064+ parse_reference_uri (ref_backend_uri , & format , & repo_fmt .ref_storage_payload );
2065+ repo_fmt .ref_storage_format = ref_storage_format_by_name (format );
2066+ if (repo_fmt .ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN )
2067+ die (_ ("unknown ref storage format: '%s'" ), format );
2068+
2069+ free (format );
2070+ }
2071+
20712072 if (startup_info -> have_repository ) {
20722073 struct strbuf err = STRBUF_INIT ;
20732074
@@ -2095,25 +2096,6 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
20952096 setenv (GIT_PREFIX_ENVIRONMENT , "" , 1 );
20962097 }
20972098
2098- /*
2099- * The env variable should override the repository config
2100- * for 'extensions.refStorage'.
2101- */
2102- ref_backend_uri = getenv (GIT_REFERENCE_BACKEND_ENVIRONMENT );
2103- if (ref_backend_uri ) {
2104- char * backend , * payload ;
2105- enum ref_storage_format format ;
2106-
2107- parse_reference_uri (ref_backend_uri , & backend , & payload );
2108- format = ref_storage_format_by_name (backend );
2109- if (format == REF_STORAGE_FORMAT_UNKNOWN )
2110- die (_ ("unknown ref storage format: '%s'" ), backend );
2111- repo_set_ref_storage_format (repo , format , payload );
2112-
2113- free (backend );
2114- free (payload );
2115- }
2116-
21172099 setup_original_cwd (repo );
21182100
21192101 strbuf_release (& dir );
@@ -2748,8 +2730,7 @@ static int read_default_format_config(const char *key, const char *value,
27482730 return ret ;
27492731}
27502732
2751- static void repository_format_configure (struct repository * repo ,
2752- struct repository_format * repo_fmt ,
2733+ static void repository_format_configure (struct repository_format * repo_fmt ,
27532734 int hash , enum ref_storage_format ref_format )
27542735{
27552736 struct default_format_config cfg = {
@@ -2786,7 +2767,6 @@ static void repository_format_configure(struct repository *repo,
27862767 } else if (cfg .hash != GIT_HASH_UNKNOWN ) {
27872768 repo_fmt -> hash_algo = cfg .hash ;
27882769 }
2789- repo_set_hash_algo (repo , repo_fmt -> hash_algo );
27902770
27912771 env = getenv ("GIT_DEFAULT_REF_FORMAT" );
27922772 if (repo_fmt -> version >= 0 &&
@@ -2824,9 +2804,6 @@ static void repository_format_configure(struct repository *repo,
28242804
28252805 free (backend );
28262806 }
2827-
2828- repo_set_ref_storage_format (repo , repo_fmt -> ref_storage_format ,
2829- repo_fmt -> ref_storage_payload );
28302807}
28312808
28322809int init_db (struct repository * repo ,
@@ -2840,6 +2817,7 @@ int init_db(struct repository *repo,
28402817 int exist_ok = flags & INIT_DB_EXIST_OK ;
28412818 char * original_git_dir = real_pathdup (git_dir , 1 );
28422819 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT ;
2820+ struct strbuf err = STRBUF_INIT ;
28432821
28442822 if (real_git_dir ) {
28452823 struct stat st ;
@@ -2866,10 +2844,11 @@ int init_db(struct repository *repo,
28662844 * config file, so this will not fail. What we are catching
28672845 * is an attempt to reinitialize new repository with an old tool.
28682846 */
2869- check_and_apply_repository_format (repo , & repo_fmt ,
2870- APPLY_REPOSITORY_FORMAT_HONOR_ENV );
2871-
2872- repository_format_configure (repo , & repo_fmt , hash , ref_storage_format );
2847+ check_repository_format_gently (repo_get_git_dir (repo ), & repo_fmt , NULL );
2848+ repository_format_configure (& repo_fmt , hash , ref_storage_format );
2849+ if (apply_repository_format (repo , & repo_fmt , APPLY_REPOSITORY_FORMAT_HONOR_ENV , & err ) < 0 )
2850+ die ("%s" , err .buf );
2851+ startup_info -> have_repository = 1 ;
28732852
28742853 /*
28752854 * Ensure `core.hidedotfiles` is processed. This must happen after we
@@ -2924,6 +2903,7 @@ int init_db(struct repository *repo,
29242903 }
29252904
29262905 clear_repository_format (& repo_fmt );
2906+ strbuf_release (& err );
29272907 free (original_git_dir );
29282908 return 0 ;
29292909}
0 commit comments