]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #define DISABLE_SIGN_COMPARE_WARNINGS | |
3 | ||
4 | #include "git-compat-util.h" | |
5 | #include "dir.h" | |
6 | #include "environment.h" | |
7 | #include "gettext.h" | |
8 | #include "hex.h" | |
9 | #include "path.h" | |
10 | #include "repository.h" | |
11 | #include "config.h" | |
12 | #include "submodule-config.h" | |
13 | #include "submodule.h" | |
14 | #include "strbuf.h" | |
15 | #include "object-name.h" | |
16 | #include "object-store.h" | |
17 | #include "parse-options.h" | |
18 | #include "thread-utils.h" | |
19 | #include "tree-walk.h" | |
20 | #include "url.h" | |
21 | #include "urlmatch.h" | |
22 | ||
23 | /* | |
24 | * submodule cache lookup structure | |
25 | * There is one shared set of 'struct submodule' entries which can be | |
26 | * looked up by their sha1 blob id of the .gitmodules file and either | |
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 | */ | |
31 | struct submodule_cache { | |
32 | struct hashmap for_path; | |
33 | struct hashmap for_name; | |
34 | unsigned initialized:1; | |
35 | unsigned gitmodules_read:1; | |
36 | }; | |
37 | ||
38 | /* | |
39 | * thin wrapper struct needed to insert 'struct submodule' entries to | |
40 | * the hashmap | |
41 | */ | |
42 | struct submodule_entry { | |
43 | struct hashmap_entry ent; | |
44 | struct submodule *config; | |
45 | }; | |
46 | ||
47 | enum lookup_type { | |
48 | lookup_name, | |
49 | lookup_path | |
50 | }; | |
51 | ||
52 | static int config_path_cmp(const void *cmp_data UNUSED, | |
53 | const struct hashmap_entry *eptr, | |
54 | const struct hashmap_entry *entry_or_key, | |
55 | const void *keydata UNUSED) | |
56 | { | |
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); | |
61 | ||
62 | return strcmp(a->config->path, b->config->path) || | |
63 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); | |
64 | } | |
65 | ||
66 | static int config_name_cmp(const void *cmp_data UNUSED, | |
67 | const struct hashmap_entry *eptr, | |
68 | const struct hashmap_entry *entry_or_key, | |
69 | const void *keydata UNUSED) | |
70 | { | |
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); | |
75 | ||
76 | return strcmp(a->config->name, b->config->name) || | |
77 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); | |
78 | } | |
79 | ||
80 | static struct submodule_cache *submodule_cache_alloc(void) | |
81 | { | |
82 | return xcalloc(1, sizeof(struct submodule_cache)); | |
83 | } | |
84 | ||
85 | static void submodule_cache_init(struct submodule_cache *cache) | |
86 | { | |
87 | hashmap_init(&cache->for_path, config_path_cmp, NULL, 0); | |
88 | hashmap_init(&cache->for_name, config_name_cmp, NULL, 0); | |
89 | cache->initialized = 1; | |
90 | } | |
91 | ||
92 | static void free_one_config(struct submodule_entry *entry) | |
93 | { | |
94 | free((void *) entry->config->path); | |
95 | free((void *) entry->config->name); | |
96 | free((void *) entry->config->branch); | |
97 | free((void *) entry->config->url); | |
98 | free((void *) entry->config->ignore); | |
99 | submodule_update_strategy_release(&entry->config->update_strategy); | |
100 | free(entry->config); | |
101 | } | |
102 | ||
103 | static void submodule_cache_clear(struct submodule_cache *cache) | |
104 | { | |
105 | struct hashmap_iter iter; | |
106 | struct submodule_entry *entry; | |
107 | ||
108 | if (!cache->initialized) | |
109 | return; | |
110 | ||
111 | /* | |
112 | * We iterate over the name hash here to be symmetric with the | |
113 | * allocation of struct submodule entries. Each is allocated by | |
114 | * their .gitmodules blob sha1 and submodule name. | |
115 | */ | |
116 | hashmap_for_each_entry(&cache->for_name, &iter, entry, | |
117 | ent /* member name */) | |
118 | free_one_config(entry); | |
119 | ||
120 | hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent); | |
121 | hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent); | |
122 | cache->initialized = 0; | |
123 | cache->gitmodules_read = 0; | |
124 | } | |
125 | ||
126 | void submodule_cache_free(struct submodule_cache *cache) | |
127 | { | |
128 | submodule_cache_clear(cache); | |
129 | free(cache); | |
130 | } | |
131 | ||
132 | static unsigned int hash_oid_string(const struct object_id *oid, | |
133 | const char *string) | |
134 | { | |
135 | return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string); | |
136 | } | |
137 | ||
138 | static void cache_put_path(struct submodule_cache *cache, | |
139 | struct submodule *submodule) | |
140 | { | |
141 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, | |
142 | submodule->path); | |
143 | struct submodule_entry *e = xmalloc(sizeof(*e)); | |
144 | hashmap_entry_init(&e->ent, hash); | |
145 | e->config = submodule; | |
146 | hashmap_put(&cache->for_path, &e->ent); | |
147 | } | |
148 | ||
149 | static void cache_remove_path(struct submodule_cache *cache, | |
150 | struct submodule *submodule) | |
151 | { | |
152 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, | |
153 | submodule->path); | |
154 | struct submodule_entry e; | |
155 | struct submodule_entry *removed; | |
156 | hashmap_entry_init(&e.ent, hash); | |
157 | e.config = submodule; | |
158 | removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL); | |
159 | free(removed); | |
160 | } | |
161 | ||
162 | static void cache_add(struct submodule_cache *cache, | |
163 | struct submodule *submodule) | |
164 | { | |
165 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, | |
166 | submodule->name); | |
167 | struct submodule_entry *e = xmalloc(sizeof(*e)); | |
168 | hashmap_entry_init(&e->ent, hash); | |
169 | e->config = submodule; | |
170 | hashmap_add(&cache->for_name, &e->ent); | |
171 | } | |
172 | ||
173 | static const struct submodule *cache_lookup_path(struct submodule_cache *cache, | |
174 | const struct object_id *gitmodules_oid, const char *path) | |
175 | { | |
176 | struct submodule_entry *entry; | |
177 | unsigned int hash = hash_oid_string(gitmodules_oid, path); | |
178 | struct submodule_entry key; | |
179 | struct submodule key_config; | |
180 | ||
181 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); | |
182 | key_config.path = path; | |
183 | ||
184 | hashmap_entry_init(&key.ent, hash); | |
185 | key.config = &key_config; | |
186 | ||
187 | entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL); | |
188 | if (entry) | |
189 | return entry->config; | |
190 | return NULL; | |
191 | } | |
192 | ||
193 | static struct submodule *cache_lookup_name(struct submodule_cache *cache, | |
194 | const struct object_id *gitmodules_oid, const char *name) | |
195 | { | |
196 | struct submodule_entry *entry; | |
197 | unsigned int hash = hash_oid_string(gitmodules_oid, name); | |
198 | struct submodule_entry key; | |
199 | struct submodule key_config; | |
200 | ||
201 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); | |
202 | key_config.name = name; | |
203 | ||
204 | hashmap_entry_init(&key.ent, hash); | |
205 | key.config = &key_config; | |
206 | ||
207 | entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL); | |
208 | if (entry) | |
209 | return entry->config; | |
210 | return NULL; | |
211 | } | |
212 | ||
213 | int check_submodule_name(const char *name) | |
214 | { | |
215 | /* Disallow empty names */ | |
216 | if (!*name) | |
217 | return -1; | |
218 | ||
219 | /* | |
220 | * Look for '..' as a path component. Check is_xplatform_dir_sep() as | |
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++; | |
227 | if (is_xplatform_dir_sep(c)) { | |
228 | in_component: | |
229 | if (name[0] == '.' && name[1] == '.' && | |
230 | (!name[2] || is_xplatform_dir_sep(name[2]))) | |
231 | return -1; | |
232 | } | |
233 | } | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static 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 | ||
244 | static 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 | ||
250 | static 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 | */ | |
266 | static 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 | */ | |
299 | static 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 | ||
322 | int 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)) { | |
358 | int ret = 0; | |
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 { | |
367 | ret = -1; | |
368 | } | |
369 | ||
370 | return ret; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | static int name_and_item_from_var(const char *var, struct strbuf *name, | |
377 | struct strbuf *item) | |
378 | { | |
379 | const char *subsection, *key; | |
380 | size_t subsection_len; | |
381 | int parse; | |
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); | |
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 | ||
394 | strbuf_addstr(item, key); | |
395 | ||
396 | return 1; | |
397 | } | |
398 | ||
399 | static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache, | |
400 | const struct object_id *gitmodules_oid, const char *name) | |
401 | { | |
402 | struct submodule *submodule; | |
403 | struct strbuf name_buf = STRBUF_INIT; | |
404 | ||
405 | submodule = cache_lookup_name(cache, gitmodules_oid, name); | |
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; | |
416 | submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED; | |
417 | submodule->update_strategy.command = NULL; | |
418 | submodule->fetch_recurse = RECURSE_SUBMODULES_NONE; | |
419 | submodule->ignore = NULL; | |
420 | submodule->branch = NULL; | |
421 | submodule->recommend_shallow = -1; | |
422 | ||
423 | oidcpy(&submodule->gitmodules_oid, gitmodules_oid); | |
424 | ||
425 | cache_add(cache, submodule); | |
426 | ||
427 | return submodule; | |
428 | } | |
429 | ||
430 | static int parse_fetch_recurse(const char *opt, const char *arg, | |
431 | int die_on_error) | |
432 | { | |
433 | switch (git_parse_maybe_bool(arg)) { | |
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; | |
441 | /* | |
442 | * Please update $__git_fetch_recurse_submodules in | |
443 | * git-completion.bash when you add new options. | |
444 | */ | |
445 | if (die_on_error) | |
446 | die("bad %s argument: %s", opt, arg); | |
447 | else | |
448 | return RECURSE_SUBMODULES_ERROR; | |
449 | } | |
450 | } | |
451 | ||
452 | int parse_submodule_fetchjobs(const char *var, const char *value, | |
453 | const struct key_value_info *kvi) | |
454 | { | |
455 | int fetchjobs = git_config_int(var, value, kvi); | |
456 | if (fetchjobs < 0) | |
457 | die(_("negative values not allowed for submodule.fetchJobs")); | |
458 | if (!fetchjobs) | |
459 | fetchjobs = online_cpus(); | |
460 | return fetchjobs; | |
461 | } | |
462 | ||
463 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) | |
464 | { | |
465 | return parse_fetch_recurse(opt, arg, 1); | |
466 | } | |
467 | ||
468 | int 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 | ||
489 | static int parse_update_recurse(const char *opt, const char *arg, | |
490 | int die_on_error) | |
491 | { | |
492 | switch (git_parse_maybe_bool(arg)) { | |
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 | ||
504 | int parse_update_recurse_submodules_arg(const char *opt, const char *arg) | |
505 | { | |
506 | return parse_update_recurse(opt, arg, 1); | |
507 | } | |
508 | ||
509 | static int parse_push_recurse(const char *opt, const char *arg, | |
510 | int die_on_error) | |
511 | { | |
512 | switch (git_parse_maybe_bool(arg)) { | |
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; | |
526 | else if (!strcmp(arg, "only")) | |
527 | return RECURSE_SUBMODULES_ONLY; | |
528 | /* | |
529 | * Please update $__git_push_recurse_submodules in | |
530 | * git-completion.bash when you add new modes. | |
531 | */ | |
532 | else if (die_on_error) | |
533 | die("bad %s argument: %s", opt, arg); | |
534 | else | |
535 | return RECURSE_SUBMODULES_ERROR; | |
536 | } | |
537 | } | |
538 | ||
539 | int parse_push_recurse_submodules_arg(const char *opt, const char *arg) | |
540 | { | |
541 | return parse_push_recurse(opt, arg, 1); | |
542 | } | |
543 | ||
544 | static void warn_multiple_config(const struct object_id *treeish_name, | |
545 | const char *name, const char *option) | |
546 | { | |
547 | const char *commit_string = "WORKTREE"; | |
548 | if (treeish_name) | |
549 | commit_string = oid_to_hex(treeish_name); | |
550 | warning("%s:.gitmodules, multiple configurations found for " | |
551 | "'submodule.%s.%s'. Skipping second one!", | |
552 | commit_string, name, option); | |
553 | } | |
554 | ||
555 | static 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 | ||
561 | struct parse_config_parameter { | |
562 | struct submodule_cache *cache; | |
563 | const struct object_id *treeish_name; | |
564 | const struct object_id *gitmodules_oid; | |
565 | int overwrite; | |
566 | }; | |
567 | ||
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 | */ | |
575 | static int parse_config(const char *var, const char *value, | |
576 | const struct config_context *ctx UNUSED, void *data) | |
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 | ||
587 | submodule = lookup_or_create_by_name(me->cache, | |
588 | me->gitmodules_oid, | |
589 | name.buf); | |
590 | ||
591 | if (!strcmp(item.buf, "path")) { | |
592 | if (!value) | |
593 | ret = config_error_nonbool(var); | |
594 | else if (looks_like_command_line_option(value)) | |
595 | warn_command_line_option(var, value); | |
596 | else if (!me->overwrite && submodule->path) | |
597 | warn_multiple_config(me->treeish_name, submodule->name, | |
598 | "path"); | |
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); | |
605 | } | |
606 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { | |
607 | /* when parsing worktree configurations we can die early */ | |
608 | int die_on_error = is_null_oid(me->gitmodules_oid); | |
609 | if (!me->overwrite && | |
610 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) | |
611 | warn_multiple_config(me->treeish_name, submodule->name, | |
612 | "fetchrecursesubmodules"); | |
613 | else | |
614 | submodule->fetch_recurse = parse_fetch_recurse( | |
615 | var, value, | |
616 | die_on_error); | |
617 | } else if (!strcmp(item.buf, "ignore")) { | |
618 | if (!value) | |
619 | ret = config_error_nonbool(var); | |
620 | else if (!me->overwrite && submodule->ignore) | |
621 | warn_multiple_config(me->treeish_name, submodule->name, | |
622 | "ignore"); | |
623 | else if (strcmp(value, "untracked") && | |
624 | strcmp(value, "dirty") && | |
625 | strcmp(value, "all") && | |
626 | strcmp(value, "none")) | |
627 | warning("Invalid parameter '%s' for config option " | |
628 | "'submodule.%s.ignore'", value, name.buf); | |
629 | else { | |
630 | free((void *) submodule->ignore); | |
631 | submodule->ignore = xstrdup(value); | |
632 | } | |
633 | } else if (!strcmp(item.buf, "url")) { | |
634 | if (!value) { | |
635 | ret = config_error_nonbool(var); | |
636 | } else if (looks_like_command_line_option(value)) { | |
637 | warn_command_line_option(var, value); | |
638 | } else if (!me->overwrite && submodule->url) { | |
639 | warn_multiple_config(me->treeish_name, submodule->name, | |
640 | "url"); | |
641 | } else { | |
642 | free((void *) submodule->url); | |
643 | submodule->url = xstrdup(value); | |
644 | } | |
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) | |
650 | warn_multiple_config(me->treeish_name, submodule->name, | |
651 | "update"); | |
652 | else if (parse_submodule_update_strategy(value, | |
653 | &submodule->update_strategy) < 0 || | |
654 | submodule->update_strategy.type == SM_UPDATE_COMMAND) | |
655 | die(_("invalid value for '%s'"), var); | |
656 | } else if (!strcmp(item.buf, "shallow")) { | |
657 | if (!me->overwrite && submodule->recommend_shallow != -1) | |
658 | warn_multiple_config(me->treeish_name, submodule->name, | |
659 | "shallow"); | |
660 | else | |
661 | submodule->recommend_shallow = | |
662 | git_config_bool(var, value); | |
663 | } else if (!strcmp(item.buf, "branch")) { | |
664 | if (!value) | |
665 | ret = config_error_nonbool(var); | |
666 | else if (!me->overwrite && submodule->branch) | |
667 | warn_multiple_config(me->treeish_name, submodule->name, | |
668 | "branch"); | |
669 | else { | |
670 | free((void *)submodule->branch); | |
671 | submodule->branch = xstrdup(value); | |
672 | } | |
673 | } | |
674 | ||
675 | strbuf_release(&name); | |
676 | strbuf_release(&item); | |
677 | ||
678 | return ret; | |
679 | } | |
680 | ||
681 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, | |
682 | struct object_id *gitmodules_oid, | |
683 | struct strbuf *rev) | |
684 | { | |
685 | int ret = 0; | |
686 | ||
687 | if (is_null_oid(treeish_name)) { | |
688 | oidclr(gitmodules_oid, the_repository->hash_algo); | |
689 | return 1; | |
690 | } | |
691 | ||
692 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); | |
693 | if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0) | |
694 | ret = 1; | |
695 | ||
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 | */ | |
703 | static const struct submodule *config_from(struct submodule_cache *cache, | |
704 | const struct object_id *treeish_name, const char *key, | |
705 | enum lookup_type lookup_type) | |
706 | { | |
707 | struct strbuf rev = STRBUF_INIT; | |
708 | unsigned long config_size; | |
709 | char *config = NULL; | |
710 | struct object_id oid; | |
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 | */ | |
720 | if (!treeish_name || !key) { | |
721 | struct hashmap_iter iter; | |
722 | struct submodule_entry *entry; | |
723 | ||
724 | entry = hashmap_iter_first_entry(&cache->for_name, &iter, | |
725 | struct submodule_entry, | |
726 | ent /* member name */); | |
727 | if (!entry) | |
728 | return NULL; | |
729 | return entry->config; | |
730 | } | |
731 | ||
732 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) | |
733 | goto out; | |
734 | ||
735 | switch (lookup_type) { | |
736 | case lookup_name: | |
737 | submodule = cache_lookup_name(cache, &oid, key); | |
738 | break; | |
739 | case lookup_path: | |
740 | submodule = cache_lookup_path(cache, &oid, key); | |
741 | break; | |
742 | } | |
743 | if (submodule) | |
744 | goto out; | |
745 | ||
746 | config = repo_read_object_file(the_repository, &oid, &type, | |
747 | &config_size); | |
748 | if (!config || type != OBJ_BLOB) | |
749 | goto out; | |
750 | ||
751 | /* fill the submodule config into the cache */ | |
752 | parameter.cache = cache; | |
753 | parameter.treeish_name = treeish_name; | |
754 | parameter.gitmodules_oid = &oid; | |
755 | parameter.overwrite = 0; | |
756 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, | |
757 | config, config_size, ¶meter, CONFIG_SCOPE_UNKNOWN, NULL); | |
758 | strbuf_release(&rev); | |
759 | free(config); | |
760 | ||
761 | switch (lookup_type) { | |
762 | case lookup_name: | |
763 | return cache_lookup_name(cache, &oid, key); | |
764 | case lookup_path: | |
765 | return cache_lookup_path(cache, &oid, key); | |
766 | default: | |
767 | return NULL; | |
768 | } | |
769 | ||
770 | out: | |
771 | strbuf_release(&rev); | |
772 | free(config); | |
773 | return submodule; | |
774 | } | |
775 | ||
776 | static void submodule_cache_check_init(struct repository *repo) | |
777 | { | |
778 | if (repo->submodule_cache && repo->submodule_cache->initialized) | |
779 | return; | |
780 | ||
781 | if (!repo->submodule_cache) | |
782 | repo->submodule_cache = submodule_cache_alloc(); | |
783 | ||
784 | submodule_cache_init(repo->submodule_cache); | |
785 | } | |
786 | ||
787 | /* | |
788 | * Note: This function is private for a reason, the '.gitmodules' file should | |
789 | * not be used as a mechanism to retrieve arbitrary configuration stored in | |
790 | * the repository. | |
791 | * | |
792 | * Runs the provided config function on the '.gitmodules' file found in the | |
793 | * working directory. | |
794 | */ | |
795 | static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data) | |
796 | { | |
797 | if (repo->worktree) { | |
798 | struct git_config_source config_source = { | |
799 | 0, .scope = CONFIG_SCOPE_SUBMODULE | |
800 | }; | |
801 | const struct config_options opts = { 0 }; | |
802 | struct object_id oid; | |
803 | char *file; | |
804 | char *oidstr = NULL; | |
805 | ||
806 | file = repo_worktree_path(repo, GITMODULES_FILE); | |
807 | if (file_exists(file)) { | |
808 | config_source.file = file; | |
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) | |
813 | add_submodule_odb_by_path(repo->objects->odb->path); | |
814 | } else { | |
815 | goto out; | |
816 | } | |
817 | ||
818 | config_with_options(fn, data, &config_source, repo, &opts); | |
819 | ||
820 | out: | |
821 | free(oidstr); | |
822 | free(file); | |
823 | } | |
824 | } | |
825 | ||
826 | static int gitmodules_cb(const char *var, const char *value, | |
827 | const struct config_context *ctx, void *data) | |
828 | { | |
829 | struct repository *repo = data; | |
830 | struct parse_config_parameter parameter; | |
831 | ||
832 | parameter.cache = repo->submodule_cache; | |
833 | parameter.treeish_name = NULL; | |
834 | parameter.gitmodules_oid = null_oid(the_hash_algo); | |
835 | parameter.overwrite = 1; | |
836 | ||
837 | return parse_config(var, value, ctx, ¶meter); | |
838 | } | |
839 | ||
840 | void repo_read_gitmodules(struct repository *repo, int skip_if_read) | |
841 | { | |
842 | submodule_cache_check_init(repo); | |
843 | ||
844 | if (repo->submodule_cache->gitmodules_read && skip_if_read) | |
845 | return; | |
846 | ||
847 | if (repo_read_index(repo) < 0) | |
848 | return; | |
849 | ||
850 | if (!is_gitmodules_unmerged(repo->index)) | |
851 | config_from_gitmodules(gitmodules_cb, repo, repo); | |
852 | ||
853 | repo->submodule_cache->gitmodules_read = 1; | |
854 | } | |
855 | ||
856 | void gitmodules_config_oid(const struct object_id *commit_oid) | |
857 | { | |
858 | struct strbuf rev = STRBUF_INIT; | |
859 | struct object_id oid; | |
860 | ||
861 | submodule_cache_check_init(the_repository); | |
862 | ||
863 | if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { | |
864 | git_config_from_blob_oid(gitmodules_cb, rev.buf, | |
865 | the_repository, &oid, the_repository, | |
866 | CONFIG_SCOPE_UNKNOWN); | |
867 | } | |
868 | strbuf_release(&rev); | |
869 | ||
870 | the_repository->submodule_cache->gitmodules_read = 1; | |
871 | } | |
872 | ||
873 | const struct submodule *submodule_from_name(struct repository *r, | |
874 | const struct object_id *treeish_name, | |
875 | const char *name) | |
876 | { | |
877 | repo_read_gitmodules(r, 1); | |
878 | return config_from(r->submodule_cache, treeish_name, name, lookup_name); | |
879 | } | |
880 | ||
881 | const struct submodule *submodule_from_path(struct repository *r, | |
882 | const struct object_id *treeish_name, | |
883 | const char *path) | |
884 | { | |
885 | repo_read_gitmodules(r, 1); | |
886 | return config_from(r->submodule_cache, treeish_name, path, lookup_path); | |
887 | } | |
888 | ||
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 | */ | |
895 | static 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; | |
903 | struct name_entry name_entry; | |
904 | char *tree_path = NULL; | |
905 | char *tree_buf; | |
906 | ||
907 | tree_buf = fill_tree_descriptor(r, &tree, treeish_name); | |
908 | while (tree_entry(&tree, &name_entry)) { | |
909 | if (prefix) | |
910 | tree_path = | |
911 | mkpathdup("%s/%s", prefix, name_entry.path); | |
912 | else | |
913 | tree_path = xstrdup(name_entry.path); | |
914 | ||
915 | if (S_ISGITLINK(name_entry.mode) && | |
916 | is_tree_submodule_active(r, root_tree, tree_path)) { | |
917 | ALLOC_GROW(out->entries, out->entry_nr + 1, | |
918 | out->entry_alloc); | |
919 | st_entry = &out->entries[out->entry_nr++]; | |
920 | ||
921 | st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry)); | |
922 | *st_entry->name_entry = name_entry; | |
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 | ||
930 | } else if (S_ISDIR(name_entry.mode)) | |
931 | traverse_tree_submodules(r, root_tree, tree_path, | |
932 | &name_entry.oid, out); | |
933 | free(tree_path); | |
934 | } | |
935 | ||
936 | free(tree_buf); | |
937 | } | |
938 | ||
939 | void 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 | ||
950 | void 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 | ||
960 | void submodule_free(struct repository *r) | |
961 | { | |
962 | if (r->submodule_cache) | |
963 | submodule_cache_clear(r->submodule_cache); | |
964 | } | |
965 | ||
966 | static int config_print_callback(const char *var, const char *value, | |
967 | const struct config_context *ctx UNUSED, | |
968 | void *cb_data) | |
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 | ||
978 | int 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 | ||
993 | int config_set_in_gitmodules_file_gently(const char *key, const char *value) | |
994 | { | |
995 | int ret; | |
996 | ||
997 | ret = git_config_set_in_file_gently(GITMODULES_FILE, key, NULL, value); | |
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 | ||
1005 | struct fetch_config { | |
1006 | int *max_children; | |
1007 | int *recurse_submodules; | |
1008 | }; | |
1009 | ||
1010 | static int gitmodules_fetch_config(const char *var, const char *value, | |
1011 | const struct config_context *ctx, | |
1012 | void *cb) | |
1013 | { | |
1014 | struct fetch_config *config = cb; | |
1015 | if (!strcmp(var, "submodule.fetchjobs")) { | |
1016 | if (config->max_children) | |
1017 | *(config->max_children) = | |
1018 | parse_submodule_fetchjobs(var, value, ctx->kvi); | |
1019 | return 0; | |
1020 | } else if (!strcmp(var, "fetch.recursesubmodules")) { | |
1021 | if (config->recurse_submodules) | |
1022 | *(config->recurse_submodules) = | |
1023 | parse_fetch_recurse_submodules_arg(var, value); | |
1024 | return 0; | |
1025 | } | |
1026 | ||
1027 | return 0; | |
1028 | } | |
1029 | ||
1030 | void 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 | }; | |
1036 | config_from_gitmodules(gitmodules_fetch_config, the_repository, &config); | |
1037 | } | |
1038 | ||
1039 | static int gitmodules_update_clone_config(const char *var, const char *value, | |
1040 | const struct config_context *ctx, | |
1041 | void *cb) | |
1042 | { | |
1043 | int *max_jobs = cb; | |
1044 | if (!strcmp(var, "submodule.fetchjobs")) | |
1045 | *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi); | |
1046 | return 0; | |
1047 | } | |
1048 | ||
1049 | void update_clone_config_from_gitmodules(int *max_jobs) | |
1050 | { | |
1051 | config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs); | |
1052 | } |