submodule-config: pass repo upon blob config read

When reading the config of a submodule, if reading from a blob, read using an explicitly specified repository instead of by adding the submodule's ODB as an alternate and then reading an object from the_repository. This makes the "grep --recurse-submodules with submodules without .gitmodules in the working tree" test in t7814 work when GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB is true. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Reviewed-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 16, 2021 at 14:09 UTC e3e8bf046e9682b0d67c07c6bc83ec9717d9c941
3 files changed +19 -9
config.c
+13 -7
@@ -1796,6 +1796,7 @@ int git_config_from_mem(config_fn_t fn,
1796
1797 int git_config_from_blob_oid(config_fn_t fn,
1798 const char *name,
1799 + struct repository *repo,
1800 const struct object_id *oid,
1801 void *data)
1802 {
@@ -1804,7 +1805,7 @@ int git_config_from_blob_oid(config_fn_t fn,
1805 unsigned long size;
1806 int ret;
1807
1807 - buf = read_object_file(oid, &type, &size);
1808 + buf = repo_read_object_file(repo, oid, &type, &size);
1809 if (!buf)
1810 return error(_("unable to load config blob object '%s'"), name);
1811 if (type != OBJ_BLOB) {
@@ -1820,14 +1821,15 @@ int git_config_from_blob_oid(config_fn_t fn,
1821 }
1822
1823 static int git_config_from_blob_ref(config_fn_t fn,
1824 + struct repository *repo,
1825 const char *name,
1826 void *data)
1827 {
1828 struct object_id oid;
1829
1828 - if (get_oid(name, &oid) < 0)
1830 + if (repo_get_oid(repo, name, &oid) < 0)
1831 return error(_("unable to resolve config blob '%s'"), name);
1830 - return git_config_from_blob_oid(fn, name, &oid, data);
1832 + return git_config_from_blob_oid(fn, name, repo, &oid, data);
1833 }
1834
1835 char *git_system_config(void)
@@ -1958,12 +1960,16 @@ int config_with_options(config_fn_t fn, void *data,
1960 * If we have a specific filename, use it. Otherwise, follow the
1961 * regular lookup sequence.
1962 */
1961 - if (config_source && config_source->use_stdin)
1963 + if (config_source && config_source->use_stdin) {
1964 return git_config_from_stdin(fn, data);
1963 - else if (config_source && config_source->file)
1965 + } else if (config_source && config_source->file) {
1966 return git_config_from_file(fn, config_source->file, data);
1965 - else if (config_source && config_source->blob)
1966 - return git_config_from_blob_ref(fn, config_source->blob, data);
1967 + } else if (config_source && config_source->blob) {
1968 + struct repository *repo = config_source->repo ?
1969 + config_source->repo : the_repository;
1970 + return git_config_from_blob_ref(fn, repo, config_source->blob,
1971 + data);
1972 + }
1973
1974 return do_git_config_sequence(opts, fn, data);
1975 }
config.h
+3
@@ -49,6 +49,8 @@ const char *config_scope_name(enum config_scope scope);
49 struct git_config_source {
50 unsigned int use_stdin:1;
51 const char *file;
52 + /* The repository if blob is not NULL; leave blank for the_repository */
53 + struct repository *repo;
54 const char *blob;
55 enum config_scope scope;
56 };
@@ -136,6 +138,7 @@ int git_config_from_mem(config_fn_t fn,
138 const char *buf, size_t len,
139 void *data, const struct config_options *opts);
140 int git_config_from_blob_oid(config_fn_t fn, const char *name,
141 + struct repository *repo,
142 const struct object_id *oid, void *data);
143 void git_config_push_parameter(const char *text);
144 void git_config_push_env(const char *spec);
submodule-config.c
+3 -2
@@ -649,9 +649,10 @@ static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void
649 config_source.file = file;
650 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
651 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
652 + config_source.repo = repo;
653 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
654 if (repo != the_repository)
654 - add_to_alternates_memory(repo->objects->odb->path);
655 + add_submodule_odb_by_path(repo->objects->odb->path);
656 } else {
657 goto out;
658 }
@@ -702,7 +703,7 @@ void gitmodules_config_oid(const struct object_id *commit_oid)
703
704 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
705 git_config_from_blob_oid(gitmodules_cb, rev.buf,
705 - &oid, the_repository);
706 + the_repository, &oid, the_repository);
707 }
708 strbuf_release(&rev);
709