]>
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 | 17 | #include "url.h" |
8430b438 | 18 | #include "urlmatch.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)) { | |
13320ff6 | 353 | int ret = 0; |
8430b438 VD |
354 | char *normalized = url_normalize(curl_url, NULL); |
355 | if (normalized) { | |
356 | char *decoded = url_decode(normalized); | |
357 | if (strchr(decoded, '\n')) | |
358 | ret = -1; | |
359 | free(normalized); | |
360 | free(decoded); | |
361 | } else { | |
13320ff6 | 362 | ret = -1; |
8430b438 VD |
363 | } |
364 | ||
13320ff6 VD |
365 | return ret; |
366 | } | |
367 | ||
368 | return 0; | |
369 | } | |
370 | ||
959b5455 HV |
371 | static int name_and_item_from_var(const char *var, struct strbuf *name, |
372 | struct strbuf *item) | |
373 | { | |
374 | const char *subsection, *key; | |
f5914f4b JK |
375 | size_t subsection_len; |
376 | int parse; | |
959b5455 HV |
377 | parse = parse_config_key(var, "submodule", &subsection, |
378 | &subsection_len, &key); | |
379 | if (parse < 0 || !subsection) | |
380 | return 0; | |
381 | ||
382 | strbuf_add(name, subsection, subsection_len); | |
0383bbb9 JK |
383 | if (check_submodule_name(name->buf) < 0) { |
384 | warning(_("ignoring suspicious submodule name: %s"), name->buf); | |
385 | strbuf_release(name); | |
386 | return 0; | |
387 | } | |
388 | ||
959b5455 HV |
389 | strbuf_addstr(item, key); |
390 | ||
391 | return 1; | |
392 | } | |
393 | ||
394 | static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache, | |
34caab02 | 395 | const struct object_id *gitmodules_oid, const char *name) |
959b5455 HV |
396 | { |
397 | struct submodule *submodule; | |
398 | struct strbuf name_buf = STRBUF_INIT; | |
399 | ||
34caab02 | 400 | submodule = cache_lookup_name(cache, gitmodules_oid, name); |
959b5455 HV |
401 | if (submodule) |
402 | return submodule; | |
403 | ||
404 | submodule = xmalloc(sizeof(*submodule)); | |
405 | ||
406 | strbuf_addstr(&name_buf, name); | |
407 | submodule->name = strbuf_detach(&name_buf, NULL); | |
408 | ||
409 | submodule->path = NULL; | |
410 | submodule->url = NULL; | |
ea2fa5a3 SB |
411 | submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED; |
412 | submodule->update_strategy.command = NULL; | |
959b5455 HV |
413 | submodule->fetch_recurse = RECURSE_SUBMODULES_NONE; |
414 | submodule->ignore = NULL; | |
b5944f34 | 415 | submodule->branch = NULL; |
37f52e93 | 416 | submodule->recommend_shallow = -1; |
959b5455 | 417 | |
34caab02 | 418 | oidcpy(&submodule->gitmodules_oid, gitmodules_oid); |
959b5455 HV |
419 | |
420 | cache_add(cache, submodule); | |
421 | ||
422 | return submodule; | |
423 | } | |
424 | ||
027771fc HV |
425 | static int parse_fetch_recurse(const char *opt, const char *arg, |
426 | int die_on_error) | |
427 | { | |
89576613 | 428 | switch (git_parse_maybe_bool(arg)) { |
027771fc HV |
429 | case 1: |
430 | return RECURSE_SUBMODULES_ON; | |
431 | case 0: | |
432 | return RECURSE_SUBMODULES_OFF; | |
433 | default: | |
434 | if (!strcmp(arg, "on-demand")) | |
435 | return RECURSE_SUBMODULES_ON_DEMAND; | |
5a59a230 NTND |
436 | /* |
437 | * Please update $__git_fetch_recurse_submodules in | |
438 | * git-completion.bash when you add new options. | |
439 | */ | |
027771fc HV |
440 | if (die_on_error) |
441 | die("bad %s argument: %s", opt, arg); | |
442 | else | |
443 | return RECURSE_SUBMODULES_ERROR; | |
444 | } | |
445 | } | |
446 | ||
8868b1eb GC |
447 | int parse_submodule_fetchjobs(const char *var, const char *value, |
448 | const struct key_value_info *kvi) | |
f20e7c1e | 449 | { |
8868b1eb | 450 | int fetchjobs = git_config_int(var, value, kvi); |
f20e7c1e | 451 | if (fetchjobs < 0) |
b4eda05d | 452 | die(_("negative values not allowed for submodule.fetchJobs")); |
51243f9f ÆAB |
453 | if (!fetchjobs) |
454 | fetchjobs = online_cpus(); | |
f20e7c1e BW |
455 | return fetchjobs; |
456 | } | |
457 | ||
027771fc HV |
458 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
459 | { | |
460 | return parse_fetch_recurse(opt, arg, 1); | |
461 | } | |
462 | ||
886dc154 SB |
463 | int option_fetch_parse_recurse_submodules(const struct option *opt, |
464 | const char *arg, int unset) | |
465 | { | |
466 | int *v; | |
467 | ||
468 | if (!opt->value) | |
469 | return -1; | |
470 | ||
471 | v = opt->value; | |
472 | ||
473 | if (unset) { | |
474 | *v = RECURSE_SUBMODULES_OFF; | |
475 | } else { | |
476 | if (arg) | |
477 | *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg); | |
478 | else | |
479 | *v = RECURSE_SUBMODULES_ON; | |
480 | } | |
481 | return 0; | |
482 | } | |
483 | ||
d601fd09 SB |
484 | static int parse_update_recurse(const char *opt, const char *arg, |
485 | int die_on_error) | |
486 | { | |
bdfcdefd | 487 | switch (git_parse_maybe_bool(arg)) { |
d601fd09 SB |
488 | case 1: |
489 | return RECURSE_SUBMODULES_ON; | |
490 | case 0: | |
491 | return RECURSE_SUBMODULES_OFF; | |
492 | default: | |
493 | if (die_on_error) | |
494 | die("bad %s argument: %s", opt, arg); | |
495 | return RECURSE_SUBMODULES_ERROR; | |
496 | } | |
497 | } | |
498 | ||
499 | int parse_update_recurse_submodules_arg(const char *opt, const char *arg) | |
500 | { | |
501 | return parse_update_recurse(opt, arg, 1); | |
502 | } | |
503 | ||
b33a15b0 MC |
504 | static int parse_push_recurse(const char *opt, const char *arg, |
505 | int die_on_error) | |
506 | { | |
89576613 | 507 | switch (git_parse_maybe_bool(arg)) { |
b33a15b0 MC |
508 | case 1: |
509 | /* There's no simple "on" value when pushing */ | |
510 | if (die_on_error) | |
511 | die("bad %s argument: %s", opt, arg); | |
512 | else | |
513 | return RECURSE_SUBMODULES_ERROR; | |
514 | case 0: | |
515 | return RECURSE_SUBMODULES_OFF; | |
516 | default: | |
517 | if (!strcmp(arg, "on-demand")) | |
518 | return RECURSE_SUBMODULES_ON_DEMAND; | |
519 | else if (!strcmp(arg, "check")) | |
520 | return RECURSE_SUBMODULES_CHECK; | |
6c656c3f BW |
521 | else if (!strcmp(arg, "only")) |
522 | return RECURSE_SUBMODULES_ONLY; | |
5a59a230 NTND |
523 | /* |
524 | * Please update $__git_push_recurse_submodules in | |
525 | * git-completion.bash when you add new modes. | |
526 | */ | |
b33a15b0 MC |
527 | else if (die_on_error) |
528 | die("bad %s argument: %s", opt, arg); | |
529 | else | |
530 | return RECURSE_SUBMODULES_ERROR; | |
531 | } | |
532 | } | |
533 | ||
534 | int parse_push_recurse_submodules_arg(const char *opt, const char *arg) | |
535 | { | |
536 | return parse_push_recurse(opt, arg, 1); | |
537 | } | |
538 | ||
34caab02 | 539 | static void warn_multiple_config(const struct object_id *treeish_name, |
959b5455 HV |
540 | const char *name, const char *option) |
541 | { | |
542 | const char *commit_string = "WORKTREE"; | |
73c293bb | 543 | if (treeish_name) |
34caab02 | 544 | commit_string = oid_to_hex(treeish_name); |
959b5455 HV |
545 | warning("%s:.gitmodules, multiple configurations found for " |
546 | "'submodule.%s.%s'. Skipping second one!", | |
547 | commit_string, name, option); | |
548 | } | |
549 | ||
f6adec4e JK |
550 | static void warn_command_line_option(const char *var, const char *value) |
551 | { | |
552 | warning(_("ignoring '%s' which may be interpreted as" | |
553 | " a command-line option: %s"), var, value); | |
554 | } | |
555 | ||
959b5455 HV |
556 | struct parse_config_parameter { |
557 | struct submodule_cache *cache; | |
34caab02 | 558 | const struct object_id *treeish_name; |
559 | const struct object_id *gitmodules_oid; | |
959b5455 HV |
560 | int overwrite; |
561 | }; | |
562 | ||
e904deb8 JN |
563 | /* |
564 | * Parse a config item from .gitmodules. | |
565 | * | |
566 | * This does not handle submodule-related configuration from the main | |
567 | * config store (.git/config, etc). Callers are responsible for | |
568 | * checking for overrides in the main config store when appropriate. | |
569 | */ | |
a4e7e317 GC |
570 | static int parse_config(const char *var, const char *value, |
571 | const struct config_context *ctx UNUSED, void *data) | |
959b5455 HV |
572 | { |
573 | struct parse_config_parameter *me = data; | |
574 | struct submodule *submodule; | |
575 | struct strbuf name = STRBUF_INIT, item = STRBUF_INIT; | |
576 | int ret = 0; | |
577 | ||
578 | /* this also ensures that we only parse submodule entries */ | |
579 | if (!name_and_item_from_var(var, &name, &item)) | |
580 | return 0; | |
581 | ||
147875fd | 582 | submodule = lookup_or_create_by_name(me->cache, |
34caab02 | 583 | me->gitmodules_oid, |
147875fd | 584 | name.buf); |
959b5455 HV |
585 | |
586 | if (!strcmp(item.buf, "path")) { | |
147875fd | 587 | if (!value) |
959b5455 | 588 | ret = config_error_nonbool(var); |
273c6149 JK |
589 | else if (looks_like_command_line_option(value)) |
590 | warn_command_line_option(var, value); | |
f73da110 | 591 | else if (!me->overwrite && submodule->path) |
73c293bb | 592 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 593 | "path"); |
147875fd SB |
594 | else { |
595 | if (submodule->path) | |
596 | cache_remove_path(me->cache, submodule); | |
597 | free((void *) submodule->path); | |
598 | submodule->path = xstrdup(value); | |
599 | cache_put_path(me->cache, submodule); | |
959b5455 | 600 | } |
959b5455 | 601 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { |
027771fc | 602 | /* when parsing worktree configurations we can die early */ |
34caab02 | 603 | int die_on_error = is_null_oid(me->gitmodules_oid); |
959b5455 | 604 | if (!me->overwrite && |
147875fd | 605 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) |
73c293bb | 606 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 607 | "fetchrecursesubmodules"); |
147875fd SB |
608 | else |
609 | submodule->fetch_recurse = parse_fetch_recurse( | |
610 | var, value, | |
027771fc | 611 | die_on_error); |
959b5455 | 612 | } else if (!strcmp(item.buf, "ignore")) { |
147875fd SB |
613 | if (!value) |
614 | ret = config_error_nonbool(var); | |
f73da110 | 615 | else if (!me->overwrite && submodule->ignore) |
73c293bb | 616 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 617 | "ignore"); |
147875fd SB |
618 | else if (strcmp(value, "untracked") && |
619 | strcmp(value, "dirty") && | |
620 | strcmp(value, "all") && | |
621 | strcmp(value, "none")) | |
959b5455 | 622 | warning("Invalid parameter '%s' for config option " |
5ea30489 | 623 | "'submodule.%s.ignore'", value, name.buf); |
147875fd SB |
624 | else { |
625 | free((void *) submodule->ignore); | |
626 | submodule->ignore = xstrdup(value); | |
959b5455 | 627 | } |
959b5455 | 628 | } else if (!strcmp(item.buf, "url")) { |
959b5455 HV |
629 | if (!value) { |
630 | ret = config_error_nonbool(var); | |
f6adec4e JK |
631 | } else if (looks_like_command_line_option(value)) { |
632 | warn_command_line_option(var, value); | |
f73da110 | 633 | } else if (!me->overwrite && submodule->url) { |
73c293bb | 634 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 635 | "url"); |
147875fd SB |
636 | } else { |
637 | free((void *) submodule->url); | |
638 | submodule->url = xstrdup(value); | |
959b5455 | 639 | } |
ea2fa5a3 SB |
640 | } else if (!strcmp(item.buf, "update")) { |
641 | if (!value) | |
642 | ret = config_error_nonbool(var); | |
643 | else if (!me->overwrite && | |
644 | submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) | |
73c293bb | 645 | warn_multiple_config(me->treeish_name, submodule->name, |
ea2fa5a3 SB |
646 | "update"); |
647 | else if (parse_submodule_update_strategy(value, | |
e904deb8 JN |
648 | &submodule->update_strategy) < 0 || |
649 | submodule->update_strategy.type == SM_UPDATE_COMMAND) | |
1a8aea85 | 650 | die(_("invalid value for '%s'"), var); |
37f52e93 SB |
651 | } else if (!strcmp(item.buf, "shallow")) { |
652 | if (!me->overwrite && submodule->recommend_shallow != -1) | |
73c293bb | 653 | warn_multiple_config(me->treeish_name, submodule->name, |
37f52e93 | 654 | "shallow"); |
b5944f34 | 655 | else |
37f52e93 SB |
656 | submodule->recommend_shallow = |
657 | git_config_bool(var, value); | |
b5944f34 | 658 | } else if (!strcmp(item.buf, "branch")) { |
34b1a0d3 JK |
659 | if (!value) |
660 | ret = config_error_nonbool(var); | |
661 | else if (!me->overwrite && submodule->branch) | |
73c293bb | 662 | warn_multiple_config(me->treeish_name, submodule->name, |
b5944f34 SB |
663 | "branch"); |
664 | else { | |
665 | free((void *)submodule->branch); | |
666 | submodule->branch = xstrdup(value); | |
37f52e93 | 667 | } |
959b5455 HV |
668 | } |
669 | ||
959b5455 HV |
670 | strbuf_release(&name); |
671 | strbuf_release(&item); | |
672 | ||
673 | return ret; | |
674 | } | |
675 | ||
1b796ace BW |
676 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, |
677 | struct object_id *gitmodules_oid, | |
678 | struct strbuf *rev) | |
959b5455 | 679 | { |
959b5455 HV |
680 | int ret = 0; |
681 | ||
cd73de47 | 682 | if (is_null_oid(treeish_name)) { |
683 | oidclr(gitmodules_oid); | |
959b5455 HV |
684 | return 1; |
685 | } | |
686 | ||
cd73de47 | 687 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); |
d850b7a5 | 688 | if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0) |
959b5455 HV |
689 | ret = 1; |
690 | ||
959b5455 HV |
691 | return ret; |
692 | } | |
693 | ||
694 | /* This does a lookup of a submodule configuration by name or by path | |
695 | * (key) with on-demand reading of the appropriate .gitmodules from | |
696 | * revisions. | |
697 | */ | |
698 | static const struct submodule *config_from(struct submodule_cache *cache, | |
cd73de47 | 699 | const struct object_id *treeish_name, const char *key, |
959b5455 HV |
700 | enum lookup_type lookup_type) |
701 | { | |
702 | struct strbuf rev = STRBUF_INIT; | |
703 | unsigned long config_size; | |
0918e250 | 704 | char *config = NULL; |
cd73de47 | 705 | struct object_id oid; |
959b5455 HV |
706 | enum object_type type; |
707 | const struct submodule *submodule = NULL; | |
708 | struct parse_config_parameter parameter; | |
709 | ||
710 | /* | |
711 | * If any parameter except the cache is a NULL pointer just | |
712 | * return the first submodule. Can be used to check whether | |
713 | * there are any submodules parsed. | |
714 | */ | |
73c293bb | 715 | if (!treeish_name || !key) { |
959b5455 HV |
716 | struct hashmap_iter iter; |
717 | struct submodule_entry *entry; | |
718 | ||
87571c3f EW |
719 | entry = hashmap_iter_first_entry(&cache->for_name, &iter, |
720 | struct submodule_entry, | |
721 | ent /* member name */); | |
959b5455 HV |
722 | if (!entry) |
723 | return NULL; | |
724 | return entry->config; | |
725 | } | |
726 | ||
cd73de47 | 727 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) |
0918e250 | 728 | goto out; |
959b5455 HV |
729 | |
730 | switch (lookup_type) { | |
731 | case lookup_name: | |
34caab02 | 732 | submodule = cache_lookup_name(cache, &oid, key); |
959b5455 HV |
733 | break; |
734 | case lookup_path: | |
34caab02 | 735 | submodule = cache_lookup_path(cache, &oid, key); |
959b5455 HV |
736 | break; |
737 | } | |
738 | if (submodule) | |
0918e250 | 739 | goto out; |
959b5455 | 740 | |
bc726bd0 ÆAB |
741 | config = repo_read_object_file(the_repository, &oid, &type, |
742 | &config_size); | |
0918e250 HV |
743 | if (!config || type != OBJ_BLOB) |
744 | goto out; | |
959b5455 HV |
745 | |
746 | /* fill the submodule config into the cache */ | |
747 | parameter.cache = cache; | |
34caab02 | 748 | parameter.treeish_name = treeish_name; |
749 | parameter.gitmodules_oid = &oid; | |
959b5455 | 750 | parameter.overwrite = 0; |
1b8132d9 | 751 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, |
809d8680 | 752 | config, config_size, ¶meter, CONFIG_SCOPE_UNKNOWN, NULL); |
514dea90 | 753 | strbuf_release(&rev); |
959b5455 HV |
754 | free(config); |
755 | ||
756 | switch (lookup_type) { | |
757 | case lookup_name: | |
34caab02 | 758 | return cache_lookup_name(cache, &oid, key); |
959b5455 | 759 | case lookup_path: |
34caab02 | 760 | return cache_lookup_path(cache, &oid, key); |
959b5455 HV |
761 | default: |
762 | return NULL; | |
763 | } | |
0918e250 HV |
764 | |
765 | out: | |
766 | strbuf_release(&rev); | |
767 | free(config); | |
768 | return submodule; | |
959b5455 HV |
769 | } |
770 | ||
bf12fcdf | 771 | static void submodule_cache_check_init(struct repository *repo) |
959b5455 | 772 | { |
bf12fcdf | 773 | if (repo->submodule_cache && repo->submodule_cache->initialized) |
959b5455 HV |
774 | return; |
775 | ||
bf12fcdf BW |
776 | if (!repo->submodule_cache) |
777 | repo->submodule_cache = submodule_cache_alloc(); | |
778 | ||
779 | submodule_cache_init(repo->submodule_cache); | |
959b5455 HV |
780 | } |
781 | ||
db64d112 AO |
782 | /* |
783 | * Note: This function is private for a reason, the '.gitmodules' file should | |
571fb965 | 784 | * not be used as a mechanism to retrieve arbitrary configuration stored in |
db64d112 AO |
785 | * the repository. |
786 | * | |
787 | * Runs the provided config function on the '.gitmodules' file found in the | |
788 | * working directory. | |
789 | */ | |
790 | static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data) | |
791 | { | |
792 | if (repo->worktree) { | |
9a83d088 MR |
793 | struct git_config_source config_source = { |
794 | 0, .scope = CONFIG_SCOPE_SUBMODULE | |
795 | }; | |
76e9bdc4 AO |
796 | const struct config_options opts = { 0 }; |
797 | struct object_id oid; | |
798 | char *file; | |
d9b8b8f8 | 799 | char *oidstr = NULL; |
76e9bdc4 AO |
800 | |
801 | file = repo_worktree_path(repo, GITMODULES_FILE); | |
802 | if (file_exists(file)) { | |
803 | config_source.file = file; | |
d9b8b8f8 NTND |
804 | } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 || |
805 | repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) { | |
806 | config_source.blob = oidstr = xstrdup(oid_to_hex(&oid)); | |
807 | if (repo != the_repository) | |
e3e8bf04 | 808 | add_submodule_odb_by_path(repo->objects->odb->path); |
76e9bdc4 AO |
809 | } else { |
810 | goto out; | |
811 | } | |
812 | ||
9b6b06c1 | 813 | config_with_options(fn, data, &config_source, repo, &opts); |
76e9bdc4 AO |
814 | |
815 | out: | |
d9b8b8f8 | 816 | free(oidstr); |
db64d112 AO |
817 | free(file); |
818 | } | |
819 | } | |
820 | ||
a4e7e317 GC |
821 | static int gitmodules_cb(const char *var, const char *value, |
822 | const struct config_context *ctx, void *data) | |
851e18c3 | 823 | { |
1b796ace | 824 | struct repository *repo = data; |
851e18c3 | 825 | struct parse_config_parameter parameter; |
bf12fcdf | 826 | |
bf12fcdf | 827 | parameter.cache = repo->submodule_cache; |
73c293bb | 828 | parameter.treeish_name = NULL; |
14228447 | 829 | parameter.gitmodules_oid = null_oid(); |
851e18c3 HV |
830 | parameter.overwrite = 1; |
831 | ||
a4e7e317 | 832 | return parse_config(var, value, ctx, ¶meter); |
851e18c3 HV |
833 | } |
834 | ||
d7992421 | 835 | void repo_read_gitmodules(struct repository *repo, int skip_if_read) |
bf12fcdf | 836 | { |
ff6f1f56 BW |
837 | submodule_cache_check_init(repo); |
838 | ||
d7992421 MT |
839 | if (repo->submodule_cache->gitmodules_read && skip_if_read) |
840 | return; | |
841 | ||
db64d112 AO |
842 | if (repo_read_index(repo) < 0) |
843 | return; | |
1b796ace | 844 | |
db64d112 AO |
845 | if (!is_gitmodules_unmerged(repo->index)) |
846 | config_from_gitmodules(gitmodules_cb, repo, repo); | |
ff6f1f56 BW |
847 | |
848 | repo->submodule_cache->gitmodules_read = 1; | |
1b796ace BW |
849 | } |
850 | ||
851 | void gitmodules_config_oid(const struct object_id *commit_oid) | |
852 | { | |
853 | struct strbuf rev = STRBUF_INIT; | |
854 | struct object_id oid; | |
855 | ||
ff6f1f56 BW |
856 | submodule_cache_check_init(the_repository); |
857 | ||
1b796ace BW |
858 | if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { |
859 | git_config_from_blob_oid(gitmodules_cb, rev.buf, | |
809d8680 GC |
860 | the_repository, &oid, the_repository, |
861 | CONFIG_SCOPE_UNKNOWN); | |
1b796ace BW |
862 | } |
863 | strbuf_release(&rev); | |
ff6f1f56 BW |
864 | |
865 | the_repository->submodule_cache->gitmodules_read = 1; | |
866 | } | |
867 | ||
3b8fb393 SB |
868 | const struct submodule *submodule_from_name(struct repository *r, |
869 | const struct object_id *treeish_name, | |
959b5455 HV |
870 | const char *name) |
871 | { | |
d7992421 | 872 | repo_read_gitmodules(r, 1); |
3b8fb393 | 873 | return config_from(r->submodule_cache, treeish_name, name, lookup_name); |
959b5455 HV |
874 | } |
875 | ||
3b8fb393 SB |
876 | const struct submodule *submodule_from_path(struct repository *r, |
877 | const struct object_id *treeish_name, | |
959b5455 HV |
878 | const char *path) |
879 | { | |
d7992421 | 880 | repo_read_gitmodules(r, 1); |
3b8fb393 | 881 | return config_from(r->submodule_cache, treeish_name, path, lookup_path); |
bf12fcdf BW |
882 | } |
883 | ||
961b130d GC |
884 | /** |
885 | * Used internally by submodules_of_tree(). Recurses into 'treeish_name' | |
886 | * and appends submodule entries to 'out'. The submodule_cache expects | |
887 | * a root-level treeish_name and paths, so keep track of these values | |
888 | * with 'root_tree' and 'prefix'. | |
889 | */ | |
890 | static void traverse_tree_submodules(struct repository *r, | |
891 | const struct object_id *root_tree, | |
892 | char *prefix, | |
893 | const struct object_id *treeish_name, | |
894 | struct submodule_entry_list *out) | |
895 | { | |
896 | struct tree_desc tree; | |
897 | struct submodule_tree_entry *st_entry; | |
898 | struct name_entry *name_entry; | |
899 | char *tree_path = NULL; | |
900 | ||
901 | name_entry = xmalloc(sizeof(*name_entry)); | |
902 | ||
903 | fill_tree_descriptor(r, &tree, treeish_name); | |
904 | while (tree_entry(&tree, name_entry)) { | |
905 | if (prefix) | |
906 | tree_path = | |
907 | mkpathdup("%s/%s", prefix, name_entry->path); | |
908 | else | |
909 | tree_path = xstrdup(name_entry->path); | |
910 | ||
911 | if (S_ISGITLINK(name_entry->mode) && | |
912 | is_tree_submodule_active(r, root_tree, tree_path)) { | |
f5355922 JS |
913 | ALLOC_GROW(out->entries, out->entry_nr + 1, |
914 | out->entry_alloc); | |
915 | st_entry = &out->entries[out->entry_nr++]; | |
916 | ||
961b130d GC |
917 | st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry)); |
918 | *st_entry->name_entry = *name_entry; | |
919 | st_entry->submodule = | |
920 | submodule_from_path(r, root_tree, tree_path); | |
921 | st_entry->repo = xmalloc(sizeof(*st_entry->repo)); | |
922 | if (repo_submodule_init(st_entry->repo, r, tree_path, | |
923 | root_tree)) | |
924 | FREE_AND_NULL(st_entry->repo); | |
925 | ||
961b130d GC |
926 | } else if (S_ISDIR(name_entry->mode)) |
927 | traverse_tree_submodules(r, root_tree, tree_path, | |
928 | &name_entry->oid, out); | |
929 | free(tree_path); | |
930 | } | |
931 | } | |
932 | ||
933 | void submodules_of_tree(struct repository *r, | |
934 | const struct object_id *treeish_name, | |
935 | struct submodule_entry_list *out) | |
936 | { | |
937 | CALLOC_ARRAY(out->entries, 0); | |
938 | out->entry_nr = 0; | |
939 | out->entry_alloc = 0; | |
940 | ||
941 | traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out); | |
942 | } | |
943 | ||
f793b895 | 944 | void submodule_free(struct repository *r) |
bf12fcdf | 945 | { |
f793b895 SB |
946 | if (r->submodule_cache) |
947 | submodule_cache_clear(r->submodule_cache); | |
959b5455 | 948 | } |
ad136370 | 949 | |
a4e7e317 GC |
950 | static int config_print_callback(const char *var, const char *value, |
951 | const struct config_context *ctx UNUSED, | |
952 | void *cb_data) | |
bcbc780d AO |
953 | { |
954 | char *wanted_key = cb_data; | |
955 | ||
956 | if (!strcmp(wanted_key, var)) | |
957 | printf("%s\n", value); | |
958 | ||
959 | return 0; | |
960 | } | |
961 | ||
962 | int print_config_from_gitmodules(struct repository *repo, const char *key) | |
963 | { | |
964 | int ret; | |
965 | char *store_key; | |
966 | ||
967 | ret = git_config_parse_key(key, &store_key, NULL); | |
968 | if (ret < 0) | |
969 | return CONFIG_INVALID_KEY; | |
970 | ||
971 | config_from_gitmodules(config_print_callback, repo, store_key); | |
972 | ||
973 | free(store_key); | |
974 | return 0; | |
975 | } | |
976 | ||
45f5ef3d AO |
977 | int config_set_in_gitmodules_file_gently(const char *key, const char *value) |
978 | { | |
979 | int ret; | |
980 | ||
981 | ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value); | |
982 | if (ret < 0) | |
983 | /* Maybe the user already did that, don't error out here */ | |
984 | warning(_("Could not update .gitmodules entry %s"), key); | |
985 | ||
986 | return ret; | |
987 | } | |
988 | ||
71a6953d AO |
989 | struct fetch_config { |
990 | int *max_children; | |
991 | int *recurse_submodules; | |
992 | }; | |
993 | ||
a4e7e317 | 994 | static int gitmodules_fetch_config(const char *var, const char *value, |
8868b1eb | 995 | const struct config_context *ctx, |
a4e7e317 | 996 | void *cb) |
71a6953d AO |
997 | { |
998 | struct fetch_config *config = cb; | |
999 | if (!strcmp(var, "submodule.fetchjobs")) { | |
e5b94213 JT |
1000 | if (config->max_children) |
1001 | *(config->max_children) = | |
8868b1eb | 1002 | parse_submodule_fetchjobs(var, value, ctx->kvi); |
71a6953d AO |
1003 | return 0; |
1004 | } else if (!strcmp(var, "fetch.recursesubmodules")) { | |
e5b94213 JT |
1005 | if (config->recurse_submodules) |
1006 | *(config->recurse_submodules) = | |
1007 | parse_fetch_recurse_submodules_arg(var, value); | |
71a6953d AO |
1008 | return 0; |
1009 | } | |
1010 | ||
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules) | |
1015 | { | |
1016 | struct fetch_config config = { | |
1017 | .max_children = max_children, | |
1018 | .recurse_submodules = recurse_submodules | |
1019 | }; | |
9a0fb3e7 | 1020 | config_from_gitmodules(gitmodules_fetch_config, the_repository, &config); |
71a6953d | 1021 | } |
05744997 AO |
1022 | |
1023 | static int gitmodules_update_clone_config(const char *var, const char *value, | |
8868b1eb | 1024 | const struct config_context *ctx, |
05744997 AO |
1025 | void *cb) |
1026 | { | |
1027 | int *max_jobs = cb; | |
1028 | if (!strcmp(var, "submodule.fetchjobs")) | |
8868b1eb | 1029 | *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi); |
05744997 AO |
1030 | return 0; |
1031 | } | |
1032 | ||
1033 | void update_clone_config_from_gitmodules(int *max_jobs) | |
1034 | { | |
9a0fb3e7 | 1035 | config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs); |
05744997 | 1036 | } |