pretty: respect color settings for %C placeholders
[git.git] / submodule-config.c
blob37cfcceb950b1777b46abecd5415196c76e11826
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"
8 /*
9 * submodule cache lookup structure
10 * There is one shared set of 'struct submodule' entries which can be
11 * looked up by their sha1 blob id of the .gitmodule file and either
12 * using path or name as key.
13 * for_path stores submodule entries with path as key
14 * for_name stores submodule entries with name as key
16 struct submodule_cache {
17 struct hashmap for_path;
18 struct hashmap for_name;
19 unsigned initialized:1;
23 * thin wrapper struct needed to insert 'struct submodule' entries to
24 * the hashmap
26 struct submodule_entry {
27 struct hashmap_entry ent;
28 struct submodule *config;
31 enum lookup_type {
32 lookup_name,
33 lookup_path
36 static int config_path_cmp(const struct submodule_entry *a,
37 const struct submodule_entry *b,
38 const void *unused)
40 return strcmp(a->config->path, b->config->path) ||
41 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
44 static int config_name_cmp(const struct submodule_entry *a,
45 const struct submodule_entry *b,
46 const void *unused)
48 return strcmp(a->config->name, b->config->name) ||
49 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
52 static struct submodule_cache *submodule_cache_alloc(void)
54 return xcalloc(1, sizeof(struct submodule_cache));
57 static void submodule_cache_init(struct submodule_cache *cache)
59 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
60 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
61 cache->initialized = 1;
64 static void free_one_config(struct submodule_entry *entry)
66 free((void *) entry->config->path);
67 free((void *) entry->config->name);
68 free((void *) entry->config->branch);
69 free((void *) entry->config->update_strategy.command);
70 free(entry->config);
73 static void submodule_cache_clear(struct submodule_cache *cache)
75 struct hashmap_iter iter;
76 struct submodule_entry *entry;
78 if (!cache->initialized)
79 return;
82 * We iterate over the name hash here to be symmetric with the
83 * allocation of struct submodule entries. Each is allocated by
84 * their .gitmodule blob sha1 and submodule name.
86 hashmap_iter_init(&cache->for_name, &iter);
87 while ((entry = hashmap_iter_next(&iter)))
88 free_one_config(entry);
90 hashmap_free(&cache->for_path, 1);
91 hashmap_free(&cache->for_name, 1);
92 cache->initialized = 0;
95 void submodule_cache_free(struct submodule_cache *cache)
97 submodule_cache_clear(cache);
98 free(cache);
101 static unsigned int hash_sha1_string(const unsigned char *sha1,
102 const char *string)
104 return memhash(sha1, 20) + strhash(string);
107 static void cache_put_path(struct submodule_cache *cache,
108 struct submodule *submodule)
110 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
111 submodule->path);
112 struct submodule_entry *e = xmalloc(sizeof(*e));
113 hashmap_entry_init(e, hash);
114 e->config = submodule;
115 hashmap_put(&cache->for_path, e);
118 static void cache_remove_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;
124 struct submodule_entry *removed;
125 hashmap_entry_init(&e, hash);
126 e.config = submodule;
127 removed = hashmap_remove(&cache->for_path, &e, NULL);
128 free(removed);
131 static void cache_add(struct submodule_cache *cache,
132 struct submodule *submodule)
134 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
135 submodule->name);
136 struct submodule_entry *e = xmalloc(sizeof(*e));
137 hashmap_entry_init(e, hash);
138 e->config = submodule;
139 hashmap_add(&cache->for_name, e);
142 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
143 const unsigned char *gitmodules_sha1, const char *path)
145 struct submodule_entry *entry;
146 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
147 struct submodule_entry key;
148 struct submodule key_config;
150 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
151 key_config.path = path;
153 hashmap_entry_init(&key, hash);
154 key.config = &key_config;
156 entry = hashmap_get(&cache->for_path, &key, NULL);
157 if (entry)
158 return entry->config;
159 return NULL;
162 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
163 const unsigned char *gitmodules_sha1, const char *name)
165 struct submodule_entry *entry;
166 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
167 struct submodule_entry key;
168 struct submodule key_config;
170 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
171 key_config.name = name;
173 hashmap_entry_init(&key, hash);
174 key.config = &key_config;
176 entry = hashmap_get(&cache->for_name, &key, NULL);
177 if (entry)
178 return entry->config;
179 return NULL;
182 static int name_and_item_from_var(const char *var, struct strbuf *name,
183 struct strbuf *item)
185 const char *subsection, *key;
186 int subsection_len, parse;
187 parse = parse_config_key(var, "submodule", &subsection,
188 &subsection_len, &key);
189 if (parse < 0 || !subsection)
190 return 0;
192 strbuf_add(name, subsection, subsection_len);
193 strbuf_addstr(item, key);
195 return 1;
198 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
199 const unsigned char *gitmodules_sha1, const char *name)
201 struct submodule *submodule;
202 struct strbuf name_buf = STRBUF_INIT;
204 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
205 if (submodule)
206 return submodule;
208 submodule = xmalloc(sizeof(*submodule));
210 strbuf_addstr(&name_buf, name);
211 submodule->name = strbuf_detach(&name_buf, NULL);
213 submodule->path = NULL;
214 submodule->url = NULL;
215 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
216 submodule->update_strategy.command = NULL;
217 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
218 submodule->ignore = NULL;
219 submodule->branch = NULL;
220 submodule->recommend_shallow = -1;
222 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
224 cache_add(cache, submodule);
226 return submodule;
229 static int parse_fetch_recurse(const char *opt, const char *arg,
230 int die_on_error)
232 switch (git_config_maybe_bool(opt, arg)) {
233 case 1:
234 return RECURSE_SUBMODULES_ON;
235 case 0:
236 return RECURSE_SUBMODULES_OFF;
237 default:
238 if (!strcmp(arg, "on-demand"))
239 return RECURSE_SUBMODULES_ON_DEMAND;
241 if (die_on_error)
242 die("bad %s argument: %s", opt, arg);
243 else
244 return RECURSE_SUBMODULES_ERROR;
248 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
250 return parse_fetch_recurse(opt, arg, 1);
253 static int parse_update_recurse(const char *opt, const char *arg,
254 int die_on_error)
256 switch (git_config_maybe_bool(opt, arg)) {
257 case 1:
258 return RECURSE_SUBMODULES_ON;
259 case 0:
260 return RECURSE_SUBMODULES_OFF;
261 default:
262 if (die_on_error)
263 die("bad %s argument: %s", opt, arg);
264 return RECURSE_SUBMODULES_ERROR;
268 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
270 return parse_update_recurse(opt, arg, 1);
273 static int parse_push_recurse(const char *opt, const char *arg,
274 int die_on_error)
276 switch (git_config_maybe_bool(opt, arg)) {
277 case 1:
278 /* There's no simple "on" value when pushing */
279 if (die_on_error)
280 die("bad %s argument: %s", opt, arg);
281 else
282 return RECURSE_SUBMODULES_ERROR;
283 case 0:
284 return RECURSE_SUBMODULES_OFF;
285 default:
286 if (!strcmp(arg, "on-demand"))
287 return RECURSE_SUBMODULES_ON_DEMAND;
288 else if (!strcmp(arg, "check"))
289 return RECURSE_SUBMODULES_CHECK;
290 else if (!strcmp(arg, "only"))
291 return RECURSE_SUBMODULES_ONLY;
292 else if (die_on_error)
293 die("bad %s argument: %s", opt, arg);
294 else
295 return RECURSE_SUBMODULES_ERROR;
299 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
301 return parse_push_recurse(opt, arg, 1);
304 static void warn_multiple_config(const unsigned char *treeish_name,
305 const char *name, const char *option)
307 const char *commit_string = "WORKTREE";
308 if (treeish_name)
309 commit_string = sha1_to_hex(treeish_name);
310 warning("%s:.gitmodules, multiple configurations found for "
311 "'submodule.%s.%s'. Skipping second one!",
312 commit_string, name, option);
315 struct parse_config_parameter {
316 struct submodule_cache *cache;
317 const unsigned char *treeish_name;
318 const unsigned char *gitmodules_sha1;
319 int overwrite;
322 static int parse_config(const char *var, const char *value, void *data)
324 struct parse_config_parameter *me = data;
325 struct submodule *submodule;
326 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
327 int ret = 0;
329 /* this also ensures that we only parse submodule entries */
330 if (!name_and_item_from_var(var, &name, &item))
331 return 0;
333 submodule = lookup_or_create_by_name(me->cache,
334 me->gitmodules_sha1,
335 name.buf);
337 if (!strcmp(item.buf, "path")) {
338 if (!value)
339 ret = config_error_nonbool(var);
340 else if (!me->overwrite && submodule->path)
341 warn_multiple_config(me->treeish_name, submodule->name,
342 "path");
343 else {
344 if (submodule->path)
345 cache_remove_path(me->cache, submodule);
346 free((void *) submodule->path);
347 submodule->path = xstrdup(value);
348 cache_put_path(me->cache, submodule);
350 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
351 /* when parsing worktree configurations we can die early */
352 int die_on_error = is_null_sha1(me->gitmodules_sha1);
353 if (!me->overwrite &&
354 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
355 warn_multiple_config(me->treeish_name, submodule->name,
356 "fetchrecursesubmodules");
357 else
358 submodule->fetch_recurse = parse_fetch_recurse(
359 var, value,
360 die_on_error);
361 } else if (!strcmp(item.buf, "ignore")) {
362 if (!value)
363 ret = config_error_nonbool(var);
364 else if (!me->overwrite && submodule->ignore)
365 warn_multiple_config(me->treeish_name, submodule->name,
366 "ignore");
367 else if (strcmp(value, "untracked") &&
368 strcmp(value, "dirty") &&
369 strcmp(value, "all") &&
370 strcmp(value, "none"))
371 warning("Invalid parameter '%s' for config option "
372 "'submodule.%s.ignore'", value, name.buf);
373 else {
374 free((void *) submodule->ignore);
375 submodule->ignore = xstrdup(value);
377 } else if (!strcmp(item.buf, "url")) {
378 if (!value) {
379 ret = config_error_nonbool(var);
380 } else if (!me->overwrite && submodule->url) {
381 warn_multiple_config(me->treeish_name, submodule->name,
382 "url");
383 } else {
384 free((void *) submodule->url);
385 submodule->url = xstrdup(value);
387 } else if (!strcmp(item.buf, "update")) {
388 if (!value)
389 ret = config_error_nonbool(var);
390 else if (!me->overwrite &&
391 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
392 warn_multiple_config(me->treeish_name, submodule->name,
393 "update");
394 else if (parse_submodule_update_strategy(value,
395 &submodule->update_strategy) < 0)
396 die(_("invalid value for %s"), var);
397 } else if (!strcmp(item.buf, "shallow")) {
398 if (!me->overwrite && submodule->recommend_shallow != -1)
399 warn_multiple_config(me->treeish_name, submodule->name,
400 "shallow");
401 else
402 submodule->recommend_shallow =
403 git_config_bool(var, value);
404 } else if (!strcmp(item.buf, "branch")) {
405 if (!me->overwrite && submodule->branch)
406 warn_multiple_config(me->treeish_name, submodule->name,
407 "branch");
408 else {
409 free((void *)submodule->branch);
410 submodule->branch = xstrdup(value);
414 strbuf_release(&name);
415 strbuf_release(&item);
417 return ret;
420 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
421 unsigned char *gitmodules_sha1,
422 struct strbuf *rev)
424 int ret = 0;
426 if (is_null_sha1(treeish_name)) {
427 hashclr(gitmodules_sha1);
428 return 1;
431 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
432 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
433 ret = 1;
435 return ret;
438 /* This does a lookup of a submodule configuration by name or by path
439 * (key) with on-demand reading of the appropriate .gitmodules from
440 * revisions.
442 static const struct submodule *config_from(struct submodule_cache *cache,
443 const unsigned char *treeish_name, const char *key,
444 enum lookup_type lookup_type)
446 struct strbuf rev = STRBUF_INIT;
447 unsigned long config_size;
448 char *config = NULL;
449 unsigned char sha1[20];
450 enum object_type type;
451 const struct submodule *submodule = NULL;
452 struct parse_config_parameter parameter;
455 * If any parameter except the cache is a NULL pointer just
456 * return the first submodule. Can be used to check whether
457 * there are any submodules parsed.
459 if (!treeish_name || !key) {
460 struct hashmap_iter iter;
461 struct submodule_entry *entry;
463 entry = hashmap_iter_first(&cache->for_name, &iter);
464 if (!entry)
465 return NULL;
466 return entry->config;
469 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
470 goto out;
472 switch (lookup_type) {
473 case lookup_name:
474 submodule = cache_lookup_name(cache, sha1, key);
475 break;
476 case lookup_path:
477 submodule = cache_lookup_path(cache, sha1, key);
478 break;
480 if (submodule)
481 goto out;
483 config = read_sha1_file(sha1, &type, &config_size);
484 if (!config || type != OBJ_BLOB)
485 goto out;
487 /* fill the submodule config into the cache */
488 parameter.cache = cache;
489 parameter.treeish_name = treeish_name;
490 parameter.gitmodules_sha1 = sha1;
491 parameter.overwrite = 0;
492 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
493 config, config_size, &parameter);
494 strbuf_release(&rev);
495 free(config);
497 switch (lookup_type) {
498 case lookup_name:
499 return cache_lookup_name(cache, sha1, key);
500 case lookup_path:
501 return cache_lookup_path(cache, sha1, key);
502 default:
503 return NULL;
506 out:
507 strbuf_release(&rev);
508 free(config);
509 return submodule;
512 static void submodule_cache_check_init(struct repository *repo)
514 if (repo->submodule_cache && repo->submodule_cache->initialized)
515 return;
517 if (!repo->submodule_cache)
518 repo->submodule_cache = submodule_cache_alloc();
520 submodule_cache_init(repo->submodule_cache);
523 int submodule_config_option(struct repository *repo,
524 const char *var, const char *value)
526 struct parse_config_parameter parameter;
528 submodule_cache_check_init(repo);
530 parameter.cache = repo->submodule_cache;
531 parameter.treeish_name = NULL;
532 parameter.gitmodules_sha1 = null_sha1;
533 parameter.overwrite = 1;
535 return parse_config(var, value, &parameter);
538 int parse_submodule_config_option(const char *var, const char *value)
540 return submodule_config_option(the_repository, var, value);
543 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
544 const char *name)
546 submodule_cache_check_init(the_repository);
547 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
550 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
551 const char *path)
553 submodule_cache_check_init(the_repository);
554 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
557 const struct submodule *submodule_from_cache(struct repository *repo,
558 const unsigned char *treeish_name,
559 const char *key)
561 submodule_cache_check_init(repo);
562 return config_from(repo->submodule_cache, treeish_name,
563 key, lookup_path);
566 void submodule_free(void)
568 if (the_repository->submodule_cache)
569 submodule_cache_clear(the_repository->submodule_cache);