Git 2.13.7
[git.git] / submodule-config.c
blobde54351c6f26bc7db4a3f4c6fa8304da6bc9d0c7
1 #include "cache.h"
2 #include "submodule-config.h"
3 #include "submodule.h"
4 #include "strbuf.h"
6 /*
7 * submodule cache lookup structure
8 * There is one shared set of 'struct submodule' entries which can be
9 * looked up by their sha1 blob id of the .gitmodule file and either
10 * using path or name as key.
11 * for_path stores submodule entries with path as key
12 * for_name stores submodule entries with name as key
14 struct submodule_cache {
15 struct hashmap for_path;
16 struct hashmap for_name;
20 * thin wrapper struct needed to insert 'struct submodule' entries to
21 * the hashmap
23 struct submodule_entry {
24 struct hashmap_entry ent;
25 struct submodule *config;
28 enum lookup_type {
29 lookup_name,
30 lookup_path
33 static struct submodule_cache the_submodule_cache;
34 static int is_cache_init;
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 void cache_init(struct submodule_cache *cache)
54 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, 0);
55 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, 0);
58 static void free_one_config(struct submodule_entry *entry)
60 free((void *) entry->config->path);
61 free((void *) entry->config->name);
62 free((void *) entry->config->branch);
63 free((void *) entry->config->update_strategy.command);
64 free(entry->config);
67 static void cache_free(struct submodule_cache *cache)
69 struct hashmap_iter iter;
70 struct submodule_entry *entry;
73 * We iterate over the name hash here to be symmetric with the
74 * allocation of struct submodule entries. Each is allocated by
75 * their .gitmodule blob sha1 and submodule name.
77 hashmap_iter_init(&cache->for_name, &iter);
78 while ((entry = hashmap_iter_next(&iter)))
79 free_one_config(entry);
81 hashmap_free(&cache->for_path, 1);
82 hashmap_free(&cache->for_name, 1);
85 static unsigned int hash_sha1_string(const unsigned char *sha1,
86 const char *string)
88 return memhash(sha1, 20) + strhash(string);
91 static void cache_put_path(struct submodule_cache *cache,
92 struct submodule *submodule)
94 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
95 submodule->path);
96 struct submodule_entry *e = xmalloc(sizeof(*e));
97 hashmap_entry_init(e, hash);
98 e->config = submodule;
99 hashmap_put(&cache->for_path, e);
102 static void cache_remove_path(struct submodule_cache *cache,
103 struct submodule *submodule)
105 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
106 submodule->path);
107 struct submodule_entry e;
108 struct submodule_entry *removed;
109 hashmap_entry_init(&e, hash);
110 e.config = submodule;
111 removed = hashmap_remove(&cache->for_path, &e, NULL);
112 free(removed);
115 static void cache_add(struct submodule_cache *cache,
116 struct submodule *submodule)
118 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
119 submodule->name);
120 struct submodule_entry *e = xmalloc(sizeof(*e));
121 hashmap_entry_init(e, hash);
122 e->config = submodule;
123 hashmap_add(&cache->for_name, e);
126 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
127 const unsigned char *gitmodules_sha1, const char *path)
129 struct submodule_entry *entry;
130 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
131 struct submodule_entry key;
132 struct submodule key_config;
134 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
135 key_config.path = path;
137 hashmap_entry_init(&key, hash);
138 key.config = &key_config;
140 entry = hashmap_get(&cache->for_path, &key, NULL);
141 if (entry)
142 return entry->config;
143 return NULL;
146 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
147 const unsigned char *gitmodules_sha1, const char *name)
149 struct submodule_entry *entry;
150 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
151 struct submodule_entry key;
152 struct submodule key_config;
154 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
155 key_config.name = name;
157 hashmap_entry_init(&key, hash);
158 key.config = &key_config;
160 entry = hashmap_get(&cache->for_name, &key, NULL);
161 if (entry)
162 return entry->config;
163 return NULL;
166 int check_submodule_name(const char *name)
168 /* Disallow empty names */
169 if (!*name)
170 return -1;
173 * Look for '..' as a path component. Check both '/' and '\\' as
174 * separators rather than is_dir_sep(), because we want the name rules
175 * to be consistent across platforms.
177 goto in_component; /* always start inside component */
178 while (*name) {
179 char c = *name++;
180 if (c == '/' || c == '\\') {
181 in_component:
182 if (name[0] == '.' && name[1] == '.' &&
183 (!name[2] || name[2] == '/' || name[2] == '\\'))
184 return -1;
188 return 0;
191 static int name_and_item_from_var(const char *var, struct strbuf *name,
192 struct strbuf *item)
194 const char *subsection, *key;
195 int subsection_len, parse;
196 parse = parse_config_key(var, "submodule", &subsection,
197 &subsection_len, &key);
198 if (parse < 0 || !subsection)
199 return 0;
201 strbuf_add(name, subsection, subsection_len);
202 if (check_submodule_name(name->buf) < 0) {
203 warning(_("ignoring suspicious submodule name: %s"), name->buf);
204 strbuf_release(name);
205 return 0;
208 strbuf_addstr(item, key);
210 return 1;
213 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
214 const unsigned char *gitmodules_sha1, const char *name)
216 struct submodule *submodule;
217 struct strbuf name_buf = STRBUF_INIT;
219 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
220 if (submodule)
221 return submodule;
223 submodule = xmalloc(sizeof(*submodule));
225 strbuf_addstr(&name_buf, name);
226 submodule->name = strbuf_detach(&name_buf, NULL);
228 submodule->path = NULL;
229 submodule->url = NULL;
230 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
231 submodule->update_strategy.command = NULL;
232 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
233 submodule->ignore = NULL;
234 submodule->branch = NULL;
235 submodule->recommend_shallow = -1;
237 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
239 cache_add(cache, submodule);
241 return submodule;
244 static int parse_fetch_recurse(const char *opt, const char *arg,
245 int die_on_error)
247 switch (git_config_maybe_bool(opt, arg)) {
248 case 1:
249 return RECURSE_SUBMODULES_ON;
250 case 0:
251 return RECURSE_SUBMODULES_OFF;
252 default:
253 if (!strcmp(arg, "on-demand"))
254 return RECURSE_SUBMODULES_ON_DEMAND;
256 if (die_on_error)
257 die("bad %s argument: %s", opt, arg);
258 else
259 return RECURSE_SUBMODULES_ERROR;
263 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
265 return parse_fetch_recurse(opt, arg, 1);
268 static int parse_update_recurse(const char *opt, const char *arg,
269 int die_on_error)
271 switch (git_config_maybe_bool(opt, arg)) {
272 case 1:
273 return RECURSE_SUBMODULES_ON;
274 case 0:
275 return RECURSE_SUBMODULES_OFF;
276 default:
277 if (die_on_error)
278 die("bad %s argument: %s", opt, arg);
279 return RECURSE_SUBMODULES_ERROR;
283 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
285 return parse_update_recurse(opt, arg, 1);
288 static int parse_push_recurse(const char *opt, const char *arg,
289 int die_on_error)
291 switch (git_config_maybe_bool(opt, arg)) {
292 case 1:
293 /* There's no simple "on" value when pushing */
294 if (die_on_error)
295 die("bad %s argument: %s", opt, arg);
296 else
297 return RECURSE_SUBMODULES_ERROR;
298 case 0:
299 return RECURSE_SUBMODULES_OFF;
300 default:
301 if (!strcmp(arg, "on-demand"))
302 return RECURSE_SUBMODULES_ON_DEMAND;
303 else if (!strcmp(arg, "check"))
304 return RECURSE_SUBMODULES_CHECK;
305 else if (!strcmp(arg, "only"))
306 return RECURSE_SUBMODULES_ONLY;
307 else if (die_on_error)
308 die("bad %s argument: %s", opt, arg);
309 else
310 return RECURSE_SUBMODULES_ERROR;
314 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
316 return parse_push_recurse(opt, arg, 1);
319 static void warn_multiple_config(const unsigned char *treeish_name,
320 const char *name, const char *option)
322 const char *commit_string = "WORKTREE";
323 if (treeish_name)
324 commit_string = sha1_to_hex(treeish_name);
325 warning("%s:.gitmodules, multiple configurations found for "
326 "'submodule.%s.%s'. Skipping second one!",
327 commit_string, name, option);
330 struct parse_config_parameter {
331 struct submodule_cache *cache;
332 const unsigned char *treeish_name;
333 const unsigned char *gitmodules_sha1;
334 int overwrite;
337 static int parse_config(const char *var, const char *value, void *data)
339 struct parse_config_parameter *me = data;
340 struct submodule *submodule;
341 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
342 int ret = 0;
344 /* this also ensures that we only parse submodule entries */
345 if (!name_and_item_from_var(var, &name, &item))
346 return 0;
348 submodule = lookup_or_create_by_name(me->cache,
349 me->gitmodules_sha1,
350 name.buf);
352 if (!strcmp(item.buf, "path")) {
353 if (!value)
354 ret = config_error_nonbool(var);
355 else if (!me->overwrite && submodule->path)
356 warn_multiple_config(me->treeish_name, submodule->name,
357 "path");
358 else {
359 if (submodule->path)
360 cache_remove_path(me->cache, submodule);
361 free((void *) submodule->path);
362 submodule->path = xstrdup(value);
363 cache_put_path(me->cache, submodule);
365 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
366 /* when parsing worktree configurations we can die early */
367 int die_on_error = is_null_sha1(me->gitmodules_sha1);
368 if (!me->overwrite &&
369 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
370 warn_multiple_config(me->treeish_name, submodule->name,
371 "fetchrecursesubmodules");
372 else
373 submodule->fetch_recurse = parse_fetch_recurse(
374 var, value,
375 die_on_error);
376 } else if (!strcmp(item.buf, "ignore")) {
377 if (!value)
378 ret = config_error_nonbool(var);
379 else if (!me->overwrite && submodule->ignore)
380 warn_multiple_config(me->treeish_name, submodule->name,
381 "ignore");
382 else if (strcmp(value, "untracked") &&
383 strcmp(value, "dirty") &&
384 strcmp(value, "all") &&
385 strcmp(value, "none"))
386 warning("Invalid parameter '%s' for config option "
387 "'submodule.%s.ignore'", value, name.buf);
388 else {
389 free((void *) submodule->ignore);
390 submodule->ignore = xstrdup(value);
392 } else if (!strcmp(item.buf, "url")) {
393 if (!value) {
394 ret = config_error_nonbool(var);
395 } else if (!me->overwrite && submodule->url) {
396 warn_multiple_config(me->treeish_name, submodule->name,
397 "url");
398 } else {
399 free((void *) submodule->url);
400 submodule->url = xstrdup(value);
402 } else if (!strcmp(item.buf, "update")) {
403 if (!value)
404 ret = config_error_nonbool(var);
405 else if (!me->overwrite &&
406 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
407 warn_multiple_config(me->treeish_name, submodule->name,
408 "update");
409 else if (parse_submodule_update_strategy(value,
410 &submodule->update_strategy) < 0)
411 die(_("invalid value for %s"), var);
412 } else if (!strcmp(item.buf, "shallow")) {
413 if (!me->overwrite && submodule->recommend_shallow != -1)
414 warn_multiple_config(me->treeish_name, submodule->name,
415 "shallow");
416 else
417 submodule->recommend_shallow =
418 git_config_bool(var, value);
419 } else if (!strcmp(item.buf, "branch")) {
420 if (!me->overwrite && submodule->branch)
421 warn_multiple_config(me->treeish_name, submodule->name,
422 "branch");
423 else {
424 free((void *)submodule->branch);
425 submodule->branch = xstrdup(value);
429 strbuf_release(&name);
430 strbuf_release(&item);
432 return ret;
435 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
436 unsigned char *gitmodules_sha1,
437 struct strbuf *rev)
439 int ret = 0;
441 if (is_null_sha1(treeish_name)) {
442 hashclr(gitmodules_sha1);
443 return 1;
446 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
447 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
448 ret = 1;
450 return ret;
453 /* This does a lookup of a submodule configuration by name or by path
454 * (key) with on-demand reading of the appropriate .gitmodules from
455 * revisions.
457 static const struct submodule *config_from(struct submodule_cache *cache,
458 const unsigned char *treeish_name, const char *key,
459 enum lookup_type lookup_type)
461 struct strbuf rev = STRBUF_INIT;
462 unsigned long config_size;
463 char *config = NULL;
464 unsigned char sha1[20];
465 enum object_type type;
466 const struct submodule *submodule = NULL;
467 struct parse_config_parameter parameter;
470 * If any parameter except the cache is a NULL pointer just
471 * return the first submodule. Can be used to check whether
472 * there are any submodules parsed.
474 if (!treeish_name || !key) {
475 struct hashmap_iter iter;
476 struct submodule_entry *entry;
478 entry = hashmap_iter_first(&cache->for_name, &iter);
479 if (!entry)
480 return NULL;
481 return entry->config;
484 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
485 goto out;
487 switch (lookup_type) {
488 case lookup_name:
489 submodule = cache_lookup_name(cache, sha1, key);
490 break;
491 case lookup_path:
492 submodule = cache_lookup_path(cache, sha1, key);
493 break;
495 if (submodule)
496 goto out;
498 config = read_sha1_file(sha1, &type, &config_size);
499 if (!config || type != OBJ_BLOB)
500 goto out;
502 /* fill the submodule config into the cache */
503 parameter.cache = cache;
504 parameter.treeish_name = treeish_name;
505 parameter.gitmodules_sha1 = sha1;
506 parameter.overwrite = 0;
507 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
508 config, config_size, &parameter);
509 strbuf_release(&rev);
510 free(config);
512 switch (lookup_type) {
513 case lookup_name:
514 return cache_lookup_name(cache, sha1, key);
515 case lookup_path:
516 return cache_lookup_path(cache, sha1, key);
517 default:
518 return NULL;
521 out:
522 strbuf_release(&rev);
523 free(config);
524 return submodule;
527 static void ensure_cache_init(void)
529 if (is_cache_init)
530 return;
532 cache_init(&the_submodule_cache);
533 is_cache_init = 1;
536 int parse_submodule_config_option(const char *var, const char *value)
538 struct parse_config_parameter parameter;
539 parameter.cache = &the_submodule_cache;
540 parameter.treeish_name = NULL;
541 parameter.gitmodules_sha1 = null_sha1;
542 parameter.overwrite = 1;
544 ensure_cache_init();
545 return parse_config(var, value, &parameter);
548 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
549 const char *name)
551 ensure_cache_init();
552 return config_from(&the_submodule_cache, treeish_name, name, lookup_name);
555 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
556 const char *path)
558 ensure_cache_init();
559 return config_from(&the_submodule_cache, treeish_name, path, lookup_path);
562 void submodule_free(void)
564 cache_free(&the_submodule_cache);
565 is_cache_init = 0;