]> git.ipfire.org Git - thirdparty/git.git/blame - submodule-config.c
Merge branch 'jk/escaped-wildcard-dwim'
[thirdparty/git.git] / submodule-config.c
CommitLineData
959b5455 1#include "cache.h"
76e9bdc4 2#include "dir.h"
bf12fcdf 3#include "repository.h"
b2141fc1 4#include "config.h"
959b5455
HV
5#include "submodule-config.h"
6#include "submodule.h"
7#include "strbuf.h"
cbd53a21 8#include "object-store.h"
886dc154 9#include "parse-options.h"
959b5455
HV
10
11/*
12 * submodule cache lookup structure
13 * There is one shared set of 'struct submodule' entries which can be
5aea9fe6 14 * looked up by their sha1 blob id of the .gitmodules file and either
959b5455
HV
15 * using path or name as key.
16 * for_path stores submodule entries with path as key
17 * for_name stores submodule entries with name as key
18 */
19struct submodule_cache {
20 struct hashmap for_path;
21 struct hashmap for_name;
bf12fcdf 22 unsigned initialized:1;
ff6f1f56 23 unsigned gitmodules_read:1;
959b5455
HV
24};
25
26/*
27 * thin wrapper struct needed to insert 'struct submodule' entries to
28 * the hashmap
29 */
30struct submodule_entry {
31 struct hashmap_entry ent;
32 struct submodule *config;
33};
34
35enum lookup_type {
36 lookup_name,
37 lookup_path
38};
39
7663cdc8 40static int config_path_cmp(const void *unused_cmp_data,
939af16e
EW
41 const struct hashmap_entry *eptr,
42 const struct hashmap_entry *entry_or_key,
7663cdc8 43 const void *unused_keydata)
959b5455 44{
939af16e
EW
45 const struct submodule_entry *a, *b;
46
47 a = container_of(eptr, const struct submodule_entry, ent);
48 b = container_of(entry_or_key, const struct submodule_entry, ent);
152cbdc6 49
959b5455 50 return strcmp(a->config->path, b->config->path) ||
9001dc2a 51 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
959b5455
HV
52}
53
7663cdc8 54static int config_name_cmp(const void *unused_cmp_data,
939af16e
EW
55 const struct hashmap_entry *eptr,
56 const struct hashmap_entry *entry_or_key,
7663cdc8 57 const void *unused_keydata)
959b5455 58{
939af16e
EW
59 const struct submodule_entry *a, *b;
60
61 a = container_of(eptr, const struct submodule_entry, ent);
62 b = container_of(entry_or_key, const struct submodule_entry, ent);
152cbdc6 63
959b5455 64 return strcmp(a->config->name, b->config->name) ||
9001dc2a 65 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
959b5455
HV
66}
67
bf12fcdf
BW
68static struct submodule_cache *submodule_cache_alloc(void)
69{
70 return xcalloc(1, sizeof(struct submodule_cache));
71}
72
73static void submodule_cache_init(struct submodule_cache *cache)
959b5455 74{
152cbdc6
SB
75 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
76 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
bf12fcdf 77 cache->initialized = 1;
959b5455
HV
78}
79
80static void free_one_config(struct submodule_entry *entry)
81{
82 free((void *) entry->config->path);
83 free((void *) entry->config->name);
b5944f34 84 free((void *) entry->config->branch);
ea2fa5a3 85 free((void *) entry->config->update_strategy.command);
959b5455
HV
86 free(entry->config);
87}
88
bf12fcdf 89static void submodule_cache_clear(struct submodule_cache *cache)
959b5455
HV
90{
91 struct hashmap_iter iter;
92 struct submodule_entry *entry;
93
bf12fcdf
BW
94 if (!cache->initialized)
95 return;
96
959b5455
HV
97 /*
98 * We iterate over the name hash here to be symmetric with the
99 * allocation of struct submodule entries. Each is allocated by
5aea9fe6 100 * their .gitmodules blob sha1 and submodule name.
959b5455 101 */
87571c3f 102 hashmap_for_each_entry(&cache->for_name, &iter, entry,
23dee69f 103 ent /* member name */)
959b5455
HV
104 free_one_config(entry);
105
c8e424c9
EW
106 hashmap_free_entries(&cache->for_path, struct submodule_entry, ent);
107 hashmap_free_entries(&cache->for_name, struct submodule_entry, ent);
bf12fcdf 108 cache->initialized = 0;
ff6f1f56 109 cache->gitmodules_read = 0;
bf12fcdf
BW
110}
111
112void submodule_cache_free(struct submodule_cache *cache)
113{
114 submodule_cache_clear(cache);
115 free(cache);
959b5455
HV
116}
117
34caab02 118static unsigned int hash_oid_string(const struct object_id *oid,
119 const char *string)
959b5455 120{
34caab02 121 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
959b5455
HV
122}
123
124static void cache_put_path(struct submodule_cache *cache,
125 struct submodule *submodule)
126{
34caab02 127 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
128 submodule->path);
959b5455 129 struct submodule_entry *e = xmalloc(sizeof(*e));
d22245a2 130 hashmap_entry_init(&e->ent, hash);
959b5455 131 e->config = submodule;
26b455f2 132 hashmap_put(&cache->for_path, &e->ent);
959b5455
HV
133}
134
135static void cache_remove_path(struct submodule_cache *cache,
136 struct submodule *submodule)
137{
34caab02 138 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
139 submodule->path);
959b5455
HV
140 struct submodule_entry e;
141 struct submodule_entry *removed;
d22245a2 142 hashmap_entry_init(&e.ent, hash);
959b5455 143 e.config = submodule;
404ab78e 144 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
959b5455
HV
145 free(removed);
146}
147
148static void cache_add(struct submodule_cache *cache,
149 struct submodule *submodule)
150{
34caab02 151 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
152 submodule->name);
959b5455 153 struct submodule_entry *e = xmalloc(sizeof(*e));
d22245a2 154 hashmap_entry_init(&e->ent, hash);
959b5455 155 e->config = submodule;
b94e5c1d 156 hashmap_add(&cache->for_name, &e->ent);
959b5455
HV
157}
158
159static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
34caab02 160 const struct object_id *gitmodules_oid, const char *path)
959b5455
HV
161{
162 struct submodule_entry *entry;
34caab02 163 unsigned int hash = hash_oid_string(gitmodules_oid, path);
959b5455
HV
164 struct submodule_entry key;
165 struct submodule key_config;
166
34caab02 167 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
959b5455
HV
168 key_config.path = path;
169
d22245a2 170 hashmap_entry_init(&key.ent, hash);
959b5455
HV
171 key.config = &key_config;
172
404ab78e 173 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
959b5455
HV
174 if (entry)
175 return entry->config;
176 return NULL;
177}
178
179static struct submodule *cache_lookup_name(struct submodule_cache *cache,
34caab02 180 const struct object_id *gitmodules_oid, const char *name)
959b5455
HV
181{
182 struct submodule_entry *entry;
34caab02 183 unsigned int hash = hash_oid_string(gitmodules_oid, name);
959b5455
HV
184 struct submodule_entry key;
185 struct submodule key_config;
186
34caab02 187 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
959b5455
HV
188 key_config.name = name;
189
d22245a2 190 hashmap_entry_init(&key.ent, hash);
959b5455
HV
191 key.config = &key_config;
192
404ab78e 193 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
959b5455
HV
194 if (entry)
195 return entry->config;
196 return NULL;
197}
198
0383bbb9
JK
199int check_submodule_name(const char *name)
200{
201 /* Disallow empty names */
202 if (!*name)
203 return -1;
204
205 /*
206 * Look for '..' as a path component. Check both '/' and '\\' as
207 * separators rather than is_dir_sep(), because we want the name rules
208 * to be consistent across platforms.
209 */
210 goto in_component; /* always start inside component */
211 while (*name) {
212 char c = *name++;
213 if (c == '/' || c == '\\') {
214in_component:
215 if (name[0] == '.' && name[1] == '.' &&
216 (!name[2] || name[2] == '/' || name[2] == '\\'))
217 return -1;
218 }
219 }
220
221 return 0;
222}
223
959b5455
HV
224static int name_and_item_from_var(const char *var, struct strbuf *name,
225 struct strbuf *item)
226{
227 const char *subsection, *key;
228 int subsection_len, parse;
229 parse = parse_config_key(var, "submodule", &subsection,
230 &subsection_len, &key);
231 if (parse < 0 || !subsection)
232 return 0;
233
234 strbuf_add(name, subsection, subsection_len);
0383bbb9
JK
235 if (check_submodule_name(name->buf) < 0) {
236 warning(_("ignoring suspicious submodule name: %s"), name->buf);
237 strbuf_release(name);
238 return 0;
239 }
240
959b5455
HV
241 strbuf_addstr(item, key);
242
243 return 1;
244}
245
246static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
34caab02 247 const struct object_id *gitmodules_oid, const char *name)
959b5455
HV
248{
249 struct submodule *submodule;
250 struct strbuf name_buf = STRBUF_INIT;
251
34caab02 252 submodule = cache_lookup_name(cache, gitmodules_oid, name);
959b5455
HV
253 if (submodule)
254 return submodule;
255
256 submodule = xmalloc(sizeof(*submodule));
257
258 strbuf_addstr(&name_buf, name);
259 submodule->name = strbuf_detach(&name_buf, NULL);
260
261 submodule->path = NULL;
262 submodule->url = NULL;
ea2fa5a3
SB
263 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
264 submodule->update_strategy.command = NULL;
959b5455
HV
265 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
266 submodule->ignore = NULL;
b5944f34 267 submodule->branch = NULL;
37f52e93 268 submodule->recommend_shallow = -1;
959b5455 269
34caab02 270 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
959b5455
HV
271
272 cache_add(cache, submodule);
273
274 return submodule;
275}
276
027771fc
HV
277static int parse_fetch_recurse(const char *opt, const char *arg,
278 int die_on_error)
279{
89576613 280 switch (git_parse_maybe_bool(arg)) {
027771fc
HV
281 case 1:
282 return RECURSE_SUBMODULES_ON;
283 case 0:
284 return RECURSE_SUBMODULES_OFF;
285 default:
286 if (!strcmp(arg, "on-demand"))
287 return RECURSE_SUBMODULES_ON_DEMAND;
5a59a230
NTND
288 /*
289 * Please update $__git_fetch_recurse_submodules in
290 * git-completion.bash when you add new options.
291 */
027771fc
HV
292 if (die_on_error)
293 die("bad %s argument: %s", opt, arg);
294 else
295 return RECURSE_SUBMODULES_ERROR;
296 }
297}
298
f20e7c1e
BW
299int parse_submodule_fetchjobs(const char *var, const char *value)
300{
301 int fetchjobs = git_config_int(var, value);
302 if (fetchjobs < 0)
303 die(_("negative values not allowed for submodule.fetchjobs"));
304 return fetchjobs;
305}
306
027771fc
HV
307int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
308{
309 return parse_fetch_recurse(opt, arg, 1);
310}
311
886dc154
SB
312int option_fetch_parse_recurse_submodules(const struct option *opt,
313 const char *arg, int unset)
314{
315 int *v;
316
317 if (!opt->value)
318 return -1;
319
320 v = opt->value;
321
322 if (unset) {
323 *v = RECURSE_SUBMODULES_OFF;
324 } else {
325 if (arg)
326 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
327 else
328 *v = RECURSE_SUBMODULES_ON;
329 }
330 return 0;
331}
332
d601fd09
SB
333static int parse_update_recurse(const char *opt, const char *arg,
334 int die_on_error)
335{
bdfcdefd 336 switch (git_parse_maybe_bool(arg)) {
d601fd09
SB
337 case 1:
338 return RECURSE_SUBMODULES_ON;
339 case 0:
340 return RECURSE_SUBMODULES_OFF;
341 default:
342 if (die_on_error)
343 die("bad %s argument: %s", opt, arg);
344 return RECURSE_SUBMODULES_ERROR;
345 }
346}
347
348int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
349{
350 return parse_update_recurse(opt, arg, 1);
351}
352
b33a15b0
MC
353static int parse_push_recurse(const char *opt, const char *arg,
354 int die_on_error)
355{
89576613 356 switch (git_parse_maybe_bool(arg)) {
b33a15b0
MC
357 case 1:
358 /* There's no simple "on" value when pushing */
359 if (die_on_error)
360 die("bad %s argument: %s", opt, arg);
361 else
362 return RECURSE_SUBMODULES_ERROR;
363 case 0:
364 return RECURSE_SUBMODULES_OFF;
365 default:
366 if (!strcmp(arg, "on-demand"))
367 return RECURSE_SUBMODULES_ON_DEMAND;
368 else if (!strcmp(arg, "check"))
369 return RECURSE_SUBMODULES_CHECK;
6c656c3f
BW
370 else if (!strcmp(arg, "only"))
371 return RECURSE_SUBMODULES_ONLY;
5a59a230
NTND
372 /*
373 * Please update $__git_push_recurse_submodules in
374 * git-completion.bash when you add new modes.
375 */
b33a15b0
MC
376 else if (die_on_error)
377 die("bad %s argument: %s", opt, arg);
378 else
379 return RECURSE_SUBMODULES_ERROR;
380 }
381}
382
383int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
384{
385 return parse_push_recurse(opt, arg, 1);
386}
387
34caab02 388static void warn_multiple_config(const struct object_id *treeish_name,
959b5455
HV
389 const char *name, const char *option)
390{
391 const char *commit_string = "WORKTREE";
73c293bb 392 if (treeish_name)
34caab02 393 commit_string = oid_to_hex(treeish_name);
959b5455
HV
394 warning("%s:.gitmodules, multiple configurations found for "
395 "'submodule.%s.%s'. Skipping second one!",
396 commit_string, name, option);
397}
398
f6adec4e
JK
399static void warn_command_line_option(const char *var, const char *value)
400{
401 warning(_("ignoring '%s' which may be interpreted as"
402 " a command-line option: %s"), var, value);
403}
404
959b5455
HV
405struct parse_config_parameter {
406 struct submodule_cache *cache;
34caab02 407 const struct object_id *treeish_name;
408 const struct object_id *gitmodules_oid;
959b5455
HV
409 int overwrite;
410};
411
e904deb8
JN
412/*
413 * Parse a config item from .gitmodules.
414 *
415 * This does not handle submodule-related configuration from the main
416 * config store (.git/config, etc). Callers are responsible for
417 * checking for overrides in the main config store when appropriate.
418 */
959b5455
HV
419static int parse_config(const char *var, const char *value, void *data)
420{
421 struct parse_config_parameter *me = data;
422 struct submodule *submodule;
423 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
424 int ret = 0;
425
426 /* this also ensures that we only parse submodule entries */
427 if (!name_and_item_from_var(var, &name, &item))
428 return 0;
429
147875fd 430 submodule = lookup_or_create_by_name(me->cache,
34caab02 431 me->gitmodules_oid,
147875fd 432 name.buf);
959b5455
HV
433
434 if (!strcmp(item.buf, "path")) {
147875fd 435 if (!value)
959b5455 436 ret = config_error_nonbool(var);
273c6149
JK
437 else if (looks_like_command_line_option(value))
438 warn_command_line_option(var, value);
f73da110 439 else if (!me->overwrite && submodule->path)
73c293bb 440 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 441 "path");
147875fd
SB
442 else {
443 if (submodule->path)
444 cache_remove_path(me->cache, submodule);
445 free((void *) submodule->path);
446 submodule->path = xstrdup(value);
447 cache_put_path(me->cache, submodule);
959b5455 448 }
959b5455 449 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
027771fc 450 /* when parsing worktree configurations we can die early */
34caab02 451 int die_on_error = is_null_oid(me->gitmodules_oid);
959b5455 452 if (!me->overwrite &&
147875fd 453 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
73c293bb 454 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 455 "fetchrecursesubmodules");
147875fd
SB
456 else
457 submodule->fetch_recurse = parse_fetch_recurse(
458 var, value,
027771fc 459 die_on_error);
959b5455 460 } else if (!strcmp(item.buf, "ignore")) {
147875fd
SB
461 if (!value)
462 ret = config_error_nonbool(var);
f73da110 463 else if (!me->overwrite && submodule->ignore)
73c293bb 464 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 465 "ignore");
147875fd
SB
466 else if (strcmp(value, "untracked") &&
467 strcmp(value, "dirty") &&
468 strcmp(value, "all") &&
469 strcmp(value, "none"))
959b5455 470 warning("Invalid parameter '%s' for config option "
5ea30489 471 "'submodule.%s.ignore'", value, name.buf);
147875fd
SB
472 else {
473 free((void *) submodule->ignore);
474 submodule->ignore = xstrdup(value);
959b5455 475 }
959b5455 476 } else if (!strcmp(item.buf, "url")) {
959b5455
HV
477 if (!value) {
478 ret = config_error_nonbool(var);
f6adec4e
JK
479 } else if (looks_like_command_line_option(value)) {
480 warn_command_line_option(var, value);
f73da110 481 } else if (!me->overwrite && submodule->url) {
73c293bb 482 warn_multiple_config(me->treeish_name, submodule->name,
959b5455 483 "url");
147875fd
SB
484 } else {
485 free((void *) submodule->url);
486 submodule->url = xstrdup(value);
959b5455 487 }
ea2fa5a3
SB
488 } else if (!strcmp(item.buf, "update")) {
489 if (!value)
490 ret = config_error_nonbool(var);
491 else if (!me->overwrite &&
492 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
73c293bb 493 warn_multiple_config(me->treeish_name, submodule->name,
ea2fa5a3
SB
494 "update");
495 else if (parse_submodule_update_strategy(value,
e904deb8
JN
496 &submodule->update_strategy) < 0 ||
497 submodule->update_strategy.type == SM_UPDATE_COMMAND)
498 die(_("invalid value for %s"), var);
37f52e93
SB
499 } else if (!strcmp(item.buf, "shallow")) {
500 if (!me->overwrite && submodule->recommend_shallow != -1)
73c293bb 501 warn_multiple_config(me->treeish_name, submodule->name,
37f52e93 502 "shallow");
b5944f34 503 else
37f52e93
SB
504 submodule->recommend_shallow =
505 git_config_bool(var, value);
b5944f34
SB
506 } else if (!strcmp(item.buf, "branch")) {
507 if (!me->overwrite && submodule->branch)
73c293bb 508 warn_multiple_config(me->treeish_name, submodule->name,
b5944f34
SB
509 "branch");
510 else {
511 free((void *)submodule->branch);
512 submodule->branch = xstrdup(value);
37f52e93 513 }
959b5455
HV
514 }
515
959b5455
HV
516 strbuf_release(&name);
517 strbuf_release(&item);
518
519 return ret;
520}
521
1b796ace
BW
522static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
523 struct object_id *gitmodules_oid,
524 struct strbuf *rev)
959b5455 525{
959b5455
HV
526 int ret = 0;
527
cd73de47 528 if (is_null_oid(treeish_name)) {
529 oidclr(gitmodules_oid);
959b5455
HV
530 return 1;
531 }
532
cd73de47 533 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
534 if (get_oid(rev->buf, gitmodules_oid) >= 0)
959b5455
HV
535 ret = 1;
536
959b5455
HV
537 return ret;
538}
539
540/* This does a lookup of a submodule configuration by name or by path
541 * (key) with on-demand reading of the appropriate .gitmodules from
542 * revisions.
543 */
544static const struct submodule *config_from(struct submodule_cache *cache,
cd73de47 545 const struct object_id *treeish_name, const char *key,
959b5455
HV
546 enum lookup_type lookup_type)
547{
548 struct strbuf rev = STRBUF_INIT;
549 unsigned long config_size;
0918e250 550 char *config = NULL;
cd73de47 551 struct object_id oid;
959b5455
HV
552 enum object_type type;
553 const struct submodule *submodule = NULL;
554 struct parse_config_parameter parameter;
555
556 /*
557 * If any parameter except the cache is a NULL pointer just
558 * return the first submodule. Can be used to check whether
559 * there are any submodules parsed.
560 */
73c293bb 561 if (!treeish_name || !key) {
959b5455
HV
562 struct hashmap_iter iter;
563 struct submodule_entry *entry;
564
87571c3f
EW
565 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
566 struct submodule_entry,
567 ent /* member name */);
959b5455
HV
568 if (!entry)
569 return NULL;
570 return entry->config;
571 }
572
cd73de47 573 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
0918e250 574 goto out;
959b5455
HV
575
576 switch (lookup_type) {
577 case lookup_name:
34caab02 578 submodule = cache_lookup_name(cache, &oid, key);
959b5455
HV
579 break;
580 case lookup_path:
34caab02 581 submodule = cache_lookup_path(cache, &oid, key);
959b5455
HV
582 break;
583 }
584 if (submodule)
0918e250 585 goto out;
959b5455 586
b4f5aca4 587 config = read_object_file(&oid, &type, &config_size);
0918e250
HV
588 if (!config || type != OBJ_BLOB)
589 goto out;
959b5455
HV
590
591 /* fill the submodule config into the cache */
592 parameter.cache = cache;
34caab02 593 parameter.treeish_name = treeish_name;
594 parameter.gitmodules_oid = &oid;
959b5455 595 parameter.overwrite = 0;
1b8132d9 596 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
4574f1aa 597 config, config_size, &parameter, NULL);
514dea90 598 strbuf_release(&rev);
959b5455
HV
599 free(config);
600
601 switch (lookup_type) {
602 case lookup_name:
34caab02 603 return cache_lookup_name(cache, &oid, key);
959b5455 604 case lookup_path:
34caab02 605 return cache_lookup_path(cache, &oid, key);
959b5455
HV
606 default:
607 return NULL;
608 }
0918e250
HV
609
610out:
611 strbuf_release(&rev);
612 free(config);
613 return submodule;
959b5455
HV
614}
615
bf12fcdf 616static void submodule_cache_check_init(struct repository *repo)
959b5455 617{
bf12fcdf 618 if (repo->submodule_cache && repo->submodule_cache->initialized)
959b5455
HV
619 return;
620
bf12fcdf
BW
621 if (!repo->submodule_cache)
622 repo->submodule_cache = submodule_cache_alloc();
623
624 submodule_cache_init(repo->submodule_cache);
959b5455
HV
625}
626
db64d112
AO
627/*
628 * Note: This function is private for a reason, the '.gitmodules' file should
571fb965 629 * not be used as a mechanism to retrieve arbitrary configuration stored in
db64d112
AO
630 * the repository.
631 *
632 * Runs the provided config function on the '.gitmodules' file found in the
633 * working directory.
634 */
635static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
636{
637 if (repo->worktree) {
76e9bdc4
AO
638 struct git_config_source config_source = { 0 };
639 const struct config_options opts = { 0 };
640 struct object_id oid;
641 char *file;
d9b8b8f8 642 char *oidstr = NULL;
76e9bdc4
AO
643
644 file = repo_worktree_path(repo, GITMODULES_FILE);
645 if (file_exists(file)) {
646 config_source.file = file;
d9b8b8f8
NTND
647 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
648 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
649 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
650 if (repo != the_repository)
651 add_to_alternates_memory(repo->objects->odb->path);
76e9bdc4
AO
652 } else {
653 goto out;
654 }
655
656 config_with_options(fn, data, &config_source, &opts);
657
658out:
d9b8b8f8 659 free(oidstr);
db64d112
AO
660 free(file);
661 }
662}
663
1b796ace 664static int gitmodules_cb(const char *var, const char *value, void *data)
851e18c3 665{
1b796ace 666 struct repository *repo = data;
851e18c3 667 struct parse_config_parameter parameter;
bf12fcdf 668
bf12fcdf 669 parameter.cache = repo->submodule_cache;
73c293bb 670 parameter.treeish_name = NULL;
34caab02 671 parameter.gitmodules_oid = &null_oid;
851e18c3
HV
672 parameter.overwrite = 1;
673
851e18c3
HV
674 return parse_config(var, value, &parameter);
675}
676
1b796ace 677void repo_read_gitmodules(struct repository *repo)
bf12fcdf 678{
ff6f1f56
BW
679 submodule_cache_check_init(repo);
680
db64d112
AO
681 if (repo_read_index(repo) < 0)
682 return;
1b796ace 683
db64d112
AO
684 if (!is_gitmodules_unmerged(repo->index))
685 config_from_gitmodules(gitmodules_cb, repo, repo);
ff6f1f56
BW
686
687 repo->submodule_cache->gitmodules_read = 1;
1b796ace
BW
688}
689
690void gitmodules_config_oid(const struct object_id *commit_oid)
691{
692 struct strbuf rev = STRBUF_INIT;
693 struct object_id oid;
694
ff6f1f56
BW
695 submodule_cache_check_init(the_repository);
696
1b796ace
BW
697 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
698 git_config_from_blob_oid(gitmodules_cb, rev.buf,
699 &oid, the_repository);
700 }
701 strbuf_release(&rev);
ff6f1f56
BW
702
703 the_repository->submodule_cache->gitmodules_read = 1;
704}
705
706static void gitmodules_read_check(struct repository *repo)
707{
708 submodule_cache_check_init(repo);
709
710 /* read the repo's .gitmodules file if it hasn't been already */
711 if (!repo->submodule_cache->gitmodules_read)
712 repo_read_gitmodules(repo);
bf12fcdf
BW
713}
714
3b8fb393
SB
715const struct submodule *submodule_from_name(struct repository *r,
716 const struct object_id *treeish_name,
959b5455
HV
717 const char *name)
718{
3b8fb393
SB
719 gitmodules_read_check(r);
720 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
959b5455
HV
721}
722
3b8fb393
SB
723const struct submodule *submodule_from_path(struct repository *r,
724 const struct object_id *treeish_name,
959b5455
HV
725 const char *path)
726{
3b8fb393
SB
727 gitmodules_read_check(r);
728 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
bf12fcdf
BW
729}
730
f793b895 731void submodule_free(struct repository *r)
bf12fcdf 732{
f793b895
SB
733 if (r->submodule_cache)
734 submodule_cache_clear(r->submodule_cache);
959b5455 735}
ad136370 736
bcbc780d
AO
737static int config_print_callback(const char *var, const char *value, void *cb_data)
738{
739 char *wanted_key = cb_data;
740
741 if (!strcmp(wanted_key, var))
742 printf("%s\n", value);
743
744 return 0;
745}
746
747int print_config_from_gitmodules(struct repository *repo, const char *key)
748{
749 int ret;
750 char *store_key;
751
752 ret = git_config_parse_key(key, &store_key, NULL);
753 if (ret < 0)
754 return CONFIG_INVALID_KEY;
755
756 config_from_gitmodules(config_print_callback, repo, store_key);
757
758 free(store_key);
759 return 0;
760}
761
45f5ef3d
AO
762int config_set_in_gitmodules_file_gently(const char *key, const char *value)
763{
764 int ret;
765
766 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
767 if (ret < 0)
768 /* Maybe the user already did that, don't error out here */
769 warning(_("Could not update .gitmodules entry %s"), key);
770
771 return ret;
772}
773
71a6953d
AO
774struct fetch_config {
775 int *max_children;
776 int *recurse_submodules;
777};
778
779static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
780{
781 struct fetch_config *config = cb;
782 if (!strcmp(var, "submodule.fetchjobs")) {
783 *(config->max_children) = parse_submodule_fetchjobs(var, value);
784 return 0;
785 } else if (!strcmp(var, "fetch.recursesubmodules")) {
786 *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value);
787 return 0;
788 }
789
790 return 0;
791}
792
793void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
794{
795 struct fetch_config config = {
796 .max_children = max_children,
797 .recurse_submodules = recurse_submodules
798 };
9a0fb3e7 799 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
71a6953d 800}
05744997
AO
801
802static int gitmodules_update_clone_config(const char *var, const char *value,
803 void *cb)
804{
805 int *max_jobs = cb;
806 if (!strcmp(var, "submodule.fetchjobs"))
807 *max_jobs = parse_submodule_fetchjobs(var, value);
808 return 0;
809}
810
811void update_clone_config_from_gitmodules(int *max_jobs)
812{
9a0fb3e7 813 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
05744997 814}