wt-status: actually ignore submodules when requested
[git.git] / submodule-config.c
blob56d9d76d408d0bba967e5b8509f971e93a090b12
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 struct submodule_entry *a,
40 const struct submodule_entry *b,
41 const void *unused_keydata)
43 return strcmp(a->config->path, b->config->path) ||
44 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
47 static int config_name_cmp(const void *unused_cmp_data,
48 const struct submodule_entry *a,
49 const struct submodule_entry *b,
50 const void *unused_keydata)
52 return strcmp(a->config->name, b->config->name) ||
53 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
56 static struct submodule_cache *submodule_cache_alloc(void)
58 return xcalloc(1, sizeof(struct submodule_cache));
61 static void submodule_cache_init(struct submodule_cache *cache)
63 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
64 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
65 cache->initialized = 1;
68 static void free_one_config(struct submodule_entry *entry)
70 free((void *) entry->config->path);
71 free((void *) entry->config->name);
72 free((void *) entry->config->branch);
73 free((void *) entry->config->update_strategy.command);
74 free(entry->config);
77 static void submodule_cache_clear(struct submodule_cache *cache)
79 struct hashmap_iter iter;
80 struct submodule_entry *entry;
82 if (!cache->initialized)
83 return;
86 * We iterate over the name hash here to be symmetric with the
87 * allocation of struct submodule entries. Each is allocated by
88 * their .gitmodule blob sha1 and submodule name.
90 hashmap_iter_init(&cache->for_name, &iter);
91 while ((entry = hashmap_iter_next(&iter)))
92 free_one_config(entry);
94 hashmap_free(&cache->for_path, 1);
95 hashmap_free(&cache->for_name, 1);
96 cache->initialized = 0;
97 cache->gitmodules_read = 0;
100 void submodule_cache_free(struct submodule_cache *cache)
102 submodule_cache_clear(cache);
103 free(cache);
106 static unsigned int hash_sha1_string(const unsigned char *sha1,
107 const char *string)
109 return memhash(sha1, 20) + strhash(string);
112 static void cache_put_path(struct submodule_cache *cache,
113 struct submodule *submodule)
115 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
116 submodule->path);
117 struct submodule_entry *e = xmalloc(sizeof(*e));
118 hashmap_entry_init(e, hash);
119 e->config = submodule;
120 hashmap_put(&cache->for_path, e);
123 static void cache_remove_path(struct submodule_cache *cache,
124 struct submodule *submodule)
126 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
127 submodule->path);
128 struct submodule_entry e;
129 struct submodule_entry *removed;
130 hashmap_entry_init(&e, hash);
131 e.config = submodule;
132 removed = hashmap_remove(&cache->for_path, &e, NULL);
133 free(removed);
136 static void cache_add(struct submodule_cache *cache,
137 struct submodule *submodule)
139 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
140 submodule->name);
141 struct submodule_entry *e = xmalloc(sizeof(*e));
142 hashmap_entry_init(e, hash);
143 e->config = submodule;
144 hashmap_add(&cache->for_name, e);
147 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
148 const unsigned char *gitmodules_sha1, const char *path)
150 struct submodule_entry *entry;
151 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
152 struct submodule_entry key;
153 struct submodule key_config;
155 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
156 key_config.path = path;
158 hashmap_entry_init(&key, hash);
159 key.config = &key_config;
161 entry = hashmap_get(&cache->for_path, &key, NULL);
162 if (entry)
163 return entry->config;
164 return NULL;
167 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
168 const unsigned char *gitmodules_sha1, const char *name)
170 struct submodule_entry *entry;
171 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
172 struct submodule_entry key;
173 struct submodule key_config;
175 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
176 key_config.name = name;
178 hashmap_entry_init(&key, hash);
179 key.config = &key_config;
181 entry = hashmap_get(&cache->for_name, &key, NULL);
182 if (entry)
183 return entry->config;
184 return NULL;
187 static int name_and_item_from_var(const char *var, struct strbuf *name,
188 struct strbuf *item)
190 const char *subsection, *key;
191 int subsection_len, parse;
192 parse = parse_config_key(var, "submodule", &subsection,
193 &subsection_len, &key);
194 if (parse < 0 || !subsection)
195 return 0;
197 strbuf_add(name, subsection, subsection_len);
198 strbuf_addstr(item, key);
200 return 1;
203 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
204 const unsigned char *gitmodules_sha1, const char *name)
206 struct submodule *submodule;
207 struct strbuf name_buf = STRBUF_INIT;
209 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
210 if (submodule)
211 return submodule;
213 submodule = xmalloc(sizeof(*submodule));
215 strbuf_addstr(&name_buf, name);
216 submodule->name = strbuf_detach(&name_buf, NULL);
218 submodule->path = NULL;
219 submodule->url = NULL;
220 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
221 submodule->update_strategy.command = NULL;
222 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
223 submodule->ignore = NULL;
224 submodule->branch = NULL;
225 submodule->recommend_shallow = -1;
227 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
229 cache_add(cache, submodule);
231 return submodule;
234 static int parse_fetch_recurse(const char *opt, const char *arg,
235 int die_on_error)
237 switch (git_config_maybe_bool(opt, arg)) {
238 case 1:
239 return RECURSE_SUBMODULES_ON;
240 case 0:
241 return RECURSE_SUBMODULES_OFF;
242 default:
243 if (!strcmp(arg, "on-demand"))
244 return RECURSE_SUBMODULES_ON_DEMAND;
246 if (die_on_error)
247 die("bad %s argument: %s", opt, arg);
248 else
249 return RECURSE_SUBMODULES_ERROR;
253 int parse_submodule_fetchjobs(const char *var, const char *value)
255 int fetchjobs = git_config_int(var, value);
256 if (fetchjobs < 0)
257 die(_("negative values not allowed for submodule.fetchjobs"));
258 return fetchjobs;
261 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
263 return parse_fetch_recurse(opt, arg, 1);
266 int option_fetch_parse_recurse_submodules(const struct option *opt,
267 const char *arg, int unset)
269 int *v;
271 if (!opt->value)
272 return -1;
274 v = opt->value;
276 if (unset) {
277 *v = RECURSE_SUBMODULES_OFF;
278 } else {
279 if (arg)
280 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
281 else
282 *v = RECURSE_SUBMODULES_ON;
284 return 0;
287 static int parse_update_recurse(const char *opt, const char *arg,
288 int die_on_error)
290 switch (git_config_maybe_bool(opt, arg)) {
291 case 1:
292 return RECURSE_SUBMODULES_ON;
293 case 0:
294 return RECURSE_SUBMODULES_OFF;
295 default:
296 if (die_on_error)
297 die("bad %s argument: %s", opt, arg);
298 return RECURSE_SUBMODULES_ERROR;
302 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
304 return parse_update_recurse(opt, arg, 1);
307 static int parse_push_recurse(const char *opt, const char *arg,
308 int die_on_error)
310 switch (git_config_maybe_bool(opt, arg)) {
311 case 1:
312 /* There's no simple "on" value when pushing */
313 if (die_on_error)
314 die("bad %s argument: %s", opt, arg);
315 else
316 return RECURSE_SUBMODULES_ERROR;
317 case 0:
318 return RECURSE_SUBMODULES_OFF;
319 default:
320 if (!strcmp(arg, "on-demand"))
321 return RECURSE_SUBMODULES_ON_DEMAND;
322 else if (!strcmp(arg, "check"))
323 return RECURSE_SUBMODULES_CHECK;
324 else if (!strcmp(arg, "only"))
325 return RECURSE_SUBMODULES_ONLY;
326 else if (die_on_error)
327 die("bad %s argument: %s", opt, arg);
328 else
329 return RECURSE_SUBMODULES_ERROR;
333 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
335 return parse_push_recurse(opt, arg, 1);
338 static void warn_multiple_config(const unsigned char *treeish_name,
339 const char *name, const char *option)
341 const char *commit_string = "WORKTREE";
342 if (treeish_name)
343 commit_string = sha1_to_hex(treeish_name);
344 warning("%s:.gitmodules, multiple configurations found for "
345 "'submodule.%s.%s'. Skipping second one!",
346 commit_string, name, option);
349 struct parse_config_parameter {
350 struct submodule_cache *cache;
351 const unsigned char *treeish_name;
352 const unsigned char *gitmodules_sha1;
353 int overwrite;
356 static int parse_config(const char *var, const char *value, void *data)
358 struct parse_config_parameter *me = data;
359 struct submodule *submodule;
360 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
361 int ret = 0;
363 /* this also ensures that we only parse submodule entries */
364 if (!name_and_item_from_var(var, &name, &item))
365 return 0;
367 submodule = lookup_or_create_by_name(me->cache,
368 me->gitmodules_sha1,
369 name.buf);
371 if (!strcmp(item.buf, "path")) {
372 if (!value)
373 ret = config_error_nonbool(var);
374 else if (!me->overwrite && submodule->path)
375 warn_multiple_config(me->treeish_name, submodule->name,
376 "path");
377 else {
378 if (submodule->path)
379 cache_remove_path(me->cache, submodule);
380 free((void *) submodule->path);
381 submodule->path = xstrdup(value);
382 cache_put_path(me->cache, submodule);
384 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
385 /* when parsing worktree configurations we can die early */
386 int die_on_error = is_null_sha1(me->gitmodules_sha1);
387 if (!me->overwrite &&
388 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
389 warn_multiple_config(me->treeish_name, submodule->name,
390 "fetchrecursesubmodules");
391 else
392 submodule->fetch_recurse = parse_fetch_recurse(
393 var, value,
394 die_on_error);
395 } else if (!strcmp(item.buf, "ignore")) {
396 if (!value)
397 ret = config_error_nonbool(var);
398 else if (!me->overwrite && submodule->ignore)
399 warn_multiple_config(me->treeish_name, submodule->name,
400 "ignore");
401 else if (strcmp(value, "untracked") &&
402 strcmp(value, "dirty") &&
403 strcmp(value, "all") &&
404 strcmp(value, "none"))
405 warning("Invalid parameter '%s' for config option "
406 "'submodule.%s.ignore'", value, name.buf);
407 else {
408 free((void *) submodule->ignore);
409 submodule->ignore = xstrdup(value);
411 } else if (!strcmp(item.buf, "url")) {
412 if (!value) {
413 ret = config_error_nonbool(var);
414 } else if (!me->overwrite && submodule->url) {
415 warn_multiple_config(me->treeish_name, submodule->name,
416 "url");
417 } else {
418 free((void *) submodule->url);
419 submodule->url = xstrdup(value);
421 } else if (!strcmp(item.buf, "update")) {
422 if (!value)
423 ret = config_error_nonbool(var);
424 else if (!me->overwrite &&
425 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
426 warn_multiple_config(me->treeish_name, submodule->name,
427 "update");
428 else if (parse_submodule_update_strategy(value,
429 &submodule->update_strategy) < 0)
430 die(_("invalid value for %s"), var);
431 } else if (!strcmp(item.buf, "shallow")) {
432 if (!me->overwrite && submodule->recommend_shallow != -1)
433 warn_multiple_config(me->treeish_name, submodule->name,
434 "shallow");
435 else
436 submodule->recommend_shallow =
437 git_config_bool(var, value);
438 } else if (!strcmp(item.buf, "branch")) {
439 if (!me->overwrite && submodule->branch)
440 warn_multiple_config(me->treeish_name, submodule->name,
441 "branch");
442 else {
443 free((void *)submodule->branch);
444 submodule->branch = xstrdup(value);
448 strbuf_release(&name);
449 strbuf_release(&item);
451 return ret;
454 static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
455 struct object_id *gitmodules_oid,
456 struct strbuf *rev)
458 int ret = 0;
460 if (is_null_oid(treeish_name)) {
461 oidclr(gitmodules_oid);
462 return 1;
465 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
466 if (get_oid(rev->buf, gitmodules_oid) >= 0)
467 ret = 1;
469 return ret;
472 /* This does a lookup of a submodule configuration by name or by path
473 * (key) with on-demand reading of the appropriate .gitmodules from
474 * revisions.
476 static const struct submodule *config_from(struct submodule_cache *cache,
477 const struct object_id *treeish_name, const char *key,
478 enum lookup_type lookup_type)
480 struct strbuf rev = STRBUF_INIT;
481 unsigned long config_size;
482 char *config = NULL;
483 struct object_id oid;
484 enum object_type type;
485 const struct submodule *submodule = NULL;
486 struct parse_config_parameter parameter;
489 * If any parameter except the cache is a NULL pointer just
490 * return the first submodule. Can be used to check whether
491 * there are any submodules parsed.
493 if (!treeish_name || !key) {
494 struct hashmap_iter iter;
495 struct submodule_entry *entry;
497 entry = hashmap_iter_first(&cache->for_name, &iter);
498 if (!entry)
499 return NULL;
500 return entry->config;
503 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
504 goto out;
506 switch (lookup_type) {
507 case lookup_name:
508 submodule = cache_lookup_name(cache, oid.hash, key);
509 break;
510 case lookup_path:
511 submodule = cache_lookup_path(cache, oid.hash, key);
512 break;
514 if (submodule)
515 goto out;
517 config = read_sha1_file(oid.hash, &type, &config_size);
518 if (!config || type != OBJ_BLOB)
519 goto out;
521 /* fill the submodule config into the cache */
522 parameter.cache = cache;
523 parameter.treeish_name = treeish_name->hash;
524 parameter.gitmodules_sha1 = oid.hash;
525 parameter.overwrite = 0;
526 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
527 config, config_size, &parameter);
528 strbuf_release(&rev);
529 free(config);
531 switch (lookup_type) {
532 case lookup_name:
533 return cache_lookup_name(cache, oid.hash, key);
534 case lookup_path:
535 return cache_lookup_path(cache, oid.hash, key);
536 default:
537 return NULL;
540 out:
541 strbuf_release(&rev);
542 free(config);
543 return submodule;
546 static void submodule_cache_check_init(struct repository *repo)
548 if (repo->submodule_cache && repo->submodule_cache->initialized)
549 return;
551 if (!repo->submodule_cache)
552 repo->submodule_cache = submodule_cache_alloc();
554 submodule_cache_init(repo->submodule_cache);
557 static int gitmodules_cb(const char *var, const char *value, void *data)
559 struct repository *repo = data;
560 struct parse_config_parameter parameter;
562 parameter.cache = repo->submodule_cache;
563 parameter.treeish_name = NULL;
564 parameter.gitmodules_sha1 = null_sha1;
565 parameter.overwrite = 1;
567 return parse_config(var, value, &parameter);
570 void repo_read_gitmodules(struct repository *repo)
572 submodule_cache_check_init(repo);
574 if (repo->worktree) {
575 char *gitmodules;
577 if (repo_read_index(repo) < 0)
578 return;
580 gitmodules = repo_worktree_path(repo, GITMODULES_FILE);
582 if (!is_gitmodules_unmerged(repo->index))
583 git_config_from_file(gitmodules_cb, gitmodules, repo);
585 free(gitmodules);
588 repo->submodule_cache->gitmodules_read = 1;
591 void gitmodules_config_oid(const struct object_id *commit_oid)
593 struct strbuf rev = STRBUF_INIT;
594 struct object_id oid;
596 submodule_cache_check_init(the_repository);
598 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
599 git_config_from_blob_oid(gitmodules_cb, rev.buf,
600 &oid, the_repository);
602 strbuf_release(&rev);
604 the_repository->submodule_cache->gitmodules_read = 1;
607 static void gitmodules_read_check(struct repository *repo)
609 submodule_cache_check_init(repo);
611 /* read the repo's .gitmodules file if it hasn't been already */
612 if (!repo->submodule_cache->gitmodules_read)
613 repo_read_gitmodules(repo);
616 const struct submodule *submodule_from_name(const struct object_id *treeish_name,
617 const char *name)
619 gitmodules_read_check(the_repository);
620 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
623 const struct submodule *submodule_from_path(const struct object_id *treeish_name,
624 const char *path)
626 gitmodules_read_check(the_repository);
627 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
630 const struct submodule *submodule_from_cache(struct repository *repo,
631 const struct object_id *treeish_name,
632 const char *key)
634 gitmodules_read_check(repo);
635 return config_from(repo->submodule_cache, treeish_name,
636 key, lookup_path);
639 void submodule_free(void)
641 if (the_repository->submodule_cache)
642 submodule_cache_clear(the_repository->submodule_cache);