Git 2.15.4
[alt-git.git] / submodule-config.c
blob464908df76f5a63768c6d38c8ce6f1cd191427c9
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "submodule-config.h"
5 #include "submodule.h"
6 #include "strbuf.h"
7 #include "parse-options.h"
9 /*
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;
21 unsigned gitmodules_read:1;
25 * thin wrapper struct needed to insert 'struct submodule' entries to
26 * the hashmap
28 struct submodule_entry {
29 struct hashmap_entry ent;
30 struct submodule *config;
33 enum lookup_type {
34 lookup_name,
35 lookup_path
38 static int config_path_cmp(const void *unused_cmp_data,
39 const void *entry,
40 const void *entry_or_key,
41 const void *unused_keydata)
43 const struct submodule_entry *a = entry;
44 const struct submodule_entry *b = entry_or_key;
46 return strcmp(a->config->path, b->config->path) ||
47 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
50 static int config_name_cmp(const void *unused_cmp_data,
51 const void *entry,
52 const void *entry_or_key,
53 const void *unused_keydata)
55 const struct submodule_entry *a = entry;
56 const struct submodule_entry *b = entry_or_key;
58 return strcmp(a->config->name, b->config->name) ||
59 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
62 static struct submodule_cache *submodule_cache_alloc(void)
64 return xcalloc(1, sizeof(struct submodule_cache));
67 static void submodule_cache_init(struct submodule_cache *cache)
69 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
70 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
71 cache->initialized = 1;
74 static void free_one_config(struct submodule_entry *entry)
76 free((void *) entry->config->path);
77 free((void *) entry->config->name);
78 free((void *) entry->config->branch);
79 free((void *) entry->config->update_strategy.command);
80 free(entry->config);
83 static void submodule_cache_clear(struct submodule_cache *cache)
85 struct hashmap_iter iter;
86 struct submodule_entry *entry;
88 if (!cache->initialized)
89 return;
92 * We iterate over the name hash here to be symmetric with the
93 * allocation of struct submodule entries. Each is allocated by
94 * their .gitmodule blob sha1 and submodule name.
96 hashmap_iter_init(&cache->for_name, &iter);
97 while ((entry = hashmap_iter_next(&iter)))
98 free_one_config(entry);
100 hashmap_free(&cache->for_path, 1);
101 hashmap_free(&cache->for_name, 1);
102 cache->initialized = 0;
103 cache->gitmodules_read = 0;
106 void submodule_cache_free(struct submodule_cache *cache)
108 submodule_cache_clear(cache);
109 free(cache);
112 static unsigned int hash_sha1_string(const unsigned char *sha1,
113 const char *string)
115 return memhash(sha1, 20) + strhash(string);
118 static void cache_put_path(struct submodule_cache *cache,
119 struct submodule *submodule)
121 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
122 submodule->path);
123 struct submodule_entry *e = xmalloc(sizeof(*e));
124 hashmap_entry_init(e, hash);
125 e->config = submodule;
126 hashmap_put(&cache->for_path, e);
129 static void cache_remove_path(struct submodule_cache *cache,
130 struct submodule *submodule)
132 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
133 submodule->path);
134 struct submodule_entry e;
135 struct submodule_entry *removed;
136 hashmap_entry_init(&e, hash);
137 e.config = submodule;
138 removed = hashmap_remove(&cache->for_path, &e, NULL);
139 free(removed);
142 static void cache_add(struct submodule_cache *cache,
143 struct submodule *submodule)
145 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
146 submodule->name);
147 struct submodule_entry *e = xmalloc(sizeof(*e));
148 hashmap_entry_init(e, hash);
149 e->config = submodule;
150 hashmap_add(&cache->for_name, e);
153 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
154 const unsigned char *gitmodules_sha1, const char *path)
156 struct submodule_entry *entry;
157 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
158 struct submodule_entry key;
159 struct submodule key_config;
161 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
162 key_config.path = path;
164 hashmap_entry_init(&key, hash);
165 key.config = &key_config;
167 entry = hashmap_get(&cache->for_path, &key, NULL);
168 if (entry)
169 return entry->config;
170 return NULL;
173 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
174 const unsigned char *gitmodules_sha1, const char *name)
176 struct submodule_entry *entry;
177 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
178 struct submodule_entry key;
179 struct submodule key_config;
181 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
182 key_config.name = name;
184 hashmap_entry_init(&key, hash);
185 key.config = &key_config;
187 entry = hashmap_get(&cache->for_name, &key, NULL);
188 if (entry)
189 return entry->config;
190 return NULL;
193 int check_submodule_name(const char *name)
195 /* Disallow empty names */
196 if (!*name)
197 return -1;
200 * Look for '..' as a path component. Check both '/' and '\\' as
201 * separators rather than is_dir_sep(), because we want the name rules
202 * to be consistent across platforms.
204 goto in_component; /* always start inside component */
205 while (*name) {
206 char c = *name++;
207 if (c == '/' || c == '\\') {
208 in_component:
209 if (name[0] == '.' && name[1] == '.' &&
210 (!name[2] || name[2] == '/' || name[2] == '\\'))
211 return -1;
215 return 0;
218 static int name_and_item_from_var(const char *var, struct strbuf *name,
219 struct strbuf *item)
221 const char *subsection, *key;
222 int subsection_len, parse;
223 parse = parse_config_key(var, "submodule", &subsection,
224 &subsection_len, &key);
225 if (parse < 0 || !subsection)
226 return 0;
228 strbuf_add(name, subsection, subsection_len);
229 if (check_submodule_name(name->buf) < 0) {
230 warning(_("ignoring suspicious submodule name: %s"), name->buf);
231 strbuf_release(name);
232 return 0;
235 strbuf_addstr(item, key);
237 return 1;
240 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
241 const unsigned char *gitmodules_sha1, const char *name)
243 struct submodule *submodule;
244 struct strbuf name_buf = STRBUF_INIT;
246 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
247 if (submodule)
248 return submodule;
250 submodule = xmalloc(sizeof(*submodule));
252 strbuf_addstr(&name_buf, name);
253 submodule->name = strbuf_detach(&name_buf, NULL);
255 submodule->path = NULL;
256 submodule->url = NULL;
257 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
258 submodule->update_strategy.command = NULL;
259 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
260 submodule->ignore = NULL;
261 submodule->branch = NULL;
262 submodule->recommend_shallow = -1;
264 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
266 cache_add(cache, submodule);
268 return submodule;
271 static int parse_fetch_recurse(const char *opt, const char *arg,
272 int die_on_error)
274 switch (git_parse_maybe_bool(arg)) {
275 case 1:
276 return RECURSE_SUBMODULES_ON;
277 case 0:
278 return RECURSE_SUBMODULES_OFF;
279 default:
280 if (!strcmp(arg, "on-demand"))
281 return RECURSE_SUBMODULES_ON_DEMAND;
283 if (die_on_error)
284 die("bad %s argument: %s", opt, arg);
285 else
286 return RECURSE_SUBMODULES_ERROR;
290 int parse_submodule_fetchjobs(const char *var, const char *value)
292 int fetchjobs = git_config_int(var, value);
293 if (fetchjobs < 0)
294 die(_("negative values not allowed for submodule.fetchjobs"));
295 return fetchjobs;
298 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
300 return parse_fetch_recurse(opt, arg, 1);
303 int option_fetch_parse_recurse_submodules(const struct option *opt,
304 const char *arg, int unset)
306 int *v;
308 if (!opt->value)
309 return -1;
311 v = opt->value;
313 if (unset) {
314 *v = RECURSE_SUBMODULES_OFF;
315 } else {
316 if (arg)
317 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
318 else
319 *v = RECURSE_SUBMODULES_ON;
321 return 0;
324 static int parse_update_recurse(const char *opt, const char *arg,
325 int die_on_error)
327 switch (git_parse_maybe_bool(arg)) {
328 case 1:
329 return RECURSE_SUBMODULES_ON;
330 case 0:
331 return RECURSE_SUBMODULES_OFF;
332 default:
333 if (die_on_error)
334 die("bad %s argument: %s", opt, arg);
335 return RECURSE_SUBMODULES_ERROR;
339 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
341 return parse_update_recurse(opt, arg, 1);
344 static int parse_push_recurse(const char *opt, const char *arg,
345 int die_on_error)
347 switch (git_parse_maybe_bool(arg)) {
348 case 1:
349 /* There's no simple "on" value when pushing */
350 if (die_on_error)
351 die("bad %s argument: %s", opt, arg);
352 else
353 return RECURSE_SUBMODULES_ERROR;
354 case 0:
355 return RECURSE_SUBMODULES_OFF;
356 default:
357 if (!strcmp(arg, "on-demand"))
358 return RECURSE_SUBMODULES_ON_DEMAND;
359 else if (!strcmp(arg, "check"))
360 return RECURSE_SUBMODULES_CHECK;
361 else if (!strcmp(arg, "only"))
362 return RECURSE_SUBMODULES_ONLY;
363 else if (die_on_error)
364 die("bad %s argument: %s", opt, arg);
365 else
366 return RECURSE_SUBMODULES_ERROR;
370 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
372 return parse_push_recurse(opt, arg, 1);
375 static void warn_multiple_config(const unsigned char *treeish_name,
376 const char *name, const char *option)
378 const char *commit_string = "WORKTREE";
379 if (treeish_name)
380 commit_string = sha1_to_hex(treeish_name);
381 warning("%s:.gitmodules, multiple configurations found for "
382 "'submodule.%s.%s'. Skipping second one!",
383 commit_string, name, option);
386 static void warn_command_line_option(const char *var, const char *value)
388 warning(_("ignoring '%s' which may be interpreted as"
389 " a command-line option: %s"), var, value);
392 struct parse_config_parameter {
393 struct submodule_cache *cache;
394 const unsigned char *treeish_name;
395 const unsigned char *gitmodules_sha1;
396 int overwrite;
400 * Parse a config item from .gitmodules.
402 * This does not handle submodule-related configuration from the main
403 * config store (.git/config, etc). Callers are responsible for
404 * checking for overrides in the main config store when appropriate.
406 static int parse_config(const char *var, const char *value, void *data)
408 struct parse_config_parameter *me = data;
409 struct submodule *submodule;
410 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
411 int ret = 0;
413 /* this also ensures that we only parse submodule entries */
414 if (!name_and_item_from_var(var, &name, &item))
415 return 0;
417 submodule = lookup_or_create_by_name(me->cache,
418 me->gitmodules_sha1,
419 name.buf);
421 if (!strcmp(item.buf, "path")) {
422 if (!value)
423 ret = config_error_nonbool(var);
424 else if (looks_like_command_line_option(value))
425 warn_command_line_option(var, value);
426 else if (!me->overwrite && submodule->path)
427 warn_multiple_config(me->treeish_name, submodule->name,
428 "path");
429 else {
430 if (submodule->path)
431 cache_remove_path(me->cache, submodule);
432 free((void *) submodule->path);
433 submodule->path = xstrdup(value);
434 cache_put_path(me->cache, submodule);
436 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
437 /* when parsing worktree configurations we can die early */
438 int die_on_error = is_null_sha1(me->gitmodules_sha1);
439 if (!me->overwrite &&
440 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
441 warn_multiple_config(me->treeish_name, submodule->name,
442 "fetchrecursesubmodules");
443 else
444 submodule->fetch_recurse = parse_fetch_recurse(
445 var, value,
446 die_on_error);
447 } else if (!strcmp(item.buf, "ignore")) {
448 if (!value)
449 ret = config_error_nonbool(var);
450 else if (!me->overwrite && submodule->ignore)
451 warn_multiple_config(me->treeish_name, submodule->name,
452 "ignore");
453 else if (strcmp(value, "untracked") &&
454 strcmp(value, "dirty") &&
455 strcmp(value, "all") &&
456 strcmp(value, "none"))
457 warning("Invalid parameter '%s' for config option "
458 "'submodule.%s.ignore'", value, name.buf);
459 else {
460 free((void *) submodule->ignore);
461 submodule->ignore = xstrdup(value);
463 } else if (!strcmp(item.buf, "url")) {
464 if (!value) {
465 ret = config_error_nonbool(var);
466 } else if (looks_like_command_line_option(value)) {
467 warn_command_line_option(var, value);
468 } else if (!me->overwrite && submodule->url) {
469 warn_multiple_config(me->treeish_name, submodule->name,
470 "url");
471 } else {
472 free((void *) submodule->url);
473 submodule->url = xstrdup(value);
475 } else if (!strcmp(item.buf, "update")) {
476 if (!value)
477 ret = config_error_nonbool(var);
478 else if (!me->overwrite &&
479 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
480 warn_multiple_config(me->treeish_name, submodule->name,
481 "update");
482 else if (parse_submodule_update_strategy(value,
483 &submodule->update_strategy) < 0 ||
484 submodule->update_strategy.type == SM_UPDATE_COMMAND)
485 die(_("invalid value for %s"), var);
486 } else if (!strcmp(item.buf, "shallow")) {
487 if (!me->overwrite && submodule->recommend_shallow != -1)
488 warn_multiple_config(me->treeish_name, submodule->name,
489 "shallow");
490 else
491 submodule->recommend_shallow =
492 git_config_bool(var, value);
493 } else if (!strcmp(item.buf, "branch")) {
494 if (!me->overwrite && submodule->branch)
495 warn_multiple_config(me->treeish_name, submodule->name,
496 "branch");
497 else {
498 free((void *)submodule->branch);
499 submodule->branch = xstrdup(value);
503 strbuf_release(&name);
504 strbuf_release(&item);
506 return ret;
509 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
510 struct object_id *gitmodules_oid,
511 struct strbuf *rev)
513 int ret = 0;
515 if (is_null_oid(treeish_name)) {
516 oidclr(gitmodules_oid);
517 return 1;
520 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
521 if (get_oid(rev->buf, gitmodules_oid) >= 0)
522 ret = 1;
524 return ret;
527 /* This does a lookup of a submodule configuration by name or by path
528 * (key) with on-demand reading of the appropriate .gitmodules from
529 * revisions.
531 static const struct submodule *config_from(struct submodule_cache *cache,
532 const struct object_id *treeish_name, const char *key,
533 enum lookup_type lookup_type)
535 struct strbuf rev = STRBUF_INIT;
536 unsigned long config_size;
537 char *config = NULL;
538 struct object_id oid;
539 enum object_type type;
540 const struct submodule *submodule = NULL;
541 struct parse_config_parameter parameter;
544 * If any parameter except the cache is a NULL pointer just
545 * return the first submodule. Can be used to check whether
546 * there are any submodules parsed.
548 if (!treeish_name || !key) {
549 struct hashmap_iter iter;
550 struct submodule_entry *entry;
552 entry = hashmap_iter_first(&cache->for_name, &iter);
553 if (!entry)
554 return NULL;
555 return entry->config;
558 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
559 goto out;
561 switch (lookup_type) {
562 case lookup_name:
563 submodule = cache_lookup_name(cache, oid.hash, key);
564 break;
565 case lookup_path:
566 submodule = cache_lookup_path(cache, oid.hash, key);
567 break;
569 if (submodule)
570 goto out;
572 config = read_sha1_file(oid.hash, &type, &config_size);
573 if (!config || type != OBJ_BLOB)
574 goto out;
576 /* fill the submodule config into the cache */
577 parameter.cache = cache;
578 parameter.treeish_name = treeish_name->hash;
579 parameter.gitmodules_sha1 = oid.hash;
580 parameter.overwrite = 0;
581 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
582 config, config_size, &parameter);
583 strbuf_release(&rev);
584 free(config);
586 switch (lookup_type) {
587 case lookup_name:
588 return cache_lookup_name(cache, oid.hash, key);
589 case lookup_path:
590 return cache_lookup_path(cache, oid.hash, key);
591 default:
592 return NULL;
595 out:
596 strbuf_release(&rev);
597 free(config);
598 return submodule;
601 static void submodule_cache_check_init(struct repository *repo)
603 if (repo->submodule_cache && repo->submodule_cache->initialized)
604 return;
606 if (!repo->submodule_cache)
607 repo->submodule_cache = submodule_cache_alloc();
609 submodule_cache_init(repo->submodule_cache);
612 static int gitmodules_cb(const char *var, const char *value, void *data)
614 struct repository *repo = data;
615 struct parse_config_parameter parameter;
617 parameter.cache = repo->submodule_cache;
618 parameter.treeish_name = NULL;
619 parameter.gitmodules_sha1 = null_sha1;
620 parameter.overwrite = 1;
622 return parse_config(var, value, &parameter);
625 void repo_read_gitmodules(struct repository *repo)
627 submodule_cache_check_init(repo);
629 if (repo->worktree) {
630 char *gitmodules;
632 if (repo_read_index(repo) < 0)
633 return;
635 gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
637 if (!is_gitmodules_unmerged(repo->index))
638 git_config_from_file(gitmodules_cb, gitmodules, repo);
640 free(gitmodules);
643 repo->submodule_cache->gitmodules_read = 1;
646 void gitmodules_config_oid(const struct object_id *commit_oid)
648 struct strbuf rev = STRBUF_INIT;
649 struct object_id oid;
651 submodule_cache_check_init(the_repository);
653 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
654 git_config_from_blob_oid(gitmodules_cb, rev.buf,
655 &oid, the_repository);
657 strbuf_release(&rev);
659 the_repository->submodule_cache->gitmodules_read = 1;
662 static void gitmodules_read_check(struct repository *repo)
664 submodule_cache_check_init(repo);
666 /* read the repo's .gitmodules file if it hasn't been already */
667 if (!repo->submodule_cache->gitmodules_read)
668 repo_read_gitmodules(repo);
671 const struct submodule *submodule_from_name(const struct object_id *treeish_name,
672 const char *name)
674 gitmodules_read_check(the_repository);
675 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
678 const struct submodule *submodule_from_path(const struct object_id *treeish_name,
679 const char *path)
681 gitmodules_read_check(the_repository);
682 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
685 const struct submodule *submodule_from_cache(struct repository *repo,
686 const struct object_id *treeish_name,
687 const char *key)
689 gitmodules_read_check(repo);
690 return config_from(repo->submodule_cache, treeish_name,
691 key, lookup_path);
694 void submodule_free(void)
696 if (the_repository->submodule_cache)
697 submodule_cache_clear(the_repository->submodule_cache);