Merge branch 'sb/hashmap-customize-comparison'
[git/raj.git] / submodule-config.c
blobeeb154a894d6bfdec877d94d5922438b5f934730
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 void *unused_cmp_data,
37 const struct submodule_entry *a,
38 const struct submodule_entry *b,
39 const void *unused_keydata)
41 return strcmp(a->config->path, b->config->path) ||
42 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
45 static int config_name_cmp(const void *unused_cmp_data,
46 const struct submodule_entry *a,
47 const struct submodule_entry *b,
48 const void *unused_keydata)
50 return strcmp(a->config->name, b->config->name) ||
51 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
54 static struct submodule_cache *submodule_cache_alloc(void)
56 return xcalloc(1, sizeof(struct submodule_cache));
59 static void submodule_cache_init(struct submodule_cache *cache)
61 hashmap_init(&cache->for_path, (hashmap_cmp_fn) config_path_cmp, NULL, 0);
62 hashmap_init(&cache->for_name, (hashmap_cmp_fn) config_name_cmp, NULL, 0);
63 cache->initialized = 1;
66 static void free_one_config(struct submodule_entry *entry)
68 free((void *) entry->config->path);
69 free((void *) entry->config->name);
70 free((void *) entry->config->branch);
71 free((void *) entry->config->update_strategy.command);
72 free(entry->config);
75 static void submodule_cache_clear(struct submodule_cache *cache)
77 struct hashmap_iter iter;
78 struct submodule_entry *entry;
80 if (!cache->initialized)
81 return;
84 * We iterate over the name hash here to be symmetric with the
85 * allocation of struct submodule entries. Each is allocated by
86 * their .gitmodule blob sha1 and submodule name.
88 hashmap_iter_init(&cache->for_name, &iter);
89 while ((entry = hashmap_iter_next(&iter)))
90 free_one_config(entry);
92 hashmap_free(&cache->for_path, 1);
93 hashmap_free(&cache->for_name, 1);
94 cache->initialized = 0;
97 void submodule_cache_free(struct submodule_cache *cache)
99 submodule_cache_clear(cache);
100 free(cache);
103 static unsigned int hash_sha1_string(const unsigned char *sha1,
104 const char *string)
106 return memhash(sha1, 20) + strhash(string);
109 static void cache_put_path(struct submodule_cache *cache,
110 struct submodule *submodule)
112 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
113 submodule->path);
114 struct submodule_entry *e = xmalloc(sizeof(*e));
115 hashmap_entry_init(e, hash);
116 e->config = submodule;
117 hashmap_put(&cache->for_path, e);
120 static void cache_remove_path(struct submodule_cache *cache,
121 struct submodule *submodule)
123 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
124 submodule->path);
125 struct submodule_entry e;
126 struct submodule_entry *removed;
127 hashmap_entry_init(&e, hash);
128 e.config = submodule;
129 removed = hashmap_remove(&cache->for_path, &e, NULL);
130 free(removed);
133 static void cache_add(struct submodule_cache *cache,
134 struct submodule *submodule)
136 unsigned int hash = hash_sha1_string(submodule->gitmodules_sha1,
137 submodule->name);
138 struct submodule_entry *e = xmalloc(sizeof(*e));
139 hashmap_entry_init(e, hash);
140 e->config = submodule;
141 hashmap_add(&cache->for_name, e);
144 static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
145 const unsigned char *gitmodules_sha1, const char *path)
147 struct submodule_entry *entry;
148 unsigned int hash = hash_sha1_string(gitmodules_sha1, path);
149 struct submodule_entry key;
150 struct submodule key_config;
152 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
153 key_config.path = path;
155 hashmap_entry_init(&key, hash);
156 key.config = &key_config;
158 entry = hashmap_get(&cache->for_path, &key, NULL);
159 if (entry)
160 return entry->config;
161 return NULL;
164 static struct submodule *cache_lookup_name(struct submodule_cache *cache,
165 const unsigned char *gitmodules_sha1, const char *name)
167 struct submodule_entry *entry;
168 unsigned int hash = hash_sha1_string(gitmodules_sha1, name);
169 struct submodule_entry key;
170 struct submodule key_config;
172 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
173 key_config.name = name;
175 hashmap_entry_init(&key, hash);
176 key.config = &key_config;
178 entry = hashmap_get(&cache->for_name, &key, NULL);
179 if (entry)
180 return entry->config;
181 return NULL;
184 static int name_and_item_from_var(const char *var, struct strbuf *name,
185 struct strbuf *item)
187 const char *subsection, *key;
188 int subsection_len, parse;
189 parse = parse_config_key(var, "submodule", &subsection,
190 &subsection_len, &key);
191 if (parse < 0 || !subsection)
192 return 0;
194 strbuf_add(name, subsection, subsection_len);
195 strbuf_addstr(item, key);
197 return 1;
200 static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
201 const unsigned char *gitmodules_sha1, const char *name)
203 struct submodule *submodule;
204 struct strbuf name_buf = STRBUF_INIT;
206 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
207 if (submodule)
208 return submodule;
210 submodule = xmalloc(sizeof(*submodule));
212 strbuf_addstr(&name_buf, name);
213 submodule->name = strbuf_detach(&name_buf, NULL);
215 submodule->path = NULL;
216 submodule->url = NULL;
217 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
218 submodule->update_strategy.command = NULL;
219 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
220 submodule->ignore = NULL;
221 submodule->branch = NULL;
222 submodule->recommend_shallow = -1;
224 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
226 cache_add(cache, submodule);
228 return submodule;
231 static int parse_fetch_recurse(const char *opt, const char *arg,
232 int die_on_error)
234 switch (git_config_maybe_bool(opt, arg)) {
235 case 1:
236 return RECURSE_SUBMODULES_ON;
237 case 0:
238 return RECURSE_SUBMODULES_OFF;
239 default:
240 if (!strcmp(arg, "on-demand"))
241 return RECURSE_SUBMODULES_ON_DEMAND;
243 if (die_on_error)
244 die("bad %s argument: %s", opt, arg);
245 else
246 return RECURSE_SUBMODULES_ERROR;
250 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
252 return parse_fetch_recurse(opt, arg, 1);
255 static int parse_update_recurse(const char *opt, const char *arg,
256 int die_on_error)
258 switch (git_config_maybe_bool(opt, arg)) {
259 case 1:
260 return RECURSE_SUBMODULES_ON;
261 case 0:
262 return RECURSE_SUBMODULES_OFF;
263 default:
264 if (die_on_error)
265 die("bad %s argument: %s", opt, arg);
266 return RECURSE_SUBMODULES_ERROR;
270 int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
272 return parse_update_recurse(opt, arg, 1);
275 static int parse_push_recurse(const char *opt, const char *arg,
276 int die_on_error)
278 switch (git_config_maybe_bool(opt, arg)) {
279 case 1:
280 /* There's no simple "on" value when pushing */
281 if (die_on_error)
282 die("bad %s argument: %s", opt, arg);
283 else
284 return RECURSE_SUBMODULES_ERROR;
285 case 0:
286 return RECURSE_SUBMODULES_OFF;
287 default:
288 if (!strcmp(arg, "on-demand"))
289 return RECURSE_SUBMODULES_ON_DEMAND;
290 else if (!strcmp(arg, "check"))
291 return RECURSE_SUBMODULES_CHECK;
292 else if (!strcmp(arg, "only"))
293 return RECURSE_SUBMODULES_ONLY;
294 else if (die_on_error)
295 die("bad %s argument: %s", opt, arg);
296 else
297 return RECURSE_SUBMODULES_ERROR;
301 int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
303 return parse_push_recurse(opt, arg, 1);
306 static void warn_multiple_config(const unsigned char *treeish_name,
307 const char *name, const char *option)
309 const char *commit_string = "WORKTREE";
310 if (treeish_name)
311 commit_string = sha1_to_hex(treeish_name);
312 warning("%s:.gitmodules, multiple configurations found for "
313 "'submodule.%s.%s'. Skipping second one!",
314 commit_string, name, option);
317 struct parse_config_parameter {
318 struct submodule_cache *cache;
319 const unsigned char *treeish_name;
320 const unsigned char *gitmodules_sha1;
321 int overwrite;
324 static int parse_config(const char *var, const char *value, void *data)
326 struct parse_config_parameter *me = data;
327 struct submodule *submodule;
328 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
329 int ret = 0;
331 /* this also ensures that we only parse submodule entries */
332 if (!name_and_item_from_var(var, &name, &item))
333 return 0;
335 submodule = lookup_or_create_by_name(me->cache,
336 me->gitmodules_sha1,
337 name.buf);
339 if (!strcmp(item.buf, "path")) {
340 if (!value)
341 ret = config_error_nonbool(var);
342 else if (!me->overwrite && submodule->path)
343 warn_multiple_config(me->treeish_name, submodule->name,
344 "path");
345 else {
346 if (submodule->path)
347 cache_remove_path(me->cache, submodule);
348 free((void *) submodule->path);
349 submodule->path = xstrdup(value);
350 cache_put_path(me->cache, submodule);
352 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
353 /* when parsing worktree configurations we can die early */
354 int die_on_error = is_null_sha1(me->gitmodules_sha1);
355 if (!me->overwrite &&
356 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
357 warn_multiple_config(me->treeish_name, submodule->name,
358 "fetchrecursesubmodules");
359 else
360 submodule->fetch_recurse = parse_fetch_recurse(
361 var, value,
362 die_on_error);
363 } else if (!strcmp(item.buf, "ignore")) {
364 if (!value)
365 ret = config_error_nonbool(var);
366 else if (!me->overwrite && submodule->ignore)
367 warn_multiple_config(me->treeish_name, submodule->name,
368 "ignore");
369 else if (strcmp(value, "untracked") &&
370 strcmp(value, "dirty") &&
371 strcmp(value, "all") &&
372 strcmp(value, "none"))
373 warning("Invalid parameter '%s' for config option "
374 "'submodule.%s.ignore'", value, name.buf);
375 else {
376 free((void *) submodule->ignore);
377 submodule->ignore = xstrdup(value);
379 } else if (!strcmp(item.buf, "url")) {
380 if (!value) {
381 ret = config_error_nonbool(var);
382 } else if (!me->overwrite && submodule->url) {
383 warn_multiple_config(me->treeish_name, submodule->name,
384 "url");
385 } else {
386 free((void *) submodule->url);
387 submodule->url = xstrdup(value);
389 } else if (!strcmp(item.buf, "update")) {
390 if (!value)
391 ret = config_error_nonbool(var);
392 else if (!me->overwrite &&
393 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
394 warn_multiple_config(me->treeish_name, submodule->name,
395 "update");
396 else if (parse_submodule_update_strategy(value,
397 &submodule->update_strategy) < 0)
398 die(_("invalid value for %s"), var);
399 } else if (!strcmp(item.buf, "shallow")) {
400 if (!me->overwrite && submodule->recommend_shallow != -1)
401 warn_multiple_config(me->treeish_name, submodule->name,
402 "shallow");
403 else
404 submodule->recommend_shallow =
405 git_config_bool(var, value);
406 } else if (!strcmp(item.buf, "branch")) {
407 if (!me->overwrite && submodule->branch)
408 warn_multiple_config(me->treeish_name, submodule->name,
409 "branch");
410 else {
411 free((void *)submodule->branch);
412 submodule->branch = xstrdup(value);
416 strbuf_release(&name);
417 strbuf_release(&item);
419 return ret;
422 int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
423 unsigned char *gitmodules_sha1,
424 struct strbuf *rev)
426 int ret = 0;
428 if (is_null_sha1(treeish_name)) {
429 hashclr(gitmodules_sha1);
430 return 1;
433 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
434 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
435 ret = 1;
437 return ret;
440 /* This does a lookup of a submodule configuration by name or by path
441 * (key) with on-demand reading of the appropriate .gitmodules from
442 * revisions.
444 static const struct submodule *config_from(struct submodule_cache *cache,
445 const unsigned char *treeish_name, const char *key,
446 enum lookup_type lookup_type)
448 struct strbuf rev = STRBUF_INIT;
449 unsigned long config_size;
450 char *config = NULL;
451 unsigned char sha1[20];
452 enum object_type type;
453 const struct submodule *submodule = NULL;
454 struct parse_config_parameter parameter;
457 * If any parameter except the cache is a NULL pointer just
458 * return the first submodule. Can be used to check whether
459 * there are any submodules parsed.
461 if (!treeish_name || !key) {
462 struct hashmap_iter iter;
463 struct submodule_entry *entry;
465 entry = hashmap_iter_first(&cache->for_name, &iter);
466 if (!entry)
467 return NULL;
468 return entry->config;
471 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
472 goto out;
474 switch (lookup_type) {
475 case lookup_name:
476 submodule = cache_lookup_name(cache, sha1, key);
477 break;
478 case lookup_path:
479 submodule = cache_lookup_path(cache, sha1, key);
480 break;
482 if (submodule)
483 goto out;
485 config = read_sha1_file(sha1, &type, &config_size);
486 if (!config || type != OBJ_BLOB)
487 goto out;
489 /* fill the submodule config into the cache */
490 parameter.cache = cache;
491 parameter.treeish_name = treeish_name;
492 parameter.gitmodules_sha1 = sha1;
493 parameter.overwrite = 0;
494 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
495 config, config_size, &parameter);
496 strbuf_release(&rev);
497 free(config);
499 switch (lookup_type) {
500 case lookup_name:
501 return cache_lookup_name(cache, sha1, key);
502 case lookup_path:
503 return cache_lookup_path(cache, sha1, key);
504 default:
505 return NULL;
508 out:
509 strbuf_release(&rev);
510 free(config);
511 return submodule;
514 static void submodule_cache_check_init(struct repository *repo)
516 if (repo->submodule_cache && repo->submodule_cache->initialized)
517 return;
519 if (!repo->submodule_cache)
520 repo->submodule_cache = submodule_cache_alloc();
522 submodule_cache_init(repo->submodule_cache);
525 int submodule_config_option(struct repository *repo,
526 const char *var, const char *value)
528 struct parse_config_parameter parameter;
530 submodule_cache_check_init(repo);
532 parameter.cache = repo->submodule_cache;
533 parameter.treeish_name = NULL;
534 parameter.gitmodules_sha1 = null_sha1;
535 parameter.overwrite = 1;
537 return parse_config(var, value, &parameter);
540 int parse_submodule_config_option(const char *var, const char *value)
542 return submodule_config_option(the_repository, var, value);
545 const struct submodule *submodule_from_name(const unsigned char *treeish_name,
546 const char *name)
548 submodule_cache_check_init(the_repository);
549 return config_from(the_repository->submodule_cache, treeish_name, name, lookup_name);
552 const struct submodule *submodule_from_path(const unsigned char *treeish_name,
553 const char *path)
555 submodule_cache_check_init(the_repository);
556 return config_from(the_repository->submodule_cache, treeish_name, path, lookup_path);
559 const struct submodule *submodule_from_cache(struct repository *repo,
560 const unsigned char *treeish_name,
561 const char *key)
563 submodule_cache_check_init(repo);
564 return config_from(repo->submodule_cache, treeish_name,
565 key, lookup_path);
568 void submodule_free(void)
570 if (the_repository->submodule_cache)
571 submodule_cache_clear(the_repository->submodule_cache);