pathspec: only match across submodule boundaries when requested
[gitweb.git] / repository.c
index 6f6f4d91ef511062d8a6980ca6c80f8593002637..c0e0e0e7e76015e3f93b21528bff58286fab0fb2 100644 (file)
@@ -1,9 +1,12 @@
 #include "cache.h"
 #include "repository.h"
 #include "config.h"
+#include "submodule-config.h"
 
 /* The main repository */
-static struct repository the_repo;
+static struct repository the_repo = {
+       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &the_index, 0, 0
+};
 struct repository *the_repository = &the_repo;
 
 static char *git_path_from_env(const char *envvar, const char *git_dir,
@@ -143,6 +146,58 @@ int repo_init(struct repository *repo, const char *gitdir, const char *worktree)
        return -1;
 }
 
+/*
+ * Initialize 'submodule' as the submodule given by 'path' in parent repository
+ * 'superproject'.
+ * Return 0 upon success and a non-zero value upon failure.
+ */
+int repo_submodule_init(struct repository *submodule,
+                       struct repository *superproject,
+                       const char *path)
+{
+       const struct submodule *sub;
+       struct strbuf gitdir = STRBUF_INIT;
+       struct strbuf worktree = STRBUF_INIT;
+       int ret = 0;
+
+       sub = submodule_from_cache(superproject, null_sha1, path);
+       if (!sub) {
+               ret = -1;
+               goto out;
+       }
+
+       strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
+       strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
+
+       if (repo_init(submodule, gitdir.buf, worktree.buf)) {
+               /*
+                * If initilization fails then it may be due to the submodule
+                * not being populated in the superproject's worktree.  Instead
+                * we can try to initilize the submodule by finding it's gitdir
+                * in the superproject's 'modules' directory.  In this case the
+                * submodule would not have a worktree.
+                */
+               strbuf_reset(&gitdir);
+               strbuf_repo_git_path(&gitdir, superproject,
+                                    "modules/%s", sub->name);
+
+               if (repo_init(submodule, gitdir.buf, NULL)) {
+                       ret = -1;
+                       goto out;
+               }
+       }
+
+       submodule->submodule_prefix = xstrfmt("%s%s/",
+                                             superproject->submodule_prefix ?
+                                             superproject->submodule_prefix :
+                                             "", path);
+
+out:
+       strbuf_release(&gitdir);
+       strbuf_release(&worktree);
+       return ret;
+}
+
 void repo_clear(struct repository *repo)
 {
        free(repo->gitdir);
@@ -157,6 +212,8 @@ void repo_clear(struct repository *repo)
        repo->index_file = NULL;
        free(repo->worktree);
        repo->worktree = NULL;
+       free(repo->submodule_prefix);
+       repo->submodule_prefix = NULL;
 
        if (repo->config) {
                git_configset_clear(repo->config);
@@ -164,6 +221,11 @@ void repo_clear(struct repository *repo)
                repo->config = NULL;
        }
 
+       if (repo->submodule_cache) {
+               submodule_cache_free(repo->submodule_cache);
+               repo->submodule_cache = NULL;
+       }
+
        if (repo->index) {
                discard_index(repo->index);
                free(repo->index);
@@ -175,8 +237,6 @@ int repo_read_index(struct repository *repo)
 {
        if (!repo->index)
                repo->index = xcalloc(1, sizeof(*repo->index));
-       else
-               discard_index(repo->index);
 
        return read_index_from(repo->index, repo->index_file);
 }