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