summaryrefslogtreecommitdiff
path: root/submodule-config.c
diff options
context:
space:
mode:
authorAntonio Ospite <ao2@ao2.it>2018-10-25 18:18:12 +0200
committerJunio C Hamano <gitster@pobox.com>2018-10-31 15:01:30 +0900
commit76e9bdc437ca3111883eb57a479e5d8f582dd670 (patch)
tree70442b23252b1c0a3441d41dd9c62e955a3b4c9f /submodule-config.c
parentb5c259f226ba06f64b399ff4c6b843542352395f (diff)
downloadgit-76e9bdc437ca3111883eb57a479e5d8f582dd670.tar.gz
submodule: support reading .gitmodules when it's not in the working tree
When the .gitmodules file is not available in the working tree, try using the content from the index and from the current branch. This covers the case when the file is part of the repository but for some reason it is not checked out, for example because of a sparse checkout. This makes it possible to use at least the 'git submodule' commands which *read* the gitmodules configuration file without fully populating the working tree. Writing to .gitmodules will still require that the file is checked out, so check for that before calling config_set_in_gitmodules_file_gently. Add a similar check also in git-submodule.sh::cmd_add() to anticipate the eventual failure of the "git submodule add" command when .gitmodules is not safely writeable; this prevents the command from leaving the repository in a spurious state (e.g. the submodule repository was cloned but .gitmodules was not updated because config_set_in_gitmodules_file_gently failed). Moreover, since config_from_gitmodules() now accesses the global object store, it is necessary to protect all code paths which call the function against concurrent access to the global object store. Currently this only happens in builtin/grep.c::grep_submodules(), so call grep_read_lock() before invoking code involving config_from_gitmodules(). Finally, add t7418-submodule-sparse-gitmodules.sh to verify that reading from .gitmodules succeeds and that writing to it fails when the file is not checked out. NOTE: there is one rare case where this new feature does not work properly yet: nested submodules without .gitmodules in their working tree. This has been documented with a warning and a test_expect_failure item in t7814, and in this case the current behavior is not altered: no config is read. Signed-off-by: Antonio Ospite <ao2@ao2.it> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'submodule-config.c')
-rw-r--r--submodule-config.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/submodule-config.c b/submodule-config.c
index 9faf141e47..6869394c32 100644
--- a/submodule-config.c
+++ b/submodule-config.c
@@ -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);
}
}