Merge branch 'ma/ts-cleanups' into maint
[alt-git.git] / submodule-config.c
blob5fe2d0787745de4d97218c738b808667dc8348d3
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;
24 * thin wrapper struct needed to insert 'struct submodule' entries to
25 * the hashmap
27 struct submodule_entry {
28 struct hashmap_entry ent;
29 struct submodule *config;
32 enum lookup_type {
33 lookup_name,
34 lookup_path
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);
73 free(entry->config);
76 static void submodule_cache_clear(struct submodule_cache *cache)
78 struct hashmap_iter iter;
79 struct submodule_entry *entry;
81 if (!cache->initialized)
82 return;
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);
101 free(cache);
104 static unsigned int hash_sha1_string(const unsigned char *sha1,
105 const char *string)
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,
114 submodule->path);
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,
125 submodule->path);
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);
131 free(removed);
134 static void cache_add(struct submodule_cache *cache,
135 struct submodule *submodule)
137 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
138 submodule->name);
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);
160 if (entry)
161 return entry->config;
162 return NULL;
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);
180 if (entry)
181 return entry->config;
182 return NULL;
185 static int name_and_item_from_var(const char *var, struct strbuf *name,
186 struct strbuf *item)
188 const char *subsection, *key;
189 int subsection_len, parse;
190 parse = parse_config_key(var, "submodule", &subsection,
191 &subsection_len, &key);
192 if (parse < 0 || !subsection)
193 return 0;
195 strbuf_add(name, subsection, subsection_len);
196 strbuf_addstr(item, key);
198 return 1;
201 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
202 const unsigned char *gitmodules_sha1, const char *name)
204 struct submodule *submodule;
205 struct strbuf name_buf = STRBUF_INIT;
207 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
208 if (submodule)
209 return submodule;
211 submodule = xmalloc(sizeof(*submodule));
213 strbuf_addstr(&name_buf, name);
214 submodule->name = strbuf_detach(&name_buf, NULL);
216 submodule->path = NULL;
217 submodule->url = NULL;
218 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
219 submodule->update_strategy.command = NULL;
220 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
221 submodule->ignore = NULL;
222 submodule->branch = NULL;
223 submodule->recommend_shallow = -1;
225 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
227 cache_add(cache, submodule);
229 return submodule;
232 static int parse_fetch_recurse(const char *opt, const char *arg,
233 int die_on_error)
235 switch (git_config_maybe_bool(opt, arg)) {
236 case 1:
237 return RECURSE_SUBMODULES_ON;
238 case 0:
239 return RECURSE_SUBMODULES_OFF;
240 default:
241 if (!strcmp(arg, "on-demand"))
242 return RECURSE_SUBMODULES_ON_DEMAND;
244 if (die_on_error)
245 die("bad %s argument: %s", opt, arg);
246 else
247 return RECURSE_SUBMODULES_ERROR;
251 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
253 return parse_fetch_recurse(opt, arg, 1);
256 int option_fetch_parse_recurse_submodules(const struct option *opt,
257 const char *arg, int unset)
259 int *v;
261 if (!opt->value)
262 return -1;
264 v = opt->value;
266 if (unset) {
267 *v = RECURSE_SUBMODULES_OFF;
268 } else {
269 if (arg)
270 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
271 else
272 *v = RECURSE_SUBMODULES_ON;
274 return 0;
277 static int parse_update_recurse(const char *opt, const char *arg,
278 int die_on_error)
280 switch (git_config_maybe_bool(opt, arg)) {
281 case 1:
282 return RECURSE_SUBMODULES_ON;
283 case 0:
284 return RECURSE_SUBMODULES_OFF;
285 default:
286 if (die_on_error)
287 die("bad %s argument: %s", opt, arg);
288 return RECURSE_SUBMODULES_ERROR;
292 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
294 return parse_update_recurse(opt, arg, 1);
297 static int parse_push_recurse(const char *opt, const char *arg,
298 int die_on_error)
300 switch (git_config_maybe_bool(opt, arg)) {
301 case 1:
302 /* There's no simple "on" value when pushing */
303 if (die_on_error)
304 die("bad %s argument: %s", opt, arg);
305 else
306 return RECURSE_SUBMODULES_ERROR;
307 case 0:
308 return RECURSE_SUBMODULES_OFF;
309 default:
310 if (!strcmp(arg, "on-demand"))
311 return RECURSE_SUBMODULES_ON_DEMAND;
312 else if (!strcmp(arg, "check"))
313 return RECURSE_SUBMODULES_CHECK;
314 else if (!strcmp(arg, "only"))
315 return RECURSE_SUBMODULES_ONLY;
316 else if (die_on_error)
317 die("bad %s argument: %s", opt, arg);
318 else
319 return RECURSE_SUBMODULES_ERROR;
323 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
325 return parse_push_recurse(opt, arg, 1);
328 static void warn_multiple_config(const unsigned char *treeish_name,
329 const char *name, const char *option)
331 const char *commit_string = "WORKTREE";
332 if (treeish_name)
333 commit_string = sha1_to_hex(treeish_name);
334 warning("%s:.gitmodules, multiple configurations found for "
335 "'submodule.%s.%s'. Skipping second one!",
336 commit_string, name, option);
339 struct parse_config_parameter {
340 struct submodule_cache *cache;
341 const unsigned char *treeish_name;
342 const unsigned char *gitmodules_sha1;
343 int overwrite;
346 static int parse_config(const char *var, const char *value, void *data)
348 struct parse_config_parameter *me = data;
349 struct submodule *submodule;
350 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
351 int ret = 0;
353 /* this also ensures that we only parse submodule entries */
354 if (!name_and_item_from_var(var, &name, &item))
355 return 0;
357 submodule = lookup_or_create_by_name(me->cache,
358 me->gitmodules_sha1,
359 name.buf);
361 if (!strcmp(item.buf, "path")) {
362 if (!value)
363 ret = config_error_nonbool(var);
364 else if (!me->overwrite && submodule->path)
365 warn_multiple_config(me->treeish_name, submodule->name,
366 "path");
367 else {
368 if (submodule->path)
369 cache_remove_path(me->cache, submodule);
370 free((void *) submodule->path);
371 submodule->path = xstrdup(value);
372 cache_put_path(me->cache, submodule);
374 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
375 /* when parsing worktree configurations we can die early */
376 int die_on_error = is_null_sha1(me->gitmodules_sha1);
377 if (!me->overwrite &&
378 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
379 warn_multiple_config(me->treeish_name, submodule->name,
380 "fetchrecursesubmodules");
381 else
382 submodule->fetch_recurse = parse_fetch_recurse(
383 var, value,
384 die_on_error);
385 } else if (!strcmp(item.buf, "ignore")) {
386 if (!value)
387 ret = config_error_nonbool(var);
388 else if (!me->overwrite && submodule->ignore)
389 warn_multiple_config(me->treeish_name, submodule->name,
390 "ignore");
391 else if (strcmp(value, "untracked") &&
392 strcmp(value, "dirty") &&
393 strcmp(value, "all") &&
394 strcmp(value, "none"))
395 warning("Invalid parameter '%s' for config option "
396 "'submodule.%s.ignore'", value, name.buf);
397 else {
398 free((void *) submodule->ignore);
399 submodule->ignore = xstrdup(value);
401 } else if (!strcmp(item.buf, "url")) {
402 if (!value) {
403 ret = config_error_nonbool(var);
404 } else if (!me->overwrite && submodule->url) {
405 warn_multiple_config(me->treeish_name, submodule->name,
406 "url");
407 } else {
408 free((void *) submodule->url);
409 submodule->url = xstrdup(value);
411 } else if (!strcmp(item.buf, "update")) {
412 if (!value)
413 ret = config_error_nonbool(var);
414 else if (!me->overwrite &&
415 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
416 warn_multiple_config(me->treeish_name, submodule->name,
417 "update");
418 else if (parse_submodule_update_strategy(value,
419 &submodule->update_strategy) < 0)
420 die(_("invalid value for %s"), var);
421 } else if (!strcmp(item.buf, "shallow")) {
422 if (!me->overwrite && submodule->recommend_shallow != -1)
423 warn_multiple_config(me->treeish_name, submodule->name,
424 "shallow");
425 else
426 submodule->recommend_shallow =
427 git_config_bool(var, value);
428 } else if (!strcmp(item.buf, "branch")) {
429 if (!me->overwrite && submodule->branch)
430 warn_multiple_config(me->treeish_name, submodule->name,
431 "branch");
432 else {
433 free((void *)submodule->branch);
434 submodule->branch = xstrdup(value);
438 strbuf_release(&name);
439 strbuf_release(&item);
441 return ret;
444 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
445 unsigned char *gitmodules_sha1,
446 struct strbuf *rev)
448 int ret = 0;
450 if (is_null_sha1(treeish_name)) {
451 hashclr(gitmodules_sha1);
452 return 1;
455 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
456 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
457 ret = 1;
459 return ret;
462 /* This does a lookup of a submodule configuration by name or by path
463 * (key) with on-demand reading of the appropriate .gitmodules from
464 * revisions.
466 static const struct submodule *config_from(struct submodule_cache *cache,
467 const unsigned char *treeish_name, const char *key,
468 enum lookup_type lookup_type)
470 struct strbuf rev = STRBUF_INIT;
471 unsigned long config_size;
472 char *config = NULL;
473 unsigned char sha1[20];
474 enum object_type type;
475 const struct submodule *submodule = NULL;
476 struct parse_config_parameter parameter;
479 * If any parameter except the cache is a NULL pointer just
480 * return the first submodule. Can be used to check whether
481 * there are any submodules parsed.
483 if (!treeish_name || !key) {
484 struct hashmap_iter iter;
485 struct submodule_entry *entry;
487 entry = hashmap_iter_first(&cache->for_name, &iter);
488 if (!entry)
489 return NULL;
490 return entry->config;
493 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
494 goto out;
496 switch (lookup_type) {
497 case lookup_name:
498 submodule = cache_lookup_name(cache, sha1, key);
499 break;
500 case lookup_path:
501 submodule = cache_lookup_path(cache, sha1, key);
502 break;
504 if (submodule)
505 goto out;
507 config = read_sha1_file(sha1, &type, &config_size);
508 if (!config || type != OBJ_BLOB)
509 goto out;
511 /* fill the submodule config into the cache */
512 parameter.cache = cache;
513 parameter.treeish_name = treeish_name;
514 parameter.gitmodules_sha1 = sha1;
515 parameter.overwrite = 0;
516 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
517 config, config_size, &parameter);
518 strbuf_release(&rev);
519 free(config);
521 switch (lookup_type) {
522 case lookup_name:
523 return cache_lookup_name(cache, sha1, key);
524 case lookup_path:
525 return cache_lookup_path(cache, sha1, key);
526 default:
527 return NULL;
530 out:
531 strbuf_release(&rev);
532 free(config);
533 return submodule;
536 static void submodule_cache_check_init(struct repository *repo)
538 if (repo->submodule_cache && repo->submodule_cache->initialized)
539 return;
541 if (!repo->submodule_cache)
542 repo->submodule_cache = submodule_cache_alloc();
544 submodule_cache_init(repo->submodule_cache);
547 int submodule_config_option(struct repository *repo,
548 const char *var, const char *value)
550 struct parse_config_parameter parameter;
552 submodule_cache_check_init(repo);
554 parameter.cache = repo->submodule_cache;
555 parameter.treeish_name = NULL;
556 parameter.gitmodules_sha1 = null_sha1;
557 parameter.overwrite = 1;
559 return parse_config(var, value, &parameter);
562 int parse_submodule_config_option(const char *var, const char *value)
564 return submodule_config_option(the_repository, var, value);
567 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
568 const char *name)
570 submodule_cache_check_init(the_repository);
571 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
574 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
575 const char *path)
577 submodule_cache_check_init(the_repository);
578 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
581 const struct submodule *submodule_from_cache(struct repository *repo,
582 const unsigned char *treeish_name,
583 const char *key)
585 submodule_cache_check_init(repo);
586 return config_from(repo->submodule_cache, treeish_name,
587 key, lookup_path);
590 void submodule_free(void)
592 if (the_repository->submodule_cache)
593 submodule_cache_clear(the_repository->submodule_cache);