2 #include "submodule-config.h"
5 #include "parse-options.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 int option_fetch_parse_recurse_submodules(const struct option *opt,
239 const char *arg, int unset)
249 *v = RECURSE_SUBMODULES_OFF;
252 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
254 *v = RECURSE_SUBMODULES_ON;
259 static int parse_update_recurse(const char *opt, const char *arg,
262 switch (git_config_maybe_bool(opt, arg)) {
264 return RECURSE_SUBMODULES_ON;
266 return RECURSE_SUBMODULES_OFF;
269 die("bad %s argument: %s", opt, arg);
270 return RECURSE_SUBMODULES_ERROR;
274 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
276 return parse_update_recurse(opt, arg, 1);
279 static int parse_push_recurse(const char *opt, const char *arg,
282 switch (git_config_maybe_bool(opt, arg)) {
284 /* There's no simple "on" value when pushing */
286 die("bad %s argument: %s", opt, arg);
288 return RECURSE_SUBMODULES_ERROR;
290 return RECURSE_SUBMODULES_OFF;
292 if (!strcmp(arg, "on-demand"))
293 return RECURSE_SUBMODULES_ON_DEMAND;
294 else if (!strcmp(arg, "check"))
295 return RECURSE_SUBMODULES_CHECK;
296 else if (!strcmp(arg, "only"))
297 return RECURSE_SUBMODULES_ONLY;
298 else if (die_on_error)
299 die("bad %s argument: %s", opt, arg);
301 return RECURSE_SUBMODULES_ERROR;
305 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
307 return parse_push_recurse(opt, arg, 1);
310 static void warn_multiple_config(const unsigned char *treeish_name,
311 const char *name, const char *option)
313 const char *commit_string = "WORKTREE";
315 commit_string = sha1_to_hex(treeish_name);
316 warning("%s:.gitmodules, multiple configurations found for "
317 "'submodule.%s.%s'. Skipping second one!",
318 commit_string, name, option);
321 struct parse_config_parameter {
322 struct submodule_cache *cache;
323 const unsigned char *treeish_name;
324 const unsigned char *gitmodules_sha1;
328 static int parse_config(const char *var, const char *value, void *data)
330 struct parse_config_parameter *me = data;
331 struct submodule *submodule;
332 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
335 /* this also ensures that we only parse submodule entries */
336 if (!name_and_item_from_var(var, &name, &item))
339 submodule = lookup_or_create_by_name(me->cache,
343 if (!strcmp(item.buf, "path")) {
345 ret = config_error_nonbool(var);
346 else if (!me->overwrite && submodule->path)
347 warn_multiple_config(me->treeish_name, submodule->name,
351 cache_remove_path(me->cache, submodule);
352 free((void *) submodule->path);
353 submodule->path = xstrdup(value);
354 cache_put_path(me->cache, submodule);
356 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
357 /* when parsing worktree configurations we can die early */
358 int die_on_error = is_null_sha1(me->gitmodules_sha1);
359 if (!me->overwrite &&
360 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
361 warn_multiple_config(me->treeish_name, submodule->name,
362 "fetchrecursesubmodules");
364 submodule->fetch_recurse = parse_fetch_recurse(
367 } else if (!strcmp(item.buf, "ignore")) {
369 ret = config_error_nonbool(var);
370 else if (!me->overwrite && submodule->ignore)
371 warn_multiple_config(me->treeish_name, submodule->name,
373 else if (strcmp(value, "untracked") &&
374 strcmp(value, "dirty") &&
375 strcmp(value, "all") &&
376 strcmp(value, "none"))
377 warning("Invalid parameter '%s' for config option "
378 "'submodule.%s.ignore'", value, name.buf);
380 free((void *) submodule->ignore);
381 submodule->ignore = xstrdup(value);
383 } else if (!strcmp(item.buf, "url")) {
385 ret = config_error_nonbool(var);
386 } else if (!me->overwrite && submodule->url) {
387 warn_multiple_config(me->treeish_name, submodule->name,
390 free((void *) submodule->url);
391 submodule->url = xstrdup(value);
393 } else if (!strcmp(item.buf, "update")) {
395 ret = config_error_nonbool(var);
396 else if (!me->overwrite &&
397 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
398 warn_multiple_config(me->treeish_name, submodule->name,
400 else if (parse_submodule_update_strategy(value,
401 &submodule->update_strategy) < 0)
402 die(_("invalid value for %s"), var);
403 } else if (!strcmp(item.buf, "shallow")) {
404 if (!me->overwrite && submodule->recommend_shallow != -1)
405 warn_multiple_config(me->treeish_name, submodule->name,
408 submodule->recommend_shallow =
409 git_config_bool(var, value);
410 } else if (!strcmp(item.buf, "branch")) {
411 if (!me->overwrite && submodule->branch)
412 warn_multiple_config(me->treeish_name, submodule->name,
415 free((void *)submodule->branch);
416 submodule->branch = xstrdup(value);
420 strbuf_release(&name);
421 strbuf_release(&item);
426 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
427 unsigned char *gitmodules_sha1,
432 if (is_null_sha1(treeish_name)) {
433 hashclr(gitmodules_sha1);
437 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
438 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
444 /* This does a lookup of a submodule configuration by name or by path
445 * (key) with on-demand reading of the appropriate .gitmodules from
448 static const struct submodule *config_from(struct submodule_cache *cache,
449 const unsigned char *treeish_name, const char *key,
450 enum lookup_type lookup_type)
452 struct strbuf rev = STRBUF_INIT;
453 unsigned long config_size;
455 unsigned char sha1[20];
456 enum object_type type;
457 const struct submodule *submodule = NULL;
458 struct parse_config_parameter parameter;
461 * If any parameter except the cache is a NULL pointer just
462 * return the first submodule. Can be used to check whether
463 * there are any submodules parsed.
465 if (!treeish_name || !key) {
466 struct hashmap_iter iter;
467 struct submodule_entry *entry;
469 entry = hashmap_iter_first(&cache->for_name, &iter);
472 return entry->config;
475 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
478 switch (lookup_type) {
480 submodule = cache_lookup_name(cache, sha1, key);
483 submodule = cache_lookup_path(cache, sha1, key);
489 config = read_sha1_file(sha1, &type, &config_size);
490 if (!config || type != OBJ_BLOB)
493 /* fill the submodule config into the cache */
494 parameter.cache = cache;
495 parameter.treeish_name = treeish_name;
496 parameter.gitmodules_sha1 = sha1;
497 parameter.overwrite = 0;
498 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
499 config, config_size, ¶meter);
500 strbuf_release(&rev);
503 switch (lookup_type) {
505 return cache_lookup_name(cache, sha1, key);
507 return cache_lookup_path(cache, sha1, key);
513 strbuf_release(&rev);
518 static void ensure_cache_init(void)
523 cache_init(&the_submodule_cache);
527 int parse_submodule_config_option(const char *var, const char *value)
529 struct parse_config_parameter parameter;
530 parameter.cache = &the_submodule_cache;
531 parameter.treeish_name = NULL;
532 parameter.gitmodules_sha1 = null_sha1;
533 parameter.overwrite = 1;
536 return parse_config(var, value, ¶meter);
539 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
543 return config_from(&the_submodule_cache, treeish_name, name, lookup_name);
546 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
550 return config_from(&the_submodule_cache, treeish_name, path, lookup_path);
553 void submodule_free(void)
555 cache_free(&the_submodule_cache);