make is_submodule_populated gently
[git/git.git] / submodule-config.c
CommitLineData
959b5455
HV
1#include "cache.h"
2#include "submodule-config.h"
3#include "submodule.h"
4#include "strbuf.h"
5
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
13 */
14struct submodule_cache {
15 struct hashmap for_path;
16 struct hashmap for_name;
17};
18
19/*
20 * thin wrapper struct needed to insert 'struct submodule' entries to
21 * the hashmap
22 */
23struct submodule_entry {
24 struct hashmap_entry ent;
25 struct submodule *config;
26};
27
28enum lookup_type {
29 lookup_name,
30 lookup_path
31};
32
99dab168 33static struct submodule_cache the_submodule_cache;
959b5455
HV
34static int is_cache_init;
35
36static int config_path_cmp(const struct submodule_entry *a,
37 const struct submodule_entry *b,
38 const void *unused)
39{
40 return strcmp(a->config->path, b->config->path) ||
41 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
42}
43
44static int config_name_cmp(const struct submodule_entry *a,
45 const struct submodule_entry *b,
46 const void *unused)
47{
48 return strcmp(a->config->name, b->config->name) ||
49 hashcmp(a->config->gitmodules_sha1, b->config->gitmodules_sha1);
50}
51
52static void cache_init(struct submodule_cache *cache)
53{
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);
56}
57
58static void free_one_config(struct submodule_entry *entry)
59{
60 free((void *) entry->config->path);
61 free((void *) entry->config->name);
b5944f34 62 free((void *) entry->config->branch);
ea2fa5a3 63 free((void *) entry->config->update_strategy.command);
959b5455
HV
64 free(entry->config);
65}
66
67static void cache_free(struct submodule_cache *cache)
68{
69 struct hashmap_iter iter;
70 struct submodule_entry *entry;
71
72 /*
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.
76 */
77 hashmap_iter_init(&cache->for_name, &iter);
78 while ((entry = hashmap_iter_next(&iter)))
79 free_one_config(entry);
80
81 hashmap_free(&cache->for_path, 1);
82 hashmap_free(&cache->for_name, 1);
83}
84
85static unsigned int hash_sha1_string(const unsigned char *sha1,
86 const char *string)
87{
88 return memhash(sha1, 20) + strhash(string);
89}
90
91static void cache_put_path(struct submodule_cache *cache,
92 struct submodule *submodule)
93{
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);
100}
101
102static void cache_remove_path(struct submodule_cache *cache,
103 struct submodule *submodule)
104{
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);
113}
114
115static void cache_add(struct submodule_cache *cache,
116 struct submodule *submodule)
117{
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);
124}
125
126static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
127 const unsigned char *gitmodules_sha1, const char *path)
128{
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;
133
134 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
135 key_config.path = path;
136
137 hashmap_entry_init(&key, hash);
138 key.config = &key_config;
139
140 entry = hashmap_get(&cache->for_path, &key, NULL);
141 if (entry)
142 return entry->config;
143 return NULL;
144}
145
146static struct submodule *cache_lookup_name(struct submodule_cache *cache,
147 const unsigned char *gitmodules_sha1, const char *name)
148{
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;
153
154 hashcpy(key_config.gitmodules_sha1, gitmodules_sha1);
155 key_config.name = name;
156
157 hashmap_entry_init(&key, hash);
158 key.config = &key_config;
159
160 entry = hashmap_get(&cache->for_name, &key, NULL);
161 if (entry)
162 return entry->config;
163 return NULL;
164}
165
166static int name_and_item_from_var(const char *var, struct strbuf *name,
167 struct strbuf *item)
168{
169 const char *subsection, *key;
170 int subsection_len, parse;
171 parse = parse_config_key(var, "submodule", &subsection,
172 &subsection_len, &key);
173 if (parse < 0 || !subsection)
174 return 0;
175
176 strbuf_add(name, subsection, subsection_len);
177 strbuf_addstr(item, key);
178
179 return 1;
180}
181
182static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
183 const unsigned char *gitmodules_sha1, const char *name)
184{
185 struct submodule *submodule;
186 struct strbuf name_buf = STRBUF_INIT;
187
188 submodule = cache_lookup_name(cache, gitmodules_sha1, name);
189 if (submodule)
190 return submodule;
191
192 submodule = xmalloc(sizeof(*submodule));
193
194 strbuf_addstr(&name_buf, name);
195 submodule->name = strbuf_detach(&name_buf, NULL);
196
197 submodule->path = NULL;
198 submodule->url = NULL;
ea2fa5a3
SB
199 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
200 submodule->update_strategy.command = NULL;
959b5455
HV
201 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
202 submodule->ignore = NULL;
b5944f34 203 submodule->branch = NULL;
37f52e93 204 submodule->recommend_shallow = -1;
959b5455
HV
205
206 hashcpy(submodule->gitmodules_sha1, gitmodules_sha1);
207
208 cache_add(cache, submodule);
209
210 return submodule;
211}
212
027771fc
HV
213static int parse_fetch_recurse(const char *opt, const char *arg,
214 int die_on_error)
215{
216 switch (git_config_maybe_bool(opt, arg)) {
217 case 1:
218 return RECURSE_SUBMODULES_ON;
219 case 0:
220 return RECURSE_SUBMODULES_OFF;
221 default:
222 if (!strcmp(arg, "on-demand"))
223 return RECURSE_SUBMODULES_ON_DEMAND;
224
225 if (die_on_error)
226 die("bad %s argument: %s", opt, arg);
227 else
228 return RECURSE_SUBMODULES_ERROR;
229 }
230}
231
232int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
233{
234 return parse_fetch_recurse(opt, arg, 1);
235}
236
b33a15b0
MC
237static int parse_push_recurse(const char *opt, const char *arg,
238 int die_on_error)
239{
240 switch (git_config_maybe_bool(opt, arg)) {
241 case 1:
242 /* There's no simple "on" value when pushing */
243 if (die_on_error)
244 die("bad %s argument: %s", opt, arg);
245 else
246 return RECURSE_SUBMODULES_ERROR;
247 case 0:
248 return RECURSE_SUBMODULES_OFF;
249 default:
250 if (!strcmp(arg, "on-demand"))
251 return RECURSE_SUBMODULES_ON_DEMAND;
252 else if (!strcmp(arg, "check"))
253 return RECURSE_SUBMODULES_CHECK;
6c656c3f
BW
254 else if (!strcmp(arg, "only"))
255 return RECURSE_SUBMODULES_ONLY;
b33a15b0
MC
256 else if (die_on_error)
257 die("bad %s argument: %s", opt, arg);
258 else
259 return RECURSE_SUBMODULES_ERROR;
260 }
261}
262
263int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
264{
265 return parse_push_recurse(opt, arg, 1);
266}
267
73c293bb 268static void warn_multiple_config(const unsigned char *treeish_name,
959b5455
HV
269 const char *name, const char *option)
270{
271 const char *commit_string = "WORKTREE";
73c293bb
SB
272 if (treeish_name)
273 commit_string = sha1_to_hex(treeish_name);
959b5455
HV
274 warning("%s:.gitmodules, multiple configurations found for "
275 "'submodule.%s.%s'. Skipping second one!",
276 commit_string, name, option);
277}
278
279struct parse_config_parameter {
280 struct submodule_cache *cache;
73c293bb 281 const unsigned char *treeish_name;
959b5455
HV
282 const unsigned char *gitmodules_sha1;
283 int overwrite;
284};
285
286static int parse_config(const char *var, const char *value, void *data)
287{
288 struct parse_config_parameter *me = data;
289 struct submodule *submodule;
290 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
291 int ret = 0;
292
293 /* this also ensures that we only parse submodule entries */
294 if (!name_and_item_from_var(var, &name, &item))
295 return 0;
296
147875fd
SB
297 submodule = lookup_or_create_by_name(me->cache,
298 me->gitmodules_sha1,
299 name.buf);
959b5455
HV
300
301 if (!strcmp(item.buf, "path")) {
147875fd 302 if (!value)
959b5455 303 ret = config_error_nonbool(var);
f73da110 304 else if (!me->overwrite && submodule->path)
73c293bb 305 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 306 "path");
147875fd
SB
307 else {
308 if (submodule->path)
309 cache_remove_path(me->cache, submodule);
310 free((void *) submodule->path);
311 submodule->path = xstrdup(value);
312 cache_put_path(me->cache, submodule);
959b5455 313 }
959b5455 314 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
027771fc
HV
315 /* when parsing worktree configurations we can die early */
316 int die_on_error = is_null_sha1(me->gitmodules_sha1);
959b5455 317 if (!me->overwrite &&
147875fd 318 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
73c293bb 319 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 320 "fetchrecursesubmodules");
147875fd
SB
321 else
322 submodule->fetch_recurse = parse_fetch_recurse(
323 var, value,
027771fc 324 die_on_error);
959b5455 325 } else if (!strcmp(item.buf, "ignore")) {
147875fd
SB
326 if (!value)
327 ret = config_error_nonbool(var);
f73da110 328 else if (!me->overwrite && submodule->ignore)
73c293bb 329 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 330 "ignore");
147875fd
SB
331 else if (strcmp(value, "untracked") &&
332 strcmp(value, "dirty") &&
333 strcmp(value, "all") &&
334 strcmp(value, "none"))
959b5455
HV
335 warning("Invalid parameter '%s' for config option "
336 "'submodule.%s.ignore'", value, var);
147875fd
SB
337 else {
338 free((void *) submodule->ignore);
339 submodule->ignore = xstrdup(value);
959b5455 340 }
959b5455 341 } else if (!strcmp(item.buf, "url")) {
959b5455
HV
342 if (!value) {
343 ret = config_error_nonbool(var);
f73da110 344 } else if (!me->overwrite && submodule->url) {
73c293bb 345 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 346 "url");
147875fd
SB
347 } else {
348 free((void *) submodule->url);
349 submodule->url = xstrdup(value);
959b5455 350 }
ea2fa5a3
SB
351 } else if (!strcmp(item.buf, "update")) {
352 if (!value)
353 ret = config_error_nonbool(var);
354 else if (!me->overwrite &&
355 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
73c293bb 356 warn_multiple_config(me->treeish_name, submodule->name,
ea2fa5a3
SB
357 "update");
358 else if (parse_submodule_update_strategy(value,
359 &submodule->update_strategy) < 0)
360 die(_("invalid value for %s"), var);
37f52e93
SB
361 } else if (!strcmp(item.buf, "shallow")) {
362 if (!me->overwrite && submodule->recommend_shallow != -1)
73c293bb 363 warn_multiple_config(me->treeish_name, submodule->name,
37f52e93 364 "shallow");
b5944f34 365 else
37f52e93
SB
366 submodule->recommend_shallow =
367 git_config_bool(var, value);
b5944f34
SB
368 } else if (!strcmp(item.buf, "branch")) {
369 if (!me->overwrite && submodule->branch)
73c293bb 370 warn_multiple_config(me->treeish_name, submodule->name,
b5944f34
SB
371 "branch");
372 else {
373 free((void *)submodule->branch);
374 submodule->branch = xstrdup(value);
37f52e93 375 }
959b5455
HV
376 }
377
959b5455
HV
378 strbuf_release(&name);
379 strbuf_release(&item);
380
381 return ret;
382}
383
55d128ae 384int gitmodule_sha1_from_commit(const unsigned char *treeish_name,
514dea90
HV
385 unsigned char *gitmodules_sha1,
386 struct strbuf *rev)
959b5455 387{
959b5455
HV
388 int ret = 0;
389
73c293bb 390 if (is_null_sha1(treeish_name)) {
f449198e 391 hashclr(gitmodules_sha1);
959b5455
HV
392 return 1;
393 }
394
73c293bb 395 strbuf_addf(rev, "%s:.gitmodules", sha1_to_hex(treeish_name));
514dea90 396 if (get_sha1(rev->buf, gitmodules_sha1) >= 0)
959b5455
HV
397 ret = 1;
398
959b5455
HV
399 return ret;
400}
401
402/* This does a lookup of a submodule configuration by name or by path
403 * (key) with on-demand reading of the appropriate .gitmodules from
404 * revisions.
405 */
406static const struct submodule *config_from(struct submodule_cache *cache,
73c293bb 407 const unsigned char *treeish_name, const char *key,
959b5455
HV
408 enum lookup_type lookup_type)
409{
410 struct strbuf rev = STRBUF_INIT;
411 unsigned long config_size;
0918e250 412 char *config = NULL;
959b5455
HV
413 unsigned char sha1[20];
414 enum object_type type;
415 const struct submodule *submodule = NULL;
416 struct parse_config_parameter parameter;
417
418 /*
419 * If any parameter except the cache is a NULL pointer just
420 * return the first submodule. Can be used to check whether
421 * there are any submodules parsed.
422 */
73c293bb 423 if (!treeish_name || !key) {
959b5455
HV
424 struct hashmap_iter iter;
425 struct submodule_entry *entry;
426
01d98e8a 427 entry = hashmap_iter_first(&cache->for_name, &iter);
959b5455
HV
428 if (!entry)
429 return NULL;
430 return entry->config;
431 }
432
73c293bb 433 if (!gitmodule_sha1_from_commit(treeish_name, sha1, &rev))
0918e250 434 goto out;
959b5455
HV
435
436 switch (lookup_type) {
437 case lookup_name:
438 submodule = cache_lookup_name(cache, sha1, key);
439 break;
440 case lookup_path:
441 submodule = cache_lookup_path(cache, sha1, key);
442 break;
443 }
444 if (submodule)
0918e250 445 goto out;
959b5455
HV
446
447 config = read_sha1_file(sha1, &type, &config_size);
0918e250
HV
448 if (!config || type != OBJ_BLOB)
449 goto out;
959b5455
HV
450
451 /* fill the submodule config into the cache */
452 parameter.cache = cache;
73c293bb 453 parameter.treeish_name = treeish_name;
959b5455
HV
454 parameter.gitmodules_sha1 = sha1;
455 parameter.overwrite = 0;
1b8132d9 456 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
473166b9 457 config, config_size, &parameter);
514dea90 458 strbuf_release(&rev);
959b5455
HV
459 free(config);
460
461 switch (lookup_type) {
462 case lookup_name:
463 return cache_lookup_name(cache, sha1, key);
464 case lookup_path:
465 return cache_lookup_path(cache, sha1, key);
466 default:
467 return NULL;
468 }
0918e250
HV
469
470out:
471 strbuf_release(&rev);
472 free(config);
473 return submodule;
959b5455
HV
474}
475
959b5455
HV
476static void ensure_cache_init(void)
477{
478 if (is_cache_init)
479 return;
480
99dab168 481 cache_init(&the_submodule_cache);
959b5455
HV
482 is_cache_init = 1;
483}
484
851e18c3
HV
485int parse_submodule_config_option(const char *var, const char *value)
486{
487 struct parse_config_parameter parameter;
99dab168 488 parameter.cache = &the_submodule_cache;
73c293bb 489 parameter.treeish_name = NULL;
851e18c3
HV
490 parameter.gitmodules_sha1 = null_sha1;
491 parameter.overwrite = 1;
492
493 ensure_cache_init();
494 return parse_config(var, value, &parameter);
495}
496
73c293bb 497const struct submodule *submodule_from_name(const unsigned char *treeish_name,
959b5455
HV
498 const char *name)
499{
500 ensure_cache_init();
73c293bb 501 return config_from(&the_submodule_cache, treeish_name, name, lookup_name);
959b5455
HV
502}
503
73c293bb 504const struct submodule *submodule_from_path(const unsigned char *treeish_name,
959b5455
HV
505 const char *path)
506{
507 ensure_cache_init();
73c293bb 508 return config_from(&the_submodule_cache, treeish_name, path, lookup_path);
959b5455
HV
509}
510
511void submodule_free(void)
512{
99dab168 513 cache_free(&the_submodule_cache);
959b5455
HV
514 is_cache_init = 0;
515}