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