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