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