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