3 #include "repository.h"
5 #include "submodule-config.h"
8 #include "object-store.h"
9 #include "parse-options.h"
12 * submodule cache lookup structure
13 * There is one shared set of 'struct submodule' entries which can be
14 * looked up by their sha1 blob id of the .gitmodules file and either
15 * using path or name as key.
16 * for_path stores submodule entries with path as key
17 * for_name stores submodule entries with name as key
19 struct submodule_cache {
20 struct hashmap for_path;
21 struct hashmap for_name;
22 unsigned initialized:1;
23 unsigned gitmodules_read:1;
27 * thin wrapper struct needed to insert 'struct submodule' entries to
30 struct submodule_entry {
31 struct hashmap_entry ent;
32 struct submodule *config;
40 static int config_path_cmp(const void *unused_cmp_data,
42 const void *entry_or_key,
43 const void *unused_keydata)
45 const struct submodule_entry *a = entry;
46 const struct submodule_entry *b = entry_or_key;
48 return strcmp(a->config->path, b->config->path) ||
49 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
52 static int config_name_cmp(const void *unused_cmp_data,
54 const void *entry_or_key,
55 const void *unused_keydata)
57 const struct submodule_entry *a = entry;
58 const struct submodule_entry *b = entry_or_key;
60 return strcmp(a->config->name, b->config->name) ||
61 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
64 static struct submodule_cache *submodule_cache_alloc(void)
66 return xcalloc(1, sizeof(struct submodule_cache));
69 static void submodule_cache_init(struct submodule_cache *cache)
71 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
72 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
73 cache->initialized = 1;
76 static void free_one_config(struct submodule_entry *entry)
78 free((void *) entry->config->path);
79 free((void *) entry->config->name);
80 free((void *) entry->config->branch);
81 free((void *) entry->config->update_strategy.command);
85 static void submodule_cache_clear(struct submodule_cache *cache)
87 struct hashmap_iter iter;
88 struct submodule_entry *entry;
90 if (!cache->initialized)
94 * We iterate over the name hash here to be symmetric with the
95 * allocation of struct submodule entries. Each is allocated by
96 * their .gitmodules blob sha1 and submodule name.
98 hashmap_iter_init(&cache->for_name, &iter);
99 while ((entry = hashmap_iter_next(&iter)))
100 free_one_config(entry);
102 hashmap_free(&cache->for_path, 1);
103 hashmap_free(&cache->for_name, 1);
104 cache->initialized = 0;
105 cache->gitmodules_read = 0;
108 void submodule_cache_free(struct submodule_cache *cache)
110 submodule_cache_clear(cache);
114 static unsigned int hash_oid_string(const struct object_id *oid,
117 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
120 static void cache_put_path(struct submodule_cache *cache,
121 struct submodule *submodule)
123 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
125 struct submodule_entry *e = xmalloc(sizeof(*e));
126 hashmap_entry_init(e, hash);
127 e->config = submodule;
128 hashmap_put(&cache->for_path, e);
131 static void cache_remove_path(struct submodule_cache *cache,
132 struct submodule *submodule)
134 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
136 struct submodule_entry e;
137 struct submodule_entry *removed;
138 hashmap_entry_init(&e, hash);
139 e.config = submodule;
140 removed = hashmap_remove(&cache->for_path, &e, NULL);
144 static void cache_add(struct submodule_cache *cache,
145 struct submodule *submodule)
147 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
149 struct submodule_entry *e = xmalloc(sizeof(*e));
150 hashmap_entry_init(e, hash);
151 e->config = submodule;
152 hashmap_add(&cache->for_name, e);
155 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
156 const struct object_id *gitmodules_oid, const char *path)
158 struct submodule_entry *entry;
159 unsigned int hash = hash_oid_string(gitmodules_oid, path);
160 struct submodule_entry key;
161 struct submodule key_config;
163 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
164 key_config.path = path;
166 hashmap_entry_init(&key, hash);
167 key.config = &key_config;
169 entry = hashmap_get(&cache->for_path, &key, NULL);
171 return entry->config;
175 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
176 const struct object_id *gitmodules_oid, const char *name)
178 struct submodule_entry *entry;
179 unsigned int hash = hash_oid_string(gitmodules_oid, name);
180 struct submodule_entry key;
181 struct submodule key_config;
183 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
184 key_config.name = name;
186 hashmap_entry_init(&key, hash);
187 key.config = &key_config;
189 entry = hashmap_get(&cache->for_name, &key, NULL);
191 return entry->config;
195 int check_submodule_name(const char *name)
197 /* Disallow empty names */
202 * Look for '..' as a path component. Check both '/' and '\\' as
203 * separators rather than is_dir_sep(), because we want the name rules
204 * to be consistent across platforms.
206 goto in_component; /* always start inside component */
209 if (c == '/' || c == '\\') {
211 if (name[0] == '.' && name[1] == '.' &&
212 (!name[2] || name[2] == '/' || name[2] == '\\'))
220 static int name_and_item_from_var(const char *var, struct strbuf *name,
223 const char *subsection, *key;
224 int subsection_len, parse;
225 parse = parse_config_key(var, "submodule", &subsection,
226 &subsection_len, &key);
227 if (parse < 0 || !subsection)
230 strbuf_add(name, subsection, subsection_len);
231 if (check_submodule_name(name->buf) < 0) {
232 warning(_("ignoring suspicious submodule name: %s"), name->buf);
233 strbuf_release(name);
237 strbuf_addstr(item, key);
242 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
243 const struct object_id *gitmodules_oid, const char *name)
245 struct submodule *submodule;
246 struct strbuf name_buf = STRBUF_INIT;
248 submodule = cache_lookup_name(cache, gitmodules_oid, name);
252 submodule = xmalloc(sizeof(*submodule));
254 strbuf_addstr(&name_buf, name);
255 submodule->name = strbuf_detach(&name_buf, NULL);
257 submodule->path = NULL;
258 submodule->url = NULL;
259 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
260 submodule->update_strategy.command = NULL;
261 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
262 submodule->ignore = NULL;
263 submodule->branch = NULL;
264 submodule->recommend_shallow = -1;
266 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
268 cache_add(cache, submodule);
273 static int parse_fetch_recurse(const char *opt, const char *arg,
276 switch (git_parse_maybe_bool(arg)) {
278 return RECURSE_SUBMODULES_ON;
280 return RECURSE_SUBMODULES_OFF;
282 if (!strcmp(arg, "on-demand"))
283 return RECURSE_SUBMODULES_ON_DEMAND;
286 die("bad %s argument: %s", opt, arg);
288 return RECURSE_SUBMODULES_ERROR;
292 int parse_submodule_fetchjobs(const char *var, const char *value)
294 int fetchjobs = git_config_int(var, value);
296 die(_("negative values not allowed for submodule.fetchjobs"));
300 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
302 return parse_fetch_recurse(opt, arg, 1);
305 int option_fetch_parse_recurse_submodules(const struct option *opt,
306 const char *arg, int unset)
316 *v = RECURSE_SUBMODULES_OFF;
319 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
321 *v = RECURSE_SUBMODULES_ON;
326 static int parse_update_recurse(const char *opt, const char *arg,
329 switch (git_parse_maybe_bool(arg)) {
331 return RECURSE_SUBMODULES_ON;
333 return RECURSE_SUBMODULES_OFF;
336 die("bad %s argument: %s", opt, arg);
337 return RECURSE_SUBMODULES_ERROR;
341 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
343 return parse_update_recurse(opt, arg, 1);
346 static int parse_push_recurse(const char *opt, const char *arg,
349 switch (git_parse_maybe_bool(arg)) {
351 /* There's no simple "on" value when pushing */
353 die("bad %s argument: %s", opt, arg);
355 return RECURSE_SUBMODULES_ERROR;
357 return RECURSE_SUBMODULES_OFF;
359 if (!strcmp(arg, "on-demand"))
360 return RECURSE_SUBMODULES_ON_DEMAND;
361 else if (!strcmp(arg, "check"))
362 return RECURSE_SUBMODULES_CHECK;
363 else if (!strcmp(arg, "only"))
364 return RECURSE_SUBMODULES_ONLY;
365 else if (die_on_error)
366 die("bad %s argument: %s", opt, arg);
368 return RECURSE_SUBMODULES_ERROR;
372 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
374 return parse_push_recurse(opt, arg, 1);
377 static void warn_multiple_config(const struct object_id *treeish_name,
378 const char *name, const char *option)
380 const char *commit_string = "WORKTREE";
382 commit_string = oid_to_hex(treeish_name);
383 warning("%s:.gitmodules, multiple configurations found for "
384 "'submodule.%s.%s'. Skipping second one!",
385 commit_string, name, option);
388 static void warn_command_line_option(const char *var, const char *value)
390 warning(_("ignoring '%s' which may be interpreted as"
391 " a command-line option: %s"), var, value);
394 struct parse_config_parameter {
395 struct submodule_cache *cache;
396 const struct object_id *treeish_name;
397 const struct object_id *gitmodules_oid;
401 static int parse_config(const char *var, const char *value, void *data)
403 struct parse_config_parameter *me = data;
404 struct submodule *submodule;
405 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
408 /* this also ensures that we only parse submodule entries */
409 if (!name_and_item_from_var(var, &name, &item))
412 submodule = lookup_or_create_by_name(me->cache,
416 if (!strcmp(item.buf, "path")) {
418 ret = config_error_nonbool(var);
419 else if (looks_like_command_line_option(value))
420 warn_command_line_option(var, value);
421 else if (!me->overwrite && submodule->path)
422 warn_multiple_config(me->treeish_name, submodule->name,
426 cache_remove_path(me->cache, submodule);
427 free((void *) submodule->path);
428 submodule->path = xstrdup(value);
429 cache_put_path(me->cache, submodule);
431 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
432 /* when parsing worktree configurations we can die early */
433 int die_on_error = is_null_oid(me->gitmodules_oid);
434 if (!me->overwrite &&
435 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
436 warn_multiple_config(me->treeish_name, submodule->name,
437 "fetchrecursesubmodules");
439 submodule->fetch_recurse = parse_fetch_recurse(
442 } else if (!strcmp(item.buf, "ignore")) {
444 ret = config_error_nonbool(var);
445 else if (!me->overwrite && submodule->ignore)
446 warn_multiple_config(me->treeish_name, submodule->name,
448 else if (strcmp(value, "untracked") &&
449 strcmp(value, "dirty") &&
450 strcmp(value, "all") &&
451 strcmp(value, "none"))
452 warning("Invalid parameter '%s' for config option "
453 "'submodule.%s.ignore'", value, name.buf);
455 free((void *) submodule->ignore);
456 submodule->ignore = xstrdup(value);
458 } else if (!strcmp(item.buf, "url")) {
460 ret = config_error_nonbool(var);
461 } else if (looks_like_command_line_option(value)) {
462 warn_command_line_option(var, value);
463 } else if (!me->overwrite && submodule->url) {
464 warn_multiple_config(me->treeish_name, submodule->name,
467 free((void *) submodule->url);
468 submodule->url = xstrdup(value);
470 } else if (!strcmp(item.buf, "update")) {
472 ret = config_error_nonbool(var);
473 else if (!me->overwrite &&
474 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
475 warn_multiple_config(me->treeish_name, submodule->name,
477 else if (parse_submodule_update_strategy(value,
478 &submodule->update_strategy) < 0)
479 die(_("invalid value for %s"), var);
480 } else if (!strcmp(item.buf, "shallow")) {
481 if (!me->overwrite && submodule->recommend_shallow != -1)
482 warn_multiple_config(me->treeish_name, submodule->name,
485 submodule->recommend_shallow =
486 git_config_bool(var, value);
487 } else if (!strcmp(item.buf, "branch")) {
488 if (!me->overwrite && submodule->branch)
489 warn_multiple_config(me->treeish_name, submodule->name,
492 free((void *)submodule->branch);
493 submodule->branch = xstrdup(value);
497 strbuf_release(&name);
498 strbuf_release(&item);
503 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
504 struct object_id *gitmodules_oid,
509 if (is_null_oid(treeish_name)) {
510 oidclr(gitmodules_oid);
514 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
515 if (get_oid(rev->buf, gitmodules_oid) >= 0)
521 /* This does a lookup of a submodule configuration by name or by path
522 * (key) with on-demand reading of the appropriate .gitmodules from
525 static const struct submodule *config_from(struct submodule_cache *cache,
526 const struct object_id *treeish_name, const char *key,
527 enum lookup_type lookup_type)
529 struct strbuf rev = STRBUF_INIT;
530 unsigned long config_size;
532 struct object_id oid;
533 enum object_type type;
534 const struct submodule *submodule = NULL;
535 struct parse_config_parameter parameter;
538 * If any parameter except the cache is a NULL pointer just
539 * return the first submodule. Can be used to check whether
540 * there are any submodules parsed.
542 if (!treeish_name || !key) {
543 struct hashmap_iter iter;
544 struct submodule_entry *entry;
546 entry = hashmap_iter_first(&cache->for_name, &iter);
549 return entry->config;
552 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
555 switch (lookup_type) {
557 submodule = cache_lookup_name(cache, &oid, key);
560 submodule = cache_lookup_path(cache, &oid, key);
566 config = read_object_file(&oid, &type, &config_size);
567 if (!config || type != OBJ_BLOB)
570 /* fill the submodule config into the cache */
571 parameter.cache = cache;
572 parameter.treeish_name = treeish_name;
573 parameter.gitmodules_oid = &oid;
574 parameter.overwrite = 0;
575 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
576 config, config_size, ¶meter, NULL);
577 strbuf_release(&rev);
580 switch (lookup_type) {
582 return cache_lookup_name(cache, &oid, key);
584 return cache_lookup_path(cache, &oid, key);
590 strbuf_release(&rev);
595 static void submodule_cache_check_init(struct repository *repo)
597 if (repo->submodule_cache && repo->submodule_cache->initialized)
600 if (!repo->submodule_cache)
601 repo->submodule_cache = submodule_cache_alloc();
603 submodule_cache_init(repo->submodule_cache);
607 * Note: This function is private for a reason, the '.gitmodules' file should
608 * not be used as as a mechanism to retrieve arbitrary configuration stored in
611 * Runs the provided config function on the '.gitmodules' file found in the
614 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
616 if (repo->worktree) {
617 struct git_config_source config_source = { 0 };
618 const struct config_options opts = { 0 };
619 struct object_id oid;
622 file = repo_worktree_path(repo, GITMODULES_FILE);
623 if (file_exists(file)) {
624 config_source.file = file;
625 } else if (repo->submodule_prefix) {
627 * When get_oid and config_with_options, used below,
628 * become able to work on a specific repository, this
629 * warning branch can be removed.
631 warning("nested submodules without %s in the working tree are not supported yet",
634 } else if (get_oid(GITMODULES_INDEX, &oid) >= 0) {
635 config_source.blob = GITMODULES_INDEX;
636 } else if (get_oid(GITMODULES_HEAD, &oid) >= 0) {
637 config_source.blob = GITMODULES_HEAD;
642 config_with_options(fn, data, &config_source, &opts);
649 static int gitmodules_cb(const char *var, const char *value, void *data)
651 struct repository *repo = data;
652 struct parse_config_parameter parameter;
654 parameter.cache = repo->submodule_cache;
655 parameter.treeish_name = NULL;
656 parameter.gitmodules_oid = &null_oid;
657 parameter.overwrite = 1;
659 return parse_config(var, value, ¶meter);
662 void repo_read_gitmodules(struct repository *repo)
664 submodule_cache_check_init(repo);
666 if (repo_read_index(repo) < 0)
669 if (!is_gitmodules_unmerged(repo->index))
670 config_from_gitmodules(gitmodules_cb, repo, repo);
672 repo->submodule_cache->gitmodules_read = 1;
675 void gitmodules_config_oid(const struct object_id *commit_oid)
677 struct strbuf rev = STRBUF_INIT;
678 struct object_id oid;
680 submodule_cache_check_init(the_repository);
682 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
683 git_config_from_blob_oid(gitmodules_cb, rev.buf,
684 &oid, the_repository);
686 strbuf_release(&rev);
688 the_repository->submodule_cache->gitmodules_read = 1;
691 static void gitmodules_read_check(struct repository *repo)
693 submodule_cache_check_init(repo);
695 /* read the repo's .gitmodules file if it hasn't been already */
696 if (!repo->submodule_cache->gitmodules_read)
697 repo_read_gitmodules(repo);
700 const struct submodule *submodule_from_name(struct repository *r,
701 const struct object_id *treeish_name,
704 gitmodules_read_check(r);
705 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
708 const struct submodule *submodule_from_path(struct repository *r,
709 const struct object_id *treeish_name,
712 gitmodules_read_check(r);
713 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
716 void submodule_free(struct repository *r)
718 if (r->submodule_cache)
719 submodule_cache_clear(r->submodule_cache);
722 static int config_print_callback(const char *var, const char *value, void *cb_data)
724 char *wanted_key = cb_data;
726 if (!strcmp(wanted_key, var))
727 printf("%s\n", value);
732 int print_config_from_gitmodules(struct repository *repo, const char *key)
737 ret = git_config_parse_key(key, &store_key, NULL);
739 return CONFIG_INVALID_KEY;
741 config_from_gitmodules(config_print_callback, repo, store_key);
747 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
751 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
753 /* Maybe the user already did that, don't error out here */
754 warning(_("Could not update .gitmodules entry %s"), key);
759 struct fetch_config {
761 int *recurse_submodules;
764 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
766 struct fetch_config *config = cb;
767 if (!strcmp(var, "submodule.fetchjobs")) {
768 *(config->max_children) = parse_submodule_fetchjobs(var, value);
770 } else if (!strcmp(var, "fetch.recursesubmodules")) {
771 *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
778 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
780 struct fetch_config config = {
781 .max_children = max_children,
782 .recurse_submodules = recurse_submodules
784 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
787 static int gitmodules_update_clone_config(const char *var, const char *value,
791 if (!strcmp(var, "submodule.fetchjobs"))
792 *max_jobs = parse_submodule_fetchjobs(var, value);
796 void update_clone_config_from_gitmodules(int *max_jobs)
798 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);