fast-import: replace custom hash with hashmap.c
[git/raj.git] / submodule-config.c
blob4d1c92d5826da800360a98e88d577f6f25d27f96
1 #include "cache.h"
2 #include "dir.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "submodule-config.h"
6 #include "submodule.h"
7 #include "strbuf.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
28 * the hashmap
30 struct submodule_entry {
31 struct hashmap_entry ent;
32 struct submodule *config;
35 enum lookup_type {
36 lookup_name,
37 lookup_path
40 static int config_path_cmp(const void *unused_cmp_data,
41 const struct hashmap_entry *eptr,
42 const struct hashmap_entry *entry_or_key,
43 const void *unused_keydata)
45 const struct submodule_entry *a, *b;
47 a = container_of(eptr, const struct submodule_entry, ent);
48 b = container_of(entry_or_key, const struct submodule_entry, ent);
50 return strcmp(a->config->path, b->config->path) ||
51 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
54 static int config_name_cmp(const void *unused_cmp_data,
55 const struct hashmap_entry *eptr,
56 const struct hashmap_entry *entry_or_key,
57 const void *unused_keydata)
59 const struct submodule_entry *a, *b;
61 a = container_of(eptr, const struct submodule_entry, ent);
62 b = container_of(entry_or_key, const struct submodule_entry, ent);
64 return strcmp(a->config->name, b->config->name) ||
65 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
68 static struct submodule_cache *submodule_cache_alloc(void)
70 return xcalloc(1, sizeof(struct submodule_cache));
73 static void submodule_cache_init(struct submodule_cache *cache)
75 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
76 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
77 cache->initialized = 1;
80 static void free_one_config(struct submodule_entry *entry)
82 free((void *) entry->config->path);
83 free((void *) entry->config->name);
84 free((void *) entry->config->branch);
85 free((void *) entry->config->update_strategy.command);
86 free(entry->config);
89 static void submodule_cache_clear(struct submodule_cache *cache)
91 struct hashmap_iter iter;
92 struct submodule_entry *entry;
94 if (!cache->initialized)
95 return;
98 * We iterate over the name hash here to be symmetric with the
99 * allocation of struct submodule entries. Each is allocated by
100 * their .gitmodules blob sha1 and submodule name.
102 hashmap_for_each_entry(&cache->for_name, &iter, entry,
103 ent /* member name */)
104 free_one_config(entry);
106 hashmap_free_entries(&cache->for_path, struct submodule_entry, ent);
107 hashmap_free_entries(&cache->for_name, struct submodule_entry, ent);
108 cache->initialized = 0;
109 cache->gitmodules_read = 0;
112 void submodule_cache_free(struct submodule_cache *cache)
114 submodule_cache_clear(cache);
115 free(cache);
118 static unsigned int hash_oid_string(const struct object_id *oid,
119 const char *string)
121 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
124 static void cache_put_path(struct submodule_cache *cache,
125 struct submodule *submodule)
127 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
128 submodule->path);
129 struct submodule_entry *e = xmalloc(sizeof(*e));
130 hashmap_entry_init(&e->ent, hash);
131 e->config = submodule;
132 hashmap_put(&cache->for_path, &e->ent);
135 static void cache_remove_path(struct submodule_cache *cache,
136 struct submodule *submodule)
138 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
139 submodule->path);
140 struct submodule_entry e;
141 struct submodule_entry *removed;
142 hashmap_entry_init(&e.ent, hash);
143 e.config = submodule;
144 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
145 free(removed);
148 static void cache_add(struct submodule_cache *cache,
149 struct submodule *submodule)
151 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
152 submodule->name);
153 struct submodule_entry *e = xmalloc(sizeof(*e));
154 hashmap_entry_init(&e->ent, hash);
155 e->config = submodule;
156 hashmap_add(&cache->for_name, &e->ent);
159 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
160 const struct object_id *gitmodules_oid, const char *path)
162 struct submodule_entry *entry;
163 unsigned int hash = hash_oid_string(gitmodules_oid, path);
164 struct submodule_entry key;
165 struct submodule key_config;
167 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
168 key_config.path = path;
170 hashmap_entry_init(&key.ent, hash);
171 key.config = &key_config;
173 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
174 if (entry)
175 return entry->config;
176 return NULL;
179 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
180 const struct object_id *gitmodules_oid, const char *name)
182 struct submodule_entry *entry;
183 unsigned int hash = hash_oid_string(gitmodules_oid, name);
184 struct submodule_entry key;
185 struct submodule key_config;
187 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
188 key_config.name = name;
190 hashmap_entry_init(&key.ent, hash);
191 key.config = &key_config;
193 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
194 if (entry)
195 return entry->config;
196 return NULL;
199 int check_submodule_name(const char *name)
201 /* Disallow empty names */
202 if (!*name)
203 return -1;
206 * Look for '..' as a path component. Check both '/' and '\\' as
207 * separators rather than is_dir_sep(), because we want the name rules
208 * to be consistent across platforms.
210 goto in_component; /* always start inside component */
211 while (*name) {
212 char c = *name++;
213 if (c == '/' || c == '\\') {
214 in_component:
215 if (name[0] == '.' && name[1] == '.' &&
216 (!name[2] || name[2] == '/' || name[2] == '\\'))
217 return -1;
221 return 0;
224 static int name_and_item_from_var(const char *var, struct strbuf *name,
225 struct strbuf *item)
227 const char *subsection, *key;
228 int subsection_len, parse;
229 parse = parse_config_key(var, "submodule", &subsection,
230 &subsection_len, &key);
231 if (parse < 0 || !subsection)
232 return 0;
234 strbuf_add(name, subsection, subsection_len);
235 if (check_submodule_name(name->buf) < 0) {
236 warning(_("ignoring suspicious submodule name: %s"), name->buf);
237 strbuf_release(name);
238 return 0;
241 strbuf_addstr(item, key);
243 return 1;
246 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
247 const struct object_id *gitmodules_oid, const char *name)
249 struct submodule *submodule;
250 struct strbuf name_buf = STRBUF_INIT;
252 submodule = cache_lookup_name(cache, gitmodules_oid, name);
253 if (submodule)
254 return submodule;
256 submodule = xmalloc(sizeof(*submodule));
258 strbuf_addstr(&name_buf, name);
259 submodule->name = strbuf_detach(&name_buf, NULL);
261 submodule->path = NULL;
262 submodule->url = NULL;
263 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
264 submodule->update_strategy.command = NULL;
265 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
266 submodule->ignore = NULL;
267 submodule->branch = NULL;
268 submodule->recommend_shallow = -1;
270 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
272 cache_add(cache, submodule);
274 return submodule;
277 static int parse_fetch_recurse(const char *opt, const char *arg,
278 int die_on_error)
280 switch (git_parse_maybe_bool(arg)) {
281 case 1:
282 return RECURSE_SUBMODULES_ON;
283 case 0:
284 return RECURSE_SUBMODULES_OFF;
285 default:
286 if (!strcmp(arg, "on-demand"))
287 return RECURSE_SUBMODULES_ON_DEMAND;
289 * Please update $__git_fetch_recurse_submodules in
290 * git-completion.bash when you add new options.
292 if (die_on_error)
293 die("bad %s argument: %s", opt, arg);
294 else
295 return RECURSE_SUBMODULES_ERROR;
299 int parse_submodule_fetchjobs(const char *var, const char *value)
301 int fetchjobs = git_config_int(var, value);
302 if (fetchjobs < 0)
303 die(_("negative values not allowed for submodule.fetchjobs"));
304 return fetchjobs;
307 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
309 return parse_fetch_recurse(opt, arg, 1);
312 int option_fetch_parse_recurse_submodules(const struct option *opt,
313 const char *arg, int unset)
315 int *v;
317 if (!opt->value)
318 return -1;
320 v = opt->value;
322 if (unset) {
323 *v = RECURSE_SUBMODULES_OFF;
324 } else {
325 if (arg)
326 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
327 else
328 *v = RECURSE_SUBMODULES_ON;
330 return 0;
333 static int parse_update_recurse(const char *opt, const char *arg,
334 int die_on_error)
336 switch (git_parse_maybe_bool(arg)) {
337 case 1:
338 return RECURSE_SUBMODULES_ON;
339 case 0:
340 return RECURSE_SUBMODULES_OFF;
341 default:
342 if (die_on_error)
343 die("bad %s argument: %s", opt, arg);
344 return RECURSE_SUBMODULES_ERROR;
348 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
350 return parse_update_recurse(opt, arg, 1);
353 static int parse_push_recurse(const char *opt, const char *arg,
354 int die_on_error)
356 switch (git_parse_maybe_bool(arg)) {
357 case 1:
358 /* There's no simple "on" value when pushing */
359 if (die_on_error)
360 die("bad %s argument: %s", opt, arg);
361 else
362 return RECURSE_SUBMODULES_ERROR;
363 case 0:
364 return RECURSE_SUBMODULES_OFF;
365 default:
366 if (!strcmp(arg, "on-demand"))
367 return RECURSE_SUBMODULES_ON_DEMAND;
368 else if (!strcmp(arg, "check"))
369 return RECURSE_SUBMODULES_CHECK;
370 else if (!strcmp(arg, "only"))
371 return RECURSE_SUBMODULES_ONLY;
373 * Please update $__git_push_recurse_submodules in
374 * git-completion.bash when you add new modes.
376 else if (die_on_error)
377 die("bad %s argument: %s", opt, arg);
378 else
379 return RECURSE_SUBMODULES_ERROR;
383 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
385 return parse_push_recurse(opt, arg, 1);
388 static void warn_multiple_config(const struct object_id *treeish_name,
389 const char *name, const char *option)
391 const char *commit_string = "WORKTREE";
392 if (treeish_name)
393 commit_string = oid_to_hex(treeish_name);
394 warning("%s:.gitmodules, multiple configurations found for "
395 "'submodule.%s.%s'. Skipping second one!",
396 commit_string, name, option);
399 static void warn_command_line_option(const char *var, const char *value)
401 warning(_("ignoring '%s' which may be interpreted as"
402 " a command-line option: %s"), var, value);
405 struct parse_config_parameter {
406 struct submodule_cache *cache;
407 const struct object_id *treeish_name;
408 const struct object_id *gitmodules_oid;
409 int overwrite;
413 * Parse a config item from .gitmodules.
415 * This does not handle submodule-related configuration from the main
416 * config store (.git/config, etc). Callers are responsible for
417 * checking for overrides in the main config store when appropriate.
419 static int parse_config(const char *var, const char *value, void *data)
421 struct parse_config_parameter *me = data;
422 struct submodule *submodule;
423 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
424 int ret = 0;
426 /* this also ensures that we only parse submodule entries */
427 if (!name_and_item_from_var(var, &name, &item))
428 return 0;
430 submodule = lookup_or_create_by_name(me->cache,
431 me->gitmodules_oid,
432 name.buf);
434 if (!strcmp(item.buf, "path")) {
435 if (!value)
436 ret = config_error_nonbool(var);
437 else if (looks_like_command_line_option(value))
438 warn_command_line_option(var, value);
439 else if (!me->overwrite && submodule->path)
440 warn_multiple_config(me->treeish_name, submodule->name,
441 "path");
442 else {
443 if (submodule->path)
444 cache_remove_path(me->cache, submodule);
445 free((void *) submodule->path);
446 submodule->path = xstrdup(value);
447 cache_put_path(me->cache, submodule);
449 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
450 /* when parsing worktree configurations we can die early */
451 int die_on_error = is_null_oid(me->gitmodules_oid);
452 if (!me->overwrite &&
453 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
454 warn_multiple_config(me->treeish_name, submodule->name,
455 "fetchrecursesubmodules");
456 else
457 submodule->fetch_recurse = parse_fetch_recurse(
458 var, value,
459 die_on_error);
460 } else if (!strcmp(item.buf, "ignore")) {
461 if (!value)
462 ret = config_error_nonbool(var);
463 else if (!me->overwrite && submodule->ignore)
464 warn_multiple_config(me->treeish_name, submodule->name,
465 "ignore");
466 else if (strcmp(value, "untracked") &&
467 strcmp(value, "dirty") &&
468 strcmp(value, "all") &&
469 strcmp(value, "none"))
470 warning("Invalid parameter '%s' for config option "
471 "'submodule.%s.ignore'", value, name.buf);
472 else {
473 free((void *) submodule->ignore);
474 submodule->ignore = xstrdup(value);
476 } else if (!strcmp(item.buf, "url")) {
477 if (!value) {
478 ret = config_error_nonbool(var);
479 } else if (looks_like_command_line_option(value)) {
480 warn_command_line_option(var, value);
481 } else if (!me->overwrite && submodule->url) {
482 warn_multiple_config(me->treeish_name, submodule->name,
483 "url");
484 } else {
485 free((void *) submodule->url);
486 submodule->url = xstrdup(value);
488 } else if (!strcmp(item.buf, "update")) {
489 if (!value)
490 ret = config_error_nonbool(var);
491 else if (!me->overwrite &&
492 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
493 warn_multiple_config(me->treeish_name, submodule->name,
494 "update");
495 else if (parse_submodule_update_strategy(value,
496 &submodule->update_strategy) < 0 ||
497 submodule->update_strategy.type == SM_UPDATE_COMMAND)
498 die(_("invalid value for %s"), var);
499 } else if (!strcmp(item.buf, "shallow")) {
500 if (!me->overwrite && submodule->recommend_shallow != -1)
501 warn_multiple_config(me->treeish_name, submodule->name,
502 "shallow");
503 else
504 submodule->recommend_shallow =
505 git_config_bool(var, value);
506 } else if (!strcmp(item.buf, "branch")) {
507 if (!me->overwrite && submodule->branch)
508 warn_multiple_config(me->treeish_name, submodule->name,
509 "branch");
510 else {
511 free((void *)submodule->branch);
512 submodule->branch = xstrdup(value);
516 strbuf_release(&name);
517 strbuf_release(&item);
519 return ret;
522 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
523 struct object_id *gitmodules_oid,
524 struct strbuf *rev)
526 int ret = 0;
528 if (is_null_oid(treeish_name)) {
529 oidclr(gitmodules_oid);
530 return 1;
533 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
534 if (get_oid(rev->buf, gitmodules_oid) >= 0)
535 ret = 1;
537 return ret;
540 /* This does a lookup of a submodule configuration by name or by path
541 * (key) with on-demand reading of the appropriate .gitmodules from
542 * revisions.
544 static const struct submodule *config_from(struct submodule_cache *cache,
545 const struct object_id *treeish_name, const char *key,
546 enum lookup_type lookup_type)
548 struct strbuf rev = STRBUF_INIT;
549 unsigned long config_size;
550 char *config = NULL;
551 struct object_id oid;
552 enum object_type type;
553 const struct submodule *submodule = NULL;
554 struct parse_config_parameter parameter;
557 * If any parameter except the cache is a NULL pointer just
558 * return the first submodule. Can be used to check whether
559 * there are any submodules parsed.
561 if (!treeish_name || !key) {
562 struct hashmap_iter iter;
563 struct submodule_entry *entry;
565 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
566 struct submodule_entry,
567 ent /* member name */);
568 if (!entry)
569 return NULL;
570 return entry->config;
573 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
574 goto out;
576 switch (lookup_type) {
577 case lookup_name:
578 submodule = cache_lookup_name(cache, &oid, key);
579 break;
580 case lookup_path:
581 submodule = cache_lookup_path(cache, &oid, key);
582 break;
584 if (submodule)
585 goto out;
587 config = read_object_file(&oid, &type, &config_size);
588 if (!config || type != OBJ_BLOB)
589 goto out;
591 /* fill the submodule config into the cache */
592 parameter.cache = cache;
593 parameter.treeish_name = treeish_name;
594 parameter.gitmodules_oid = &oid;
595 parameter.overwrite = 0;
596 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
597 config, config_size, &parameter, NULL);
598 strbuf_release(&rev);
599 free(config);
601 switch (lookup_type) {
602 case lookup_name:
603 return cache_lookup_name(cache, &oid, key);
604 case lookup_path:
605 return cache_lookup_path(cache, &oid, key);
606 default:
607 return NULL;
610 out:
611 strbuf_release(&rev);
612 free(config);
613 return submodule;
616 static void submodule_cache_check_init(struct repository *repo)
618 if (repo->submodule_cache && repo->submodule_cache->initialized)
619 return;
621 if (!repo->submodule_cache)
622 repo->submodule_cache = submodule_cache_alloc();
624 submodule_cache_init(repo->submodule_cache);
628 * Note: This function is private for a reason, the '.gitmodules' file should
629 * not be used as a mechanism to retrieve arbitrary configuration stored in
630 * the repository.
632 * Runs the provided config function on the '.gitmodules' file found in the
633 * working directory.
635 static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
637 if (repo->worktree) {
638 struct git_config_source config_source = {
639 0, .scope = CONFIG_SCOPE_SUBMODULE
641 const struct config_options opts = { 0 };
642 struct object_id oid;
643 char *file;
644 char *oidstr = NULL;
646 file = repo_worktree_path(repo, GITMODULES_FILE);
647 if (file_exists(file)) {
648 config_source.file = file;
649 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
650 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
651 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
652 if (repo != the_repository)
653 add_to_alternates_memory(repo->objects->odb->path);
654 } else {
655 goto out;
658 config_with_options(fn, data, &config_source, &opts);
660 out:
661 free(oidstr);
662 free(file);
666 static int gitmodules_cb(const char *var, const char *value, void *data)
668 struct repository *repo = data;
669 struct parse_config_parameter parameter;
671 parameter.cache = repo->submodule_cache;
672 parameter.treeish_name = NULL;
673 parameter.gitmodules_oid = &null_oid;
674 parameter.overwrite = 1;
676 return parse_config(var, value, &parameter);
679 void repo_read_gitmodules(struct repository *repo, int skip_if_read)
681 submodule_cache_check_init(repo);
683 if (repo->submodule_cache->gitmodules_read && skip_if_read)
684 return;
686 if (repo_read_index(repo) < 0)
687 return;
689 if (!is_gitmodules_unmerged(repo->index))
690 config_from_gitmodules(gitmodules_cb, repo, repo);
692 repo->submodule_cache->gitmodules_read = 1;
695 void gitmodules_config_oid(const struct object_id *commit_oid)
697 struct strbuf rev = STRBUF_INIT;
698 struct object_id oid;
700 submodule_cache_check_init(the_repository);
702 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
703 git_config_from_blob_oid(gitmodules_cb, rev.buf,
704 &oid, the_repository);
706 strbuf_release(&rev);
708 the_repository->submodule_cache->gitmodules_read = 1;
711 const struct submodule *submodule_from_name(struct repository *r,
712 const struct object_id *treeish_name,
713 const char *name)
715 repo_read_gitmodules(r, 1);
716 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
719 const struct submodule *submodule_from_path(struct repository *r,
720 const struct object_id *treeish_name,
721 const char *path)
723 repo_read_gitmodules(r, 1);
724 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
727 void submodule_free(struct repository *r)
729 if (r->submodule_cache)
730 submodule_cache_clear(r->submodule_cache);
733 static int config_print_callback(const char *var, const char *value, void *cb_data)
735 char *wanted_key = cb_data;
737 if (!strcmp(wanted_key, var))
738 printf("%s\n", value);
740 return 0;
743 int print_config_from_gitmodules(struct repository *repo, const char *key)
745 int ret;
746 char *store_key;
748 ret = git_config_parse_key(key, &store_key, NULL);
749 if (ret < 0)
750 return CONFIG_INVALID_KEY;
752 config_from_gitmodules(config_print_callback, repo, store_key);
754 free(store_key);
755 return 0;
758 int config_set_in_gitmodules_file_gently(const char *key, const char *value)
760 int ret;
762 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
763 if (ret < 0)
764 /* Maybe the user already did that, don't error out here */
765 warning(_("Could not update .gitmodules entry %s"), key);
767 return ret;
770 struct fetch_config {
771 int *max_children;
772 int *recurse_submodules;
775 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
777 struct fetch_config *config = cb;
778 if (!strcmp(var, "submodule.fetchjobs")) {
779 *(config->max_children) = parse_submodule_fetchjobs(var, value);
780 return 0;
781 } else if (!strcmp(var, "fetch.recursesubmodules")) {
782 *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
783 return 0;
786 return 0;
789 void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
791 struct fetch_config config = {
792 .max_children = max_children,
793 .recurse_submodules = recurse_submodules
795 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
798 static int gitmodules_update_clone_config(const char *var, const char *value,
799 void *cb)
801 int *max_jobs = cb;
802 if (!strcmp(var, "submodule.fetchjobs"))
803 *max_jobs = parse_submodule_fetchjobs(var, value);
804 return 0;
807 void update_clone_config_from_gitmodules(int *max_jobs)
809 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);