]> git.ipfire.org Git - thirdparty/git.git/blobdiff - submodule.c
fetch/pull: Add the --recurse-submodules option
[thirdparty/git.git] / submodule.c
index dcb4b5da7166c865d06d55014d650a35b4abb998..4d9b774cd107b5e9225c28620d2a2dc9efb665cc 100644 (file)
@@ -7,6 +7,10 @@
 #include "run-command.h"
 #include "diffcore.h"
 #include "refs.h"
+#include "string-list.h"
+
+struct string_list config_name_for_path;
+struct string_list config_ignore_for_name;
 
 static int add_submodule_odb(const char *path)
 {
@@ -47,16 +51,90 @@ done:
        return ret;
 }
 
+void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
+                                            const char *path)
+{
+       struct string_list_item *path_option, *ignore_option;
+       path_option = unsorted_string_list_lookup(&config_name_for_path, path);
+       if (path_option) {
+               ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
+               if (ignore_option)
+                       handle_ignore_submodules_arg(diffopt, ignore_option->util);
+       }
+}
+
+int submodule_config(const char *var, const char *value, void *cb)
+{
+       if (!prefixcmp(var, "submodule."))
+               return parse_submodule_config_option(var, value);
+       return 0;
+}
+
+void gitmodules_config(void)
+{
+       const char *work_tree = get_git_work_tree();
+       if (work_tree) {
+               struct strbuf gitmodules_path = STRBUF_INIT;
+               strbuf_addstr(&gitmodules_path, work_tree);
+               strbuf_addstr(&gitmodules_path, "/.gitmodules");
+               git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
+               strbuf_release(&gitmodules_path);
+       }
+}
+
+int parse_submodule_config_option(const char *var, const char *value)
+{
+       int len;
+       struct string_list_item *config;
+       struct strbuf submodname = STRBUF_INIT;
+
+       var += 10;              /* Skip "submodule." */
+
+       len = strlen(var);
+       if ((len > 5) && !strcmp(var + len - 5, ".path")) {
+               strbuf_add(&submodname, var, len - 5);
+               config = unsorted_string_list_lookup(&config_name_for_path, value);
+               if (config)
+                       free(config->util);
+               else
+                       config = string_list_append(&config_name_for_path, xstrdup(value));
+               config->util = strbuf_detach(&submodname, NULL);
+               strbuf_release(&submodname);
+       } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
+               if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
+                   strcmp(value, "all") && strcmp(value, "none")) {
+                       warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
+                       return 0;
+               }
+
+               strbuf_add(&submodname, var, len - 7);
+               config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
+               if (config)
+                       free(config->util);
+               else
+                       config = string_list_append(&config_ignore_for_name,
+                                                   strbuf_detach(&submodname, NULL));
+               strbuf_release(&submodname);
+               config->util = xstrdup(value);
+               return 0;
+       }
+       return 0;
+}
+
 void handle_ignore_submodules_arg(struct diff_options *diffopt,
                                  const char *arg)
 {
+       DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
+       DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
+       DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
+
        if (!strcmp(arg, "all"))
                DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
        else if (!strcmp(arg, "untracked"))
                DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
        else if (!strcmp(arg, "dirty"))
                DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
-       else
+       else if (strcmp(arg, "none"))
                die("bad --ignore-submodules argument: %s", arg);
 }
 
@@ -151,6 +229,70 @@ void show_submodule_summary(FILE *f, const char *path,
        strbuf_release(&sb);
 }
 
+int fetch_populated_submodules(int num_options, const char **options,
+                              const char *prefix, int quiet)
+{
+       int i, result = 0, argc = 0;
+       struct child_process cp;
+       const char **argv;
+       struct string_list_item *name_for_path;
+       const char *work_tree = get_git_work_tree();
+       if (!work_tree)
+               return 0;
+
+       if (!the_index.initialized)
+               if (read_cache() < 0)
+                       die("index file corrupt");
+
+       argv = xcalloc(num_options + 5, sizeof(const char *));
+       argv[argc++] = "fetch";
+       for (i = 0; i < num_options; i++)
+               argv[argc++] = options[i];
+       argv[argc++] = "--submodule-prefix";
+
+       memset(&cp, 0, sizeof(cp));
+       cp.argv = argv;
+       cp.env = local_repo_env;
+       cp.git_cmd = 1;
+       cp.no_stdin = 1;
+
+       for (i = 0; i < active_nr; i++) {
+               struct strbuf submodule_path = STRBUF_INIT;
+               struct strbuf submodule_git_dir = STRBUF_INIT;
+               struct strbuf submodule_prefix = STRBUF_INIT;
+               struct cache_entry *ce = active_cache[i];
+               const char *git_dir, *name;
+
+               if (!S_ISGITLINK(ce->ce_mode))
+                       continue;
+
+               name = ce->name;
+               name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
+               if (name_for_path)
+                       name = name_for_path->util;
+
+               strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
+               strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
+               strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
+               git_dir = read_gitfile_gently(submodule_git_dir.buf);
+               if (!git_dir)
+                       git_dir = submodule_git_dir.buf;
+               if (is_directory(git_dir)) {
+                       if (!quiet)
+                               printf("Fetching submodule %s%s\n", prefix, ce->name);
+                       cp.dir = submodule_path.buf;
+                       argv[argc] = submodule_prefix.buf;
+                       if (run_command(&cp))
+                               result = 1;
+               }
+               strbuf_release(&submodule_path);
+               strbuf_release(&submodule_git_dir);
+               strbuf_release(&submodule_prefix);
+       }
+       free(argv);
+       return result;
+}
+
 unsigned is_submodule_modified(const char *path, int ignore_untracked)
 {
        ssize_t len;