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