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