]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "dir.h" | |
3 | #include "repository.h" | |
4 | #include "config.h" | |
5 | #include "submodule-config.h" | |
6 | #include "submodule.h" | |
7 | #include "strbuf.h" | |
8 | #include "object-store.h" | |
9 | #include "parse-options.h" | |
10 | ||
11 | /* | |
12 | * submodule cache lookup structure | |
13 | * There is one shared set of 'struct submodule' entries which can be | |
14 | * looked up by their sha1 blob id of the .gitmodules file and either | |
15 | * using path or name as key. | |
16 | * for_path stores submodule entries with path as key | |
17 | * for_name stores submodule entries with name as key | |
18 | */ | |
19 | struct submodule_cache { | |
20 | struct hashmap for_path; | |
21 | struct hashmap for_name; | |
22 | unsigned initialized:1; | |
23 | unsigned gitmodules_read:1; | |
24 | }; | |
25 | ||
26 | /* | |
27 | * thin wrapper struct needed to insert 'struct submodule' entries to | |
28 | * the hashmap | |
29 | */ | |
30 | struct submodule_entry { | |
31 | struct hashmap_entry ent; | |
32 | struct submodule *config; | |
33 | }; | |
34 | ||
35 | enum lookup_type { | |
36 | lookup_name, | |
37 | lookup_path | |
38 | }; | |
39 | ||
40 | static int config_path_cmp(const void *unused_cmp_data, | |
41 | const void *entry, | |
42 | const void *entry_or_key, | |
43 | const void *unused_keydata) | |
44 | { | |
45 | const struct submodule_entry *a = entry; | |
46 | const struct submodule_entry *b = entry_or_key; | |
47 | ||
48 | return strcmp(a->config->path, b->config->path) || | |
49 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); | |
50 | } | |
51 | ||
52 | static int config_name_cmp(const void *unused_cmp_data, | |
53 | const void *entry, | |
54 | const void *entry_or_key, | |
55 | const void *unused_keydata) | |
56 | { | |
57 | const struct submodule_entry *a = entry; | |
58 | const struct submodule_entry *b = entry_or_key; | |
59 | ||
60 | return strcmp(a->config->name, b->config->name) || | |
61 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); | |
62 | } | |
63 | ||
64 | static struct submodule_cache *submodule_cache_alloc(void) | |
65 | { | |
66 | return xcalloc(1, sizeof(struct submodule_cache)); | |
67 | } | |
68 | ||
69 | static void submodule_cache_init(struct submodule_cache *cache) | |
70 | { | |
71 | hashmap_init(&cache->for_path, config_path_cmp, NULL, 0); | |
72 | hashmap_init(&cache->for_name, config_name_cmp, NULL, 0); | |
73 | cache->initialized = 1; | |
74 | } | |
75 | ||
76 | static void free_one_config(struct submodule_entry *entry) | |
77 | { | |
78 | free((void *) entry->config->path); | |
79 | free((void *) entry->config->name); | |
80 | free((void *) entry->config->branch); | |
81 | free((void *) entry->config->update_strategy.command); | |
82 | free(entry->config); | |
83 | } | |
84 | ||
85 | static void submodule_cache_clear(struct submodule_cache *cache) | |
86 | { | |
87 | struct hashmap_iter iter; | |
88 | struct submodule_entry *entry; | |
89 | ||
90 | if (!cache->initialized) | |
91 | return; | |
92 | ||
93 | /* | |
94 | * We iterate over the name hash here to be symmetric with the | |
95 | * allocation of struct submodule entries. Each is allocated by | |
96 | * their .gitmodules blob sha1 and submodule name. | |
97 | */ | |
98 | hashmap_iter_init(&cache->for_name, &iter); | |
99 | while ((entry = hashmap_iter_next(&iter))) | |
100 | free_one_config(entry); | |
101 | ||
102 | hashmap_free(&cache->for_path, 1); | |
103 | hashmap_free(&cache->for_name, 1); | |
104 | cache->initialized = 0; | |
105 | cache->gitmodules_read = 0; | |
106 | } | |
107 | ||
108 | void submodule_cache_free(struct submodule_cache *cache) | |
109 | { | |
110 | submodule_cache_clear(cache); | |
111 | free(cache); | |
112 | } | |
113 | ||
114 | static unsigned int hash_oid_string(const struct object_id *oid, | |
115 | const char *string) | |
116 | { | |
117 | return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string); | |
118 | } | |
119 | ||
120 | static void cache_put_path(struct submodule_cache *cache, | |
121 | struct submodule *submodule) | |
122 | { | |
123 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, | |
124 | submodule->path); | |
125 | struct submodule_entry *e = xmalloc(sizeof(*e)); | |
126 | hashmap_entry_init(&e->ent, hash); | |
127 | e->config = submodule; | |
128 | hashmap_put(&cache->for_path, &e->ent); | |
129 | } | |
130 | ||
131 | static void cache_remove_path(struct submodule_cache *cache, | |
132 | struct submodule *submodule) | |
133 | { | |
134 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, | |
135 | submodule->path); | |
136 | struct submodule_entry e; | |
137 | struct submodule_entry *removed; | |
138 | hashmap_entry_init(&e.ent, hash); | |
139 | e.config = submodule; | |
140 | removed = hashmap_remove(&cache->for_path, &e.ent, NULL); | |
141 | free(removed); | |
142 | } | |
143 | ||
144 | static void cache_add(struct submodule_cache *cache, | |
145 | struct submodule *submodule) | |
146 | { | |
147 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, | |
148 | submodule->name); | |
149 | struct submodule_entry *e = xmalloc(sizeof(*e)); | |
150 | hashmap_entry_init(&e->ent, hash); | |
151 | e->config = submodule; | |
152 | hashmap_add(&cache->for_name, &e->ent); | |
153 | } | |
154 | ||
155 | static const struct submodule *cache_lookup_path(struct submodule_cache *cache, | |
156 | const struct object_id *gitmodules_oid, const char *path) | |
157 | { | |
158 | struct submodule_entry *entry; | |
159 | unsigned int hash = hash_oid_string(gitmodules_oid, path); | |
160 | struct submodule_entry key; | |
161 | struct submodule key_config; | |
162 | ||
163 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); | |
164 | key_config.path = path; | |
165 | ||
166 | hashmap_entry_init(&key.ent, hash); | |
167 | key.config = &key_config; | |
168 | ||
169 | entry = hashmap_get_entry(&cache->for_path, &key, NULL, | |
170 | struct submodule_entry, ent); | |
171 | if (entry) | |
172 | return entry->config; | |
173 | return NULL; | |
174 | } | |
175 | ||
176 | static struct submodule *cache_lookup_name(struct submodule_cache *cache, | |
177 | const struct object_id *gitmodules_oid, const char *name) | |
178 | { | |
179 | struct submodule_entry *entry; | |
180 | unsigned int hash = hash_oid_string(gitmodules_oid, name); | |
181 | struct submodule_entry key; | |
182 | struct submodule key_config; | |
183 | ||
184 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); | |
185 | key_config.name = name; | |
186 | ||
187 | hashmap_entry_init(&key.ent, hash); | |
188 | key.config = &key_config; | |
189 | ||
190 | entry = hashmap_get_entry(&cache->for_name, &key, NULL, | |
191 | struct submodule_entry, ent); | |
192 | if (entry) | |
193 | return entry->config; | |
194 | return NULL; | |
195 | } | |
196 | ||
197 | int check_submodule_name(const char *name) | |
198 | { | |
199 | /* Disallow empty names */ | |
200 | if (!*name) | |
201 | return -1; | |
202 | ||
203 | /* | |
204 | * Look for '..' as a path component. Check both '/' and '\\' as | |
205 | * separators rather than is_dir_sep(), because we want the name rules | |
206 | * to be consistent across platforms. | |
207 | */ | |
208 | goto in_component; /* always start inside component */ | |
209 | while (*name) { | |
210 | char c = *name++; | |
211 | if (c == '/' || c == '\\') { | |
212 | in_component: | |
213 | if (name[0] == '.' && name[1] == '.' && | |
214 | (!name[2] || name[2] == '/' || name[2] == '\\')) | |
215 | return -1; | |
216 | } | |
217 | } | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | static int name_and_item_from_var(const char *var, struct strbuf *name, | |
223 | struct strbuf *item) | |
224 | { | |
225 | const char *subsection, *key; | |
226 | int subsection_len, parse; | |
227 | parse = parse_config_key(var, "submodule", &subsection, | |
228 | &subsection_len, &key); | |
229 | if (parse < 0 || !subsection) | |
230 | return 0; | |
231 | ||
232 | strbuf_add(name, subsection, subsection_len); | |
233 | if (check_submodule_name(name->buf) < 0) { | |
234 | warning(_("ignoring suspicious submodule name: %s"), name->buf); | |
235 | strbuf_release(name); | |
236 | return 0; | |
237 | } | |
238 | ||
239 | strbuf_addstr(item, key); | |
240 | ||
241 | return 1; | |
242 | } | |
243 | ||
244 | static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache, | |
245 | const struct object_id *gitmodules_oid, const char *name) | |
246 | { | |
247 | struct submodule *submodule; | |
248 | struct strbuf name_buf = STRBUF_INIT; | |
249 | ||
250 | submodule = cache_lookup_name(cache, gitmodules_oid, name); | |
251 | if (submodule) | |
252 | return submodule; | |
253 | ||
254 | submodule = xmalloc(sizeof(*submodule)); | |
255 | ||
256 | strbuf_addstr(&name_buf, name); | |
257 | submodule->name = strbuf_detach(&name_buf, NULL); | |
258 | ||
259 | submodule->path = NULL; | |
260 | submodule->url = NULL; | |
261 | submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED; | |
262 | submodule->update_strategy.command = NULL; | |
263 | submodule->fetch_recurse = RECURSE_SUBMODULES_NONE; | |
264 | submodule->ignore = NULL; | |
265 | submodule->branch = NULL; | |
266 | submodule->recommend_shallow = -1; | |
267 | ||
268 | oidcpy(&submodule->gitmodules_oid, gitmodules_oid); | |
269 | ||
270 | cache_add(cache, submodule); | |
271 | ||
272 | return submodule; | |
273 | } | |
274 | ||
275 | static int parse_fetch_recurse(const char *opt, const char *arg, | |
276 | int die_on_error) | |
277 | { | |
278 | switch (git_parse_maybe_bool(arg)) { | |
279 | case 1: | |
280 | return RECURSE_SUBMODULES_ON; | |
281 | case 0: | |
282 | return RECURSE_SUBMODULES_OFF; | |
283 | default: | |
284 | if (!strcmp(arg, "on-demand")) | |
285 | return RECURSE_SUBMODULES_ON_DEMAND; | |
286 | /* | |
287 | * Please update $__git_fetch_recurse_submodules in | |
288 | * git-completion.bash when you add new options. | |
289 | */ | |
290 | if (die_on_error) | |
291 | die("bad %s argument: %s", opt, arg); | |
292 | else | |
293 | return RECURSE_SUBMODULES_ERROR; | |
294 | } | |
295 | } | |
296 | ||
297 | int parse_submodule_fetchjobs(const char *var, const char *value) | |
298 | { | |
299 | int fetchjobs = git_config_int(var, value); | |
300 | if (fetchjobs < 0) | |
301 | die(_("negative values not allowed for submodule.fetchjobs")); | |
302 | return fetchjobs; | |
303 | } | |
304 | ||
305 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) | |
306 | { | |
307 | return parse_fetch_recurse(opt, arg, 1); | |
308 | } | |
309 | ||
310 | int option_fetch_parse_recurse_submodules(const struct option *opt, | |
311 | const char *arg, int unset) | |
312 | { | |
313 | int *v; | |
314 | ||
315 | if (!opt->value) | |
316 | return -1; | |
317 | ||
318 | v = opt->value; | |
319 | ||
320 | if (unset) { | |
321 | *v = RECURSE_SUBMODULES_OFF; | |
322 | } else { | |
323 | if (arg) | |
324 | *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg); | |
325 | else | |
326 | *v = RECURSE_SUBMODULES_ON; | |
327 | } | |
328 | return 0; | |
329 | } | |
330 | ||
331 | static int parse_update_recurse(const char *opt, const char *arg, | |
332 | int die_on_error) | |
333 | { | |
334 | switch (git_parse_maybe_bool(arg)) { | |
335 | case 1: | |
336 | return RECURSE_SUBMODULES_ON; | |
337 | case 0: | |
338 | return RECURSE_SUBMODULES_OFF; | |
339 | default: | |
340 | if (die_on_error) | |
341 | die("bad %s argument: %s", opt, arg); | |
342 | return RECURSE_SUBMODULES_ERROR; | |
343 | } | |
344 | } | |
345 | ||
346 | int parse_update_recurse_submodules_arg(const char *opt, const char *arg) | |
347 | { | |
348 | return parse_update_recurse(opt, arg, 1); | |
349 | } | |
350 | ||
351 | static int parse_push_recurse(const char *opt, const char *arg, | |
352 | int die_on_error) | |
353 | { | |
354 | switch (git_parse_maybe_bool(arg)) { | |
355 | case 1: | |
356 | /* There's no simple "on" value when pushing */ | |
357 | if (die_on_error) | |
358 | die("bad %s argument: %s", opt, arg); | |
359 | else | |
360 | return RECURSE_SUBMODULES_ERROR; | |
361 | case 0: | |
362 | return RECURSE_SUBMODULES_OFF; | |
363 | default: | |
364 | if (!strcmp(arg, "on-demand")) | |
365 | return RECURSE_SUBMODULES_ON_DEMAND; | |
366 | else if (!strcmp(arg, "check")) | |
367 | return RECURSE_SUBMODULES_CHECK; | |
368 | else if (!strcmp(arg, "only")) | |
369 | return RECURSE_SUBMODULES_ONLY; | |
370 | /* | |
371 | * Please update $__git_push_recurse_submodules in | |
372 | * git-completion.bash when you add new modes. | |
373 | */ | |
374 | else if (die_on_error) | |
375 | die("bad %s argument: %s", opt, arg); | |
376 | else | |
377 | return RECURSE_SUBMODULES_ERROR; | |
378 | } | |
379 | } | |
380 | ||
381 | int parse_push_recurse_submodules_arg(const char *opt, const char *arg) | |
382 | { | |
383 | return parse_push_recurse(opt, arg, 1); | |
384 | } | |
385 | ||
386 | static void warn_multiple_config(const struct object_id *treeish_name, | |
387 | const char *name, const char *option) | |
388 | { | |
389 | const char *commit_string = "WORKTREE"; | |
390 | if (treeish_name) | |
391 | commit_string = oid_to_hex(treeish_name); | |
392 | warning("%s:.gitmodules, multiple configurations found for " | |
393 | "'submodule.%s.%s'. Skipping second one!", | |
394 | commit_string, name, option); | |
395 | } | |
396 | ||
397 | static void warn_command_line_option(const char *var, const char *value) | |
398 | { | |
399 | warning(_("ignoring '%s' which may be interpreted as" | |
400 | " a command-line option: %s"), var, value); | |
401 | } | |
402 | ||
403 | struct parse_config_parameter { | |
404 | struct submodule_cache *cache; | |
405 | const struct object_id *treeish_name; | |
406 | const struct object_id *gitmodules_oid; | |
407 | int overwrite; | |
408 | }; | |
409 | ||
410 | static int parse_config(const char *var, const char *value, void *data) | |
411 | { | |
412 | struct parse_config_parameter *me = data; | |
413 | struct submodule *submodule; | |
414 | struct strbuf name = STRBUF_INIT, item = STRBUF_INIT; | |
415 | int ret = 0; | |
416 | ||
417 | /* this also ensures that we only parse submodule entries */ | |
418 | if (!name_and_item_from_var(var, &name, &item)) | |
419 | return 0; | |
420 | ||
421 | submodule = lookup_or_create_by_name(me->cache, | |
422 | me->gitmodules_oid, | |
423 | name.buf); | |
424 | ||
425 | if (!strcmp(item.buf, "path")) { | |
426 | if (!value) | |
427 | ret = config_error_nonbool(var); | |
428 | else if (looks_like_command_line_option(value)) | |
429 | warn_command_line_option(var, value); | |
430 | else if (!me->overwrite && submodule->path) | |
431 | warn_multiple_config(me->treeish_name, submodule->name, | |
432 | "path"); | |
433 | else { | |
434 | if (submodule->path) | |
435 | cache_remove_path(me->cache, submodule); | |
436 | free((void *) submodule->path); | |
437 | submodule->path = xstrdup(value); | |
438 | cache_put_path(me->cache, submodule); | |
439 | } | |
440 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { | |
441 | /* when parsing worktree configurations we can die early */ | |
442 | int die_on_error = is_null_oid(me->gitmodules_oid); | |
443 | if (!me->overwrite && | |
444 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) | |
445 | warn_multiple_config(me->treeish_name, submodule->name, | |
446 | "fetchrecursesubmodules"); | |
447 | else | |
448 | submodule->fetch_recurse = parse_fetch_recurse( | |
449 | var, value, | |
450 | die_on_error); | |
451 | } else if (!strcmp(item.buf, "ignore")) { | |
452 | if (!value) | |
453 | ret = config_error_nonbool(var); | |
454 | else if (!me->overwrite && submodule->ignore) | |
455 | warn_multiple_config(me->treeish_name, submodule->name, | |
456 | "ignore"); | |
457 | else if (strcmp(value, "untracked") && | |
458 | strcmp(value, "dirty") && | |
459 | strcmp(value, "all") && | |
460 | strcmp(value, "none")) | |
461 | warning("Invalid parameter '%s' for config option " | |
462 | "'submodule.%s.ignore'", value, name.buf); | |
463 | else { | |
464 | free((void *) submodule->ignore); | |
465 | submodule->ignore = xstrdup(value); | |
466 | } | |
467 | } else if (!strcmp(item.buf, "url")) { | |
468 | if (!value) { | |
469 | ret = config_error_nonbool(var); | |
470 | } else if (looks_like_command_line_option(value)) { | |
471 | warn_command_line_option(var, value); | |
472 | } else if (!me->overwrite && submodule->url) { | |
473 | warn_multiple_config(me->treeish_name, submodule->name, | |
474 | "url"); | |
475 | } else { | |
476 | free((void *) submodule->url); | |
477 | submodule->url = xstrdup(value); | |
478 | } | |
479 | } else if (!strcmp(item.buf, "update")) { | |
480 | if (!value) | |
481 | ret = config_error_nonbool(var); | |
482 | else if (!me->overwrite && | |
483 | submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) | |
484 | warn_multiple_config(me->treeish_name, submodule->name, | |
485 | "update"); | |
486 | else if (parse_submodule_update_strategy(value, | |
487 | &submodule->update_strategy) < 0) | |
488 | die(_("invalid value for %s"), var); | |
489 | } else if (!strcmp(item.buf, "shallow")) { | |
490 | if (!me->overwrite && submodule->recommend_shallow != -1) | |
491 | warn_multiple_config(me->treeish_name, submodule->name, | |
492 | "shallow"); | |
493 | else | |
494 | submodule->recommend_shallow = | |
495 | git_config_bool(var, value); | |
496 | } else if (!strcmp(item.buf, "branch")) { | |
497 | if (!me->overwrite && submodule->branch) | |
498 | warn_multiple_config(me->treeish_name, submodule->name, | |
499 | "branch"); | |
500 | else { | |
501 | free((void *)submodule->branch); | |
502 | submodule->branch = xstrdup(value); | |
503 | } | |
504 | } | |
505 | ||
506 | strbuf_release(&name); | |
507 | strbuf_release(&item); | |
508 | ||
509 | return ret; | |
510 | } | |
511 | ||
512 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, | |
513 | struct object_id *gitmodules_oid, | |
514 | struct strbuf *rev) | |
515 | { | |
516 | int ret = 0; | |
517 | ||
518 | if (is_null_oid(treeish_name)) { | |
519 | oidclr(gitmodules_oid); | |
520 | return 1; | |
521 | } | |
522 | ||
523 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); | |
524 | if (get_oid(rev->buf, gitmodules_oid) >= 0) | |
525 | ret = 1; | |
526 | ||
527 | return ret; | |
528 | } | |
529 | ||
530 | /* This does a lookup of a submodule configuration by name or by path | |
531 | * (key) with on-demand reading of the appropriate .gitmodules from | |
532 | * revisions. | |
533 | */ | |
534 | static const struct submodule *config_from(struct submodule_cache *cache, | |
535 | const struct object_id *treeish_name, const char *key, | |
536 | enum lookup_type lookup_type) | |
537 | { | |
538 | struct strbuf rev = STRBUF_INIT; | |
539 | unsigned long config_size; | |
540 | char *config = NULL; | |
541 | struct object_id oid; | |
542 | enum object_type type; | |
543 | const struct submodule *submodule = NULL; | |
544 | struct parse_config_parameter parameter; | |
545 | ||
546 | /* | |
547 | * If any parameter except the cache is a NULL pointer just | |
548 | * return the first submodule. Can be used to check whether | |
549 | * there are any submodules parsed. | |
550 | */ | |
551 | if (!treeish_name || !key) { | |
552 | struct hashmap_iter iter; | |
553 | struct submodule_entry *entry; | |
554 | ||
555 | entry = hashmap_iter_first(&cache->for_name, &iter); | |
556 | if (!entry) | |
557 | return NULL; | |
558 | return entry->config; | |
559 | } | |
560 | ||
561 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) | |
562 | goto out; | |
563 | ||
564 | switch (lookup_type) { | |
565 | case lookup_name: | |
566 | submodule = cache_lookup_name(cache, &oid, key); | |
567 | break; | |
568 | case lookup_path: | |
569 | submodule = cache_lookup_path(cache, &oid, key); | |
570 | break; | |
571 | } | |
572 | if (submodule) | |
573 | goto out; | |
574 | ||
575 | config = read_object_file(&oid, &type, &config_size); | |
576 | if (!config || type != OBJ_BLOB) | |
577 | goto out; | |
578 | ||
579 | /* fill the submodule config into the cache */ | |
580 | parameter.cache = cache; | |
581 | parameter.treeish_name = treeish_name; | |
582 | parameter.gitmodules_oid = &oid; | |
583 | parameter.overwrite = 0; | |
584 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, | |
585 | config, config_size, ¶meter, NULL); | |
586 | strbuf_release(&rev); | |
587 | free(config); | |
588 | ||
589 | switch (lookup_type) { | |
590 | case lookup_name: | |
591 | return cache_lookup_name(cache, &oid, key); | |
592 | case lookup_path: | |
593 | return cache_lookup_path(cache, &oid, key); | |
594 | default: | |
595 | return NULL; | |
596 | } | |
597 | ||
598 | out: | |
599 | strbuf_release(&rev); | |
600 | free(config); | |
601 | return submodule; | |
602 | } | |
603 | ||
604 | static void submodule_cache_check_init(struct repository *repo) | |
605 | { | |
606 | if (repo->submodule_cache && repo->submodule_cache->initialized) | |
607 | return; | |
608 | ||
609 | if (!repo->submodule_cache) | |
610 | repo->submodule_cache = submodule_cache_alloc(); | |
611 | ||
612 | submodule_cache_init(repo->submodule_cache); | |
613 | } | |
614 | ||
615 | /* | |
616 | * Note: This function is private for a reason, the '.gitmodules' file should | |
617 | * not be used as as a mechanism to retrieve arbitrary configuration stored in | |
618 | * the repository. | |
619 | * | |
620 | * Runs the provided config function on the '.gitmodules' file found in the | |
621 | * working directory. | |
622 | */ | |
623 | static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data) | |
624 | { | |
625 | if (repo->worktree) { | |
626 | struct git_config_source config_source = { 0 }; | |
627 | const struct config_options opts = { 0 }; | |
628 | struct object_id oid; | |
629 | char *file; | |
630 | char *oidstr = NULL; | |
631 | ||
632 | file = repo_worktree_path(repo, GITMODULES_FILE); | |
633 | if (file_exists(file)) { | |
634 | config_source.file = file; | |
635 | } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 || | |
636 | repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) { | |
637 | config_source.blob = oidstr = xstrdup(oid_to_hex(&oid)); | |
638 | if (repo != the_repository) | |
639 | add_to_alternates_memory(repo->objects->odb->path); | |
640 | } else { | |
641 | goto out; | |
642 | } | |
643 | ||
644 | config_with_options(fn, data, &config_source, &opts); | |
645 | ||
646 | out: | |
647 | free(oidstr); | |
648 | free(file); | |
649 | } | |
650 | } | |
651 | ||
652 | static int gitmodules_cb(const char *var, const char *value, void *data) | |
653 | { | |
654 | struct repository *repo = data; | |
655 | struct parse_config_parameter parameter; | |
656 | ||
657 | parameter.cache = repo->submodule_cache; | |
658 | parameter.treeish_name = NULL; | |
659 | parameter.gitmodules_oid = &null_oid; | |
660 | parameter.overwrite = 1; | |
661 | ||
662 | return parse_config(var, value, ¶meter); | |
663 | } | |
664 | ||
665 | void repo_read_gitmodules(struct repository *repo) | |
666 | { | |
667 | submodule_cache_check_init(repo); | |
668 | ||
669 | if (repo_read_index(repo) < 0) | |
670 | return; | |
671 | ||
672 | if (!is_gitmodules_unmerged(repo->index)) | |
673 | config_from_gitmodules(gitmodules_cb, repo, repo); | |
674 | ||
675 | repo->submodule_cache->gitmodules_read = 1; | |
676 | } | |
677 | ||
678 | void gitmodules_config_oid(const struct object_id *commit_oid) | |
679 | { | |
680 | struct strbuf rev = STRBUF_INIT; | |
681 | struct object_id oid; | |
682 | ||
683 | submodule_cache_check_init(the_repository); | |
684 | ||
685 | if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { | |
686 | git_config_from_blob_oid(gitmodules_cb, rev.buf, | |
687 | &oid, the_repository); | |
688 | } | |
689 | strbuf_release(&rev); | |
690 | ||
691 | the_repository->submodule_cache->gitmodules_read = 1; | |
692 | } | |
693 | ||
694 | static void gitmodules_read_check(struct repository *repo) | |
695 | { | |
696 | submodule_cache_check_init(repo); | |
697 | ||
698 | /* read the repo's .gitmodules file if it hasn't been already */ | |
699 | if (!repo->submodule_cache->gitmodules_read) | |
700 | repo_read_gitmodules(repo); | |
701 | } | |
702 | ||
703 | const struct submodule *submodule_from_name(struct repository *r, | |
704 | const struct object_id *treeish_name, | |
705 | const char *name) | |
706 | { | |
707 | gitmodules_read_check(r); | |
708 | return config_from(r->submodule_cache, treeish_name, name, lookup_name); | |
709 | } | |
710 | ||
711 | const struct submodule *submodule_from_path(struct repository *r, | |
712 | const struct object_id *treeish_name, | |
713 | const char *path) | |
714 | { | |
715 | gitmodules_read_check(r); | |
716 | return config_from(r->submodule_cache, treeish_name, path, lookup_path); | |
717 | } | |
718 | ||
719 | void submodule_free(struct repository *r) | |
720 | { | |
721 | if (r->submodule_cache) | |
722 | submodule_cache_clear(r->submodule_cache); | |
723 | } | |
724 | ||
725 | static int config_print_callback(const char *var, const char *value, void *cb_data) | |
726 | { | |
727 | char *wanted_key = cb_data; | |
728 | ||
729 | if (!strcmp(wanted_key, var)) | |
730 | printf("%s\n", value); | |
731 | ||
732 | return 0; | |
733 | } | |
734 | ||
735 | int print_config_from_gitmodules(struct repository *repo, const char *key) | |
736 | { | |
737 | int ret; | |
738 | char *store_key; | |
739 | ||
740 | ret = git_config_parse_key(key, &store_key, NULL); | |
741 | if (ret < 0) | |
742 | return CONFIG_INVALID_KEY; | |
743 | ||
744 | config_from_gitmodules(config_print_callback, repo, store_key); | |
745 | ||
746 | free(store_key); | |
747 | return 0; | |
748 | } | |
749 | ||
750 | int config_set_in_gitmodules_file_gently(const char *key, const char *value) | |
751 | { | |
752 | int ret; | |
753 | ||
754 | ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value); | |
755 | if (ret < 0) | |
756 | /* Maybe the user already did that, don't error out here */ | |
757 | warning(_("Could not update .gitmodules entry %s"), key); | |
758 | ||
759 | return ret; | |
760 | } | |
761 | ||
762 | struct fetch_config { | |
763 | int *max_children; | |
764 | int *recurse_submodules; | |
765 | }; | |
766 | ||
767 | static int gitmodules_fetch_config(const char *var, const char *value, void *cb) | |
768 | { | |
769 | struct fetch_config *config = cb; | |
770 | if (!strcmp(var, "submodule.fetchjobs")) { | |
771 | *(config->max_children) = parse_submodule_fetchjobs(var, value); | |
772 | return 0; | |
773 | } else if (!strcmp(var, "fetch.recursesubmodules")) { | |
774 | *(config->recurse_submodules) = parse_fetch_recurse_submodules_arg(var, value); | |
775 | return 0; | |
776 | } | |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
781 | void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules) | |
782 | { | |
783 | struct fetch_config config = { | |
784 | .max_children = max_children, | |
785 | .recurse_submodules = recurse_submodules | |
786 | }; | |
787 | config_from_gitmodules(gitmodules_fetch_config, the_repository, &config); | |
788 | } | |
789 | ||
790 | static int gitmodules_update_clone_config(const char *var, const char *value, | |
791 | void *cb) | |
792 | { | |
793 | int *max_jobs = cb; | |
794 | if (!strcmp(var, "submodule.fetchjobs")) | |
795 | *max_jobs = parse_submodule_fetchjobs(var, value); | |
796 | return 0; | |
797 | } | |
798 | ||
799 | void update_clone_config_from_gitmodules(int *max_jobs) | |
800 | { | |
801 | config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs); | |
802 | } |