2 #include "repository.h"
4 #include "submodule-config.h"
9 * submodule cache lookup structure
10 * There is one shared set of 'struct submodule' entries which can be
11 * looked up by their sha1 blob id of the .gitmodule file and either
12 * using path or name as key.
13 * for_path stores submodule entries with path as key
14 * for_name stores submodule entries with name as key
16 struct submodule_cache {
17 struct hashmap for_path;
18 struct hashmap for_name;
19 unsigned initialized:1;
23 * thin wrapper struct needed to insert 'struct submodule' entries to
26 struct submodule_entry {
27 struct hashmap_entry ent;
28 struct submodule *config;
36 static int config_path_cmp(const struct submodule_entry *a,
37 const struct submodule_entry *b,
40 return strcmp(a->config->path, b->config->path) ||
41 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
44 static int config_name_cmp(const struct submodule_entry *a,
45 const struct submodule_entry *b,
48 return strcmp(a->config->name, b->config->name) ||
49 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
52 static struct submodule_cache *submodule_cache_alloc(void)
54 return xcalloc(1, sizeof(struct submodule_cache));
57 static void submodule_cache_init(struct submodule_cache *cache)
59 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
60 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
61 cache->initialized = 1;
64 static void free_one_config(struct submodule_entry *entry)
66 free((void *) entry->config->path);
67 free((void *) entry->config->name);
68 free((void *) entry->config->branch);
69 free((void *) entry->config->update_strategy.command);
73 static void submodule_cache_clear(struct submodule_cache *cache)
75 struct hashmap_iter iter;
76 struct submodule_entry *entry;
78 if (!cache->initialized)
82 * We iterate over the name hash here to be symmetric with the
83 * allocation of struct submodule entries. Each is allocated by
84 * their .gitmodule blob sha1 and submodule name.
86 hashmap_iter_init(&cache->for_name, &iter);
87 while ((entry = hashmap_iter_next(&iter)))
88 free_one_config(entry);
90 hashmap_free(&cache->for_path, 1);
91 hashmap_free(&cache->for_name, 1);
92 cache->initialized = 0;
95 void submodule_cache_free(struct submodule_cache *cache)
97 submodule_cache_clear(cache);
101 static unsigned int hash_sha1_string(const unsigned char *sha1,
104 return memhash(sha1, 20) + strhash(string);
107 static void cache_put_path(struct submodule_cache *cache,
108 struct submodule *submodule)
110 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
112 struct submodule_entry *e = xmalloc(sizeof(*e));
113 hashmap_entry_init(e, hash);
114 e->config = submodule;
115 hashmap_put(&cache->for_path, e);
118 static void cache_remove_path(struct submodule_cache *cache,
119 struct submodule *submodule)
121 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
123 struct submodule_entry e;
124 struct submodule_entry *removed;
125 hashmap_entry_init(&e, hash);
126 e.config = submodule;
127 removed = hashmap_remove(&cache->for_path, &e, NULL);
131 static void cache_add(struct submodule_cache *cache,
132 struct submodule *submodule)
134 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
136 struct submodule_entry *e = xmalloc(sizeof(*e));
137 hashmap_entry_init(e, hash);
138 e->config = submodule;
139 hashmap_add(&cache->for_name, e);
142 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
143 const unsigned char *gitmodules_sha1, const char *path)
145 struct submodule_entry *entry;
146 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
147 struct submodule_entry key;
148 struct submodule key_config;
150 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
151 key_config.path = path;
153 hashmap_entry_init(&key, hash);
154 key.config = &key_config;
156 entry = hashmap_get(&cache->for_path, &key, NULL);
158 return entry->config;
162 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
163 const unsigned char *gitmodules_sha1, const char *name)
165 struct submodule_entry *entry;
166 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
167 struct submodule_entry key;
168 struct submodule key_config;
170 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
171 key_config.name = name;
173 hashmap_entry_init(&key, hash);
174 key.config = &key_config;
176 entry = hashmap_get(&cache->for_name, &key, NULL);
178 return entry->config;
182 static int name_and_item_from_var(const char *var, struct strbuf *name,
185 const char *subsection, *key;
186 int subsection_len, parse;
187 parse = parse_config_key(var, "submodule", &subsection,
188 &subsection_len, &key);
189 if (parse < 0 || !subsection)
192 strbuf_add(name, subsection, subsection_len);
193 strbuf_addstr(item, key);
198 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
199 const unsigned char *gitmodules_sha1, const char *name)
201 struct submodule *submodule;
202 struct strbuf name_buf = STRBUF_INIT;
204 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
208 submodule = xmalloc(sizeof(*submodule));
210 strbuf_addstr(&name_buf, name);
211 submodule->name = strbuf_detach(&name_buf, NULL);
213 submodule->path = NULL;
214 submodule->url = NULL;
215 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
216 submodule->update_strategy.command = NULL;
217 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
218 submodule->ignore = NULL;
219 submodule->branch = NULL;
220 submodule->recommend_shallow = -1;
222 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
224 cache_add(cache, submodule);
229 static int parse_fetch_recurse(const char *opt, const char *arg,
232 switch (git_config_maybe_bool(opt, arg)) {
234 return RECURSE_SUBMODULES_ON;
236 return RECURSE_SUBMODULES_OFF;
238 if (!strcmp(arg, "on-demand"))
239 return RECURSE_SUBMODULES_ON_DEMAND;
242 die("bad %s argument: %s", opt, arg);
244 return RECURSE_SUBMODULES_ERROR;
248 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
250 return parse_fetch_recurse(opt, arg, 1);
253 static int parse_update_recurse(const char *opt, const char *arg,
256 switch (git_config_maybe_bool(opt, arg)) {
258 return RECURSE_SUBMODULES_ON;
260 return RECURSE_SUBMODULES_OFF;
263 die("bad %s argument: %s", opt, arg);
264 return RECURSE_SUBMODULES_ERROR;
268 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
270 return parse_update_recurse(opt, arg, 1);
273 static int parse_push_recurse(const char *opt, const char *arg,
276 switch (git_config_maybe_bool(opt, arg)) {
278 /* There's no simple "on" value when pushing */
280 die("bad %s argument: %s", opt, arg);
282 return RECURSE_SUBMODULES_ERROR;
284 return RECURSE_SUBMODULES_OFF;
286 if (!strcmp(arg, "on-demand"))
287 return RECURSE_SUBMODULES_ON_DEMAND;
288 else if (!strcmp(arg, "check"))
289 return RECURSE_SUBMODULES_CHECK;
290 else if (!strcmp(arg, "only"))
291 return RECURSE_SUBMODULES_ONLY;
292 else if (die_on_error)
293 die("bad %s argument: %s", opt, arg);
295 return RECURSE_SUBMODULES_ERROR;
299 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
301 return parse_push_recurse(opt, arg, 1);
304 static void warn_multiple_config(const unsigned char *treeish_name,
305 const char *name, const char *option)
307 const char *commit_string = "WORKTREE";
309 commit_string = sha1_to_hex(treeish_name);
310 warning("%s:.gitmodules, multiple configurations found for "
311 "'submodule.%s.%s'. Skipping second one!",
312 commit_string, name, option);
315 struct parse_config_parameter {
316 struct submodule_cache *cache;
317 const unsigned char *treeish_name;
318 const unsigned char *gitmodules_sha1;
322 static int parse_config(const char *var, const char *value, void *data)
324 struct parse_config_parameter *me = data;
325 struct submodule *submodule;
326 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
329 /* this also ensures that we only parse submodule entries */
330 if (!name_and_item_from_var(var, &name, &item))
333 submodule = lookup_or_create_by_name(me->cache,
337 if (!strcmp(item.buf, "path")) {
339 ret = config_error_nonbool(var);
340 else if (!me->overwrite && submodule->path)
341 warn_multiple_config(me->treeish_name, submodule->name,
345 cache_remove_path(me->cache, submodule);
346 free((void *) submodule->path);
347 submodule->path = xstrdup(value);
348 cache_put_path(me->cache, submodule);
350 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
351 /* when parsing worktree configurations we can die early */
352 int die_on_error = is_null_sha1(me->gitmodules_sha1);
353 if (!me->overwrite &&
354 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
355 warn_multiple_config(me->treeish_name, submodule->name,
356 "fetchrecursesubmodules");
358 submodule->fetch_recurse = parse_fetch_recurse(
361 } else if (!strcmp(item.buf, "ignore")) {
363 ret = config_error_nonbool(var);
364 else if (!me->overwrite && submodule->ignore)
365 warn_multiple_config(me->treeish_name, submodule->name,
367 else if (strcmp(value, "untracked") &&
368 strcmp(value, "dirty") &&
369 strcmp(value, "all") &&
370 strcmp(value, "none"))
371 warning("Invalid parameter '%s' for config option "
372 "'submodule.%s.ignore'", value, name.buf);
374 free((void *) submodule->ignore);
375 submodule->ignore = xstrdup(value);
377 } else if (!strcmp(item.buf, "url")) {
379 ret = config_error_nonbool(var);
380 } else if (!me->overwrite && submodule->url) {
381 warn_multiple_config(me->treeish_name, submodule->name,
384 free((void *) submodule->url);
385 submodule->url = xstrdup(value);
387 } else if (!strcmp(item.buf, "update")) {
389 ret = config_error_nonbool(var);
390 else if (!me->overwrite &&
391 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
392 warn_multiple_config(me->treeish_name, submodule->name,
394 else if (parse_submodule_update_strategy(value,
395 &submodule->update_strategy) < 0)
396 die(_("invalid value for %s"), var);
397 } else if (!strcmp(item.buf, "shallow")) {
398 if (!me->overwrite && submodule->recommend_shallow != -1)
399 warn_multiple_config(me->treeish_name, submodule->name,
402 submodule->recommend_shallow =
403 git_config_bool(var, value);
404 } else if (!strcmp(item.buf, "branch")) {
405 if (!me->overwrite && submodule->branch)
406 warn_multiple_config(me->treeish_name, submodule->name,
409 free((void *)submodule->branch);
410 submodule->branch = xstrdup(value);
414 strbuf_release(&name);
415 strbuf_release(&item);
420 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
421 unsigned char *gitmodules_sha1,
426 if (is_null_sha1(treeish_name)) {
427 hashclr(gitmodules_sha1);
431 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
432 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
438 /* This does a lookup of a submodule configuration by name or by path
439 * (key) with on-demand reading of the appropriate .gitmodules from
442 static const struct submodule *config_from(struct submodule_cache *cache,
443 const unsigned char *treeish_name, const char *key,
444 enum lookup_type lookup_type)
446 struct strbuf rev = STRBUF_INIT;
447 unsigned long config_size;
449 unsigned char sha1[20];
450 enum object_type type;
451 const struct submodule *submodule = NULL;
452 struct parse_config_parameter parameter;
455 * If any parameter except the cache is a NULL pointer just
456 * return the first submodule. Can be used to check whether
457 * there are any submodules parsed.
459 if (!treeish_name || !key) {
460 struct hashmap_iter iter;
461 struct submodule_entry *entry;
463 entry = hashmap_iter_first(&cache->for_name, &iter);
466 return entry->config;
469 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
472 switch (lookup_type) {
474 submodule = cache_lookup_name(cache, sha1, key);
477 submodule = cache_lookup_path(cache, sha1, key);
483 config = read_sha1_file(sha1, &type, &config_size);
484 if (!config || type != OBJ_BLOB)
487 /* fill the submodule config into the cache */
488 parameter.cache = cache;
489 parameter.treeish_name = treeish_name;
490 parameter.gitmodules_sha1 = sha1;
491 parameter.overwrite = 0;
492 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
493 config, config_size, ¶meter);
494 strbuf_release(&rev);
497 switch (lookup_type) {
499 return cache_lookup_name(cache, sha1, key);
501 return cache_lookup_path(cache, sha1, key);
507 strbuf_release(&rev);
512 static void submodule_cache_check_init(struct repository *repo)
514 if (repo->submodule_cache && repo->submodule_cache->initialized)
517 if (!repo->submodule_cache)
518 repo->submodule_cache = submodule_cache_alloc();
520 submodule_cache_init(repo->submodule_cache);
523 int submodule_config_option(struct repository *repo,
524 const char *var, const char *value)
526 struct parse_config_parameter parameter;
528 submodule_cache_check_init(repo);
530 parameter.cache = repo->submodule_cache;
531 parameter.treeish_name = NULL;
532 parameter.gitmodules_sha1 = null_sha1;
533 parameter.overwrite = 1;
535 return parse_config(var, value, ¶meter);
538 int parse_submodule_config_option(const char *var, const char *value)
540 return submodule_config_option(the_repository, var, value);
543 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
546 submodule_cache_check_init(the_repository);
547 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
550 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
553 submodule_cache_check_init(the_repository);
554 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
557 const struct submodule *submodule_from_cache(struct repository *repo,
558 const unsigned char *treeish_name,
561 submodule_cache_check_init(repo);
562 return config_from(repo->submodule_cache, treeish_name,
566 void submodule_free(void)
568 if (the_repository->submodule_cache)
569 submodule_cache_clear(the_repository->submodule_cache);