2 #include "repository.h"
4 #include "submodule-config.h"
7 #include "parse-options.h"
10 * submodule cache lookup structure
11 * There is one shared set of 'struct submodule' entries which can be
12 * looked up by their sha1 blob id of the .gitmodule file and either
13 * using path or name as key.
14 * for_path stores submodule entries with path as key
15 * for_name stores submodule entries with name as key
17 struct submodule_cache {
18 struct hashmap for_path;
19 struct hashmap for_name;
20 unsigned initialized:1;
24 * thin wrapper struct needed to insert 'struct submodule' entries to
27 struct submodule_entry {
28 struct hashmap_entry ent;
29 struct submodule *config;
37 static int config_path_cmp(const void *unused_cmp_data,
38 const struct submodule_entry *a,
39 const struct submodule_entry *b,
40 const void *unused_keydata)
42 return strcmp(a->config->path, b->config->path) ||
43 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
46 static int config_name_cmp(const void *unused_cmp_data,
47 const struct submodule_entry *a,
48 const struct submodule_entry *b,
49 const void *unused_keydata)
51 return strcmp(a->config->name, b->config->name) ||
52 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
55 static struct submodule_cache *submodule_cache_alloc(void)
57 return xcalloc(1, sizeof(struct submodule_cache));
60 static void submodule_cache_init(struct submodule_cache *cache)
62 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
63 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
64 cache->initialized = 1;
67 static void free_one_config(struct submodule_entry *entry)
69 free((void *) entry->config->path);
70 free((void *) entry->config->name);
71 free((void *) entry->config->branch);
72 free((void *) entry->config->update_strategy.command);
76 static void submodule_cache_clear(struct submodule_cache *cache)
78 struct hashmap_iter iter;
79 struct submodule_entry *entry;
81 if (!cache->initialized)
85 * We iterate over the name hash here to be symmetric with the
86 * allocation of struct submodule entries. Each is allocated by
87 * their .gitmodule blob sha1 and submodule name.
89 hashmap_iter_init(&cache->for_name, &iter);
90 while ((entry = hashmap_iter_next(&iter)))
91 free_one_config(entry);
93 hashmap_free(&cache->for_path, 1);
94 hashmap_free(&cache->for_name, 1);
95 cache->initialized = 0;
98 void submodule_cache_free(struct submodule_cache *cache)
100 submodule_cache_clear(cache);
104 static unsigned int hash_sha1_string(const unsigned char *sha1,
107 return memhash(sha1, 20) + strhash(string);
110 static void cache_put_path(struct submodule_cache *cache,
111 struct submodule *submodule)
113 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
115 struct submodule_entry *e = xmalloc(sizeof(*e));
116 hashmap_entry_init(e, hash);
117 e->config = submodule;
118 hashmap_put(&cache->for_path, e);
121 static void cache_remove_path(struct submodule_cache *cache,
122 struct submodule *submodule)
124 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
126 struct submodule_entry e;
127 struct submodule_entry *removed;
128 hashmap_entry_init(&e, hash);
129 e.config = submodule;
130 removed = hashmap_remove(&cache->for_path, &e, NULL);
134 static void cache_add(struct submodule_cache *cache,
135 struct submodule *submodule)
137 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
139 struct submodule_entry *e = xmalloc(sizeof(*e));
140 hashmap_entry_init(e, hash);
141 e->config = submodule;
142 hashmap_add(&cache->for_name, e);
145 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
146 const unsigned char *gitmodules_sha1, const char *path)
148 struct submodule_entry *entry;
149 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
150 struct submodule_entry key;
151 struct submodule key_config;
153 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
154 key_config.path = path;
156 hashmap_entry_init(&key, hash);
157 key.config = &key_config;
159 entry = hashmap_get(&cache->for_path, &key, NULL);
161 return entry->config;
165 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
166 const unsigned char *gitmodules_sha1, const char *name)
168 struct submodule_entry *entry;
169 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
170 struct submodule_entry key;
171 struct submodule key_config;
173 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
174 key_config.name = name;
176 hashmap_entry_init(&key, hash);
177 key.config = &key_config;
179 entry = hashmap_get(&cache->for_name, &key, NULL);
181 return entry->config;
185 int check_submodule_name(const char *name)
187 /* Disallow empty names */
192 * Look for '..' as a path component. Check both '/' and '\\' as
193 * separators rather than is_dir_sep(), because we want the name rules
194 * to be consistent across platforms.
196 goto in_component; /* always start inside component */
199 if (c == '/' || c == '\\') {
201 if (name[0] == '.' && name[1] == '.' &&
202 (!name[2] || name[2] == '/' || name[2] == '\\'))
210 static int name_and_item_from_var(const char *var, struct strbuf *name,
213 const char *subsection, *key;
214 int subsection_len, parse;
215 parse = parse_config_key(var, "submodule", &subsection,
216 &subsection_len, &key);
217 if (parse < 0 || !subsection)
220 strbuf_add(name, subsection, subsection_len);
221 if (check_submodule_name(name->buf) < 0) {
222 warning(_("ignoring suspicious submodule name: %s"), name->buf);
223 strbuf_release(name);
227 strbuf_addstr(item, key);
232 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
233 const unsigned char *gitmodules_sha1, const char *name)
235 struct submodule *submodule;
236 struct strbuf name_buf = STRBUF_INIT;
238 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
242 submodule = xmalloc(sizeof(*submodule));
244 strbuf_addstr(&name_buf, name);
245 submodule->name = strbuf_detach(&name_buf, NULL);
247 submodule->path = NULL;
248 submodule->url = NULL;
249 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
250 submodule->update_strategy.command = NULL;
251 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
252 submodule->ignore = NULL;
253 submodule->branch = NULL;
254 submodule->recommend_shallow = -1;
256 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
258 cache_add(cache, submodule);
263 static int parse_fetch_recurse(const char *opt, const char *arg,
266 switch (git_config_maybe_bool(opt, arg)) {
268 return RECURSE_SUBMODULES_ON;
270 return RECURSE_SUBMODULES_OFF;
272 if (!strcmp(arg, "on-demand"))
273 return RECURSE_SUBMODULES_ON_DEMAND;
276 die("bad %s argument: %s", opt, arg);
278 return RECURSE_SUBMODULES_ERROR;
282 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
284 return parse_fetch_recurse(opt, arg, 1);
287 int option_fetch_parse_recurse_submodules(const struct option *opt,
288 const char *arg, int unset)
298 *v = RECURSE_SUBMODULES_OFF;
301 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
303 *v = RECURSE_SUBMODULES_ON;
308 static int parse_update_recurse(const char *opt, const char *arg,
311 switch (git_config_maybe_bool(opt, arg)) {
313 return RECURSE_SUBMODULES_ON;
315 return RECURSE_SUBMODULES_OFF;
318 die("bad %s argument: %s", opt, arg);
319 return RECURSE_SUBMODULES_ERROR;
323 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
325 return parse_update_recurse(opt, arg, 1);
328 static int parse_push_recurse(const char *opt, const char *arg,
331 switch (git_config_maybe_bool(opt, arg)) {
333 /* There's no simple "on" value when pushing */
335 die("bad %s argument: %s", opt, arg);
337 return RECURSE_SUBMODULES_ERROR;
339 return RECURSE_SUBMODULES_OFF;
341 if (!strcmp(arg, "on-demand"))
342 return RECURSE_SUBMODULES_ON_DEMAND;
343 else if (!strcmp(arg, "check"))
344 return RECURSE_SUBMODULES_CHECK;
345 else if (!strcmp(arg, "only"))
346 return RECURSE_SUBMODULES_ONLY;
347 else if (die_on_error)
348 die("bad %s argument: %s", opt, arg);
350 return RECURSE_SUBMODULES_ERROR;
354 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
356 return parse_push_recurse(opt, arg, 1);
359 static void warn_multiple_config(const unsigned char *treeish_name,
360 const char *name, const char *option)
362 const char *commit_string = "WORKTREE";
364 commit_string = sha1_to_hex(treeish_name);
365 warning("%s:.gitmodules, multiple configurations found for "
366 "'submodule.%s.%s'. Skipping second one!",
367 commit_string, name, option);
370 static void warn_command_line_option(const char *var, const char *value)
372 warning(_("ignoring '%s' which may be interpreted as"
373 " a command-line option: %s"), var, value);
376 struct parse_config_parameter {
377 struct submodule_cache *cache;
378 const unsigned char *treeish_name;
379 const unsigned char *gitmodules_sha1;
383 static int parse_config(const char *var, const char *value, void *data)
385 struct parse_config_parameter *me = data;
386 struct submodule *submodule;
387 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
390 /* this also ensures that we only parse submodule entries */
391 if (!name_and_item_from_var(var, &name, &item))
394 submodule = lookup_or_create_by_name(me->cache,
398 if (!strcmp(item.buf, "path")) {
400 ret = config_error_nonbool(var);
401 else if (looks_like_command_line_option(value))
402 warn_command_line_option(var, value);
403 else if (!me->overwrite && submodule->path)
404 warn_multiple_config(me->treeish_name, submodule->name,
408 cache_remove_path(me->cache, submodule);
409 free((void *) submodule->path);
410 submodule->path = xstrdup(value);
411 cache_put_path(me->cache, submodule);
413 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
414 /* when parsing worktree configurations we can die early */
415 int die_on_error = is_null_sha1(me->gitmodules_sha1);
416 if (!me->overwrite &&
417 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
418 warn_multiple_config(me->treeish_name, submodule->name,
419 "fetchrecursesubmodules");
421 submodule->fetch_recurse = parse_fetch_recurse(
424 } else if (!strcmp(item.buf, "ignore")) {
426 ret = config_error_nonbool(var);
427 else if (!me->overwrite && submodule->ignore)
428 warn_multiple_config(me->treeish_name, submodule->name,
430 else if (strcmp(value, "untracked") &&
431 strcmp(value, "dirty") &&
432 strcmp(value, "all") &&
433 strcmp(value, "none"))
434 warning("Invalid parameter '%s' for config option "
435 "'submodule.%s.ignore'", value, name.buf);
437 free((void *) submodule->ignore);
438 submodule->ignore = xstrdup(value);
440 } else if (!strcmp(item.buf, "url")) {
442 ret = config_error_nonbool(var);
443 } else if (looks_like_command_line_option(value)) {
444 warn_command_line_option(var, value);
445 } else if (!me->overwrite && submodule->url) {
446 warn_multiple_config(me->treeish_name, submodule->name,
449 free((void *) submodule->url);
450 submodule->url = xstrdup(value);
452 } else if (!strcmp(item.buf, "update")) {
454 ret = config_error_nonbool(var);
455 else if (!me->overwrite &&
456 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
457 warn_multiple_config(me->treeish_name, submodule->name,
459 else if (parse_submodule_update_strategy(value,
460 &submodule->update_strategy) < 0)
461 die(_("invalid value for %s"), var);
462 } else if (!strcmp(item.buf, "shallow")) {
463 if (!me->overwrite && submodule->recommend_shallow != -1)
464 warn_multiple_config(me->treeish_name, submodule->name,
467 submodule->recommend_shallow =
468 git_config_bool(var, value);
469 } else if (!strcmp(item.buf, "branch")) {
470 if (!me->overwrite && submodule->branch)
471 warn_multiple_config(me->treeish_name, submodule->name,
474 free((void *)submodule->branch);
475 submodule->branch = xstrdup(value);
479 strbuf_release(&name);
480 strbuf_release(&item);
485 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
486 unsigned char *gitmodules_sha1,
491 if (is_null_sha1(treeish_name)) {
492 hashclr(gitmodules_sha1);
496 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
497 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
503 /* This does a lookup of a submodule configuration by name or by path
504 * (key) with on-demand reading of the appropriate .gitmodules from
507 static const struct submodule *config_from(struct submodule_cache *cache,
508 const unsigned char *treeish_name, const char *key,
509 enum lookup_type lookup_type)
511 struct strbuf rev = STRBUF_INIT;
512 unsigned long config_size;
514 unsigned char sha1[20];
515 enum object_type type;
516 const struct submodule *submodule = NULL;
517 struct parse_config_parameter parameter;
520 * If any parameter except the cache is a NULL pointer just
521 * return the first submodule. Can be used to check whether
522 * there are any submodules parsed.
524 if (!treeish_name || !key) {
525 struct hashmap_iter iter;
526 struct submodule_entry *entry;
528 entry = hashmap_iter_first(&cache->for_name, &iter);
531 return entry->config;
534 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
537 switch (lookup_type) {
539 submodule = cache_lookup_name(cache, sha1, key);
542 submodule = cache_lookup_path(cache, sha1, key);
548 config = read_sha1_file(sha1, &type, &config_size);
549 if (!config || type != OBJ_BLOB)
552 /* fill the submodule config into the cache */
553 parameter.cache = cache;
554 parameter.treeish_name = treeish_name;
555 parameter.gitmodules_sha1 = sha1;
556 parameter.overwrite = 0;
557 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
558 config, config_size, ¶meter);
559 strbuf_release(&rev);
562 switch (lookup_type) {
564 return cache_lookup_name(cache, sha1, key);
566 return cache_lookup_path(cache, sha1, key);
572 strbuf_release(&rev);
577 static void submodule_cache_check_init(struct repository *repo)
579 if (repo->submodule_cache && repo->submodule_cache->initialized)
582 if (!repo->submodule_cache)
583 repo->submodule_cache = submodule_cache_alloc();
585 submodule_cache_init(repo->submodule_cache);
588 int submodule_config_option(struct repository *repo,
589 const char *var, const char *value)
591 struct parse_config_parameter parameter;
593 submodule_cache_check_init(repo);
595 parameter.cache = repo->submodule_cache;
596 parameter.treeish_name = NULL;
597 parameter.gitmodules_sha1 = null_sha1;
598 parameter.overwrite = 1;
600 return parse_config(var, value, ¶meter);
603 int parse_submodule_config_option(const char *var, const char *value)
605 return submodule_config_option(the_repository, var, value);
608 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
611 submodule_cache_check_init(the_repository);
612 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
615 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
618 submodule_cache_check_init(the_repository);
619 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
622 const struct submodule *submodule_from_cache(struct repository *repo,
623 const unsigned char *treeish_name,
626 submodule_cache_check_init(repo);
627 return config_from(repo->submodule_cache, treeish_name,
631 void submodule_free(void)
633 if (the_repository->submodule_cache)
634 submodule_cache_clear(the_repository->submodule_cache);