{"thread":{"id":"39397","subject":"[PATCH v3 0/4] submodule config lookup API","startedAt":"2015-05-21T17:06:16Z","lastAt":"2015-05-26T15:46:10Z","messageCount":9,"participants":["Heiko Voigt","Junio C Hamano","René Scharfe"],"isPatch":true,"patchVersion":3,"patchTotal":4},"messages":[{"id":"261803","messageId":"20150521170616.GA22979@book.hvoigt.net","threadId":"39397","inReplyTo":null,"subject":"[PATCH v3 0/4] submodule config lookup API","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-21T17:06:16Z","receivedAt":"2015-05-21T17:06:16Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"This is finally the next iteration of the submodule config api. The last\niteration can be found here:\n\nhttp://article.gmane.org/gmane.comp.version-control.git/252601\n\nThis iteration fixes the lookup of submodules by name\n(submodule_from_name()) where one needed to pass in the gitmodule sha1\nby mistake. To keep it simple for the user and behave as documented we\nshould take the commit sha1 which is now fixed here. We now also test\nthe lookup by name in the api tests.\n\nThis should be ready for inclusion.\n\nCheers Heiko\n\nHere is the interdiff to the last iteration:\n\ndiff --git a/submodule-config.c b/submodule-config.c\nindex 96623ad..177767d 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -25,6 +25,11 @@ struct submodule_entry {\n \tstruct submodule *config;\n };\n \n+enum lookup_type {\n+\tlookup_name,\n+\tlookup_path,\n+};\n+\n static struct submodule_cache cache;\n static int is_cache_init;\n \n@@ -330,8 +335,32 @@ release_return:\n \treturn ret;\n }\n \n-static const struct submodule *config_from_path(struct submodule_cache *cache,\n-\t\tconst unsigned char *commit_sha1, const char *path)\n+static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,\n+\t\t\t\t      unsigned char *gitmodules_sha1)\n+{\n+\tstruct strbuf rev = STRBUF_INIT;\n+\tint ret = 0;\n+\n+\tif (is_null_sha1(commit_sha1)) {\n+\t\thashcpy(gitmodules_sha1, null_sha1);\n+\t\treturn 1;\n+\t}\n+\n+\tstrbuf_addf(&rev, \"%s:.gitmodules\", sha1_to_hex(commit_sha1));\n+\tif (get_sha1(rev.buf, gitmodules_sha1) >= 0)\n+\t\tret = 1;\n+\n+\tstrbuf_release(&rev);\n+\treturn ret;\n+}\n+\n+/* This does a lookup of a submodule configuration by name or by path\n+ * (key) with on-demand reading of the appropriate .gitmodules from\n+ * revisions.\n+ */\n+static const struct submodule *config_from(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *key,\n+\t\tenum lookup_type lookup_type)\n {\n \tstruct strbuf rev = STRBUF_INIT;\n \tunsigned long config_size;\n@@ -346,7 +375,7 @@ static const struct submodule *config_from_path(struct submodule_cache *cache,\n \t * return the first submodule. Can be used to check whether\n \t * there are any submodules parsed.\n \t */\n-\tif (!commit_sha1 || !path) {\n+\tif (!commit_sha1 || !key) {\n \t\tstruct hashmap_iter iter;\n \t\tstruct submodule_entry *entry;\n \n@@ -357,24 +386,27 @@ static const struct submodule *config_from_path(struct submodule_cache *cache,\n \t\treturn entry->config;\n \t}\n \n-\tif (is_null_sha1(commit_sha1))\n-\t\treturn cache_lookup_path(cache, null_sha1, path);\n-\n-\tstrbuf_addf(&rev, \"%s:.gitmodules\", sha1_to_hex(commit_sha1));\n-\tif (get_sha1(rev.buf, sha1) < 0)\n-\t\tgoto free_rev;\n+\tif (!gitmodule_sha1_from_commit(commit_sha1, sha1))\n+\t\treturn submodule;\n \n-\tsubmodule = cache_lookup_path(cache, sha1, path);\n+\tswitch (lookup_type) {\n+\t\tcase lookup_name:\n+\t\t\tsubmodule = cache_lookup_name(cache, sha1, key);\n+\t\t\tbreak;\n+\t\tcase lookup_path:\n+\t\t\tsubmodule = cache_lookup_path(cache, sha1, key);\n+\t\t\tbreak;\n+\t}\n \tif (submodule)\n-\t\tgoto free_rev;\n+\t\treturn submodule;\n \n \tconfig = read_sha1_file(sha1, &type, &config_size);\n \tif (!config)\n-\t\tgoto free_rev;\n+\t\treturn submodule;\n \n \tif (type != OBJ_BLOB) {\n \t\tfree(config);\n-\t\tgoto free_rev;\n+\t\treturn submodule;\n \t}\n \n \t/* fill the submodule config into the cache */\n@@ -386,13 +418,30 @@ static const struct submodule *config_from_path(struct submodule_cache *cache,\n \t\t\t&parameter);\n \tfree(config);\n \n-\tsubmodule = cache_lookup_path(cache, sha1, path);\n+\tswitch (lookup_type) {\n+\t\tcase lookup_name:\n+\t\t\tsubmodule = cache_lookup_name(cache, sha1, key);\n+\t\t\tbreak;\n+\t\tcase lookup_path:\n+\t\t\tsubmodule = cache_lookup_path(cache, sha1, key);\n+\t\t\tbreak;\n+\t}\n \n-free_rev:\n-\tstrbuf_release(&rev);\n \treturn submodule;\n }\n \n+static const struct submodule *config_from_path(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *path)\n+{\n+\treturn config_from(cache, commit_sha1, path, lookup_path);\n+}\n+\n+static const struct submodule *config_from_name(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *name)\n+{\n+\treturn config_from(cache, commit_sha1, name, lookup_name);\n+}\n+\n static void ensure_cache_init(void)\n {\n \tif (is_cache_init)\n@@ -418,7 +467,7 @@ const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n \t\tconst char *name)\n {\n \tensure_cache_init();\n-\treturn cache_lookup_name(&cache, commit_sha1, name);\n+\treturn config_from_name(&cache, commit_sha1, name);\n }\n \n const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\ndiff --git a/submodule-config.h b/submodule-config.h\nindex 9061e4e..58afc83 100644\n--- a/submodule-config.h\n+++ b/submodule-config.h\n@@ -24,6 +24,6 @@ const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n \t\tconst char *name);\n const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\n \t\tconst char *path);\n-void submodule_free(void);\n+void submodule_free();\n \n #endif /* SUBMODULE_CONFIG_H */\ndiff --git a/t/t7411-submodule-config.sh b/t/t7411-submodule-config.sh\nindex 1ba0d64..fc97c33 100755\n--- a/t/t7411-submodule-config.sh\n+++ b/t/t7411-submodule-config.sh\n@@ -38,7 +38,7 @@ Submodule name: 'submodule' for path 'submodule'\n Submodule name: 'submodule' for path 'submodule'\n EOF\n \n-test_expect_success 'test parsing of submodule config' '\n+test_expect_success 'test parsing and lookup of submodule config by path' '\n \t(cd super &&\n \t\ttest-submodule-config \\\n \t\t\tHEAD^ a \\\n@@ -50,6 +50,18 @@ test_expect_success 'test parsing of submodule config' '\n \t)\n '\n \n+test_expect_success 'test parsing and lookup of submodule config by name' '\n+\t(cd super &&\n+\t\ttest-submodule-config --name \\\n+\t\t\tHEAD^ a \\\n+\t\t\tHEAD a \\\n+\t\t\tHEAD^ submodule \\\n+\t\t\tHEAD submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n cat >super/expect_error <<EOF\n Submodule name: 'a' for path 'b'\n Submodule name: 'submodule' for path 'submodule'\ndiff --git a/test-submodule-config.c b/test-submodule-config.c\nindex eda356e..dab8c27 100644\n--- a/test-submodule-config.c\n+++ b/test-submodule-config.c\n@@ -19,12 +19,15 @@ int main(int argc, char **argv)\n \tchar **arg = argv;\n \tint my_argc = argc;\n \tint output_url = 0;\n+\tint lookup_name = 0;\n \n \targ++;\n \tmy_argc--;\n \twhile (starts_with(arg[0], \"--\")) {\n \t\tif (!strcmp(arg[0], \"--url\"))\n \t\t\toutput_url = 1;\n+\t\tif (!strcmp(arg[0], \"--name\"))\n+\t\t\tlookup_name = 1;\n \t\targ++;\n \t\tmy_argc--;\n \t}\n@@ -40,26 +43,29 @@ int main(int argc, char **argv)\n \t\tunsigned char commit_sha1[20];\n \t\tconst struct submodule *submodule;\n \t\tconst char *commit;\n-\t\tconst char *path;\n+\t\tconst char *path_or_name;\n \n \t\tcommit = arg[0];\n-\t\tpath = arg[1];\n+\t\tpath_or_name = arg[1];\n \n \t\tif (commit[0] == '\\0')\n \t\t\thashcpy(commit_sha1, null_sha1);\n \t\telse if (get_sha1(commit, commit_sha1) < 0)\n \t\t\tdie_usage(argc, argv, \"Commit not found.\");\n \n-\t\tsubmodule = submodule_from_path(commit_sha1, path);\n+\t\tif (lookup_name) {\n+\t\t\tsubmodule = submodule_from_name(commit_sha1, path_or_name);\n+\t\t} else\n+\t\t\tsubmodule = submodule_from_path(commit_sha1, path_or_name);\n \t\tif (!submodule)\n \t\t\tdie_usage(argc, argv, \"Submodule not found.\");\n \n \t\tif (output_url)\n \t\t\tprintf(\"Submodule url: '%s' for path '%s'\\n\",\n-\t\t\t\t\tsubmodule->url, path);\n+\t\t\t\t\tsubmodule->url, submodule->path);\n \t\telse\n \t\t\tprintf(\"Submodule name: '%s' for path '%s'\\n\",\n-\t\t\t\t\tsubmodule->name, path);\n+\t\t\t\t\tsubmodule->name, submodule->path);\n \n \t\targ += 2;\n \t}\n\nHeiko Voigt (4):\n  implement submodule config cache for lookup of submodule names\n  extract functions for submodule config set and lookup\n  use new config API for worktree configurations of submodules\n  do not die on error of parsing fetchrecursesubmodules option\n\n .gitignore                                       |   1 +\n Documentation/technical/api-submodule-config.txt |  63 +++\n Makefile                                         |   2 +\n builtin/checkout.c                               |   1 +\n builtin/fetch.c                                  |   1 +\n diff.c                                           |   1 +\n submodule-config.c                               | 484 +++++++++++++++++++++++\n submodule-config.h                               |  29 ++\n submodule.c                                      | 122 ++----\n submodule.h                                      |   4 +-\n t/t7411-submodule-config.sh                      | 153 +++++++\n test-submodule-config.c                          |  76 ++++\n 12 files changed, 839 insertions(+), 98 deletions(-)\n create mode 100644 Documentation/technical/api-submodule-config.txt\n create mode 100644 submodule-config.c\n create mode 100644 submodule-config.h\n create mode 100755 t/t7411-submodule-config.sh\n create mode 100644 test-submodule-config.c\n\n-- \n2.1.0.rc0.52.gaa544bf\n"},{"id":"261804","messageId":"20150521170725.GB22979@book.hvoigt.net","threadId":"39397","inReplyTo":"20150521170616.GA22979@book.hvoigt.net","subject":"[PATCH v3 1/4] implement submodule config cache for lookup of submodule names","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-21T17:07:25Z","receivedAt":"2015-05-21T17:07:25Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"This submodule configuration cache allows us to lazily read .gitmodules\nconfigurations by commit into a runtime cache which can then be used to\neasily lookup values from it. Currently only the values for path or name\nare stored but it can be extended for any value needed.\n\nIt is expected that .gitmodules files do not change often between\ncommits. Thats why we lookup the .gitmodules sha1 from a commit and then\neither lookup an already parsed configuration or parse and cache an\nunknown one for each sha1. The cache is lazily build on demand for each\nrequested commit.\n\nThis cache can be used for all purposes which need knowledge about\nsubmodule configurations. Example use cases are:\n\n * Recursive submodule checkout needs lookup a submodule name from its\n   path when a submodule first appears. This needs be done before this\n   configuration exists in the worktree.\n\n * The implementation of submodule support for 'git archive' needs to\n   lookup the submodule name to generate the archive when given a\n   revision that is not checked out.\n\n * 'git fetch' when given the --recurse-submodules=on-demand option (or\n   configuration) needs to lookup submodule names by path from the\n   database rather than reading from the worktree. For new submodule it\n   needs to lookup the name from its path to allow cloning new\n   submodules into the .git folder so they can be checked out without\n   any network interaction when the user does a checkout of that\n   revision.\n\nSigned-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n---\n .gitignore                                       |   1 +\n Documentation/technical/api-submodule-config.txt |  46 +++\n Makefile                                         |   2 +\n submodule-config.c                               | 445 +++++++++++++++++++++++\n submodule-config.h                               |  27 ++\n submodule.c                                      |   1 +\n submodule.h                                      |   1 +\n t/t7411-submodule-config.sh                      |  85 +++++\n test-submodule-config.c                          |  66 ++++\n 9 files changed, 674 insertions(+)\n create mode 100644 Documentation/technical/api-submodule-config.txt\n create mode 100644 submodule-config.c\n create mode 100644 submodule-config.h\n create mode 100755 t/t7411-submodule-config.sh\n create mode 100644 test-submodule-config.c\n\ndiff --git a/.gitignore b/.gitignore\nindex 42294e5..96a4d43 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -198,6 +198,7 @@\n /test-sha1\n /test-sigchain\n /test-string-list\n+/test-submodule-config\n /test-subprocess\n /test-svn-fe\n /test-urlmatch-normalization\ndiff --git a/Documentation/technical/api-submodule-config.txt b/Documentation/technical/api-submodule-config.txt\nnew file mode 100644\nindex 0000000..2ff4907\n--- /dev/null\n+++ b/Documentation/technical/api-submodule-config.txt\n@@ -0,0 +1,46 @@\n+submodule config cache API\n+==========================\n+\n+The submodule config cache API allows to read submodule\n+configurations/information from specified revisions. Internally\n+information is lazily read into a cache that is used to avoid\n+unnecessary parsing of the same .gitmodule files. Lookups can be done by\n+submodule path or name.\n+\n+Usage\n+-----\n+\n+The caller can look up information about submodules by using the\n+`submodule_from_path()` or `submodule_from_name()` functions. They return\n+a `struct submodule` which contains the values. The API automatically\n+initializes and allocates the needed infrastructure on-demand.\n+\n+If the internal cache might grow too big or when the caller is done with\n+the API, all internally cached values can be freed with submodule_free().\n+\n+Data Structures\n+---------------\n+\n+`struct submodule`::\n+\n+\tThis structure is used to return the information about one\n+\tsubmodule for a certain revision. It is returned by the lookup\n+\tfunctions.\n+\n+Functions\n+---------\n+\n+`void submodule_free()`::\n+\n+\tUse these to free the internally cached values.\n+\n+`const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`::\n+\n+\tLookup values for one submodule by its commit_sha1 and path or\n+\tname.\n+\n+`const struct submodule *submodule_from_name(const unsigned char *commit_sha1, const char *name)`::\n+\n+\tThe same as above but lookup by name.\n+\n+For an example usage see test-submodule-config.c.\ndiff --git a/Makefile b/Makefile\nindex 07ea105..f286557 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -570,6 +570,7 @@ TEST_PROGRAMS_NEED_X += test-scrap-cache-tree\n TEST_PROGRAMS_NEED_X += test-sha1\n TEST_PROGRAMS_NEED_X += test-sigchain\n TEST_PROGRAMS_NEED_X += test-string-list\n+TEST_PROGRAMS_NEED_X += test-submodule-config\n TEST_PROGRAMS_NEED_X += test-subprocess\n TEST_PROGRAMS_NEED_X += test-svn-fe\n TEST_PROGRAMS_NEED_X += test-urlmatch-normalization\n@@ -879,6 +880,7 @@ LIB_OBJS += strbuf.o\n LIB_OBJS += streaming.o\n LIB_OBJS += string-list.o\n LIB_OBJS += submodule.o\n+LIB_OBJS += submodule-config.o\n LIB_OBJS += symlinks.o\n LIB_OBJS += tag.o\n LIB_OBJS += trace.o\ndiff --git a/submodule-config.c b/submodule-config.c\nnew file mode 100644\nindex 0000000..a455c22\n--- /dev/null\n+++ b/submodule-config.c\n@@ -0,0 +1,445 @@\n+#include \"cache.h\"\n+#include \"submodule-config.h\"\n+#include \"submodule.h\"\n+#include \"strbuf.h\"\n+\n+/*\n+ * submodule cache lookup structure\n+ * There is one shared set of 'struct submodule' entries which can be\n+ * looked up by their sha1 blob id of the .gitmodule file and either\n+ * using path or name as key.\n+ * for_path stores submodule entries with path as key\n+ * for_name stores submodule entries with name as key\n+ */\n+struct submodule_cache {\n+\tstruct hashmap for_path;\n+\tstruct hashmap for_name;\n+};\n+\n+/*\n+ * thin wrapper struct needed to insert 'struct submodule' entries to\n+ * the hashmap\n+ */\n+struct submodule_entry {\n+\tstruct hashmap_entry ent;\n+\tstruct submodule *config;\n+};\n+\n+enum lookup_type {\n+\tlookup_name,\n+\tlookup_path,\n+};\n+\n+static struct submodule_cache cache;\n+static int is_cache_init;\n+\n+static int config_path_cmp(const struct submodule_entry *a,\n+\t\t\t   const struct submodule_entry *b,\n+\t\t\t   const void *unused)\n+{\n+\treturn strcmp(a->config->path, b->config->path) ||\n+\t       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);\n+}\n+\n+static int config_name_cmp(const struct submodule_entry *a,\n+\t\t\t   const struct submodule_entry *b,\n+\t\t\t   const void *unused)\n+{\n+\treturn strcmp(a->config->name, b->config->name) ||\n+\t       hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);\n+}\n+\n+static void cache_init(struct submodule_cache *cache)\n+{\n+\thashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);\n+\thashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);\n+}\n+\n+static void free_one_config(struct submodule_entry *entry)\n+{\n+\tfree((void *) entry->config->path);\n+\tfree((void *) entry->config->name);\n+\tfree(entry->config);\n+}\n+\n+static void cache_free(struct submodule_cache *cache)\n+{\n+\tstruct hashmap_iter iter;\n+\tstruct submodule_entry *entry;\n+\n+\t/*\n+\t * We iterate over the name hash here to be symmetric with the\n+\t * allocation of struct submodule entries. Each is allocated by\n+\t * their .gitmodule blob sha1 and submodule name.\n+\t */\n+\thashmap_iter_init(&cache->for_name, &iter);\n+\twhile ((entry = hashmap_iter_next(&iter)))\n+\t\tfree_one_config(entry);\n+\n+\thashmap_free(&cache->for_path, 1);\n+\thashmap_free(&cache->for_name, 1);\n+}\n+\n+static unsigned int hash_sha1_string(const unsigned char *sha1,\n+\t\t\t\t     const char *string)\n+{\n+\treturn memhash(sha1, 20) + strhash(string);\n+}\n+\n+static void cache_put_path(struct submodule_cache *cache,\n+\t\t\t   struct submodule *submodule)\n+{\n+\tunsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,\n+\t\t\t\t\t     submodule->path);\n+\tstruct submodule_entry *e = xmalloc(sizeof(*e));\n+\thashmap_entry_init(e, hash);\n+\te->config = submodule;\n+\thashmap_put(&cache->for_path, e);\n+}\n+\n+static void cache_remove_path(struct submodule_cache *cache,\n+\t\t\t      struct submodule *submodule)\n+{\n+\tunsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,\n+\t\t\t\t\t     submodule->path);\n+\tstruct submodule_entry e;\n+\tstruct submodule_entry *removed;\n+\thashmap_entry_init(&e, hash);\n+\te.config = submodule;\n+\tremoved = hashmap_remove(&cache->for_path, &e, NULL);\n+\tfree(removed);\n+}\n+\n+static void cache_add(struct submodule_cache *cache,\n+\t\t      struct submodule *submodule)\n+{\n+\tunsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,\n+\t\t\t\t\t     submodule->name);\n+\tstruct submodule_entry *e = xmalloc(sizeof(*e));\n+\thashmap_entry_init(e, hash);\n+\te->config = submodule;\n+\thashmap_add(&cache->for_name, e);\n+}\n+\n+static const struct submodule *cache_lookup_path(struct submodule_cache *cache,\n+\t\tconst unsigned char *gitmodules_sha1, const char *path)\n+{\n+\tstruct submodule_entry *entry;\n+\tunsigned int hash = hash_sha1_string(gitmodules_sha1, path);\n+\tstruct submodule_entry key;\n+\tstruct submodule key_config;\n+\n+\thashcpy(key_config.gitmodules_sha1, gitmodules_sha1);\n+\tkey_config.path = path;\n+\n+\thashmap_entry_init(&key, hash);\n+\tkey.config = &key_config;\n+\n+\tentry = hashmap_get(&cache->for_path, &key, NULL);\n+\tif (entry)\n+\t\treturn entry->config;\n+\treturn NULL;\n+}\n+\n+static struct submodule *cache_lookup_name(struct submodule_cache *cache,\n+\t\tconst unsigned char *gitmodules_sha1, const char *name)\n+{\n+\tstruct submodule_entry *entry;\n+\tunsigned int hash = hash_sha1_string(gitmodules_sha1, name);\n+\tstruct submodule_entry key;\n+\tstruct submodule key_config;\n+\n+\thashcpy(key_config.gitmodules_sha1, gitmodules_sha1);\n+\tkey_config.name = name;\n+\n+\thashmap_entry_init(&key, hash);\n+\tkey.config = &key_config;\n+\n+\tentry = hashmap_get(&cache->for_name, &key, NULL);\n+\tif (entry)\n+\t\treturn entry->config;\n+\treturn NULL;\n+}\n+\n+static int name_and_item_from_var(const char *var, struct strbuf *name,\n+\t\t\t\t  struct strbuf *item)\n+{\n+\tconst char *subsection, *key;\n+\tint subsection_len, parse;\n+\tparse = parse_config_key(var, \"submodule\", &subsection,\n+\t\t\t&subsection_len, &key);\n+\tif (parse < 0 || !subsection)\n+\t\treturn 0;\n+\n+\tstrbuf_add(name, subsection, subsection_len);\n+\tstrbuf_addstr(item, key);\n+\n+\treturn 1;\n+}\n+\n+static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,\n+\t\tconst unsigned char *gitmodules_sha1, const char *name)\n+{\n+\tstruct submodule *submodule;\n+\tstruct strbuf name_buf = STRBUF_INIT;\n+\n+\tsubmodule = cache_lookup_name(cache, gitmodules_sha1, name);\n+\tif (submodule)\n+\t\treturn submodule;\n+\n+\tsubmodule = xmalloc(sizeof(*submodule));\n+\n+\tstrbuf_addstr(&name_buf, name);\n+\tsubmodule->name = strbuf_detach(&name_buf, NULL);\n+\n+\tsubmodule->path = NULL;\n+\tsubmodule->url = NULL;\n+\tsubmodule->fetch_recurse = RECURSE_SUBMODULES_NONE;\n+\tsubmodule->ignore = NULL;\n+\n+\thashcpy(submodule->gitmodules_sha1, gitmodules_sha1);\n+\n+\tcache_add(cache, submodule);\n+\n+\treturn submodule;\n+}\n+\n+static void warn_multiple_config(const unsigned char *commit_sha1,\n+\t\t\t\t const char *name, const char *option)\n+{\n+\tconst char *commit_string = \"WORKTREE\";\n+\tif (commit_sha1)\n+\t\tcommit_string = sha1_to_hex(commit_sha1);\n+\twarning(\"%s:.gitmodules, multiple configurations found for \"\n+\t\t\t\"'submodule.%s.%s'. Skipping second one!\",\n+\t\t\tcommit_string, name, option);\n+}\n+\n+struct parse_config_parameter {\n+\tstruct submodule_cache *cache;\n+\tconst unsigned char *commit_sha1;\n+\tconst unsigned char *gitmodules_sha1;\n+\tint overwrite;\n+};\n+\n+static int parse_config(const char *var, const char *value, void *data)\n+{\n+\tstruct parse_config_parameter *me = data;\n+\tstruct submodule *submodule;\n+\tstruct strbuf name = STRBUF_INIT, item = STRBUF_INIT;\n+\tint ret = 0;\n+\n+\t/* this also ensures that we only parse submodule entries */\n+\tif (!name_and_item_from_var(var, &name, &item))\n+\t\treturn 0;\n+\n+\tsubmodule = lookup_or_create_by_name(me->cache, me->gitmodules_sha1,\n+\t\t\tname.buf);\n+\n+\tif (!strcmp(item.buf, \"path\")) {\n+\t\tstruct strbuf path = STRBUF_INIT;\n+\t\tif (!value) {\n+\t\t\tret = config_error_nonbool(var);\n+\t\t\tgoto release_return;\n+\t\t}\n+\t\tif (!me->overwrite && submodule->path != NULL) {\n+\t\t\twarn_multiple_config(me->commit_sha1, submodule->name,\n+\t\t\t\t\t\"path\");\n+\t\t\tgoto release_return;\n+\t\t}\n+\n+\t\tif (submodule->path)\n+\t\t\tcache_remove_path(me->cache, submodule);\n+\t\tfree((void *) submodule->path);\n+\t\tstrbuf_addstr(&path, value);\n+\t\tsubmodule->path = strbuf_detach(&path, NULL);\n+\t\tcache_put_path(me->cache, submodule);\n+\t} else if (!strcmp(item.buf, \"fetchrecursesubmodules\")) {\n+\t\tif (!me->overwrite &&\n+\t\t    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) {\n+\t\t\twarn_multiple_config(me->commit_sha1, submodule->name,\n+\t\t\t\t\t\"fetchrecursesubmodules\");\n+\t\t\tgoto release_return;\n+\t\t}\n+\n+\t\tsubmodule->fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);\n+\t} else if (!strcmp(item.buf, \"ignore\")) {\n+\t\tstruct strbuf ignore = STRBUF_INIT;\n+\t\tif (!me->overwrite && submodule->ignore != NULL) {\n+\t\t\twarn_multiple_config(me->commit_sha1, submodule->name,\n+\t\t\t\t\t\"ignore\");\n+\t\t\tgoto release_return;\n+\t\t}\n+\t\tif (!value) {\n+\t\t\tret = config_error_nonbool(var);\n+\t\t\tgoto release_return;\n+\t\t}\n+\t\tif (strcmp(value, \"untracked\") && strcmp(value, \"dirty\") &&\n+\t\t    strcmp(value, \"all\") && strcmp(value, \"none\")) {\n+\t\t\twarning(\"Invalid parameter '%s' for config option \"\n+\t\t\t\t\t\"'submodule.%s.ignore'\", value, var);\n+\t\t\tgoto release_return;\n+\t\t}\n+\n+\t\tfree((void *) submodule->ignore);\n+\t\tstrbuf_addstr(&ignore, value);\n+\t\tsubmodule->ignore = strbuf_detach(&ignore, NULL);\n+\t} else if (!strcmp(item.buf, \"url\")) {\n+\t\tstruct strbuf url = STRBUF_INIT;\n+\t\tif (!value) {\n+\t\t\tret = config_error_nonbool(var);\n+\t\t\tgoto release_return;\n+\t\t}\n+\t\tif (!me->overwrite && submodule->url != NULL) {\n+\t\t\twarn_multiple_config(me->commit_sha1, submodule->name,\n+\t\t\t\t\t\"url\");\n+\t\t\tgoto release_return;\n+\t\t}\n+\n+\t\tfree((void *) submodule->url);\n+\t\tstrbuf_addstr(&url, value);\n+\t\tsubmodule->url = strbuf_detach(&url, NULL);\n+\t}\n+\n+release_return:\n+\tstrbuf_release(&name);\n+\tstrbuf_release(&item);\n+\n+\treturn ret;\n+}\n+\n+static int gitmodule_sha1_from_commit(const unsigned char *commit_sha1,\n+\t\t\t\t      unsigned char *gitmodules_sha1)\n+{\n+\tstruct strbuf rev = STRBUF_INIT;\n+\tint ret = 0;\n+\n+\tif (is_null_sha1(commit_sha1)) {\n+\t\thashcpy(gitmodules_sha1, null_sha1);\n+\t\treturn 1;\n+\t}\n+\n+\tstrbuf_addf(&rev, \"%s:.gitmodules\", sha1_to_hex(commit_sha1));\n+\tif (get_sha1(rev.buf, gitmodules_sha1) >= 0)\n+\t\tret = 1;\n+\n+\tstrbuf_release(&rev);\n+\treturn ret;\n+}\n+\n+/* This does a lookup of a submodule configuration by name or by path\n+ * (key) with on-demand reading of the appropriate .gitmodules from\n+ * revisions.\n+ */\n+static const struct submodule *config_from(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *key,\n+\t\tenum lookup_type lookup_type)\n+{\n+\tstruct strbuf rev = STRBUF_INIT;\n+\tunsigned long config_size;\n+\tchar *config;\n+\tunsigned char sha1[20];\n+\tenum object_type type;\n+\tconst struct submodule *submodule = NULL;\n+\tstruct parse_config_parameter parameter;\n+\n+\t/*\n+\t * If any parameter except the cache is a NULL pointer just\n+\t * return the first submodule. Can be used to check whether\n+\t * there are any submodules parsed.\n+\t */\n+\tif (!commit_sha1 || !key) {\n+\t\tstruct hashmap_iter iter;\n+\t\tstruct submodule_entry *entry;\n+\n+\t\thashmap_iter_init(&cache->for_name, &iter);\n+\t\tentry = hashmap_iter_next(&iter);\n+\t\tif (!entry)\n+\t\t\treturn NULL;\n+\t\treturn entry->config;\n+\t}\n+\n+\tif (!gitmodule_sha1_from_commit(commit_sha1, sha1))\n+\t\treturn submodule;\n+\n+\tswitch (lookup_type) {\n+\t\tcase lookup_name:\n+\t\t\tsubmodule = cache_lookup_name(cache, sha1, key);\n+\t\t\tbreak;\n+\t\tcase lookup_path:\n+\t\t\tsubmodule = cache_lookup_path(cache, sha1, key);\n+\t\t\tbreak;\n+\t}\n+\tif (submodule)\n+\t\treturn submodule;\n+\n+\tconfig = read_sha1_file(sha1, &type, &config_size);\n+\tif (!config)\n+\t\treturn submodule;\n+\n+\tif (type != OBJ_BLOB) {\n+\t\tfree(config);\n+\t\treturn submodule;\n+\t}\n+\n+\t/* fill the submodule config into the cache */\n+\tparameter.cache = cache;\n+\tparameter.commit_sha1 = commit_sha1;\n+\tparameter.gitmodules_sha1 = sha1;\n+\tparameter.overwrite = 0;\n+\tgit_config_from_buf(parse_config, rev.buf, config, config_size,\n+\t\t\t&parameter);\n+\tfree(config);\n+\n+\tswitch (lookup_type) {\n+\t\tcase lookup_name:\n+\t\t\tsubmodule = cache_lookup_name(cache, sha1, key);\n+\t\t\tbreak;\n+\t\tcase lookup_path:\n+\t\t\tsubmodule = cache_lookup_path(cache, sha1, key);\n+\t\t\tbreak;\n+\t}\n+\n+\treturn submodule;\n+}\n+\n+static const struct submodule *config_from_path(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *path)\n+{\n+\treturn config_from(cache, commit_sha1, path, lookup_path);\n+}\n+\n+static const struct submodule *config_from_name(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *name)\n+{\n+\treturn config_from(cache, commit_sha1, name, lookup_name);\n+}\n+\n+static void ensure_cache_init(void)\n+{\n+\tif (is_cache_init)\n+\t\treturn;\n+\n+\tcache_init(&cache);\n+\tis_cache_init = 1;\n+}\n+\n+const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n+\t\tconst char *name)\n+{\n+\tensure_cache_init();\n+\treturn config_from_name(&cache, commit_sha1, name);\n+}\n+\n+const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\n+\t\tconst char *path)\n+{\n+\tensure_cache_init();\n+\treturn config_from_path(&cache, commit_sha1, path);\n+}\n+\n+void submodule_free(void)\n+{\n+\tcache_free(&cache);\n+\tis_cache_init = 0;\n+}\ndiff --git a/submodule-config.h b/submodule-config.h\nnew file mode 100644\nindex 0000000..972496d\n--- /dev/null\n+++ b/submodule-config.h\n@@ -0,0 +1,27 @@\n+#ifndef SUBMODULE_CONFIG_CACHE_H\n+#define SUBMODULE_CONFIG_CACHE_H\n+\n+#include \"hashmap.h\"\n+#include \"strbuf.h\"\n+\n+/*\n+ * Submodule entry containing the information about a certain submodule\n+ * in a certain revision.\n+ */\n+struct submodule {\n+\tconst char *path;\n+\tconst char *name;\n+\tconst char *url;\n+\tint fetch_recurse;\n+\tconst char *ignore;\n+\t/* the sha1 blob id of the responsible .gitmodules file */\n+\tunsigned char gitmodules_sha1[20];\n+};\n+\n+const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n+\t\tconst char *name);\n+const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\n+\t\tconst char *path);\n+void submodule_free();\n+\n+#endif /* SUBMODULE_CONFIG_H */\ndiff --git a/submodule.c b/submodule.c\nindex b80ecac..85e2b12 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -355,6 +355,7 @@ int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)\n \tdefault:\n \t\tif (!strcmp(arg, \"on-demand\"))\n \t\t\treturn RECURSE_SUBMODULES_ON_DEMAND;\n+\t\t/* TODO: remove the die for history parsing here */\n \t\tdie(\"bad %s argument: %s\", opt, arg);\n \t}\n }\ndiff --git a/submodule.h b/submodule.h\nindex 7beec48..920fef3 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -5,6 +5,7 @@ struct diff_options;\n struct argv_array;\n \n enum {\n+\tRECURSE_SUBMODULES_NONE = -2,\n \tRECURSE_SUBMODULES_ON_DEMAND = -1,\n \tRECURSE_SUBMODULES_OFF = 0,\n \tRECURSE_SUBMODULES_DEFAULT = 1,\ndiff --git a/t/t7411-submodule-config.sh b/t/t7411-submodule-config.sh\nnew file mode 100755\nindex 0000000..2602bc5\n--- /dev/null\n+++ b/t/t7411-submodule-config.sh\n@@ -0,0 +1,85 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2014 Heiko Voigt\n+#\n+\n+test_description='Test submodules config cache infrastructure\n+\n+This test verifies that parsing .gitmodules configuration directly\n+from the database works.\n+'\n+\n+TEST_NO_CREATE_REPO=1\n+. ./test-lib.sh\n+\n+test_expect_success 'submodule config cache setup' '\n+\tmkdir submodule &&\n+\t(cd submodule &&\n+\t\tgit init &&\n+\t\techo a >a &&\n+\t\tgit add . &&\n+\t\tgit commit -ma\n+\t) &&\n+\tmkdir super &&\n+\t(cd super &&\n+\t\tgit init &&\n+\t\tgit submodule add ../submodule &&\n+\t\tgit submodule add ../submodule a &&\n+\t\tgit commit -m \"add as submodule and as a\" &&\n+\t\tgit mv a b &&\n+\t\tgit commit -m \"move a to b\"\n+\t)\n+'\n+\n+cat >super/expect <<EOF\n+Submodule name: 'a' for path 'a'\n+Submodule name: 'a' for path 'b'\n+Submodule name: 'submodule' for path 'submodule'\n+Submodule name: 'submodule' for path 'submodule'\n+EOF\n+\n+test_expect_success 'test parsing and lookup of submodule config by path' '\n+\t(cd super &&\n+\t\ttest-submodule-config \\\n+\t\t\tHEAD^ a \\\n+\t\t\tHEAD b \\\n+\t\t\tHEAD^ submodule \\\n+\t\t\tHEAD submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'test parsing and lookup of submodule config by name' '\n+\t(cd super &&\n+\t\ttest-submodule-config --name \\\n+\t\t\tHEAD^ a \\\n+\t\t\tHEAD a \\\n+\t\t\tHEAD^ submodule \\\n+\t\t\tHEAD submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+cat >super/expect_error <<EOF\n+Submodule name: 'a' for path 'b'\n+Submodule name: 'submodule' for path 'submodule'\n+EOF\n+\n+test_expect_success 'error in one submodule config lets continue' '\n+\t(cd super &&\n+\t\tcp .gitmodules .gitmodules.bak &&\n+\t\techo \"\tvalue = \\\"\" >>.gitmodules &&\n+\t\tgit add .gitmodules &&\n+\t\tmv .gitmodules.bak .gitmodules &&\n+\t\tgit commit -m \"add error\" &&\n+\t\ttest-submodule-config \\\n+\t\t\tHEAD b \\\n+\t\t\tHEAD submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect_error actual\n+\t)\n+'\n+\n+test_done\ndiff --git a/test-submodule-config.c b/test-submodule-config.c\nnew file mode 100644\nindex 0000000..f3c3918\n--- /dev/null\n+++ b/test-submodule-config.c\n@@ -0,0 +1,66 @@\n+#include \"cache.h\"\n+#include \"submodule-config.h\"\n+\n+static void die_usage(int argc, char **argv, const char *msg)\n+{\n+\tfprintf(stderr, \"%s\\n\", msg);\n+\tfprintf(stderr, \"Usage: %s [<commit> <submodulepath>] ...\\n\", argv[0]);\n+\texit(1);\n+}\n+\n+int main(int argc, char **argv)\n+{\n+\tchar **arg = argv;\n+\tint my_argc = argc;\n+\tint output_url = 0;\n+\tint lookup_name = 0;\n+\n+\targ++;\n+\tmy_argc--;\n+\twhile (starts_with(arg[0], \"--\")) {\n+\t\tif (!strcmp(arg[0], \"--url\"))\n+\t\t\toutput_url = 1;\n+\t\tif (!strcmp(arg[0], \"--name\"))\n+\t\t\tlookup_name = 1;\n+\t\targ++;\n+\t\tmy_argc--;\n+\t}\n+\n+\tif (my_argc % 2 != 0)\n+\t\tdie_usage(argc, argv, \"Wrong number of arguments.\");\n+\n+\twhile (*arg) {\n+\t\tunsigned char commit_sha1[20];\n+\t\tconst struct submodule *submodule;\n+\t\tconst char *commit;\n+\t\tconst char *path_or_name;\n+\n+\t\tcommit = arg[0];\n+\t\tpath_or_name = arg[1];\n+\n+\t\tif (commit[0] == '\\0')\n+\t\t\thashcpy(commit_sha1, null_sha1);\n+\t\telse if (get_sha1(commit, commit_sha1) < 0)\n+\t\t\tdie_usage(argc, argv, \"Commit not found.\");\n+\n+\t\tif (lookup_name) {\n+\t\t\tsubmodule = submodule_from_name(commit_sha1, path_or_name);\n+\t\t} else\n+\t\t\tsubmodule = submodule_from_path(commit_sha1, path_or_name);\n+\t\tif (!submodule)\n+\t\t\tdie_usage(argc, argv, \"Submodule not found.\");\n+\n+\t\tif (output_url)\n+\t\t\tprintf(\"Submodule url: '%s' for path '%s'\\n\",\n+\t\t\t\t\tsubmodule->url, submodule->path);\n+\t\telse\n+\t\t\tprintf(\"Submodule name: '%s' for path '%s'\\n\",\n+\t\t\t\t\tsubmodule->name, submodule->path);\n+\n+\t\targ += 2;\n+\t}\n+\n+\tsubmodule_free();\n+\n+\treturn 0;\n+}\n-- \n2.1.0.rc0.52.gaa544bf\n"},{"id":"261806","messageId":"20150521170815.GC22979@book.hvoigt.net","threadId":"39397","inReplyTo":"20150521170616.GA22979@book.hvoigt.net","subject":"[PATCH v3 2/4] extract functions for submodule config set and lookup","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-21T17:08:15Z","receivedAt":"2015-05-21T17:08:15Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"This is one step towards using the new configuration API. We just\nextract these functions to make replacing the actual code easier.\n\nSigned-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n---\n submodule.c | 142 +++++++++++++++++++++++++++++++++++++++++-------------------\n 1 file changed, 97 insertions(+), 45 deletions(-)\n\ndiff --git a/submodule.c b/submodule.c\nindex 85e2b12..86ec2e3 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -41,6 +41,76 @@ static int gitmodules_is_unmerged;\n  */\n static int gitmodules_is_modified;\n \n+static const char *get_name_for_path(const char *path)\n+{\n+\tstruct string_list_item *path_option;\n+\tif (path == NULL) {\n+\t\tif (config_name_for_path.nr > 0)\n+\t\t\treturn config_name_for_path.items[0].util;\n+\t\telse\n+\t\t\treturn NULL;\n+\t}\n+\tpath_option = unsorted_string_list_lookup(&config_name_for_path, path);\n+\tif (!path_option)\n+\t\treturn NULL;\n+\treturn path_option->util;\n+}\n+\n+static void set_name_for_path(const char *path, const char *name, int namelen)\n+{\n+\tstruct string_list_item *config;\n+\tconfig = unsorted_string_list_lookup(&config_name_for_path, path);\n+\tif (config)\n+\t\tfree(config->util);\n+\telse\n+\t\tconfig = string_list_append(&config_name_for_path, xstrdup(path));\n+\tconfig->util = xmemdupz(name, namelen);\n+}\n+\n+static const char *get_ignore_for_name(const char *name)\n+{\n+\tstruct string_list_item *ignore_option;\n+\tignore_option = unsorted_string_list_lookup(&config_ignore_for_name, name);\n+\tif (!ignore_option)\n+\t\treturn NULL;\n+\n+\treturn ignore_option->util;\n+}\n+\n+static void set_ignore_for_name(const char *name, int namelen, const char *ignore)\n+{\n+\tstruct string_list_item *config;\n+\tchar *name_cstr = xmemdupz(name, namelen);\n+\tconfig = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);\n+\tif (config) {\n+\t\tfree(config->util);\n+\t\tfree(name_cstr);\n+\t} else\n+\t\tconfig = string_list_append(&config_ignore_for_name, name_cstr);\n+\tconfig->util = xstrdup(ignore);\n+}\n+\n+static int get_fetch_recurse_for_name(const char *name)\n+{\n+\tstruct string_list_item *fetch_recurse;\n+\tfetch_recurse = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);\n+\tif (!fetch_recurse)\n+\t\treturn RECURSE_SUBMODULES_NONE;\n+\n+\treturn (intptr_t) fetch_recurse->util;\n+}\n+\n+static void set_fetch_recurse_for_name(const char *name, int namelen, int fetch_recurse)\n+{\n+\tstruct string_list_item *config;\n+\tchar *name_cstr = xmemdupz(name, namelen);\n+\tconfig = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);\n+\tif (!config)\n+\t\tconfig = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);\n+\telse\n+\t\tfree(name_cstr);\n+\tconfig->util = (void *)(intptr_t) fetch_recurse;\n+}\n \n int is_staging_gitmodules_ok(void)\n {\n@@ -55,7 +125,7 @@ int is_staging_gitmodules_ok(void)\n int update_path_in_gitmodules(const char *oldpath, const char *newpath)\n {\n \tstruct strbuf entry = STRBUF_INIT;\n-\tstruct string_list_item *path_option;\n+\tconst char *path;\n \n \tif (!file_exists(\".gitmodules\")) /* Do nothing without .gitmodules */\n \t\treturn -1;\n@@ -63,13 +133,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)\n \tif (gitmodules_is_unmerged)\n \t\tdie(_(\"Cannot change unmerged .gitmodules, resolve merge conflicts first\"));\n \n-\tpath_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);\n-\tif (!path_option) {\n+\tpath = get_name_for_path(oldpath);\n+\tif (!path) {\n \t\twarning(_(\"Could not find section in .gitmodules where path=%s\"), oldpath);\n \t\treturn -1;\n \t}\n \tstrbuf_addstr(&entry, \"submodule.\");\n-\tstrbuf_addstr(&entry, path_option->util);\n+\tstrbuf_addstr(&entry, path);\n \tstrbuf_addstr(&entry, \".path\");\n \tif (git_config_set_in_file(\".gitmodules\", entry.buf, newpath) < 0) {\n \t\t/* Maybe the user already did that, don't error out here */\n@@ -89,7 +159,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)\n int remove_path_from_gitmodules(const char *path)\n {\n \tstruct strbuf sect = STRBUF_INIT;\n-\tstruct string_list_item *path_option;\n+\tconst char *path_option;\n \n \tif (!file_exists(\".gitmodules\")) /* Do nothing without .gitmodules */\n \t\treturn -1;\n@@ -97,13 +167,13 @@ int remove_path_from_gitmodules(const char *path)\n \tif (gitmodules_is_unmerged)\n \t\tdie(_(\"Cannot change unmerged .gitmodules, resolve merge conflicts first\"));\n \n-\tpath_option = unsorted_string_list_lookup(&config_name_for_path, path);\n+\tpath_option = get_name_for_path(path);\n \tif (!path_option) {\n \t\twarning(_(\"Could not find section in .gitmodules where path=%s\"), path);\n \t\treturn -1;\n \t}\n \tstrbuf_addstr(&sect, \"submodule.\");\n-\tstrbuf_addstr(&sect, path_option->util);\n+\tstrbuf_addstr(&sect, path_option);\n \tif (git_config_rename_section_in_file(\".gitmodules\", sect.buf, NULL) < 0) {\n \t\t/* Maybe the user already did that, don't error out here */\n \t\twarning(_(\"Could not remove .gitmodules entry for %s\"), path);\n@@ -165,12 +235,11 @@ done:\n void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,\n \t\t\t\t\t     const char *path)\n {\n-\tstruct string_list_item *path_option, *ignore_option;\n-\tpath_option = unsorted_string_list_lookup(&config_name_for_path, path);\n-\tif (path_option) {\n-\t\tignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);\n-\t\tif (ignore_option)\n-\t\t\thandle_ignore_submodules_arg(diffopt, ignore_option->util);\n+\tconst char *name = get_name_for_path(path);\n+\tif (name) {\n+\t\tconst char *ignore = get_ignore_for_name(name);\n+\t\tif (ignore)\n+\t\t\thandle_ignore_submodules_arg(diffopt, ignore);\n \t\telse if (gitmodules_is_unmerged)\n \t\t\tDIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);\n \t}\n@@ -221,7 +290,6 @@ void gitmodules_config(void)\n \n int parse_submodule_config_option(const char *var, const char *value)\n {\n-\tstruct string_list_item *config;\n \tconst char *name, *key;\n \tint namelen;\n \n@@ -232,22 +300,14 @@ int parse_submodule_config_option(const char *var, const char *value)\n \t\tif (!value)\n \t\t\treturn config_error_nonbool(var);\n \n-\t\tconfig = unsorted_string_list_lookup(&config_name_for_path, value);\n-\t\tif (config)\n-\t\t\tfree(config->util);\n-\t\telse\n-\t\t\tconfig = string_list_append(&config_name_for_path, xstrdup(value));\n-\t\tconfig->util = xmemdupz(name, namelen);\n+\t\tset_name_for_path(value, name, namelen);\n+\n \t} else if (!strcmp(key, \"fetchrecursesubmodules\")) {\n-\t\tchar *name_cstr = xmemdupz(name, namelen);\n-\t\tconfig = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);\n-\t\tif (!config)\n-\t\t\tconfig = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);\n-\t\telse\n-\t\t\tfree(name_cstr);\n-\t\tconfig->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);\n+\t\tint fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);\n+\n+\t\tset_fetch_recurse_for_name(name, namelen, fetch_recurse);\n+\n \t} else if (!strcmp(key, \"ignore\")) {\n-\t\tchar *name_cstr;\n \n \t\tif (!value)\n \t\t\treturn config_error_nonbool(var);\n@@ -258,14 +318,7 @@ int parse_submodule_config_option(const char *var, const char *value)\n \t\t\treturn 0;\n \t\t}\n \n-\t\tname_cstr = xmemdupz(name, namelen);\n-\t\tconfig = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);\n-\t\tif (config) {\n-\t\t\tfree(config->util);\n-\t\t\tfree(name_cstr);\n-\t\t} else\n-\t\t\tconfig = string_list_append(&config_ignore_for_name, name_cstr);\n-\t\tconfig->util = xstrdup(value);\n+\t\tset_ignore_for_name(name, namelen, value);\n \t\treturn 0;\n \t}\n \treturn 0;\n@@ -654,7 +707,7 @@ static void calculate_changed_submodule_paths(void)\n \tstruct argv_array argv = ARGV_ARRAY_INIT;\n \n \t/* No need to check if there are no submodules configured */\n-\tif (!config_name_for_path.nr)\n+\tif (!get_name_for_path(NULL))\n \t\treturn;\n \n \tinit_revisions(&rev, NULL);\n@@ -701,7 +754,7 @@ int fetch_populated_submodules(const struct argv_array *options,\n \tint i, result = 0;\n \tstruct child_process cp;\n \tstruct argv_array argv = ARGV_ARRAY_INIT;\n-\tstruct string_list_item *name_for_path;\n+\tconst char *name_for_path;\n \tconst char *work_tree = get_git_work_tree();\n \tif (!work_tree)\n \t\tgoto out;\n@@ -733,18 +786,17 @@ int fetch_populated_submodules(const struct argv_array *options,\n \t\t\tcontinue;\n \n \t\tname = ce->name;\n-\t\tname_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);\n+\t\tname_for_path = get_name_for_path(ce->name);\n \t\tif (name_for_path)\n-\t\t\tname = name_for_path->util;\n+\t\t\tname = name_for_path;\n \n \t\tdefault_argv = \"yes\";\n \t\tif (command_line_option == RECURSE_SUBMODULES_DEFAULT) {\n-\t\t\tstruct string_list_item *fetch_recurse_submodules_option;\n-\t\t\tfetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);\n-\t\t\tif (fetch_recurse_submodules_option) {\n-\t\t\t\tif ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)\n+\t\t\tint fetch_recurse_option = get_fetch_recurse_for_name(name);\n+\t\t\tif (fetch_recurse_option != RECURSE_SUBMODULES_NONE) {\n+\t\t\t\tif (fetch_recurse_option == RECURSE_SUBMODULES_OFF)\n \t\t\t\t\tcontinue;\n-\t\t\t\tif ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {\n+\t\t\t\tif (fetch_recurse_option == RECURSE_SUBMODULES_ON_DEMAND) {\n \t\t\t\t\tif (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))\n \t\t\t\t\t\tcontinue;\n \t\t\t\t\tdefault_argv = \"on-demand\";\n-- \n2.1.0.rc0.52.gaa544bf\n"},{"id":"261805","messageId":"20150521170846.GD22979@book.hvoigt.net","threadId":"39397","inReplyTo":"20150521170616.GA22979@book.hvoigt.net","subject":"[PATCH v3 3/4] use new config API for worktree configurations of submodules","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-21T17:08:46Z","receivedAt":"2015-05-21T17:08:46Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"We remove the extracted functions and directly parse into and read out\nof the cache. This allows us to have one unified way of accessing\nsubmodule configuration values specific to single submodules. Regardless\nwhether we need to access a configuration from history or from the\nworktree.\n\nSigned-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n---\n Documentation/technical/api-submodule-config.txt |  19 ++-\n builtin/checkout.c                               |   1 +\n diff.c                                           |   1 +\n submodule-config.c                               |  12 ++\n submodule-config.h                               |   1 +\n submodule.c                                      | 160 ++++-------------------\n submodule.h                                      |   1 -\n t/t7411-submodule-config.sh                      |  37 +++++-\n test-submodule-config.c                          |  10 ++\n 9 files changed, 104 insertions(+), 138 deletions(-)\n\ndiff --git a/Documentation/technical/api-submodule-config.txt b/Documentation/technical/api-submodule-config.txt\nindex 2ff4907..2ea520a 100644\n--- a/Documentation/technical/api-submodule-config.txt\n+++ b/Documentation/technical/api-submodule-config.txt\n@@ -10,10 +10,18 @@ submodule path or name.\n Usage\n -----\n \n+To initialize the cache with configurations from the worktree the caller\n+typically first calls `gitmodules_config()` to read values from the\n+worktree .gitmodules and then to overlay the local git config values\n+`parse_submodule_config_option()` from the config parsing\n+infrastructure.\n+\n The caller can look up information about submodules by using the\n `submodule_from_path()` or `submodule_from_name()` functions. They return\n a `struct submodule` which contains the values. The API automatically\n-initializes and allocates the needed infrastructure on-demand.\n+initializes and allocates the needed infrastructure on-demand. If the\n+caller does only want to lookup values from revisions the initialization\n+can be skipped.\n \n If the internal cache might grow too big or when the caller is done with\n the API, all internally cached values can be freed with submodule_free().\n@@ -34,6 +42,11 @@ Functions\n \n \tUse these to free the internally cached values.\n \n+`int parse_submodule_config_option(const char *var, const char *value)`::\n+\n+\tCan be passed to the config parsing infrastructure to parse\n+\tlocal (worktree) submodule configurations.\n+\n `const struct submodule *submodule_from_path(const unsigned char *commit_sha1, const char *path)`::\n \n \tLookup values for one submodule by its commit_sha1 and path or\n@@ -43,4 +56,8 @@ Functions\n \n \tThe same as above but lookup by name.\n \n+If given the null_sha1 as commit_sha1 the local configuration of a\n+submodule will be returned (e.g. consolidated values from local git\n+configuration and the .gitmodules file in the worktree).\n+\n For an example usage see test-submodule-config.c.\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex f1dc56e..a820afd 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -18,6 +18,7 @@\n #include \"xdiff-interface.h\"\n #include \"ll-merge.h\"\n #include \"resolve-undo.h\"\n+#include \"submodule-config.h\"\n #include \"submodule.h\"\n #include \"argv-array.h\"\n \ndiff --git a/diff.c b/diff.c\nindex f66716f..485e0e6 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -13,6 +13,7 @@\n #include \"utf8.h\"\n #include \"userdiff.h\"\n #include \"sigchain.h\"\n+#include \"submodule-config.h\"\n #include \"submodule.h\"\n #include \"ll-merge.h\"\n #include \"string-list.h\"\ndiff --git a/submodule-config.c b/submodule-config.c\nindex a455c22..e47640f 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -424,6 +424,18 @@ static void ensure_cache_init(void)\n \tis_cache_init = 1;\n }\n \n+int parse_submodule_config_option(const char *var, const char *value)\n+{\n+\tstruct parse_config_parameter parameter;\n+\tparameter.cache = &cache;\n+\tparameter.commit_sha1 = NULL;\n+\tparameter.gitmodules_sha1 = null_sha1;\n+\tparameter.overwrite = 1;\n+\n+\tensure_cache_init();\n+\treturn parse_config(var, value, &parameter);\n+}\n+\n const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n \t\tconst char *name)\n {\ndiff --git a/submodule-config.h b/submodule-config.h\nindex 972496d..2083cb9 100644\n--- a/submodule-config.h\n+++ b/submodule-config.h\n@@ -18,6 +18,7 @@ struct submodule {\n \tunsigned char gitmodules_sha1[20];\n };\n \n+int parse_submodule_config_option(const char *var, const char *value);\n const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n \t\tconst char *name);\n const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\ndiff --git a/submodule.c b/submodule.c\nindex 86ec2e3..188b4d2 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"submodule-config.h\"\n #include \"submodule.h\"\n #include \"dir.h\"\n #include \"diff.h\"\n@@ -12,9 +13,6 @@\n #include \"argv-array.h\"\n #include \"blob.h\"\n \n-static struct string_list config_name_for_path;\n-static struct string_list config_fetch_recurse_submodules_for_name;\n-static struct string_list config_ignore_for_name;\n static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;\n static struct string_list changed_submodule_paths;\n static int initialized_fetch_ref_tips;\n@@ -41,77 +39,6 @@ static int gitmodules_is_unmerged;\n  */\n static int gitmodules_is_modified;\n \n-static const char *get_name_for_path(const char *path)\n-{\n-\tstruct string_list_item *path_option;\n-\tif (path == NULL) {\n-\t\tif (config_name_for_path.nr > 0)\n-\t\t\treturn config_name_for_path.items[0].util;\n-\t\telse\n-\t\t\treturn NULL;\n-\t}\n-\tpath_option = unsorted_string_list_lookup(&config_name_for_path, path);\n-\tif (!path_option)\n-\t\treturn NULL;\n-\treturn path_option->util;\n-}\n-\n-static void set_name_for_path(const char *path, const char *name, int namelen)\n-{\n-\tstruct string_list_item *config;\n-\tconfig = unsorted_string_list_lookup(&config_name_for_path, path);\n-\tif (config)\n-\t\tfree(config->util);\n-\telse\n-\t\tconfig = string_list_append(&config_name_for_path, xstrdup(path));\n-\tconfig->util = xmemdupz(name, namelen);\n-}\n-\n-static const char *get_ignore_for_name(const char *name)\n-{\n-\tstruct string_list_item *ignore_option;\n-\tignore_option = unsorted_string_list_lookup(&config_ignore_for_name, name);\n-\tif (!ignore_option)\n-\t\treturn NULL;\n-\n-\treturn ignore_option->util;\n-}\n-\n-static void set_ignore_for_name(const char *name, int namelen, const char *ignore)\n-{\n-\tstruct string_list_item *config;\n-\tchar *name_cstr = xmemdupz(name, namelen);\n-\tconfig = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);\n-\tif (config) {\n-\t\tfree(config->util);\n-\t\tfree(name_cstr);\n-\t} else\n-\t\tconfig = string_list_append(&config_ignore_for_name, name_cstr);\n-\tconfig->util = xstrdup(ignore);\n-}\n-\n-static int get_fetch_recurse_for_name(const char *name)\n-{\n-\tstruct string_list_item *fetch_recurse;\n-\tfetch_recurse = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);\n-\tif (!fetch_recurse)\n-\t\treturn RECURSE_SUBMODULES_NONE;\n-\n-\treturn (intptr_t) fetch_recurse->util;\n-}\n-\n-static void set_fetch_recurse_for_name(const char *name, int namelen, int fetch_recurse)\n-{\n-\tstruct string_list_item *config;\n-\tchar *name_cstr = xmemdupz(name, namelen);\n-\tconfig = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);\n-\tif (!config)\n-\t\tconfig = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);\n-\telse\n-\t\tfree(name_cstr);\n-\tconfig->util = (void *)(intptr_t) fetch_recurse;\n-}\n-\n int is_staging_gitmodules_ok(void)\n {\n \treturn !gitmodules_is_modified;\n@@ -125,7 +52,7 @@ int is_staging_gitmodules_ok(void)\n int update_path_in_gitmodules(const char *oldpath, const char *newpath)\n {\n \tstruct strbuf entry = STRBUF_INIT;\n-\tconst char *path;\n+\tconst struct submodule *submodule;\n \n \tif (!file_exists(\".gitmodules\")) /* Do nothing without .gitmodules */\n \t\treturn -1;\n@@ -133,13 +60,13 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)\n \tif (gitmodules_is_unmerged)\n \t\tdie(_(\"Cannot change unmerged .gitmodules, resolve merge conflicts first\"));\n \n-\tpath = get_name_for_path(oldpath);\n-\tif (!path) {\n+\tsubmodule = submodule_from_path(null_sha1, oldpath);\n+\tif (!submodule || !submodule->name) {\n \t\twarning(_(\"Could not find section in .gitmodules where path=%s\"), oldpath);\n \t\treturn -1;\n \t}\n \tstrbuf_addstr(&entry, \"submodule.\");\n-\tstrbuf_addstr(&entry, path);\n+\tstrbuf_addstr(&entry, submodule->name);\n \tstrbuf_addstr(&entry, \".path\");\n \tif (git_config_set_in_file(\".gitmodules\", entry.buf, newpath) < 0) {\n \t\t/* Maybe the user already did that, don't error out here */\n@@ -159,7 +86,7 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)\n int remove_path_from_gitmodules(const char *path)\n {\n \tstruct strbuf sect = STRBUF_INIT;\n-\tconst char *path_option;\n+\tconst struct submodule *submodule;\n \n \tif (!file_exists(\".gitmodules\")) /* Do nothing without .gitmodules */\n \t\treturn -1;\n@@ -167,13 +94,13 @@ int remove_path_from_gitmodules(const char *path)\n \tif (gitmodules_is_unmerged)\n \t\tdie(_(\"Cannot change unmerged .gitmodules, resolve merge conflicts first\"));\n \n-\tpath_option = get_name_for_path(path);\n-\tif (!path_option) {\n+\tsubmodule = submodule_from_path(null_sha1, path);\n+\tif (!submodule || !submodule->name) {\n \t\twarning(_(\"Could not find section in .gitmodules where path=%s\"), path);\n \t\treturn -1;\n \t}\n \tstrbuf_addstr(&sect, \"submodule.\");\n-\tstrbuf_addstr(&sect, path_option);\n+\tstrbuf_addstr(&sect, submodule->name);\n \tif (git_config_rename_section_in_file(\".gitmodules\", sect.buf, NULL) < 0) {\n \t\t/* Maybe the user already did that, don't error out here */\n \t\twarning(_(\"Could not remove .gitmodules entry for %s\"), path);\n@@ -235,11 +162,10 @@ done:\n void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,\n \t\t\t\t\t     const char *path)\n {\n-\tconst char *name = get_name_for_path(path);\n-\tif (name) {\n-\t\tconst char *ignore = get_ignore_for_name(name);\n-\t\tif (ignore)\n-\t\t\thandle_ignore_submodules_arg(diffopt, ignore);\n+\tconst struct submodule *submodule = submodule_from_path(null_sha1, path);\n+\tif (submodule) {\n+\t\tif (submodule->ignore)\n+\t\t\thandle_ignore_submodules_arg(diffopt, submodule->ignore);\n \t\telse if (gitmodules_is_unmerged)\n \t\t\tDIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);\n \t}\n@@ -288,42 +214,6 @@ void gitmodules_config(void)\n \t}\n }\n \n-int parse_submodule_config_option(const char *var, const char *value)\n-{\n-\tconst char *name, *key;\n-\tint namelen;\n-\n-\tif (parse_config_key(var, \"submodule\", &name, &namelen, &key) < 0 || !name)\n-\t\treturn 0;\n-\n-\tif (!strcmp(key, \"path\")) {\n-\t\tif (!value)\n-\t\t\treturn config_error_nonbool(var);\n-\n-\t\tset_name_for_path(value, name, namelen);\n-\n-\t} else if (!strcmp(key, \"fetchrecursesubmodules\")) {\n-\t\tint fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);\n-\n-\t\tset_fetch_recurse_for_name(name, namelen, fetch_recurse);\n-\n-\t} else if (!strcmp(key, \"ignore\")) {\n-\n-\t\tif (!value)\n-\t\t\treturn config_error_nonbool(var);\n-\n-\t\tif (strcmp(value, \"untracked\") && strcmp(value, \"dirty\") &&\n-\t\t    strcmp(value, \"all\") && strcmp(value, \"none\")) {\n-\t\t\twarning(\"Invalid parameter \\\"%s\\\" for config option \\\"submodule.%s.ignore\\\"\", value, var);\n-\t\t\treturn 0;\n-\t\t}\n-\n-\t\tset_ignore_for_name(name, namelen, value);\n-\t\treturn 0;\n-\t}\n-\treturn 0;\n-}\n-\n void handle_ignore_submodules_arg(struct diff_options *diffopt,\n \t\t\t\t  const char *arg)\n {\n@@ -707,7 +597,7 @@ static void calculate_changed_submodule_paths(void)\n \tstruct argv_array argv = ARGV_ARRAY_INIT;\n \n \t/* No need to check if there are no submodules configured */\n-\tif (!get_name_for_path(NULL))\n+\tif (!submodule_from_path(NULL, NULL))\n \t\treturn;\n \n \tinit_revisions(&rev, NULL);\n@@ -754,7 +644,6 @@ int fetch_populated_submodules(const struct argv_array *options,\n \tint i, result = 0;\n \tstruct child_process cp;\n \tstruct argv_array argv = ARGV_ARRAY_INIT;\n-\tconst char *name_for_path;\n \tconst char *work_tree = get_git_work_tree();\n \tif (!work_tree)\n \t\tgoto out;\n@@ -780,23 +669,26 @@ int fetch_populated_submodules(const struct argv_array *options,\n \t\tstruct strbuf submodule_git_dir = STRBUF_INIT;\n \t\tstruct strbuf submodule_prefix = STRBUF_INIT;\n \t\tconst struct cache_entry *ce = active_cache[i];\n-\t\tconst char *git_dir, *name, *default_argv;\n+\t\tconst char *git_dir, *default_argv;\n+\t\tconst struct submodule *submodule;\n \n \t\tif (!S_ISGITLINK(ce->ce_mode))\n \t\t\tcontinue;\n \n-\t\tname = ce->name;\n-\t\tname_for_path = get_name_for_path(ce->name);\n-\t\tif (name_for_path)\n-\t\t\tname = name_for_path;\n+\t\tsubmodule = submodule_from_path(null_sha1, ce->name);\n+\t\tif (!submodule)\n+\t\t\tsubmodule = submodule_from_name(null_sha1, ce->name);\n \n \t\tdefault_argv = \"yes\";\n \t\tif (command_line_option == RECURSE_SUBMODULES_DEFAULT) {\n-\t\t\tint fetch_recurse_option = get_fetch_recurse_for_name(name);\n-\t\t\tif (fetch_recurse_option != RECURSE_SUBMODULES_NONE) {\n-\t\t\t\tif (fetch_recurse_option == RECURSE_SUBMODULES_OFF)\n+\t\t\tif (submodule &&\n+\t\t\t    submodule->fetch_recurse !=\n+\t\t\t\t\t\tRECURSE_SUBMODULES_NONE) {\n+\t\t\t\tif (submodule->fetch_recurse ==\n+\t\t\t\t\t\tRECURSE_SUBMODULES_OFF)\n \t\t\t\t\tcontinue;\n-\t\t\t\tif (fetch_recurse_option == RECURSE_SUBMODULES_ON_DEMAND) {\n+\t\t\t\tif (submodule->fetch_recurse ==\n+\t\t\t\t\t\tRECURSE_SUBMODULES_ON_DEMAND) {\n \t\t\t\t\tif (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))\n \t\t\t\t\t\tcontinue;\n \t\t\t\t\tdefault_argv = \"on-demand\";\ndiff --git a/submodule.h b/submodule.h\nindex 920fef3..547219d 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -20,7 +20,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,\n \t\tconst char *path);\n int submodule_config(const char *var, const char *value, void *cb);\n void gitmodules_config(void);\n-int parse_submodule_config_option(const char *var, const char *value);\n void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);\n int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);\n void show_submodule_summary(FILE *f, const char *path,\ndiff --git a/t/t7411-submodule-config.sh b/t/t7411-submodule-config.sh\nindex 2602bc5..7229978 100755\n--- a/t/t7411-submodule-config.sh\n+++ b/t/t7411-submodule-config.sh\n@@ -5,8 +5,8 @@\n \n test_description='Test submodules config cache infrastructure\n \n-This test verifies that parsing .gitmodules configuration directly\n-from the database works.\n+This test verifies that parsing .gitmodules configurations directly\n+from the database and from the worktree works.\n '\n \n TEST_NO_CREATE_REPO=1\n@@ -82,4 +82,37 @@ test_expect_success 'error in one submodule config lets continue' '\n \t)\n '\n \n+cat >super/expect_url <<EOF\n+Submodule url: 'git@somewhere.else.net:a.git' for path 'b'\n+Submodule url: 'git@somewhere.else.net:submodule.git' for path 'submodule'\n+EOF\n+\n+cat >super/expect_local_path <<EOF\n+Submodule name: 'a' for path 'c'\n+Submodule name: 'submodule' for path 'submodule'\n+EOF\n+\n+test_expect_success 'reading of local configuration' '\n+\t(cd super &&\n+\t\told_a=$(git config submodule.a.url) &&\n+\t\told_submodule=$(git config submodule.submodule.url) &&\n+\t\tgit config submodule.a.url git@somewhere.else.net:a.git &&\n+\t\tgit config submodule.submodule.url git@somewhere.else.net:submodule.git &&\n+\t\ttest-submodule-config --url \\\n+\t\t\t\"\" b \\\n+\t\t\t\"\" submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect_url actual &&\n+\t\tgit config submodule.a.path c &&\n+\t\ttest-submodule-config \\\n+\t\t\t\"\" c \\\n+\t\t\t\"\" submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect_local_path actual &&\n+\t\tgit config submodule.a.url $old_a &&\n+\t\tgit config submodule.submodule.url $old_submodule &&\n+\t\tgit config --unset submodule.a.path c\n+\t)\n+'\n+\n test_done\ndiff --git a/test-submodule-config.c b/test-submodule-config.c\nindex f3c3918..dab8c27 100644\n--- a/test-submodule-config.c\n+++ b/test-submodule-config.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"submodule-config.h\"\n+#include \"submodule.h\"\n \n static void die_usage(int argc, char **argv, const char *msg)\n {\n@@ -8,6 +9,11 @@ static void die_usage(int argc, char **argv, const char *msg)\n \texit(1);\n }\n \n+static int git_test_config(const char *var, const char *value, void *cb)\n+{\n+\treturn parse_submodule_config_option(var, value);\n+}\n+\n int main(int argc, char **argv)\n {\n \tchar **arg = argv;\n@@ -29,6 +35,10 @@ int main(int argc, char **argv)\n \tif (my_argc % 2 != 0)\n \t\tdie_usage(argc, argv, \"Wrong number of arguments.\");\n \n+\tsetup_git_directory();\n+\tgitmodules_config();\n+\tgit_config(git_test_config, NULL);\n+\n \twhile (*arg) {\n \t\tunsigned char commit_sha1[20];\n \t\tconst struct submodule *submodule;\n-- \n2.1.0.rc0.52.gaa544bf\n"},{"id":"261807","messageId":"20150521170947.GE22979@book.hvoigt.net","threadId":"39397","inReplyTo":"20150521170616.GA22979@book.hvoigt.net","subject":"[PATCH v3 4/4] do not die on error of parsing fetchrecursesubmodules option","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-21T17:09:47Z","receivedAt":"2015-05-21T17:09:47Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"We should not die when reading the submodule config cache since the user\nmight not be able to get out of that situation when the configuration is\npart of the history.\n\nWe should handle this condition later when the value is about to be\nused.\n\nSigned-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n---\n builtin/fetch.c             |  1 +\n submodule-config.c          | 29 ++++++++++++++++++++++++++++-\n submodule-config.h          |  1 +\n submodule.c                 | 15 ---------------\n submodule.h                 |  2 +-\n t/t7411-submodule-config.sh | 35 +++++++++++++++++++++++++++++++++++\n 6 files changed, 66 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/fetch.c b/builtin/fetch.c\nindex 55f457c..706326f 100644\n--- a/builtin/fetch.c\n+++ b/builtin/fetch.c\n@@ -12,6 +12,7 @@\n #include \"parse-options.h\"\n #include \"sigchain.h\"\n #include \"transport.h\"\n+#include \"submodule-config.h\"\n #include \"submodule.h\"\n #include \"connected.h\"\n #include \"argv-array.h\"\ndiff --git a/submodule-config.c b/submodule-config.c\nindex e47640f..177767d 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -204,6 +204,30 @@ static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,\n \treturn submodule;\n }\n \n+static int parse_fetch_recurse(const char *opt, const char *arg,\n+\t\t\t       int die_on_error)\n+{\n+\tswitch (git_config_maybe_bool(opt, arg)) {\n+\tcase 1:\n+\t\treturn RECURSE_SUBMODULES_ON;\n+\tcase 0:\n+\t\treturn RECURSE_SUBMODULES_OFF;\n+\tdefault:\n+\t\tif (!strcmp(arg, \"on-demand\"))\n+\t\t\treturn RECURSE_SUBMODULES_ON_DEMAND;\n+\n+\t\tif (die_on_error)\n+\t\t\tdie(\"bad %s argument: %s\", opt, arg);\n+\t\telse\n+\t\t\treturn RECURSE_SUBMODULES_ERROR;\n+\t}\n+}\n+\n+int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)\n+{\n+\treturn parse_fetch_recurse(opt, arg, 1);\n+}\n+\n static void warn_multiple_config(const unsigned char *commit_sha1,\n \t\t\t\t const char *name, const char *option)\n {\n@@ -255,6 +279,8 @@ static int parse_config(const char *var, const char *value, void *data)\n \t\tsubmodule->path = strbuf_detach(&path, NULL);\n \t\tcache_put_path(me->cache, submodule);\n \t} else if (!strcmp(item.buf, \"fetchrecursesubmodules\")) {\n+\t\t/* when parsing worktree configurations we can die early */\n+\t\tint die_on_error = is_null_sha1(me->gitmodules_sha1);\n \t\tif (!me->overwrite &&\n \t\t    submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) {\n \t\t\twarn_multiple_config(me->commit_sha1, submodule->name,\n@@ -262,7 +288,8 @@ static int parse_config(const char *var, const char *value, void *data)\n \t\t\tgoto release_return;\n \t\t}\n \n-\t\tsubmodule->fetch_recurse = parse_fetch_recurse_submodules_arg(var, value);\n+\t\tsubmodule->fetch_recurse = parse_fetch_recurse(var, value,\n+\t\t\t\t\t\t\t\tdie_on_error);\n \t} else if (!strcmp(item.buf, \"ignore\")) {\n \t\tstruct strbuf ignore = STRBUF_INIT;\n \t\tif (!me->overwrite && submodule->ignore != NULL) {\ndiff --git a/submodule-config.h b/submodule-config.h\nindex 2083cb9..58afc83 100644\n--- a/submodule-config.h\n+++ b/submodule-config.h\n@@ -18,6 +18,7 @@ struct submodule {\n \tunsigned char gitmodules_sha1[20];\n };\n \n+int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);\n int parse_submodule_config_option(const char *var, const char *value);\n const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n \t\tconst char *name);\ndiff --git a/submodule.c b/submodule.c\nindex 188b4d2..75f502f 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -288,21 +288,6 @@ static void print_submodule_summary(struct rev_info *rev, FILE *f,\n \tstrbuf_release(&sb);\n }\n \n-int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)\n-{\n-\tswitch (git_config_maybe_bool(opt, arg)) {\n-\tcase 1:\n-\t\treturn RECURSE_SUBMODULES_ON;\n-\tcase 0:\n-\t\treturn RECURSE_SUBMODULES_OFF;\n-\tdefault:\n-\t\tif (!strcmp(arg, \"on-demand\"))\n-\t\t\treturn RECURSE_SUBMODULES_ON_DEMAND;\n-\t\t/* TODO: remove the die for history parsing here */\n-\t\tdie(\"bad %s argument: %s\", opt, arg);\n-\t}\n-}\n-\n void show_submodule_summary(FILE *f, const char *path,\n \t\tconst char *line_prefix,\n \t\tunsigned char one[20], unsigned char two[20],\ndiff --git a/submodule.h b/submodule.h\nindex 547219d..5507c3d 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -5,6 +5,7 @@ struct diff_options;\n struct argv_array;\n \n enum {\n+\tRECURSE_SUBMODULES_ERROR = -3,\n \tRECURSE_SUBMODULES_NONE = -2,\n \tRECURSE_SUBMODULES_ON_DEMAND = -1,\n \tRECURSE_SUBMODULES_OFF = 0,\n@@ -21,7 +22,6 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,\n int submodule_config(const char *var, const char *value, void *cb);\n void gitmodules_config(void);\n void handle_ignore_submodules_arg(struct diff_options *diffopt, const char *);\n-int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg);\n void show_submodule_summary(FILE *f, const char *path,\n \t\tconst char *line_prefix,\n \t\tunsigned char one[20], unsigned char two[20],\ndiff --git a/t/t7411-submodule-config.sh b/t/t7411-submodule-config.sh\nindex 7229978..fc97c33 100755\n--- a/t/t7411-submodule-config.sh\n+++ b/t/t7411-submodule-config.sh\n@@ -115,4 +115,39 @@ test_expect_success 'reading of local configuration' '\n \t)\n '\n \n+cat >super/expect_fetchrecurse_die.err <<EOF\n+fatal: bad submodule.submodule.fetchrecursesubmodules argument: blabla\n+EOF\n+\n+test_expect_success 'local error in fetchrecursesubmodule dies early' '\n+\t(cd super &&\n+\t\tgit config submodule.submodule.fetchrecursesubmodules blabla &&\n+\t\ttest_must_fail test-submodule-config \\\n+\t\t\t\"\" b \\\n+\t\t\t\"\" submodule \\\n+\t\t\t\t>actual.out 2>actual.err &&\n+\t\ttouch expect_fetchrecurse_die.out &&\n+\t\ttest_cmp expect_fetchrecurse_die.out actual.out  &&\n+\t\ttest_cmp expect_fetchrecurse_die.err actual.err  &&\n+\t\tgit config --unset submodule.submodule.fetchrecursesubmodules\n+\t)\n+'\n+\n+test_expect_success 'error in history in fetchrecursesubmodule lets continue' '\n+\t(cd super &&\n+\t\tgit config -f .gitmodules \\\n+\t\t\tsubmodule.submodule.fetchrecursesubmodules blabla &&\n+\t\tgit add .gitmodules &&\n+\t\tgit config --unset -f .gitmodules \\\n+\t\t\tsubmodule.submodule.fetchrecursesubmodules &&\n+\t\tgit commit -m \"add error in fetchrecursesubmodules\" &&\n+\t\ttest-submodule-config \\\n+\t\t\tHEAD b \\\n+\t\t\tHEAD submodule \\\n+\t\t\t\t>actual &&\n+\t\ttest_cmp expect_error actual  &&\n+\t\tgit reset --hard HEAD^\n+\t)\n+'\n+\n test_done\n-- \n2.1.0.rc0.52.gaa544bf\n"},{"id":"261834","messageId":"xmqqy4khixjn.fsf@gitster.dls.corp.google.com","threadId":"39397","inReplyTo":"20150521170616.GA22979@book.hvoigt.net","subject":"Re: [PATCH v3 0/4] submodule config lookup API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-05-21T18:40:44Z","receivedAt":"2015-05-21T18:40:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Heiko Voigt <hvoigt@hvoigt.net> writes:\n\n> This is finally the next iteration of the submodule config api. The last\n> iteration can be found here:\n>\n> http://article.gmane.org/gmane.comp.version-control.git/252601\n>\n> This iteration fixes the lookup of submodules by name\n> (submodule_from_name()) where one needed to pass in the gitmodule sha1\n> by mistake. To keep it simple for the user and behave as documented we\n> should take the commit sha1 which is now fixed here. We now also test\n> the lookup by name in the api tests.\n>\n> This should be ready for inclusion.\n>\n> Cheers Heiko\n>\n> Here is the interdiff to the last iteration:\n>\n> diff --git a/submodule-config.c b/submodule-config.c\n> index 96623ad..177767d 100644\n> --- a/submodule-config.c\n> +++ b/submodule-config.c\n> @@ -25,6 +25,11 @@ struct submodule_entry {\n>  \tstruct submodule *config;\n>  };\n>  \n> +enum lookup_type {\n> +\tlookup_name,\n> +\tlookup_path,\n> +};\n\nPlease lose the comma after the last element in enum.  Some\ncompilers do not like it, I was told.\n\n> +\tswitch (lookup_type) {\n> +\t\tcase lookup_name:\n> +\t\t\tsubmodule = cache_lookup_name(cache, sha1, key);\n> +\t\t\tbreak;\n> +\t\tcase lookup_path:\n> +\t\t\tsubmodule = cache_lookup_path(cache, sha1, key);\n> +\t\t\tbreak;\n\nIs this too deeply indented?\n"},{"id":"261836","messageId":"555E28E3.5020003@web.de","threadId":"39397","inReplyTo":"20150521170616.GA22979@book.hvoigt.net","subject":"Re: [PATCH v3 0/4] submodule config lookup API","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2015-05-21T18:50:11Z","receivedAt":"2015-05-21T18:50:11Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Am 21.05.2015 um 19:06 schrieb Heiko Voigt:\n> diff --git a/submodule-config.h b/submodule-config.h\n> index 9061e4e..58afc83 100644\n> --- a/submodule-config.h\n> +++ b/submodule-config.h\n> @@ -24,6 +24,6 @@ const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n>   \t\tconst char *name);\n>   const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\n>   \t\tconst char *path);\n> -void submodule_free(void);\n> +void submodule_free();\n\nWhy this change?  With void it matches the function's definition.\n\nRené\n"},{"id":"262130","messageId":"20150526152402.GA31115@book.hvoigt.net","threadId":"39397","inReplyTo":"555E28E3.5020003@web.de","subject":"Re: Re: [PATCH v3 0/4] submodule config lookup API","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-26T15:24:02Z","receivedAt":"2015-05-26T15:24:02Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Thu, May 21, 2015 at 08:50:11PM +0200, René Scharfe wrote:\n> Am 21.05.2015 um 19:06 schrieb Heiko Voigt:\n> >diff --git a/submodule-config.h b/submodule-config.h\n> >index 9061e4e..58afc83 100644\n> >--- a/submodule-config.h\n> >+++ b/submodule-config.h\n> >@@ -24,6 +24,6 @@ const struct submodule *submodule_from_name(const unsigned char *commit_sha1,\n> >  \t\tconst char *name);\n> >  const struct submodule *submodule_from_path(const unsigned char *commit_sha1,\n> >  \t\tconst char *path);\n> >-void submodule_free(void);\n> >+void submodule_free();\n> \n> Why this change?  With void it matches the function's definition.\n\nSorry oversight on my side will remove it. Junio added those on his side\nbut it seems I forgot it in the header.\n\nCheers Heiko\n"},{"id":"262131","messageId":"20150526154610.GB31115@book.hvoigt.net","threadId":"39397","inReplyTo":"xmqqy4khixjn.fsf@gitster.dls.corp.google.com","subject":"Re: Re: [PATCH v3 0/4] submodule config lookup API","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2015-05-26T15:46:10Z","receivedAt":"2015-05-26T15:46:10Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Thu, May 21, 2015 at 11:40:44AM -0700, Junio C Hamano wrote:\n> Heiko Voigt <hvoigt@hvoigt.net> writes:\n> \n> > This is finally the next iteration of the submodule config api. The last\n> > iteration can be found here:\n> >\n> > http://article.gmane.org/gmane.comp.version-control.git/252601\n> >\n> > This iteration fixes the lookup of submodules by name\n> > (submodule_from_name()) where one needed to pass in the gitmodule sha1\n> > by mistake. To keep it simple for the user and behave as documented we\n> > should take the commit sha1 which is now fixed here. We now also test\n> > the lookup by name in the api tests.\n> >\n> > This should be ready for inclusion.\n> >\n> > Cheers Heiko\n> >\n> > Here is the interdiff to the last iteration:\n> >\n> > diff --git a/submodule-config.c b/submodule-config.c\n> > index 96623ad..177767d 100644\n> > --- a/submodule-config.c\n> > +++ b/submodule-config.c\n> > @@ -25,6 +25,11 @@ struct submodule_entry {\n> >  \tstruct submodule *config;\n> >  };\n> >  \n> > +enum lookup_type {\n> > +\tlookup_name,\n> > +\tlookup_path,\n> > +};\n> \n> Please lose the comma after the last element in enum.  Some\n> compilers do not like it, I was told.\n\nFixed.\n\n> > +\tswitch (lookup_type) {\n> > +\t\tcase lookup_name:\n> > +\t\t\tsubmodule = cache_lookup_name(cache, sha1, key);\n> > +\t\t\tbreak;\n> > +\t\tcase lookup_path:\n> > +\t\t\tsubmodule = cache_lookup_path(cache, sha1, key);\n> > +\t\t\tbreak;\n> \n> Is this too deeply indented?\n\nIt seems I accidentially used vim's default indenting. Fixed.\n\nWill wait a little longer if there are more comments than the style\nfixes before sending another iteration.\n\nCheers Heiko\n"}]}