]> git.ipfire.org Git - thirdparty/git.git/blobdiff - submodule-config.c
submodule: support reading .gitmodules when it's not in the working tree
[thirdparty/git.git] / submodule-config.c
index 9faf141e47a921af7628f4ad58ee6edf051e5957..6869394c32fcede018701a4f6af1da0bffb2c105 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "dir.h"
 #include "repository.h"
 #include "config.h"
 #include "submodule-config.h"
@@ -603,8 +604,34 @@ static void submodule_cache_check_init(struct repository *repo)
 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
 {
        if (repo->worktree) {
-               char *file = repo_worktree_path(repo, GITMODULES_FILE);
-               git_config_from_file(fn, file, data);
+               struct git_config_source config_source = { 0 };
+               const struct config_options opts = { 0 };
+               struct object_id oid;
+               char *file;
+
+               file = repo_worktree_path(repo, GITMODULES_FILE);
+               if (file_exists(file)) {
+                       config_source.file = file;
+               } else if (repo->submodule_prefix) {
+                       /*
+                        * When get_oid and config_with_options, used below,
+                        * become able to work on a specific repository, this
+                        * warning branch can be removed.
+                        */
+                       warning("nested submodules without %s in the working tree are not supported yet",
+                               GITMODULES_FILE);
+                       goto out;
+               } else if (get_oid(GITMODULES_INDEX, &oid) >= 0) {
+                       config_source.blob = GITMODULES_INDEX;
+               } else if (get_oid(GITMODULES_HEAD, &oid) >= 0) {
+                       config_source.blob = GITMODULES_HEAD;
+               } else {
+                       goto out;
+               }
+
+               config_with_options(fn, data, &config_source, &opts);
+
+out:
                free(file);
        }
 }