3 #include "submodule-config.h"
8 * submodule cache lookup structure
9 * There is one shared set of 'struct submodule' entries which can be
10 * looked up by their sha1 blob id of the .gitmodule file and either
11 * using path or name as key.
12 * for_path stores submodule entries with path as key
13 * for_name stores submodule entries with name as key
15 struct submodule_cache {
16 struct hashmap for_path;
17 struct hashmap for_name;
21 * thin wrapper struct needed to insert 'struct submodule' entries to
24 struct submodule_entry {
25 struct hashmap_entry ent;
26 struct submodule *config;
34 static struct submodule_cache the_submodule_cache;
35 static int is_cache_init;
37 static int config_path_cmp(const struct submodule_entry *a,
38 const struct submodule_entry *b,
41 return strcmp(a->config->path, b->config->path) ||
42 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
45 static int config_name_cmp(const struct submodule_entry *a,
46 const struct submodule_entry *b,
49 return strcmp(a->config->name, b->config->name) ||
50 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
53 static void cache_init(struct submodule_cache *cache)
55 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
56 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
59 static void free_one_config(struct submodule_entry *entry)
61 free((void *) entry->config->path);
62 free((void *) entry->config->name);
63 free((void *) entry->config->branch);
64 free((void *) entry->config->update_strategy.command);
68 static void cache_free(struct submodule_cache *cache)
70 struct hashmap_iter iter;
71 struct submodule_entry *entry;
74 * We iterate over the name hash here to be symmetric with the
75 * allocation of struct submodule entries. Each is allocated by
76 * their .gitmodule blob sha1 and submodule name.
78 hashmap_iter_init(&cache->for_name, &iter);
79 while ((entry = hashmap_iter_next(&iter)))
80 free_one_config(entry);
82 hashmap_free(&cache->for_path, 1);
83 hashmap_free(&cache->for_name, 1);
86 static unsigned int hash_sha1_string(const unsigned char *sha1,
89 return memhash(sha1, 20) + strhash(string);
92 static void cache_put_path(struct submodule_cache *cache,
93 struct submodule *submodule)
95 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
97 struct submodule_entry *e = xmalloc(sizeof(*e));
98 hashmap_entry_init(e, hash);
99 e->config = submodule;
100 hashmap_put(&cache->for_path, e);
103 static void cache_remove_path(struct submodule_cache *cache,
104 struct submodule *submodule)
106 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
108 struct submodule_entry e;
109 struct submodule_entry *removed;
110 hashmap_entry_init(&e, hash);
111 e.config = submodule;
112 removed = hashmap_remove(&cache->for_path, &e, NULL);
116 static void cache_add(struct submodule_cache *cache,
117 struct submodule *submodule)
119 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
121 struct submodule_entry *e = xmalloc(sizeof(*e));
122 hashmap_entry_init(e, hash);
123 e->config = submodule;
124 hashmap_add(&cache->for_name, e);
127 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
128 const unsigned char *gitmodules_sha1, const char *path)
130 struct submodule_entry *entry;
131 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
132 struct submodule_entry key;
133 struct submodule key_config;
135 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
136 key_config.path = path;
138 hashmap_entry_init(&key, hash);
139 key.config = &key_config;
141 entry = hashmap_get(&cache->for_path, &key, NULL);
143 return entry->config;
147 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
148 const unsigned char *gitmodules_sha1, const char *name)
150 struct submodule_entry *entry;
151 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
152 struct submodule_entry key;
153 struct submodule key_config;
155 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
156 key_config.name = name;
158 hashmap_entry_init(&key, hash);
159 key.config = &key_config;
161 entry = hashmap_get(&cache->for_name, &key, NULL);
163 return entry->config;
167 static int name_and_item_from_var(const char *var, struct strbuf *name,
170 const char *subsection, *key;
171 int subsection_len, parse;
172 parse = parse_config_key(var, "submodule", &subsection,
173 &subsection_len, &key);
174 if (parse < 0 || !subsection)
177 strbuf_add(name, subsection, subsection_len);
178 strbuf_addstr(item, key);
183 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
184 const unsigned char *gitmodules_sha1, const char *name)
186 struct submodule *submodule;
187 struct strbuf name_buf = STRBUF_INIT;
189 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
193 submodule = xmalloc(sizeof(*submodule));
195 strbuf_addstr(&name_buf, name);
196 submodule->name = strbuf_detach(&name_buf, NULL);
198 submodule->path = NULL;
199 submodule->url = NULL;
200 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
201 submodule->update_strategy.command = NULL;
202 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
203 submodule->ignore = NULL;
204 submodule->branch = NULL;
205 submodule->recommend_shallow = -1;
207 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
209 cache_add(cache, submodule);
214 static int parse_fetch_recurse(const char *opt, const char *arg,
217 switch (git_config_maybe_bool(opt, arg)) {
219 return RECURSE_SUBMODULES_ON;
221 return RECURSE_SUBMODULES_OFF;
223 if (!strcmp(arg, "on-demand"))
224 return RECURSE_SUBMODULES_ON_DEMAND;
227 die("bad %s argument: %s", opt, arg);
229 return RECURSE_SUBMODULES_ERROR;
233 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
235 return parse_fetch_recurse(opt, arg, 1);
238 static int parse_update_recurse(const char *opt, const char *arg,
241 switch (git_config_maybe_bool(opt, arg)) {
243 return RECURSE_SUBMODULES_ON;
245 return RECURSE_SUBMODULES_OFF;
248 die("bad %s argument: %s", opt, arg);
249 return RECURSE_SUBMODULES_ERROR;
253 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
255 return parse_update_recurse(opt, arg, 1);
258 static int parse_push_recurse(const char *opt, const char *arg,
261 switch (git_config_maybe_bool(opt, arg)) {
263 /* There's no simple "on" value when pushing */
265 die("bad %s argument: %s", opt, arg);
267 return RECURSE_SUBMODULES_ERROR;
269 return RECURSE_SUBMODULES_OFF;
271 if (!strcmp(arg, "on-demand"))
272 return RECURSE_SUBMODULES_ON_DEMAND;
273 else if (!strcmp(arg, "check"))
274 return RECURSE_SUBMODULES_CHECK;
275 else if (!strcmp(arg, "only"))
276 return RECURSE_SUBMODULES_ONLY;
277 else if (die_on_error)
278 die("bad %s argument: %s", opt, arg);
280 return RECURSE_SUBMODULES_ERROR;
284 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
286 return parse_push_recurse(opt, arg, 1);
289 static void warn_multiple_config(const unsigned char *treeish_name,
290 const char *name, const char *option)
292 const char *commit_string = "WORKTREE";
294 commit_string = sha1_to_hex(treeish_name);
295 warning("%s:.gitmodules, multiple configurations found for "
296 "'submodule.%s.%s'. Skipping second one!",
297 commit_string, name, option);
300 struct parse_config_parameter {
301 struct submodule_cache *cache;
302 const unsigned char *treeish_name;
303 const unsigned char *gitmodules_sha1;
307 static int parse_config(const char *var, const char *value, void *data)
309 struct parse_config_parameter *me = data;
310 struct submodule *submodule;
311 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
314 /* this also ensures that we only parse submodule entries */
315 if (!name_and_item_from_var(var, &name, &item))
318 submodule = lookup_or_create_by_name(me->cache,
322 if (!strcmp(item.buf, "path")) {
324 ret = config_error_nonbool(var);
325 else if (!me->overwrite && submodule->path)
326 warn_multiple_config(me->treeish_name, submodule->name,
330 cache_remove_path(me->cache, submodule);
331 free((void *) submodule->path);
332 submodule->path = xstrdup(value);
333 cache_put_path(me->cache, submodule);
335 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
336 /* when parsing worktree configurations we can die early */
337 int die_on_error = is_null_sha1(me->gitmodules_sha1);
338 if (!me->overwrite &&
339 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
340 warn_multiple_config(me->treeish_name, submodule->name,
341 "fetchrecursesubmodules");
343 submodule->fetch_recurse = parse_fetch_recurse(
346 } else if (!strcmp(item.buf, "ignore")) {
348 ret = config_error_nonbool(var);
349 else if (!me->overwrite && submodule->ignore)
350 warn_multiple_config(me->treeish_name, submodule->name,
352 else if (strcmp(value, "untracked") &&
353 strcmp(value, "dirty") &&
354 strcmp(value, "all") &&
355 strcmp(value, "none"))
356 warning("Invalid parameter '%s' for config option "
357 "'submodule.%s.ignore'", value, name.buf);
359 free((void *) submodule->ignore);
360 submodule->ignore = xstrdup(value);
362 } else if (!strcmp(item.buf, "url")) {
364 ret = config_error_nonbool(var);
365 } else if (!me->overwrite && submodule->url) {
366 warn_multiple_config(me->treeish_name, submodule->name,
369 free((void *) submodule->url);
370 submodule->url = xstrdup(value);
372 } else if (!strcmp(item.buf, "update")) {
374 ret = config_error_nonbool(var);
375 else if (!me->overwrite &&
376 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
377 warn_multiple_config(me->treeish_name, submodule->name,
379 else if (parse_submodule_update_strategy(value,
380 &submodule->update_strategy) < 0)
381 die(_("invalid value for %s"), var);
382 } else if (!strcmp(item.buf, "shallow")) {
383 if (!me->overwrite && submodule->recommend_shallow != -1)
384 warn_multiple_config(me->treeish_name, submodule->name,
387 submodule->recommend_shallow =
388 git_config_bool(var, value);
389 } else if (!strcmp(item.buf, "branch")) {
390 if (!me->overwrite && submodule->branch)
391 warn_multiple_config(me->treeish_name, submodule->name,
394 free((void *)submodule->branch);
395 submodule->branch = xstrdup(value);
399 strbuf_release(&name);
400 strbuf_release(&item);
405 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
406 unsigned char *gitmodules_sha1,
411 if (is_null_sha1(treeish_name)) {
412 hashclr(gitmodules_sha1);
416 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
417 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
423 /* This does a lookup of a submodule configuration by name or by path
424 * (key) with on-demand reading of the appropriate .gitmodules from
427 static const struct submodule *config_from(struct submodule_cache *cache,
428 const unsigned char *treeish_name, const char *key,
429 enum lookup_type lookup_type)
431 struct strbuf rev = STRBUF_INIT;
432 unsigned long config_size;
434 unsigned char sha1[20];
435 enum object_type type;
436 const struct submodule *submodule = NULL;
437 struct parse_config_parameter parameter;
440 * If any parameter except the cache is a NULL pointer just
441 * return the first submodule. Can be used to check whether
442 * there are any submodules parsed.
444 if (!treeish_name || !key) {
445 struct hashmap_iter iter;
446 struct submodule_entry *entry;
448 entry = hashmap_iter_first(&cache->for_name, &iter);
451 return entry->config;
454 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
457 switch (lookup_type) {
459 submodule = cache_lookup_name(cache, sha1, key);
462 submodule = cache_lookup_path(cache, sha1, key);
468 config = read_sha1_file(sha1, &type, &config_size);
469 if (!config || type != OBJ_BLOB)
472 /* fill the submodule config into the cache */
473 parameter.cache = cache;
474 parameter.treeish_name = treeish_name;
475 parameter.gitmodules_sha1 = sha1;
476 parameter.overwrite = 0;
477 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
478 config, config_size, ¶meter);
479 strbuf_release(&rev);
482 switch (lookup_type) {
484 return cache_lookup_name(cache, sha1, key);
486 return cache_lookup_path(cache, sha1, key);
492 strbuf_release(&rev);
497 static void ensure_cache_init(void)
502 cache_init(&the_submodule_cache);
506 int parse_submodule_config_option(const char *var, const char *value)
508 struct parse_config_parameter parameter;
509 parameter.cache = &the_submodule_cache;
510 parameter.treeish_name = NULL;
511 parameter.gitmodules_sha1 = null_sha1;
512 parameter.overwrite = 1;
515 return parse_config(var, value, ¶meter);
518 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
522 return config_from(&the_submodule_cache, treeish_name, name, lookup_name);
525 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
529 return config_from(&the_submodule_cache, treeish_name, path, lookup_path);
532 void submodule_free(void)
534 cache_free(&the_submodule_cache);