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