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