3232# include "win32/w32_util.h"
3333#endif
3434
35+ static int check_repositoryformatversion (git_config * config );
36+
3537#define GIT_FILE_CONTENT_PREFIX "gitdir:"
3638
3739#define GIT_BRANCH_MASTER "master"
@@ -489,6 +491,7 @@ int git_repository_open_ext(
489491 git_buf path = GIT_BUF_INIT , parent = GIT_BUF_INIT ,
490492 link_path = GIT_BUF_INIT ;
491493 git_repository * repo ;
494+ git_config * config = NULL ;
492495
493496 if (repo_ptr )
494497 * repo_ptr = NULL ;
@@ -510,22 +513,36 @@ int git_repository_open_ext(
510513 GITERR_CHECK_ALLOC (repo -> path_gitlink );
511514 }
512515
516+ /*
517+ * We'd like to have the config, but git doesn't particularly
518+ * care if it's not there, so we need to deal with that.
519+ */
520+
521+ error = git_repository_config_snapshot (& config , repo );
522+ if (error < 0 && error != GIT_ENOTFOUND )
523+ goto cleanup ;
524+
525+ if (config && (error = check_repositoryformatversion (config )) < 0 )
526+ goto cleanup ;
527+
513528 if ((flags & GIT_REPOSITORY_OPEN_BARE ) != 0 )
514529 repo -> is_bare = 1 ;
515530 else {
516- git_config * config = NULL ;
517531
518- if ((error = git_repository_config_snapshot (& config , repo )) < 0 ||
519- (error = load_config_data (repo , config )) < 0 ||
520- (error = load_workdir (repo , config , & parent )) < 0 )
521- git_repository_free (repo );
522-
523- git_config_free (config );
532+ if (config &&
533+ ((error = load_config_data (repo , config )) < 0 ||
534+ (error = load_workdir (repo , config , & parent ))) < 0 )
535+ goto cleanup ;
524536 }
525537
526- if (!error )
527- * repo_ptr = repo ;
538+ cleanup :
528539 git_buf_free (& parent );
540+ git_config_free (config );
541+
542+ if (error < 0 )
543+ git_repository_free (repo );
544+ else
545+ * repo_ptr = repo ;
529546
530547 return error ;
531548}
@@ -931,9 +948,14 @@ bool git_repository__reserved_names(
931948
932949static int check_repositoryformatversion (git_config * config )
933950{
934- int version ;
951+ int version , error ;
935952
936- if (git_config_get_int32 (& version , config , "core.repositoryformatversion" ) < 0 )
953+ error = git_config_get_int32 (& version , config , "core.repositoryformatversion" );
954+ /* git ignores this if the config variable isn't there */
955+ if (error == GIT_ENOTFOUND )
956+ return 0 ;
957+
958+ if (error < 0 )
937959 return -1 ;
938960
939961 if (GIT_REPO_VERSION < version ) {
0 commit comments