From 959b5455d07c7724eea9d323ca8ce6bb6ddde40e Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Mon, 17 Aug 2015 17:21:57 -0700 Subject: submodule: implement a config API for lookup of .gitmodules values In a superproject some commands need to interact with submodules. They need to query values from the .gitmodules file either from the worktree of from certain revisions. At the moment this is quite hard since a caller would need to read the .gitmodules file from the history and then parse the values. We want to provide an API for this so we have one place to get values from .gitmodules from any revision (including the worktree). The API is realized as a cache which allows us to lazily read .gitmodules configurations by commit into a runtime cache which can then be used to easily lookup values from it. Currently only the values for path or name are stored but it can be extended for any value needed. It is expected that .gitmodules files do not change often between commits. Thats why we lookup the .gitmodules sha1 from a commit and then either lookup an already parsed configuration or parse and cache an unknown one for each sha1. The cache is lazily build on demand for each requested commit. This cache can be used for all purposes which need knowledge about submodule configurations. Example use cases are: * Recursive submodule checkout needs to lookup a submodule name from its path when a submodule first appears. This needs be done before this configuration exists in the worktree. * The implementation of submodule support for 'git archive' needs to lookup the submodule name to generate the archive when given a revision that is not checked out. * 'git fetch' when given the --recurse-submodules=on-demand option (or configuration) needs to lookup submodule names by path from the database rather than reading from the worktree. For new submodule it needs to lookup the name from its path to allow cloning new submodules into the .git folder so they can be checked out without any network interaction when the user does a checkout of that revision. Signed-off-by: Heiko Voigt Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- submodule.c | 1 + 1 file changed, 1 insertion(+) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index b8747f5c26..7822dc57a5 100644 --- a/submodule.c +++ b/submodule.c @@ -355,6 +355,7 @@ int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) default: if (!strcmp(arg, "on-demand")) return RECURSE_SUBMODULES_ON_DEMAND; + /* TODO: remove the die for history parsing here */ die("bad %s argument: %s", opt, arg); } } -- cgit v1.2.3 From 0d9f282c943a0799999176a2d8291ecbad060389 Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Mon, 17 Aug 2015 17:21:58 -0700 Subject: submodule: extract functions for config set and lookup This is one step towards using the new configuration API. We just extract these functions to make replacing the actual code easier. Signed-off-by: Heiko Voigt Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- submodule.c | 142 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 45 deletions(-) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index 7822dc57a5..c3b5f44008 100644 --- a/submodule.c +++ b/submodule.c @@ -41,6 +41,76 @@ static int gitmodules_is_unmerged; */ static int gitmodules_is_modified; +static const char *get_name_for_path(const char *path) +{ + struct string_list_item *path_option; + if (path == NULL) { + if (config_name_for_path.nr > 0) + return config_name_for_path.items[0].util; + else + return NULL; + } + path_option = unsorted_string_list_lookup(&config_name_for_path, path); + if (!path_option) + return NULL; + return path_option->util; +} + +static void set_name_for_path(const char *path, const char *name, int namelen) +{ + struct string_list_item *config; + config = unsorted_string_list_lookup(&config_name_for_path, path); + if (config) + free(config->util); + else + config = string_list_append(&config_name_for_path, xstrdup(path)); + config->util = xmemdupz(name, namelen); +} + +static const char *get_ignore_for_name(const char *name) +{ + struct string_list_item *ignore_option; + ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, name); + if (!ignore_option) + return NULL; + + return ignore_option->util; +} + +static void set_ignore_for_name(const char *name, int namelen, const char *ignore) +{ + struct string_list_item *config; + char *name_cstr = xmemdupz(name, namelen); + config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr); + if (config) { + free(config->util); + free(name_cstr); + } else + config = string_list_append(&config_ignore_for_name, name_cstr); + config->util = xstrdup(ignore); +} + +static int get_fetch_recurse_for_name(const char *name) +{ + struct string_list_item *fetch_recurse; + fetch_recurse = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name); + if (!fetch_recurse) + return RECURSE_SUBMODULES_NONE; + + return (intptr_t) fetch_recurse->util; +} + +static void set_fetch_recurse_for_name(const char *name, int namelen, int fetch_recurse) +{ + struct string_list_item *config; + char *name_cstr = xmemdupz(name, namelen); + config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr); + if (!config) + config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr); + else + free(name_cstr); + config->util = (void *)(intptr_t) fetch_recurse; +} int is_staging_gitmodules_ok(void) { @@ -55,7 +125,7 @@ int is_staging_gitmodules_ok(void) int update_path_in_gitmodules(const char *oldpath, const char *newpath) { struct strbuf entry = STRBUF_INIT; - struct string_list_item *path_option; + const char *path; if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ return -1; @@ -63,13 +133,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) if (gitmodules_is_unmerged) die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); - path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath); - if (!path_option) { + path = get_name_for_path(oldpath); + if (!path) { warning(_("Could not find section in .gitmodules where path=%s"), oldpath); return -1; } strbuf_addstr(&entry, "submodule."); - strbuf_addstr(&entry, path_option->util); + strbuf_addstr(&entry, path); strbuf_addstr(&entry, ".path"); if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) { /* Maybe the user already did that, don't error out here */ @@ -89,7 +159,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) int remove_path_from_gitmodules(const char *path) { struct strbuf sect = STRBUF_INIT; - struct string_list_item *path_option; + const char *path_option; if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ return -1; @@ -97,13 +167,13 @@ int remove_path_from_gitmodules(const char *path) if (gitmodules_is_unmerged) die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); - path_option = unsorted_string_list_lookup(&config_name_for_path, path); + path_option = get_name_for_path(path); if (!path_option) { warning(_("Could not find section in .gitmodules where path=%s"), path); return -1; } strbuf_addstr(§, "submodule."); - strbuf_addstr(§, path_option->util); + strbuf_addstr(§, path_option); if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) { /* Maybe the user already did that, don't error out here */ warning(_("Could not remove .gitmodules entry for %s"), path); @@ -165,12 +235,11 @@ done: 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); + const char *name = get_name_for_path(path); + if (name) { + const char *ignore = get_ignore_for_name(name); + if (ignore) + handle_ignore_submodules_arg(diffopt, ignore); else if (gitmodules_is_unmerged) DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); } @@ -221,7 +290,6 @@ void gitmodules_config(void) int parse_submodule_config_option(const char *var, const char *value) { - struct string_list_item *config; const char *name, *key; int namelen; @@ -232,22 +300,14 @@ int parse_submodule_config_option(const char *var, const char *value) if (!value) return config_error_nonbool(var); - 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 = xmemdupz(name, namelen); + set_name_for_path(value, name, namelen); + } else if (!strcmp(key, "fetchrecursesubmodules")) { - char *name_cstr = xmemdupz(name, namelen); - config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr); - if (!config) - config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr); - else - free(name_cstr); - config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value); + int fetch_recurse = parse_fetch_recurse_submodules_arg(var, value); + + set_fetch_recurse_for_name(name, namelen, fetch_recurse); + } else if (!strcmp(key, "ignore")) { - char *name_cstr; if (!value) return config_error_nonbool(var); @@ -258,14 +318,7 @@ int parse_submodule_config_option(const char *var, const char *value) return 0; } - name_cstr = xmemdupz(name, namelen); - config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr); - if (config) { - free(config->util); - free(name_cstr); - } else - config = string_list_append(&config_ignore_for_name, name_cstr); - config->util = xstrdup(value); + set_ignore_for_name(name, namelen, value); return 0; } return 0; @@ -646,7 +699,7 @@ static void calculate_changed_submodule_paths(void) struct argv_array argv = ARGV_ARRAY_INIT; /* No need to check if there are no submodules configured */ - if (!config_name_for_path.nr) + if (!get_name_for_path(NULL)) return; init_revisions(&rev, NULL); @@ -693,7 +746,7 @@ int fetch_populated_submodules(const struct argv_array *options, int i, result = 0; struct child_process cp = CHILD_PROCESS_INIT; struct argv_array argv = ARGV_ARRAY_INIT; - struct string_list_item *name_for_path; + const char *name_for_path; const char *work_tree = get_git_work_tree(); if (!work_tree) goto out; @@ -724,18 +777,17 @@ int fetch_populated_submodules(const struct argv_array *options, continue; name = ce->name; - name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name); + name_for_path = get_name_for_path(ce->name); if (name_for_path) - name = name_for_path->util; + name = name_for_path; default_argv = "yes"; if (command_line_option == RECURSE_SUBMODULES_DEFAULT) { - struct string_list_item *fetch_recurse_submodules_option; - fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name); - if (fetch_recurse_submodules_option) { - if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF) + int fetch_recurse_option = get_fetch_recurse_for_name(name); + if (fetch_recurse_option != RECURSE_SUBMODULES_NONE) { + if (fetch_recurse_option == RECURSE_SUBMODULES_OFF) continue; - if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) { + if (fetch_recurse_option == RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) continue; default_argv = "on-demand"; -- cgit v1.2.3 From 851e18c3859ad0f9f7e91fdb4d6cce5a8272420b Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Mon, 17 Aug 2015 17:21:59 -0700 Subject: submodule: use new config API for worktree configurations We remove the extracted functions and directly parse into and read out of the cache. This allows us to have one unified way of accessing submodule configuration values specific to single submodules. Regardless whether we need to access a configuration from history or from the worktree. Signed-off-by: Heiko Voigt Signed-off-by: Junio C Hamano Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- submodule.c | 160 ++++++++++-------------------------------------------------- 1 file changed, 26 insertions(+), 134 deletions(-) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index c3b5f44008..97355eb7ad 100644 --- a/submodule.c +++ b/submodule.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "submodule-config.h" #include "submodule.h" #include "dir.h" #include "diff.h" @@ -12,9 +13,6 @@ #include "argv-array.h" #include "blob.h" -static struct string_list config_name_for_path; -static struct string_list config_fetch_recurse_submodules_for_name; -static struct string_list config_ignore_for_name; static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; static struct string_list changed_submodule_paths; static int initialized_fetch_ref_tips; @@ -41,77 +39,6 @@ static int gitmodules_is_unmerged; */ static int gitmodules_is_modified; -static const char *get_name_for_path(const char *path) -{ - struct string_list_item *path_option; - if (path == NULL) { - if (config_name_for_path.nr > 0) - return config_name_for_path.items[0].util; - else - return NULL; - } - path_option = unsorted_string_list_lookup(&config_name_for_path, path); - if (!path_option) - return NULL; - return path_option->util; -} - -static void set_name_for_path(const char *path, const char *name, int namelen) -{ - struct string_list_item *config; - config = unsorted_string_list_lookup(&config_name_for_path, path); - if (config) - free(config->util); - else - config = string_list_append(&config_name_for_path, xstrdup(path)); - config->util = xmemdupz(name, namelen); -} - -static const char *get_ignore_for_name(const char *name) -{ - struct string_list_item *ignore_option; - ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, name); - if (!ignore_option) - return NULL; - - return ignore_option->util; -} - -static void set_ignore_for_name(const char *name, int namelen, const char *ignore) -{ - struct string_list_item *config; - char *name_cstr = xmemdupz(name, namelen); - config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr); - if (config) { - free(config->util); - free(name_cstr); - } else - config = string_list_append(&config_ignore_for_name, name_cstr); - config->util = xstrdup(ignore); -} - -static int get_fetch_recurse_for_name(const char *name) -{ - struct string_list_item *fetch_recurse; - fetch_recurse = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name); - if (!fetch_recurse) - return RECURSE_SUBMODULES_NONE; - - return (intptr_t) fetch_recurse->util; -} - -static void set_fetch_recurse_for_name(const char *name, int namelen, int fetch_recurse) -{ - struct string_list_item *config; - char *name_cstr = xmemdupz(name, namelen); - config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr); - if (!config) - config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr); - else - free(name_cstr); - config->util = (void *)(intptr_t) fetch_recurse; -} - int is_staging_gitmodules_ok(void) { return !gitmodules_is_modified; @@ -125,7 +52,7 @@ int is_staging_gitmodules_ok(void) int update_path_in_gitmodules(const char *oldpath, const char *newpath) { struct strbuf entry = STRBUF_INIT; - const char *path; + const struct submodule *submodule; if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ return -1; @@ -133,13 +60,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) if (gitmodules_is_unmerged) die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); - path = get_name_for_path(oldpath); - if (!path) { + submodule = submodule_from_path(null_sha1, oldpath); + if (!submodule || !submodule->name) { warning(_("Could not find section in .gitmodules where path=%s"), oldpath); return -1; } strbuf_addstr(&entry, "submodule."); - strbuf_addstr(&entry, path); + strbuf_addstr(&entry, submodule->name); strbuf_addstr(&entry, ".path"); if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) { /* Maybe the user already did that, don't error out here */ @@ -159,7 +86,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath) int remove_path_from_gitmodules(const char *path) { struct strbuf sect = STRBUF_INIT; - const char *path_option; + const struct submodule *submodule; if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ return -1; @@ -167,13 +94,13 @@ int remove_path_from_gitmodules(const char *path) if (gitmodules_is_unmerged) die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); - path_option = get_name_for_path(path); - if (!path_option) { + submodule = submodule_from_path(null_sha1, path); + if (!submodule || !submodule->name) { warning(_("Could not find section in .gitmodules where path=%s"), path); return -1; } strbuf_addstr(§, "submodule."); - strbuf_addstr(§, path_option); + strbuf_addstr(§, submodule->name); if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) { /* Maybe the user already did that, don't error out here */ warning(_("Could not remove .gitmodules entry for %s"), path); @@ -235,11 +162,10 @@ done: void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, const char *path) { - const char *name = get_name_for_path(path); - if (name) { - const char *ignore = get_ignore_for_name(name); - if (ignore) - handle_ignore_submodules_arg(diffopt, ignore); + const struct submodule *submodule = submodule_from_path(null_sha1, path); + if (submodule) { + if (submodule->ignore) + handle_ignore_submodules_arg(diffopt, submodule->ignore); else if (gitmodules_is_unmerged) DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); } @@ -288,42 +214,6 @@ void gitmodules_config(void) } } -int parse_submodule_config_option(const char *var, const char *value) -{ - const char *name, *key; - int namelen; - - if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name) - return 0; - - if (!strcmp(key, "path")) { - if (!value) - return config_error_nonbool(var); - - set_name_for_path(value, name, namelen); - - } else if (!strcmp(key, "fetchrecursesubmodules")) { - int fetch_recurse = parse_fetch_recurse_submodules_arg(var, value); - - set_fetch_recurse_for_name(name, namelen, fetch_recurse); - - } else if (!strcmp(key, "ignore")) { - - if (!value) - return config_error_nonbool(var); - - 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; - } - - set_ignore_for_name(name, namelen, value); - return 0; - } - return 0; -} - void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *arg) { @@ -699,7 +589,7 @@ static void calculate_changed_submodule_paths(void) struct argv_array argv = ARGV_ARRAY_INIT; /* No need to check if there are no submodules configured */ - if (!get_name_for_path(NULL)) + if (!submodule_from_path(NULL, NULL)) return; init_revisions(&rev, NULL); @@ -746,7 +636,6 @@ int fetch_populated_submodules(const struct argv_array *options, int i, result = 0; struct child_process cp = CHILD_PROCESS_INIT; struct argv_array argv = ARGV_ARRAY_INIT; - const char *name_for_path; const char *work_tree = get_git_work_tree(); if (!work_tree) goto out; @@ -771,23 +660,26 @@ int fetch_populated_submodules(const struct argv_array *options, struct strbuf submodule_git_dir = STRBUF_INIT; struct strbuf submodule_prefix = STRBUF_INIT; const struct cache_entry *ce = active_cache[i]; - const char *git_dir, *name, *default_argv; + const char *git_dir, *default_argv; + const struct submodule *submodule; if (!S_ISGITLINK(ce->ce_mode)) continue; - name = ce->name; - name_for_path = get_name_for_path(ce->name); - if (name_for_path) - name = name_for_path; + submodule = submodule_from_path(null_sha1, ce->name); + if (!submodule) + submodule = submodule_from_name(null_sha1, ce->name); default_argv = "yes"; if (command_line_option == RECURSE_SUBMODULES_DEFAULT) { - int fetch_recurse_option = get_fetch_recurse_for_name(name); - if (fetch_recurse_option != RECURSE_SUBMODULES_NONE) { - if (fetch_recurse_option == RECURSE_SUBMODULES_OFF) + if (submodule && + submodule->fetch_recurse != + RECURSE_SUBMODULES_NONE) { + if (submodule->fetch_recurse == + RECURSE_SUBMODULES_OFF) continue; - if (fetch_recurse_option == RECURSE_SUBMODULES_ON_DEMAND) { + if (submodule->fetch_recurse == + RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) continue; default_argv = "on-demand"; -- cgit v1.2.3 From 027771fcb153e0003bcb2d68e1838594a48b0161 Mon Sep 17 00:00:00 2001 From: Heiko Voigt Date: Mon, 17 Aug 2015 17:22:00 -0700 Subject: submodule: allow erroneous values for the fetchRecurseSubmodules option We should not die when reading the submodule config cache since the user might not be able to get out of that situation when the configuration is part of the history. We should handle this condition later when the value is about to be used. Signed-off-by: Heiko Voigt Signed-off-by: Stefan Beller Signed-off-by: Junio C Hamano --- submodule.c | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'submodule.c') diff --git a/submodule.c b/submodule.c index 97355eb7ad..48225595ef 100644 --- a/submodule.c +++ b/submodule.c @@ -288,21 +288,6 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f, strbuf_release(&sb); } -int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) -{ - switch (git_config_maybe_bool(opt, arg)) { - case 1: - return RECURSE_SUBMODULES_ON; - case 0: - return RECURSE_SUBMODULES_OFF; - default: - if (!strcmp(arg, "on-demand")) - return RECURSE_SUBMODULES_ON_DEMAND; - /* TODO: remove the die for history parsing here */ - die("bad %s argument: %s", opt, arg); - } -} - void show_submodule_summary(FILE *f, const char *path, const char *line_prefix, unsigned char one[20], unsigned char two[20], -- cgit v1.2.3