{"thread":{"id":"36842","subject":"[PATCH 0/5] submodule config lookup API","startedAt":"2014-06-05T06:04:25Z","lastAt":"2014-06-17T22:19:04Z","messageCount":26,"participants":["Heiko Voigt","W. Trevor King","Karsten Blees","Eric Sunshine","Junio C Hamano","Jens Lehmann"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"243369","messageId":"20140605060425.GA23874@sandbox-ub","threadId":"36842","inReplyTo":null,"subject":"[PATCH 0/5] submodule config lookup API","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-05T06:04:25Z","receivedAt":"2014-06-05T06:04:25Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"I have been holding back this series during the RC phase but now I think\nit is ready for another round. The most important changes:\n\n  * The API is using a singleton now. No need to pass in the cache\n    object anymore.\n\n  * Local configuration can be looked up by passing in the null_sha1\n\n  * We use the API for existing lookup of submodule values\n\nOne open question:\n\n  * Since behind the scenes there is a global cache filled with the\n    values: Do we need to free it explicitely? Or is it ok to let it be\n    dealt with on exit?\n\nThe last iteration was here:\n\nhttp://article.gmane.org/gmane.comp.version-control.git/243818\n\nHeiko Voigt (5):\n  hashmap: add enum for hashmap free_entries option\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-hashmap.txt          |   2 +-\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 diffcore-rename.c                                |   2 +-\n hashmap.c                                        |   2 +-\n hashmap.h                                        |   8 +-\n name-hash.c                                      |   4 +-\n submodule-config.c                               | 435 +++++++++++++++++++++++\n submodule-config.h                               |  29 ++\n submodule.c                                      | 122 ++-----\n submodule.h                                      |   4 +-\n t/t7410-submodule-config.sh                      | 141 ++++++++\n test-hashmap.c                                   |   6 +-\n test-submodule-config.c                          |  74 ++++\n 18 files changed, 791 insertions(+), 107 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/t7410-submodule-config.sh\n create mode 100644 test-submodule-config.c\n\n-- \n2.0.0\n"},{"id":"243371","messageId":"20140605060640.GB23874@sandbox-ub","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"[PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-05T06:06:40Z","receivedAt":"2014-06-05T06:06:40Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"This allows a reader to immediately know which options can be used and\nwhat this parameter is about.\n\nSigned-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n---\n Documentation/technical/api-hashmap.txt | 2 +-\n diffcore-rename.c                       | 2 +-\n hashmap.c                               | 2 +-\n hashmap.h                               | 8 +++++++-\n name-hash.c                             | 4 ++--\n test-hashmap.c                          | 6 +++---\n 6 files changed, 15 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/technical/api-hashmap.txt b/Documentation/technical/api-hashmap.txt\nindex b977ae8..b04bb40 100644\n--- a/Documentation/technical/api-hashmap.txt\n+++ b/Documentation/technical/api-hashmap.txt\n@@ -187,7 +187,7 @@ void long2double_init(void)\n \n void long2double_free(void)\n {\n-\thashmap_free(&map, 1);\n+\thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n }\n \n static struct long2double *find_entry(long key)\ndiff --git a/diffcore-rename.c b/diffcore-rename.c\nindex 749a35d..f30239a 100644\n--- a/diffcore-rename.c\n+++ b/diffcore-rename.c\n@@ -335,7 +335,7 @@ static int find_exact_renames(struct diff_options *options)\n \t\trenames += find_identical_files(&file_table, i, options);\n \n \t/* Free the hash data structure and entries */\n-\thashmap_free(&file_table, 1);\n+\thashmap_free(&file_table, HASHMAP_FREE_ENTRIES);\n \n \treturn renames;\n }\ndiff --git a/hashmap.c b/hashmap.c\nindex d1b8056..9a3555a 100644\n--- a/hashmap.c\n+++ b/hashmap.c\n@@ -135,7 +135,7 @@ void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,\n \talloc_table(map, size);\n }\n \n-void hashmap_free(struct hashmap *map, int free_entries)\n+void hashmap_free(struct hashmap *map, enum hashmap_free_options free_entries)\n {\n \tif (!map || !map->table)\n \t\treturn;\ndiff --git a/hashmap.h b/hashmap.h\nindex a816ad4..6c558df 100644\n--- a/hashmap.h\n+++ b/hashmap.h\n@@ -1,6 +1,11 @@\n #ifndef HASHMAP_H\n #define HASHMAP_H\n \n+enum hashmap_free_options {\n+\tHASHMAP_NO_FREE_ENTRIES = 0,\n+\tHASHMAP_FREE_ENTRIES = 1,\n+};\n+\n /*\n  * Generic implementation of hash-based key-value mappings.\n  * See Documentation/technical/api-hashmap.txt.\n@@ -39,7 +44,8 @@ struct hashmap_iter {\n \n extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,\n \t\tsize_t initial_size);\n-extern void hashmap_free(struct hashmap *map, int free_entries);\n+extern void hashmap_free(struct hashmap *map,\n+\t\t\t enum hashmap_free_options free_entries);\n \n /* hashmap_entry functions */\n \ndiff --git a/name-hash.c b/name-hash.c\nindex 97444d0..be7c4ae 100644\n--- a/name-hash.c\n+++ b/name-hash.c\n@@ -233,6 +233,6 @@ void free_name_hash(struct index_state *istate)\n \t\treturn;\n \tistate->name_hash_initialized = 0;\n \n-\thashmap_free(&istate->name_hash, 0);\n-\thashmap_free(&istate->dir_hash, 1);\n+\thashmap_free(&istate->name_hash, HASHMAP_NO_FREE_ENTRIES);\n+\thashmap_free(&istate->dir_hash, HASHMAP_FREE_ENTRIES);\n }\ndiff --git a/test-hashmap.c b/test-hashmap.c\nindex f5183fb..ac8d6a2 100644\n--- a/test-hashmap.c\n+++ b/test-hashmap.c\n@@ -100,7 +100,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)\n \t\t\t\thashmap_add(&map, entries[i]);\n \t\t\t}\n \n-\t\t\thashmap_free(&map, 0);\n+\t\t\thashmap_free(&map, HASHMAP_NO_FREE_ENTRIES);\n \t\t}\n \t} else {\n \t\t/* test map lookups */\n@@ -121,7 +121,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)\n \t\t\t}\n \t\t}\n \n-\t\thashmap_free(&map, 0);\n+\t\thashmap_free(&map, HASHMAP_NO_FREE_ENTRIES);\n \t}\n }\n \n@@ -250,6 +250,6 @@ int main(int argc, char *argv[])\n \t\t}\n \t}\n \n-\thashmap_free(&map, 1);\n+\thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n \treturn 0;\n }\n-- \n2.0.0\n"},{"id":"243372","messageId":"20140605060750.GC23874@sandbox-ub","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"[PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-05T06:07:50Z","receivedAt":"2014-06-05T06:07:50Z","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                               | 396 +++++++++++++++++++++++\n submodule-config.h                               |  27 ++\n submodule.c                                      |   1 +\n submodule.h                                      |   1 +\n t/t7410-submodule-config.sh                      |  73 +++++\n test-submodule-config.c                          |  64 ++++\n 9 files changed, 611 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/t7410-submodule-config.sh\n create mode 100644 test-submodule-config.c\n\ndiff --git a/.gitignore b/.gitignore\nindex dc600f9..9e3352a 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 08fc9ca..0c96e1f 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@@ -878,6 +879,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..e7ca2b0\n--- /dev/null\n+++ b/submodule-config.c\n@@ -0,0 +1,396 @@\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+static struct submodule_cache cache;\n+static int is_cache_init = 0;\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, HASHMAP_FREE_ENTRIES);\n+\thashmap_free(&cache->for_name, HASHMAP_FREE_ENTRIES);\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 const struct submodule *config_from_path(struct submodule_cache *cache,\n+\t\tconst unsigned char *commit_sha1, const char *path)\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 || !path) {\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 (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+\n+\tsubmodule = cache_lookup_path(cache, sha1, path);\n+\tif (submodule)\n+\t\tgoto free_rev;\n+\n+\tconfig = read_sha1_file(sha1, &type, &config_size);\n+\tif (!config)\n+\t\tgoto free_rev;\n+\n+\tif (type != OBJ_BLOB) {\n+\t\tfree(config);\n+\t\tgoto free_rev;\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+\tsubmodule = cache_lookup_path(cache, sha1, path);\n+\n+free_rev:\n+\tstrbuf_release(&rev);\n+\treturn submodule;\n+}\n+\n+static void ensure_cache_init()\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 cache_lookup_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()\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/t7410-submodule-config.sh b/t/t7410-submodule-config.sh\nnew file mode 100755\nindex 0000000..ea453c5\n--- /dev/null\n+++ b/t/t7410-submodule-config.sh\n@@ -0,0 +1,73 @@\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 of submodule config' '\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+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..969d957\n--- /dev/null\n+++ b/test-submodule-config.c\n@@ -0,0 +1,64 @@\n+#include <stdio.h>\n+#include <stdlib.h>\n+#include <string.h>\n+\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+\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\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;\n+\n+\t\tcommit = arg[0];\n+\t\tpath = 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 (!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\telse\n+\t\t\tprintf(\"Submodule name: '%s' for path '%s'\\n\",\n+\t\t\t\t\tsubmodule->name, path);\n+\n+\t\targ += 2;\n+\t}\n+\n+\tsubmodule_free();\n+\n+\treturn 0;\n+}\n-- \n2.0.0\n"},{"id":"243373","messageId":"20140605060836.GD23874@sandbox-ub","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"[PATCH 3/5] extract functions for submodule config set and lookup","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-05T06:08:36Z","receivedAt":"2014-06-05T06:08:36Z","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\nThis refactoring is included in the series to make following the series easier\n(and because it was one step I did). The extracted functions will be replaced\nin the next commit with the ones from the cache. I think its easier to follow\nthe implementation this way. In case you think its unnecessary I can squash\nthis commit into the next one.\n\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.0.0\n"},{"id":"243374","messageId":"20140605060913.GE23874@sandbox-ub","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"[PATCH 4/5] use new config API for worktree configurations of submodules","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-05T06:09:13Z","receivedAt":"2014-06-05T06:09:13Z","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/t7410-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 ff44921..4cb88e2 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 e7ca2b0..e445bf7 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -375,6 +375,18 @@ static void ensure_cache_init()\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/t7410-submodule-config.sh b/t/t7410-submodule-config.sh\nindex ea453c5..1ec26c3 100755\n--- a/t/t7410-submodule-config.sh\n+++ b/t/t7410-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@@ -70,4 +70,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 969d957..f47f046 100644\n--- a/test-submodule-config.c\n+++ b/test-submodule-config.c\n@@ -4,6 +4,7 @@\n \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@@ -12,6 +13,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@@ -30,6 +36,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.0.0\n"},{"id":"243375","messageId":"20140605060946.GF23874@sandbox-ub","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"[PATCH 5/5] do not die on error of parsing fetchrecursesubmodules option","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-05T06:09:46Z","receivedAt":"2014-06-05T06:09:46Z","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/t7410-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 e445bf7..437fbdb 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -199,6 +199,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@@ -250,6 +274,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@@ -257,7 +283,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/t7410-submodule-config.sh b/t/t7410-submodule-config.sh\nindex 1ec26c3..4a837af 100755\n--- a/t/t7410-submodule-config.sh\n+++ b/t/t7410-submodule-config.sh\n@@ -103,4 +103,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.0.0\n"},{"id":"243408","messageId":"20140605174610.GS21803@odin.tremily.us","threadId":"36842","inReplyTo":"20140605060750.GC23874@sandbox-ub","subject":"Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"W. Trevor King","fromEmail":"wking@tremily.us","sentAt":"2014-06-05T17:46:10Z","receivedAt":"2014-06-05T17:46:10Z","isPatch":true,"sender":{"key":"wking@tremily.us","avatar":"https://avatars.githubusercontent.com/u/209920?v=4"},"body":"On Thu, Jun 05, 2014 at 08:07:50AM +0200, Heiko Voigt wrote:\n> +The caller can look up information about submodules by using the\n> +`submodule_from_path()` or `submodule_from_name()` functions.\n\nThat's for an already-known submodule.  Do we need a way to list\nsubmodules (e.g. for 'submodule foreach' style operations) or is the\npreferred way to do that just walking the tree looking for gitlinks?\nThe cases where .gitmodules would lead you astray (e.g. via sloppy\ncommits after removing a submodule) are:\n\n* Listing a submodule that wasn't in the tree anymore.  Easy to check\n  for and ignore.\n\n* Not listing a submodule that is in the tree.  You'd need to walk the\n  tree to check for this, but it's a pretty broken situation already,\n  so I'd be fine just ignoring the orphaned gitlink.\n\nCheers,\nTrevor\n\n-- \nThis email may be signed or encrypted with GnuPG (http://www.gnupg.org).\nFor more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy\n"},{"id":"243444","messageId":"20140606052040.GA77405@book.hvoigt.net","threadId":"36842","inReplyTo":"20140605174610.GS21803@odin.tremily.us","subject":"Re: Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-06T05:20:40Z","receivedAt":"2014-06-06T05:20:40Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Thu, Jun 05, 2014 at 10:46:10AM -0700, W. Trevor King wrote:\n> On Thu, Jun 05, 2014 at 08:07:50AM +0200, Heiko Voigt wrote:\n> > +The caller can look up information about submodules by using the\n> > +`submodule_from_path()` or `submodule_from_name()` functions.\n> \n> That's for an already-known submodule.  Do we need a way to list\n> submodules (e.g. for 'submodule foreach' style operations) or is the\n> preferred way to do that just walking the tree looking for gitlinks?\n> The cases where .gitmodules would lead you astray (e.g. via sloppy\n> commits after removing a submodule) are:\n> \n> * Listing a submodule that wasn't in the tree anymore.  Easy to check\n>   for and ignore.\n> \n> * Not listing a submodule that is in the tree.  You'd need to walk the\n>   tree to check for this, but it's a pretty broken situation already,\n>   so I'd be fine just ignoring the orphaned gitlink.\n\nCurrently there is no need to list the submodules in a .gitmodule file.\nWe currently always begin from the gitlink and try to do things. If we\nhave enough information thats fine we go ahead, if not we stop (or\nskip?) the submodule. So for already initialized submodules it is even\nok to not have a .gitmodules entry at all and we can still go ahead with\nmost operations. Here we should not be too picky, I think.\n\nThe only use-case I can think of is for checking whether .gitmodules\ncontains any extra unneeded values. But on the other hand that is not\nso easy. Since .gitmodules are just config files they can contain user\ndefined values. That is ok.\n\nSo in summary: Yes the preferred way to list submodules is via iterating\nthe gitlinks and I do not think we need a way to iterate through the\n.gitmodules file (at least not for the use-cases we currently need this\nfor).\n\nCheers Heiko\n"},{"id":"243508","messageId":"5391FFC3.5010001@gmail.com","threadId":"36842","inReplyTo":"20140605060640.GB23874@sandbox-ub","subject":"Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Karsten Blees","fromEmail":"karsten.blees@gmail.com","sentAt":"2014-06-06T17:52:03Z","receivedAt":"2014-06-06T17:52:03Z","isPatch":true,"sender":{"key":"karsten.blees@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1111200?v=4"},"body":"Am 05.06.2014 08:06, schrieb Heiko Voigt:\n> This allows a reader to immediately know which options can be used and\n> what this parameter is about.\n> \n[...]\n> -void hashmap_free(struct hashmap *map, int free_entries)\n> +void hashmap_free(struct hashmap *map, enum hashmap_free_options free_entries)\n[...]\n>  \n> +enum hashmap_free_options {\n> +\tHASHMAP_NO_FREE_ENTRIES = 0,\n> +\tHASHMAP_FREE_ENTRIES = 1,\n> +};\n\nThis was meant as a boolean parameter. Would it make sense to have\n\nenum boolean {\n\tfalse,\n\ttrue\n};\n\nor similar in some central place?\n\nNote that an earlier version took a function pointer, and you could pass stdlib's free() in the common case, or a special free routine for nested entry structures, or NULL to do the cleanup yourself.\n"},{"id":"243601","messageId":"CAPig+cTmXu09QLca4W=VpUS82m0uDSQLch7Gb06U_XeUZ83FrQ@mail.gmail.com","threadId":"36842","inReplyTo":"20140605060750.GC23874@sandbox-ub","subject":"Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-06-08T09:04:44Z","receivedAt":"2014-06-08T09:04:44Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jun 5, 2014 at 2:07 AM, Heiko Voigt <hvoigt@hvoigt.net> wrote:\n> This submodule configuration cache allows us to lazily read .gitmodules\n> configurations by commit into a runtime cache which can then be used to\n> easily lookup values from it. Currently only the values for path or name\n> are stored but it can be extended for any value needed.\n>\n> [...]\n>\n> Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n> ---\n> diff --git a/t/t7410-submodule-config.sh b/t/t7410-submodule-config.sh\n> new file mode 100755\n> index 0000000..ea453c5\n> --- /dev/null\n> +++ b/t/t7410-submodule-config.sh\n> @@ -0,0 +1,73 @@\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> +       mkdir submodule &&\n> +       (cd submodule &&\n> +               git init\n\nBroken &&-chain.\n\n> +               echo a >a &&\n> +               git add . &&\n> +               git commit -ma\n> +       ) &&\n> +       mkdir super &&\n> +       (cd super &&\n> +               git init &&\n> +               git submodule add ../submodule &&\n> +               git submodule add ../submodule a &&\n> +               git commit -m \"add as submodule and as a\" &&\n> +               git mv a b &&\n> +               git commit -m \"move a to b\"\n> +       )\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 of submodule config' '\n> +       (cd super &&\n> +               test-submodule-config \\\n> +                       HEAD^ a \\\n> +                       HEAD b \\\n> +                       HEAD^ submodule \\\n> +                       HEAD submodule \\\n> +                               >actual &&\n> +               test_cmp expect actual\n> +       )\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> +       (cd super &&\n> +               cp .gitmodules .gitmodules.bak &&\n> +               echo \"  value = \\\"\" >>.gitmodules &&\n> +               git add .gitmodules &&\n> +               mv .gitmodules.bak .gitmodules &&\n> +               git commit -m \"add error\" &&\n> +               test-submodule-config \\\n> +                       HEAD b \\\n> +                       HEAD submodule \\\n> +                               >actual &&\n> +               test_cmp expect_error actual\n> +       )\n> +'\n> +\n> +test_done\n"},{"id":"243717","messageId":"20140610101744.GA23370@t2784.greatnet.de","threadId":"36842","inReplyTo":"5391FFC3.5010001@gmail.com","subject":"Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-10T10:17:44Z","receivedAt":"2014-06-10T10:17:44Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Fri, Jun 06, 2014 at 07:52:03PM +0200, Karsten Blees wrote:\n> Am 05.06.2014 08:06, schrieb Heiko Voigt:\n> > This allows a reader to immediately know which options can be used and\n> > what this parameter is about.\n> > \n> [...]\n> > -void hashmap_free(struct hashmap *map, int free_entries)\n> > +void hashmap_free(struct hashmap *map, enum hashmap_free_options free_entries)\n> [...]\n> >  \n> > +enum hashmap_free_options {\n> > +\tHASHMAP_NO_FREE_ENTRIES = 0,\n> > +\tHASHMAP_FREE_ENTRIES = 1,\n> > +};\n> \n> This was meant as a boolean parameter. Would it make sense to have\n> \n> enum boolean {\n> \tfalse,\n> \ttrue\n> };\n> \n> or similar in some central place?\n\nThe intention of Jonathans critique here[1] was that you do not see what\nthis parameter does on the callsite. I.e.:\n\n\thashmap_free(&map, 1);\n\ncompared to\n\n\thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n\nA boolean basically transfers the same information and would not help\nthe reader here.\n\nCheers Heiko\n\n[1] http://article.gmane.org/gmane.comp.version-control.git/243917\n"},{"id":"243718","messageId":"20140610101927.GA23384@t2784.greatnet.de","threadId":"36842","inReplyTo":"CAPig+cTmXu09QLca4W=VpUS82m0uDSQLch7Gb06U_XeUZ83FrQ@mail.gmail.com","subject":"Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-10T10:19:27Z","receivedAt":"2014-06-10T10:19:27Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Sun, Jun 08, 2014 at 05:04:44AM -0400, Eric Sunshine wrote:\n> On Thu, Jun 5, 2014 at 2:07 AM, Heiko Voigt <hvoigt@hvoigt.net> wrote:\n> > This submodule configuration cache allows us to lazily read .gitmodules\n> > configurations by commit into a runtime cache which can then be used to\n> > easily lookup values from it. Currently only the values for path or name\n> > are stored but it can be extended for any value needed.\n> >\n> > [...]\n> >\n> > Signed-off-by: Heiko Voigt <hvoigt@hvoigt.net>\n> > ---\n> > diff --git a/t/t7410-submodule-config.sh b/t/t7410-submodule-config.sh\n> > new file mode 100755\n> > index 0000000..ea453c5\n> > --- /dev/null\n> > +++ b/t/t7410-submodule-config.sh\n> > @@ -0,0 +1,73 @@\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> > +       mkdir submodule &&\n> > +       (cd submodule &&\n> > +               git init\n> \n> Broken &&-chain.\n\nWill fix. Thanks for catching.\n\nCheers Heiko\n"},{"id":"243887","messageId":"53981D6A.3090604@gmail.com","threadId":"36842","inReplyTo":"20140610101744.GA23370@t2784.greatnet.de","subject":"Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Karsten Blees","fromEmail":"karsten.blees@gmail.com","sentAt":"2014-06-11T09:12:10Z","receivedAt":"2014-06-11T09:12:10Z","isPatch":true,"sender":{"key":"karsten.blees@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1111200?v=4"},"body":"Am 10.06.2014 12:17, schrieb Heiko Voigt:\n> On Fri, Jun 06, 2014 at 07:52:03PM +0200, Karsten Blees wrote:\n>> Am 05.06.2014 08:06, schrieb Heiko Voigt:\n>>> This allows a reader to immediately know which options can be used and\n>>> what this parameter is about.\n>>>\n>> [...]\n>>> -void hashmap_free(struct hashmap *map, int free_entries)\n>>> +void hashmap_free(struct hashmap *map, enum hashmap_free_options free_entries)\n>> [...]\n>>>  \n>>> +enum hashmap_free_options {\n>>> +\tHASHMAP_NO_FREE_ENTRIES = 0,\n>>> +\tHASHMAP_FREE_ENTRIES = 1,\n>>> +};\n>>\n>> This was meant as a boolean parameter. Would it make sense to have\n>>\n>> enum boolean {\n>> \tfalse,\n>> \ttrue\n>> };\n>>\n>> or similar in some central place?\n> \n> The intention of Jonathans critique here[1] was that you do not see what\n> this parameter does on the callsite. I.e.:\n> \n> \thashmap_free(&map, 1);\n> \n> compared to\n> \n> \thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n> \n> A boolean basically transfers the same information and would not help\n> the reader here.\n> \n> Cheers Heiko\n> \n> [1] http://article.gmane.org/gmane.comp.version-control.git/243917\n> \n\nThere are languages where you can have e.g. 'hashmap_free(..., free_entries: true)'. In C, however, you do not see what a parameter does at the call site. This is a general language feature, reducing redundancy and keeping it short and concise. IMO there's no reason to treat boolean parameters differently.\n\nUsing an enum suggests that there is more to the parameter than a simple yes/no decision, underpinned by naming it '...options' (plural). I find this rather confusing.\n\nFinally, enums share a global namespace, which means long identifiers, provoking additional line breaks and thus reducing readability. Not a problem with hashmap_free per se, but if you do the same for e.g. 'free_util' in string-list.[ch] or 'icase' in name-hash.c, I suspect it'll get pretty ugly.\n\nSo please lets not spoil the global namespace with a thousand different names for 0/1. Using enums for >= tristate values and bit flags is fine, but inventing enums for every simple boolean in the system is bound to end in chaos.\n\nJust my 2c\nKarsten\n"},{"id":"244019","messageId":"xmqqegyu54cl.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"53981D6A.3090604@gmail.com","subject":"Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-12T19:12:58Z","receivedAt":"2014-06-12T19:12:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Karsten Blees <karsten.blees@gmail.com> writes:\n\n> Am 10.06.2014 12:17, schrieb Heiko Voigt:\n>> The intention of Jonathans critique here[1] was that you do not see what\n>> this parameter does on the callsite. I.e.:\n>> \n>> \thashmap_free(&map, 1);\n>> \n>> compared to\n>> \n>> \thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n>> \n>> A boolean basically transfers the same information and would not help\n>> the reader here.\n>> \n>> Cheers Heiko\n>> \n>> [1] http://article.gmane.org/gmane.comp.version-control.git/243917\n>> \n>\n> There are languages where you can have e.g. 'hashmap_free(...,\n> free_entries: true)'. In C, however, you do not see what a\n> parameter does at the call site. This is a general language\n> feature, reducing redundancy and keeping it short and concise. IMO\n> there's no reason to treat boolean parameters differently.\n\nBut given that you are writing in C, is any of that relevant?  We do\nwant to keep our call-sites readable and understandable, and 1 or\ntrue would not help, unless (1) you are the one who wrote the\nfunction and know that 1 means free the entries, or (2) the API is\nso widely used and everybody knows what 1 means free the entries.\n"},{"id":"244036","messageId":"xmqq38f96b9f.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"20140605060750.GC23874@sandbox-ub","subject":"Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-12T21:58:20Z","receivedAt":"2014-06-12T21:58:20Z","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> ...\n> +static int is_cache_init = 0;\n\nPlease don't initialise variables in the .bss to zero by hand.\n\n> + ...\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> +\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\nThese two look inconsistent in different ways.  I think we typically\nquote the names like so:\n\n\twarning(\"I have trouble with variable '%s' somehow\", var);\n"},{"id":"244037","messageId":"xmqqy4x14wn8.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"Re: [PATCH 0/5] submodule config lookup API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-12T21:59:23Z","receivedAt":"2014-06-12T21:59:23Z","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>  t/t7410-submodule-config.sh                      | 141 ++++++++\n\nWe already use 7410 for something else in 'pu'; please avoid dups\nwaiting to happen.\n\n>  test-hashmap.c                                   |   6 +-\n>  test-submodule-config.c                          |  74 ++++\n>  18 files changed, 791 insertions(+), 107 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/t7410-submodule-config.sh\n>  create mode 100644 test-submodule-config.c\n"},{"id":"244038","messageId":"xmqqtx7p4wee.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"20140605060425.GA23874@sandbox-ub","subject":"Re: [PATCH 0/5] submodule config lookup API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-12T22:04:41Z","receivedAt":"2014-06-12T22:04:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Hmph, this seems to conflict in a meaningful (and painful) way with\nJens's \"jl/submodule-recursive-checkout\".\n"},{"id":"244059","messageId":"539AA493.5030106@web.de","threadId":"36842","inReplyTo":"xmqqtx7p4wee.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH 0/5] submodule config lookup API","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2014-06-13T07:13:23Z","receivedAt":"2014-06-13T07:13:23Z","isPatch":true,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 13.06.2014 00:04, schrieb Junio C Hamano:\n> Hmph, this seems to conflict in a meaningful (and painful) way with\n> Jens's \"jl/submodule-recursive-checkout\".\n\nThen you might wanna drop my series for now, I need to rebase it\nabove Heiko's series myself to make new submodules work anyway.\n"},{"id":"244159","messageId":"xmqqlht03dih.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"539AA493.5030106@web.de","subject":"Re: [PATCH 0/5] submodule config lookup API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-13T17:50:14Z","receivedAt":"2014-06-13T17:50:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jens Lehmann <Jens.Lehmann@web.de> writes:\n\n> Am 13.06.2014 00:04, schrieb Junio C Hamano:\n>> Hmph, this seems to conflict in a meaningful (and painful) way with\n>> Jens's \"jl/submodule-recursive-checkout\".\n>\n> Then you might wanna drop my series for now, I need to rebase it\n> above Heiko's series myself to make new submodules work anyway.\n\nThanks, that makes my pile smaller by one topic ;-)\n"},{"id":"244183","messageId":"20140613223701.GA3799@sandbox-ub","threadId":"36842","inReplyTo":"xmqq38f96b9f.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH 2/5] implement submodule config cache for lookup of submodule names","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-13T22:37:12Z","receivedAt":"2014-06-13T22:37:12Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Thu, Jun 12, 2014 at 02:58:20PM -0700, Junio C Hamano wrote:\n> Heiko Voigt <hvoigt@hvoigt.net> writes:\n> \n> > ...\n> > +static int is_cache_init = 0;\n> \n> Please don't initialise variables in the .bss to zero by hand.\n\nOk will remove that.\n\n> > + ...\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> > +\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> These two look inconsistent in different ways.  I think we typically\n> quote the names like so:\n> \n> \twarning(\"I have trouble with variable '%s' somehow\", var);\n\nOk will change the quotation for the variable and parameter names.\n\nHere is the fixup[1] I will queue in my branch.\n\nCheers Heiko\n\n[1] ---8<----\nSubject: [PATCH] fixup! implement submodule config cache for lookup of\n submodule names\n\n---\n submodule-config.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/submodule-config.c b/submodule-config.c\nindex 437fbdb..f330ccc 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -26,7 +26,7 @@ struct submodule_entry {\n };\n \n static struct submodule_cache cache;\n-static int is_cache_init = 0;\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@@ -230,7 +230,7 @@ static void warn_multiple_config(const unsigned char *commit_sha1,\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\t\"'submodule.%s.%s'. Skipping second one!\",\n \t\t\tcommit_string, name, option);\n }\n \n@@ -298,8 +298,8 @@ static int parse_config(const char *var, const char *value, void *data)\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\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-- \n2.0.0\n"},{"id":"244184","messageId":"20140613224156.GA4345@sandbox-ub","threadId":"36842","inReplyTo":"xmqqy4x14wn8.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH 0/5] submodule config lookup API","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-13T22:41:57Z","receivedAt":"2014-06-13T22:41:57Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Thu, Jun 12, 2014 at 02:59:23PM -0700, Junio C Hamano wrote:\n> Heiko Voigt <hvoigt@hvoigt.net> writes:\n> \n> >  t/t7410-submodule-config.sh                      | 141 ++++++++\n> \n> We already use 7410 for something else in 'pu'; please avoid dups\n> waiting to happen.\n\nSorry about that. Should I use 7411 even though that other series is\nstill work in progress?\n"},{"id":"244274","messageId":"xmqq38f4spmm.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"20140613224156.GA4345@sandbox-ub","subject":"Re: [PATCH 0/5] submodule config lookup API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-16T17:58:25Z","receivedAt":"2014-06-16T17:58:25Z","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> On Thu, Jun 12, 2014 at 02:59:23PM -0700, Junio C Hamano wrote:\n>> Heiko Voigt <hvoigt@hvoigt.net> writes:\n>> \n>> >  t/t7410-submodule-config.sh                      | 141 ++++++++\n>> \n>> We already use 7410 for something else in 'pu'; please avoid dups\n>> waiting to happen.\n>\n> Sorry about that. Should I use 7411 even though that other series is\n> still work in progress?\n\nSurely.\n\nWhat would be an alternative?  Tell the other series to rename?  ;-)\n"},{"id":"244385","messageId":"539FFCAB.4060908@gmail.com","threadId":"36842","inReplyTo":"xmqqegyu54cl.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Karsten Blees","fromEmail":"karsten.blees@gmail.com","sentAt":"2014-06-17T08:30:35Z","receivedAt":"2014-06-17T08:30:35Z","isPatch":true,"sender":{"key":"karsten.blees@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1111200?v=4"},"body":"Am 12.06.2014 21:12, schrieb Junio C Hamano:\n> Karsten Blees <karsten.blees@gmail.com> writes:\n> \n>> Am 10.06.2014 12:17, schrieb Heiko Voigt:\n>>> The intention of Jonathans critique here[1] was that you do not see what\n>>> this parameter does on the callsite. I.e.:\n>>>\n>>> \thashmap_free(&map, 1);\n>>>\n>>> compared to\n>>>\n>>> \thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n>>>\n>>> A boolean basically transfers the same information and would not help\n>>> the reader here.\n>>>\n>>> Cheers Heiko\n>>>\n>>> [1] http://article.gmane.org/gmane.comp.version-control.git/243917\n>>>\n>>\n>> There are languages where you can have e.g. 'hashmap_free(...,\n>> free_entries: true)'. In C, however, you do not see what a\n>> parameter does at the call site. This is a general language\n>> feature, reducing redundancy and keeping it short and concise. IMO\n>> there's no reason to treat boolean parameters differently.\n> \n> But given that you are writing in C, is any of that relevant?  We do\n> want to keep our call-sites readable and understandable, \n\nBut in C, readable and understandable are opposite goals.\n'Understandable' entails long, redundant identifiers, automatically\ndecreasing readability. The compiler doesn't care about either, so\nwe could just as well keep the C part short and use plain English\nfor understandability:\n\n  /* free maps, except file entries (owned by istate->cache) */\n  hashmap_free(&istate->name_hash, 0);\n  hashmap_free(&istate->dir_hash, 1);\n\nNote that this not only explains what we're doing, but also why.\n\n> and 1 or\n> true would not help, unless (1) you are the one who wrote the\n> function and know that 1 means free the entries, or (2) the API is\n> so widely used and everybody knows what 1 means free the entries.\n> \n\nor (3) you need to check the function declaration or documentation\nanyway, to understand what the non-boolean parameters do.\n\nE.g. consider this (from remote.c:1186):\n\n  dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);\n\nvs.\n\n  dst_value = resolve_ref_unsafe(matched_src->name, sha1,\n                                 RESOLVE_REF_UNSAFE_FOR_READING,\n                                 &flag);\n\nThat's three lines vs. one, \"RESOLVE_REF_UNSAFE_\" is completely\nredundant with the function name, \"FOR_READING\" isn't particularly\nenlightening either, and you still don't know what the other three\nparameters do. IMO this would be much better:\n\n  /* fully resolve matched symref to resolved ref name and sha1 */\n  dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);\n\nSo veterans highly familiar with the code can stick to the C part\nwithout being distracted by unnecessary line breaks and\nSHOUTED_IDENTIFIERS, while everyone else may find the explanation\nhelpful.\n\n\nAs I said, using enums for hashmap_free isn't a problem on its own.\nHowever, the accepted solution for booleans in the git code base\nseems to be to use just an int and 0/1.\n\nFor consistency, we could of course change string_list*,\nresolve_ref*, index_file_exists etc. as well.\n\n...and in turn 'extern int ignore_case' (because it gets passed to\nindex_file_exists)?\n\n...and in turn all other boolean config variables?\n\nI don't think this would be an improvement, though.\n"},{"id":"244468","messageId":"20140617190018.GA2982@sandbox-ub","threadId":"36842","inReplyTo":"xmqq38f4spmm.fsf@gitster.dls.corp.google.com","subject":"Re: Re: [PATCH 0/5] submodule config lookup API","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-17T19:00:18Z","receivedAt":"2014-06-17T19:00:18Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Mon, Jun 16, 2014 at 10:58:25AM -0700, Junio C Hamano wrote:\n> Heiko Voigt <hvoigt@hvoigt.net> writes:\n> \n> > On Thu, Jun 12, 2014 at 02:59:23PM -0700, Junio C Hamano wrote:\n> >> Heiko Voigt <hvoigt@hvoigt.net> writes:\n> >> \n> >> >  t/t7410-submodule-config.sh                      | 141 ++++++++\n> >> \n> >> We already use 7410 for something else in 'pu'; please avoid dups\n> >> waiting to happen.\n> >\n> > Sorry about that. Should I use 7411 even though that other series is\n> > still work in progress?\n> \n> Surely.\n> \n> What would be an alternative?  Tell the other series to rename?  ;-)\n\nDon't know. Of course not ;-) Will rename.\n"},{"id":"244469","messageId":"20140617190425.GB2982@sandbox-ub","threadId":"36842","inReplyTo":"539FFCAB.4060908@gmail.com","subject":"Re: Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Heiko Voigt","fromEmail":"hvoigt@hvoigt.net","sentAt":"2014-06-17T19:04:25Z","receivedAt":"2014-06-17T19:04:25Z","isPatch":true,"sender":{"key":"hvoigt@hvoigt.net","avatar":"https://avatars.githubusercontent.com/u/184958?v=4"},"body":"On Tue, Jun 17, 2014 at 10:30:35AM +0200, Karsten Blees wrote:\n> Am 12.06.2014 21:12, schrieb Junio C Hamano:\n> > Karsten Blees <karsten.blees@gmail.com> writes:\n> > \n> >> Am 10.06.2014 12:17, schrieb Heiko Voigt:\n> >>> The intention of Jonathans critique here[1] was that you do not see what\n> >>> this parameter does on the callsite. I.e.:\n> >>>\n> >>> \thashmap_free(&map, 1);\n> >>>\n> >>> compared to\n> >>>\n> >>> \thashmap_free(&map, HASHMAP_FREE_ENTRIES);\n> >>>\n> >>> A boolean basically transfers the same information and would not help\n> >>> the reader here.\n> >>>\n> >>> Cheers Heiko\n> >>>\n> >>> [1] http://article.gmane.org/gmane.comp.version-control.git/243917\n> >>>\n> >>\n> >> There are languages where you can have e.g. 'hashmap_free(...,\n> >> free_entries: true)'. In C, however, you do not see what a\n> >> parameter does at the call site. This is a general language\n> >> feature, reducing redundancy and keeping it short and concise. IMO\n> >> there's no reason to treat boolean parameters differently.\n> > \n> > But given that you are writing in C, is any of that relevant?  We do\n> > want to keep our call-sites readable and understandable, \n> \n> But in C, readable and understandable are opposite goals.\n> 'Understandable' entails long, redundant identifiers, automatically\n> decreasing readability. The compiler doesn't care about either, so\n> we could just as well keep the C part short and use plain English\n> for understandability:\n> \n>   /* free maps, except file entries (owned by istate->cache) */\n>   hashmap_free(&istate->name_hash, 0);\n>   hashmap_free(&istate->dir_hash, 1);\n> \n> Note that this not only explains what we're doing, but also why.\n> \n> > and 1 or\n> > true would not help, unless (1) you are the one who wrote the\n> > function and know that 1 means free the entries, or (2) the API is\n> > so widely used and everybody knows what 1 means free the entries.\n> > \n> \n> or (3) you need to check the function declaration or documentation\n> anyway, to understand what the non-boolean parameters do.\n> \n> E.g. consider this (from remote.c:1186):\n> \n>   dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);\n> \n> vs.\n> \n>   dst_value = resolve_ref_unsafe(matched_src->name, sha1,\n>                                  RESOLVE_REF_UNSAFE_FOR_READING,\n>                                  &flag);\n> \n> That's three lines vs. one, \"RESOLVE_REF_UNSAFE_\" is completely\n> redundant with the function name, \"FOR_READING\" isn't particularly\n> enlightening either, and you still don't know what the other three\n> parameters do. IMO this would be much better:\n> \n>   /* fully resolve matched symref to resolved ref name and sha1 */\n>   dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag);\n> \n> So veterans highly familiar with the code can stick to the C part\n> without being distracted by unnecessary line breaks and\n> SHOUTED_IDENTIFIERS, while everyone else may find the explanation\n> helpful.\n> \n> \n> As I said, using enums for hashmap_free isn't a problem on its own.\n> However, the accepted solution for booleans in the git code base\n> seems to be to use just an int and 0/1.\n> \n> For consistency, we could of course change string_list*,\n> resolve_ref*, index_file_exists etc. as well.\n> \n> ...and in turn 'extern int ignore_case' (because it gets passed to\n> index_file_exists)?\n> \n> ...and in turn all other boolean config variables?\n> \n> I don't think this would be an improvement, though.\n\nIf this is such a controversial change for you I will drop this patch in\nthe next round. I think it would make the callsite more readable without\nadding much clutter but I am fine with it either way.\n"},{"id":"244494","messageId":"xmqqmwdbji1z.fsf@gitster.dls.corp.google.com","threadId":"36842","inReplyTo":"20140617190425.GB2982@sandbox-ub","subject":"Re: [PATCH 1/5] hashmap: add enum for hashmap free_entries option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-17T22:19:04Z","receivedAt":"2014-06-17T22:19:04Z","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> If this is such a controversial change for you I will drop this patch in\n> the next round. I think it would make the callsite more readable without\n> adding much clutter but I am fine with it either way.\n\nOK, let's do that.\n"}]}