]>
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" |
961b130d | 10 | #include "tree-walk.h" |
959b5455 HV |
11 | |
12 | /* | |
13 | * submodule cache lookup structure | |
14 | * There is one shared set of 'struct submodule' entries which can be | |
5aea9fe6 | 15 | * looked up by their sha1 blob id of the .gitmodules file and either |
959b5455 HV |
16 | * using path or name as key. |
17 | * for_path stores submodule entries with path as key | |
18 | * for_name stores submodule entries with name as key | |
19 | */ | |
20 | struct submodule_cache { | |
21 | struct hashmap for_path; | |
22 | struct hashmap for_name; | |
bf12fcdf | 23 | unsigned initialized:1; |
ff6f1f56 | 24 | unsigned gitmodules_read:1; |
959b5455 HV |
25 | }; |
26 | ||
27 | /* | |
28 | * thin wrapper struct needed to insert 'struct submodule' entries to | |
29 | * the hashmap | |
30 | */ | |
31 | struct submodule_entry { | |
32 | struct hashmap_entry ent; | |
33 | struct submodule *config; | |
34 | }; | |
35 | ||
36 | enum lookup_type { | |
37 | lookup_name, | |
38 | lookup_path | |
39 | }; | |
40 | ||
7663cdc8 | 41 | static int config_path_cmp(const void *unused_cmp_data, |
939af16e EW |
42 | const struct hashmap_entry *eptr, |
43 | const struct hashmap_entry *entry_or_key, | |
7663cdc8 | 44 | const void *unused_keydata) |
959b5455 | 45 | { |
939af16e EW |
46 | const struct submodule_entry *a, *b; |
47 | ||
48 | a = container_of(eptr, const struct submodule_entry, ent); | |
49 | b = container_of(entry_or_key, const struct submodule_entry, ent); | |
152cbdc6 | 50 | |
959b5455 | 51 | return strcmp(a->config->path, b->config->path) || |
9001dc2a | 52 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); |
959b5455 HV |
53 | } |
54 | ||
7663cdc8 | 55 | static int config_name_cmp(const void *unused_cmp_data, |
939af16e EW |
56 | const struct hashmap_entry *eptr, |
57 | const struct hashmap_entry *entry_or_key, | |
7663cdc8 | 58 | const void *unused_keydata) |
959b5455 | 59 | { |
939af16e EW |
60 | const struct submodule_entry *a, *b; |
61 | ||
62 | a = container_of(eptr, const struct submodule_entry, ent); | |
63 | b = container_of(entry_or_key, const struct submodule_entry, ent); | |
152cbdc6 | 64 | |
959b5455 | 65 | return strcmp(a->config->name, b->config->name) || |
9001dc2a | 66 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); |
959b5455 HV |
67 | } |
68 | ||
bf12fcdf BW |
69 | static struct submodule_cache *submodule_cache_alloc(void) |
70 | { | |
71 | return xcalloc(1, sizeof(struct submodule_cache)); | |
72 | } | |
73 | ||
74 | static void submodule_cache_init(struct submodule_cache *cache) | |
959b5455 | 75 | { |
152cbdc6 SB |
76 | hashmap_init(&cache->for_path, config_path_cmp, NULL, 0); |
77 | hashmap_init(&cache->for_name, config_name_cmp, NULL, 0); | |
bf12fcdf | 78 | cache->initialized = 1; |
959b5455 HV |
79 | } |
80 | ||
81 | static void free_one_config(struct submodule_entry *entry) | |
82 | { | |
83 | free((void *) entry->config->path); | |
84 | free((void *) entry->config->name); | |
b5944f34 | 85 | free((void *) entry->config->branch); |
ea2fa5a3 | 86 | free((void *) entry->config->update_strategy.command); |
959b5455 HV |
87 | free(entry->config); |
88 | } | |
89 | ||
bf12fcdf | 90 | static void submodule_cache_clear(struct submodule_cache *cache) |
959b5455 HV |
91 | { |
92 | struct hashmap_iter iter; | |
93 | struct submodule_entry *entry; | |
94 | ||
bf12fcdf BW |
95 | if (!cache->initialized) |
96 | return; | |
97 | ||
959b5455 HV |
98 | /* |
99 | * We iterate over the name hash here to be symmetric with the | |
100 | * allocation of struct submodule entries. Each is allocated by | |
5aea9fe6 | 101 | * their .gitmodules blob sha1 and submodule name. |
959b5455 | 102 | */ |
87571c3f | 103 | hashmap_for_each_entry(&cache->for_name, &iter, entry, |
23dee69f | 104 | ent /* member name */) |
959b5455 HV |
105 | free_one_config(entry); |
106 | ||
6da1a258 EN |
107 | hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent); |
108 | hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent); | |
bf12fcdf | 109 | cache->initialized = 0; |
ff6f1f56 | 110 | cache->gitmodules_read = 0; |
bf12fcdf BW |
111 | } |
112 | ||
113 | void submodule_cache_free(struct submodule_cache *cache) | |
114 | { | |
115 | submodule_cache_clear(cache); | |
116 | free(cache); | |
959b5455 HV |
117 | } |
118 | ||
34caab02 | 119 | static unsigned int hash_oid_string(const struct object_id *oid, |
120 | const char *string) | |
959b5455 | 121 | { |
34caab02 | 122 | return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string); |
959b5455 HV |
123 | } |
124 | ||
125 | static void cache_put_path(struct submodule_cache *cache, | |
126 | struct submodule *submodule) | |
127 | { | |
34caab02 | 128 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
129 | submodule->path); | |
959b5455 | 130 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
d22245a2 | 131 | hashmap_entry_init(&e->ent, hash); |
959b5455 | 132 | e->config = submodule; |
26b455f2 | 133 | hashmap_put(&cache->for_path, &e->ent); |
959b5455 HV |
134 | } |
135 | ||
136 | static void cache_remove_path(struct submodule_cache *cache, | |
137 | struct submodule *submodule) | |
138 | { | |
34caab02 | 139 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
140 | submodule->path); | |
959b5455 HV |
141 | struct submodule_entry e; |
142 | struct submodule_entry *removed; | |
d22245a2 | 143 | hashmap_entry_init(&e.ent, hash); |
959b5455 | 144 | e.config = submodule; |
404ab78e | 145 | removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL); |
959b5455 HV |
146 | free(removed); |
147 | } | |
148 | ||
149 | static void cache_add(struct submodule_cache *cache, | |
150 | struct submodule *submodule) | |
151 | { | |
34caab02 | 152 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
153 | submodule->name); | |
959b5455 | 154 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
d22245a2 | 155 | hashmap_entry_init(&e->ent, hash); |
959b5455 | 156 | e->config = submodule; |
b94e5c1d | 157 | hashmap_add(&cache->for_name, &e->ent); |
959b5455 HV |
158 | } |
159 | ||
160 | static const struct submodule *cache_lookup_path(struct submodule_cache *cache, | |
34caab02 | 161 | const struct object_id *gitmodules_oid, const char *path) |
959b5455 HV |
162 | { |
163 | struct submodule_entry *entry; | |
34caab02 | 164 | unsigned int hash = hash_oid_string(gitmodules_oid, path); |
959b5455 HV |
165 | struct submodule_entry key; |
166 | struct submodule key_config; | |
167 | ||
34caab02 | 168 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); |
959b5455 HV |
169 | key_config.path = path; |
170 | ||
d22245a2 | 171 | hashmap_entry_init(&key.ent, hash); |
959b5455 HV |
172 | key.config = &key_config; |
173 | ||
404ab78e | 174 | entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL); |
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 | ||
404ab78e | 194 | entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL); |
959b5455 HV |
195 | if (entry) |
196 | return entry->config; | |
197 | return NULL; | |
198 | } | |
199 | ||
0383bbb9 JK |
200 | int check_submodule_name(const char *name) |
201 | { | |
202 | /* Disallow empty names */ | |
203 | if (!*name) | |
204 | return -1; | |
205 | ||
206 | /* | |
207 | * Look for '..' as a path component. Check both '/' and '\\' as | |
208 | * separators rather than is_dir_sep(), because we want the name rules | |
209 | * to be consistent across platforms. | |
210 | */ | |
211 | goto in_component; /* always start inside component */ | |
212 | while (*name) { | |
213 | char c = *name++; | |
214 | if (c == '/' || c == '\\') { | |
215 | in_component: | |
216 | if (name[0] == '.' && name[1] == '.' && | |
217 | (!name[2] || name[2] == '/' || name[2] == '\\')) | |
218 | return -1; | |
219 | } | |
220 | } | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
959b5455 HV |
225 | static int name_and_item_from_var(const char *var, struct strbuf *name, |
226 | struct strbuf *item) | |
227 | { | |
228 | const char *subsection, *key; | |
f5914f4b JK |
229 | size_t subsection_len; |
230 | int parse; | |
959b5455 HV |
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 | ||
e904deb8 JN |
414 | /* |
415 | * Parse a config item from .gitmodules. | |
416 | * | |
417 | * This does not handle submodule-related configuration from the main | |
418 | * config store (.git/config, etc). Callers are responsible for | |
419 | * checking for overrides in the main config store when appropriate. | |
420 | */ | |
959b5455 HV |
421 | static int parse_config(const char *var, const char *value, void *data) |
422 | { | |
423 | struct parse_config_parameter *me = data; | |
424 | struct submodule *submodule; | |
425 | struct strbuf name = STRBUF_INIT, item = STRBUF_INIT; | |
426 | int ret = 0; | |
427 | ||
428 | /* this also ensures that we only parse submodule entries */ | |
429 | if (!name_and_item_from_var(var, &name, &item)) | |
430 | return 0; | |
431 | ||
147875fd | 432 | submodule = lookup_or_create_by_name(me->cache, |
34caab02 | 433 | me->gitmodules_oid, |
147875fd | 434 | name.buf); |
959b5455 HV |
435 | |
436 | if (!strcmp(item.buf, "path")) { | |
147875fd | 437 | if (!value) |
959b5455 | 438 | ret = config_error_nonbool(var); |
273c6149 JK |
439 | else if (looks_like_command_line_option(value)) |
440 | warn_command_line_option(var, value); | |
f73da110 | 441 | else if (!me->overwrite && submodule->path) |
73c293bb | 442 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 443 | "path"); |
147875fd SB |
444 | else { |
445 | if (submodule->path) | |
446 | cache_remove_path(me->cache, submodule); | |
447 | free((void *) submodule->path); | |
448 | submodule->path = xstrdup(value); | |
449 | cache_put_path(me->cache, submodule); | |
959b5455 | 450 | } |
959b5455 | 451 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { |
027771fc | 452 | /* when parsing worktree configurations we can die early */ |
34caab02 | 453 | int die_on_error = is_null_oid(me->gitmodules_oid); |
959b5455 | 454 | if (!me->overwrite && |
147875fd | 455 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) |
73c293bb | 456 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 457 | "fetchrecursesubmodules"); |
147875fd SB |
458 | else |
459 | submodule->fetch_recurse = parse_fetch_recurse( | |
460 | var, value, | |
027771fc | 461 | die_on_error); |
959b5455 | 462 | } else if (!strcmp(item.buf, "ignore")) { |
147875fd SB |
463 | if (!value) |
464 | ret = config_error_nonbool(var); | |
f73da110 | 465 | else if (!me->overwrite && submodule->ignore) |
73c293bb | 466 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 467 | "ignore"); |
147875fd SB |
468 | else if (strcmp(value, "untracked") && |
469 | strcmp(value, "dirty") && | |
470 | strcmp(value, "all") && | |
471 | strcmp(value, "none")) | |
959b5455 | 472 | warning("Invalid parameter '%s' for config option " |
5ea30489 | 473 | "'submodule.%s.ignore'", value, name.buf); |
147875fd SB |
474 | else { |
475 | free((void *) submodule->ignore); | |
476 | submodule->ignore = xstrdup(value); | |
959b5455 | 477 | } |
959b5455 | 478 | } else if (!strcmp(item.buf, "url")) { |
959b5455 HV |
479 | if (!value) { |
480 | ret = config_error_nonbool(var); | |
f6adec4e JK |
481 | } else if (looks_like_command_line_option(value)) { |
482 | warn_command_line_option(var, value); | |
f73da110 | 483 | } else if (!me->overwrite && submodule->url) { |
73c293bb | 484 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 485 | "url"); |
147875fd SB |
486 | } else { |
487 | free((void *) submodule->url); | |
488 | submodule->url = xstrdup(value); | |
959b5455 | 489 | } |
ea2fa5a3 SB |
490 | } else if (!strcmp(item.buf, "update")) { |
491 | if (!value) | |
492 | ret = config_error_nonbool(var); | |
493 | else if (!me->overwrite && | |
494 | submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) | |
73c293bb | 495 | warn_multiple_config(me->treeish_name, submodule->name, |
ea2fa5a3 SB |
496 | "update"); |
497 | else if (parse_submodule_update_strategy(value, | |
e904deb8 JN |
498 | &submodule->update_strategy) < 0 || |
499 | submodule->update_strategy.type == SM_UPDATE_COMMAND) | |
1a8aea85 | 500 | die(_("invalid value for '%s'"), var); |
37f52e93 SB |
501 | } else if (!strcmp(item.buf, "shallow")) { |
502 | if (!me->overwrite && submodule->recommend_shallow != -1) | |
73c293bb | 503 | warn_multiple_config(me->treeish_name, submodule->name, |
37f52e93 | 504 | "shallow"); |
b5944f34 | 505 | else |
37f52e93 SB |
506 | submodule->recommend_shallow = |
507 | git_config_bool(var, value); | |
b5944f34 SB |
508 | } else if (!strcmp(item.buf, "branch")) { |
509 | if (!me->overwrite && submodule->branch) | |
73c293bb | 510 | warn_multiple_config(me->treeish_name, submodule->name, |
b5944f34 SB |
511 | "branch"); |
512 | else { | |
513 | free((void *)submodule->branch); | |
514 | submodule->branch = xstrdup(value); | |
37f52e93 | 515 | } |
959b5455 HV |
516 | } |
517 | ||
959b5455 HV |
518 | strbuf_release(&name); |
519 | strbuf_release(&item); | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
1b796ace BW |
524 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, |
525 | struct object_id *gitmodules_oid, | |
526 | struct strbuf *rev) | |
959b5455 | 527 | { |
959b5455 HV |
528 | int ret = 0; |
529 | ||
cd73de47 | 530 | if (is_null_oid(treeish_name)) { |
531 | oidclr(gitmodules_oid); | |
959b5455 HV |
532 | return 1; |
533 | } | |
534 | ||
cd73de47 | 535 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); |
536 | if (get_oid(rev->buf, gitmodules_oid) >= 0) | |
959b5455 HV |
537 | ret = 1; |
538 | ||
959b5455 HV |
539 | return ret; |
540 | } | |
541 | ||
542 | /* This does a lookup of a submodule configuration by name or by path | |
543 | * (key) with on-demand reading of the appropriate .gitmodules from | |
544 | * revisions. | |
545 | */ | |
546 | static const struct submodule *config_from(struct submodule_cache *cache, | |
cd73de47 | 547 | const struct object_id *treeish_name, const char *key, |
959b5455 HV |
548 | enum lookup_type lookup_type) |
549 | { | |
550 | struct strbuf rev = STRBUF_INIT; | |
551 | unsigned long config_size; | |
0918e250 | 552 | char *config = NULL; |
cd73de47 | 553 | struct object_id oid; |
959b5455 HV |
554 | enum object_type type; |
555 | const struct submodule *submodule = NULL; | |
556 | struct parse_config_parameter parameter; | |
557 | ||
558 | /* | |
559 | * If any parameter except the cache is a NULL pointer just | |
560 | * return the first submodule. Can be used to check whether | |
561 | * there are any submodules parsed. | |
562 | */ | |
73c293bb | 563 | if (!treeish_name || !key) { |
959b5455 HV |
564 | struct hashmap_iter iter; |
565 | struct submodule_entry *entry; | |
566 | ||
87571c3f EW |
567 | entry = hashmap_iter_first_entry(&cache->for_name, &iter, |
568 | struct submodule_entry, | |
569 | ent /* member name */); | |
959b5455 HV |
570 | if (!entry) |
571 | return NULL; | |
572 | return entry->config; | |
573 | } | |
574 | ||
cd73de47 | 575 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) |
0918e250 | 576 | goto out; |
959b5455 HV |
577 | |
578 | switch (lookup_type) { | |
579 | case lookup_name: | |
34caab02 | 580 | submodule = cache_lookup_name(cache, &oid, key); |
959b5455 HV |
581 | break; |
582 | case lookup_path: | |
34caab02 | 583 | submodule = cache_lookup_path(cache, &oid, key); |
959b5455 HV |
584 | break; |
585 | } | |
586 | if (submodule) | |
0918e250 | 587 | goto out; |
959b5455 | 588 | |
b4f5aca4 | 589 | config = read_object_file(&oid, &type, &config_size); |
0918e250 HV |
590 | if (!config || type != OBJ_BLOB) |
591 | goto out; | |
959b5455 HV |
592 | |
593 | /* fill the submodule config into the cache */ | |
594 | parameter.cache = cache; | |
34caab02 | 595 | parameter.treeish_name = treeish_name; |
596 | parameter.gitmodules_oid = &oid; | |
959b5455 | 597 | parameter.overwrite = 0; |
1b8132d9 | 598 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, |
4574f1aa | 599 | config, config_size, ¶meter, NULL); |
514dea90 | 600 | strbuf_release(&rev); |
959b5455 HV |
601 | free(config); |
602 | ||
603 | switch (lookup_type) { | |
604 | case lookup_name: | |
34caab02 | 605 | return cache_lookup_name(cache, &oid, key); |
959b5455 | 606 | case lookup_path: |
34caab02 | 607 | return cache_lookup_path(cache, &oid, key); |
959b5455 HV |
608 | default: |
609 | return NULL; | |
610 | } | |
0918e250 HV |
611 | |
612 | out: | |
613 | strbuf_release(&rev); | |
614 | free(config); | |
615 | return submodule; | |
959b5455 HV |
616 | } |
617 | ||
bf12fcdf | 618 | static void submodule_cache_check_init(struct repository *repo) |
959b5455 | 619 | { |
bf12fcdf | 620 | if (repo->submodule_cache && repo->submodule_cache->initialized) |
959b5455 HV |
621 | return; |
622 | ||
bf12fcdf BW |
623 | if (!repo->submodule_cache) |
624 | repo->submodule_cache = submodule_cache_alloc(); | |
625 | ||
626 | submodule_cache_init(repo->submodule_cache); | |
959b5455 HV |
627 | } |
628 | ||
db64d112 AO |
629 | /* |
630 | * Note: This function is private for a reason, the '.gitmodules' file should | |
571fb965 | 631 | * not be used as a mechanism to retrieve arbitrary configuration stored in |
db64d112 AO |
632 | * the repository. |
633 | * | |
634 | * Runs the provided config function on the '.gitmodules' file found in the | |
635 | * working directory. | |
636 | */ | |
637 | static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data) | |
638 | { | |
639 | if (repo->worktree) { | |
9a83d088 MR |
640 | struct git_config_source config_source = { |
641 | 0, .scope = CONFIG_SCOPE_SUBMODULE | |
642 | }; | |
76e9bdc4 AO |
643 | const struct config_options opts = { 0 }; |
644 | struct object_id oid; | |
645 | char *file; | |
d9b8b8f8 | 646 | char *oidstr = NULL; |
76e9bdc4 AO |
647 | |
648 | file = repo_worktree_path(repo, GITMODULES_FILE); | |
649 | if (file_exists(file)) { | |
650 | config_source.file = file; | |
d9b8b8f8 NTND |
651 | } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 || |
652 | repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) { | |
e3e8bf04 | 653 | config_source.repo = repo; |
d9b8b8f8 NTND |
654 | config_source.blob = oidstr = xstrdup(oid_to_hex(&oid)); |
655 | if (repo != the_repository) | |
e3e8bf04 | 656 | add_submodule_odb_by_path(repo->objects->odb->path); |
76e9bdc4 AO |
657 | } else { |
658 | goto out; | |
659 | } | |
660 | ||
661 | config_with_options(fn, data, &config_source, &opts); | |
662 | ||
663 | out: | |
d9b8b8f8 | 664 | free(oidstr); |
db64d112 AO |
665 | free(file); |
666 | } | |
667 | } | |
668 | ||
1b796ace | 669 | static int gitmodules_cb(const char *var, const char *value, void *data) |
851e18c3 | 670 | { |
1b796ace | 671 | struct repository *repo = data; |
851e18c3 | 672 | struct parse_config_parameter parameter; |
bf12fcdf | 673 | |
bf12fcdf | 674 | parameter.cache = repo->submodule_cache; |
73c293bb | 675 | parameter.treeish_name = NULL; |
14228447 | 676 | parameter.gitmodules_oid = null_oid(); |
851e18c3 HV |
677 | parameter.overwrite = 1; |
678 | ||
851e18c3 HV |
679 | return parse_config(var, value, ¶meter); |
680 | } | |
681 | ||
d7992421 | 682 | void repo_read_gitmodules(struct repository *repo, int skip_if_read) |
bf12fcdf | 683 | { |
ff6f1f56 BW |
684 | submodule_cache_check_init(repo); |
685 | ||
d7992421 MT |
686 | if (repo->submodule_cache->gitmodules_read && skip_if_read) |
687 | return; | |
688 | ||
db64d112 AO |
689 | if (repo_read_index(repo) < 0) |
690 | return; | |
1b796ace | 691 | |
db64d112 AO |
692 | if (!is_gitmodules_unmerged(repo->index)) |
693 | config_from_gitmodules(gitmodules_cb, repo, repo); | |
ff6f1f56 BW |
694 | |
695 | repo->submodule_cache->gitmodules_read = 1; | |
1b796ace BW |
696 | } |
697 | ||
698 | void gitmodules_config_oid(const struct object_id *commit_oid) | |
699 | { | |
700 | struct strbuf rev = STRBUF_INIT; | |
701 | struct object_id oid; | |
702 | ||
ff6f1f56 BW |
703 | submodule_cache_check_init(the_repository); |
704 | ||
1b796ace BW |
705 | if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) { |
706 | git_config_from_blob_oid(gitmodules_cb, rev.buf, | |
e3e8bf04 | 707 | the_repository, &oid, the_repository); |
1b796ace BW |
708 | } |
709 | strbuf_release(&rev); | |
ff6f1f56 BW |
710 | |
711 | the_repository->submodule_cache->gitmodules_read = 1; | |
712 | } | |
713 | ||
3b8fb393 SB |
714 | const struct submodule *submodule_from_name(struct repository *r, |
715 | const struct object_id *treeish_name, | |
959b5455 HV |
716 | const char *name) |
717 | { | |
d7992421 | 718 | repo_read_gitmodules(r, 1); |
3b8fb393 | 719 | return config_from(r->submodule_cache, treeish_name, name, lookup_name); |
959b5455 HV |
720 | } |
721 | ||
3b8fb393 SB |
722 | const struct submodule *submodule_from_path(struct repository *r, |
723 | const struct object_id *treeish_name, | |
959b5455 HV |
724 | const char *path) |
725 | { | |
d7992421 | 726 | repo_read_gitmodules(r, 1); |
3b8fb393 | 727 | return config_from(r->submodule_cache, treeish_name, path, lookup_path); |
bf12fcdf BW |
728 | } |
729 | ||
961b130d GC |
730 | /** |
731 | * Used internally by submodules_of_tree(). Recurses into 'treeish_name' | |
732 | * and appends submodule entries to 'out'. The submodule_cache expects | |
733 | * a root-level treeish_name and paths, so keep track of these values | |
734 | * with 'root_tree' and 'prefix'. | |
735 | */ | |
736 | static void traverse_tree_submodules(struct repository *r, | |
737 | const struct object_id *root_tree, | |
738 | char *prefix, | |
739 | const struct object_id *treeish_name, | |
740 | struct submodule_entry_list *out) | |
741 | { | |
742 | struct tree_desc tree; | |
743 | struct submodule_tree_entry *st_entry; | |
744 | struct name_entry *name_entry; | |
745 | char *tree_path = NULL; | |
746 | ||
747 | name_entry = xmalloc(sizeof(*name_entry)); | |
748 | ||
749 | fill_tree_descriptor(r, &tree, treeish_name); | |
750 | while (tree_entry(&tree, name_entry)) { | |
751 | if (prefix) | |
752 | tree_path = | |
753 | mkpathdup("%s/%s", prefix, name_entry->path); | |
754 | else | |
755 | tree_path = xstrdup(name_entry->path); | |
756 | ||
757 | if (S_ISGITLINK(name_entry->mode) && | |
758 | is_tree_submodule_active(r, root_tree, tree_path)) { | |
759 | st_entry = xmalloc(sizeof(*st_entry)); | |
760 | st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry)); | |
761 | *st_entry->name_entry = *name_entry; | |
762 | st_entry->submodule = | |
763 | submodule_from_path(r, root_tree, tree_path); | |
764 | st_entry->repo = xmalloc(sizeof(*st_entry->repo)); | |
765 | if (repo_submodule_init(st_entry->repo, r, tree_path, | |
766 | root_tree)) | |
767 | FREE_AND_NULL(st_entry->repo); | |
768 | ||
769 | ALLOC_GROW(out->entries, out->entry_nr + 1, | |
770 | out->entry_alloc); | |
771 | out->entries[out->entry_nr++] = *st_entry; | |
772 | } else if (S_ISDIR(name_entry->mode)) | |
773 | traverse_tree_submodules(r, root_tree, tree_path, | |
774 | &name_entry->oid, out); | |
775 | free(tree_path); | |
776 | } | |
777 | } | |
778 | ||
779 | void submodules_of_tree(struct repository *r, | |
780 | const struct object_id *treeish_name, | |
781 | struct submodule_entry_list *out) | |
782 | { | |
783 | CALLOC_ARRAY(out->entries, 0); | |
784 | out->entry_nr = 0; | |
785 | out->entry_alloc = 0; | |
786 | ||
787 | traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out); | |
788 | } | |
789 | ||
f793b895 | 790 | void submodule_free(struct repository *r) |
bf12fcdf | 791 | { |
f793b895 SB |
792 | if (r->submodule_cache) |
793 | submodule_cache_clear(r->submodule_cache); | |
959b5455 | 794 | } |
ad136370 | 795 | |
bcbc780d AO |
796 | static int config_print_callback(const char *var, const char *value, void *cb_data) |
797 | { | |
798 | char *wanted_key = cb_data; | |
799 | ||
800 | if (!strcmp(wanted_key, var)) | |
801 | printf("%s\n", value); | |
802 | ||
803 | return 0; | |
804 | } | |
805 | ||
806 | int print_config_from_gitmodules(struct repository *repo, const char *key) | |
807 | { | |
808 | int ret; | |
809 | char *store_key; | |
810 | ||
811 | ret = git_config_parse_key(key, &store_key, NULL); | |
812 | if (ret < 0) | |
813 | return CONFIG_INVALID_KEY; | |
814 | ||
815 | config_from_gitmodules(config_print_callback, repo, store_key); | |
816 | ||
817 | free(store_key); | |
818 | return 0; | |
819 | } | |
820 | ||
45f5ef3d AO |
821 | int config_set_in_gitmodules_file_gently(const char *key, const char *value) |
822 | { | |
823 | int ret; | |
824 | ||
825 | ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value); | |
826 | if (ret < 0) | |
827 | /* Maybe the user already did that, don't error out here */ | |
828 | warning(_("Could not update .gitmodules entry %s"), key); | |
829 | ||
830 | return ret; | |
831 | } | |
832 | ||
71a6953d AO |
833 | struct fetch_config { |
834 | int *max_children; | |
835 | int *recurse_submodules; | |
836 | }; | |
837 | ||
838 | static int gitmodules_fetch_config(const char *var, const char *value, void *cb) | |
839 | { | |
840 | struct fetch_config *config = cb; | |
841 | if (!strcmp(var, "submodule.fetchjobs")) { | |
e5b94213 JT |
842 | if (config->max_children) |
843 | *(config->max_children) = | |
844 | parse_submodule_fetchjobs(var, value); | |
71a6953d AO |
845 | return 0; |
846 | } else if (!strcmp(var, "fetch.recursesubmodules")) { | |
e5b94213 JT |
847 | if (config->recurse_submodules) |
848 | *(config->recurse_submodules) = | |
849 | parse_fetch_recurse_submodules_arg(var, value); | |
71a6953d AO |
850 | return 0; |
851 | } | |
852 | ||
853 | return 0; | |
854 | } | |
855 | ||
856 | void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules) | |
857 | { | |
858 | struct fetch_config config = { | |
859 | .max_children = max_children, | |
860 | .recurse_submodules = recurse_submodules | |
861 | }; | |
9a0fb3e7 | 862 | config_from_gitmodules(gitmodules_fetch_config, the_repository, &config); |
71a6953d | 863 | } |
05744997 AO |
864 | |
865 | static int gitmodules_update_clone_config(const char *var, const char *value, | |
866 | void *cb) | |
867 | { | |
868 | int *max_jobs = cb; | |
869 | if (!strcmp(var, "submodule.fetchjobs")) | |
870 | *max_jobs = parse_submodule_fetchjobs(var, value); | |
871 | return 0; | |
872 | } | |
873 | ||
874 | void update_clone_config_from_gitmodules(int *max_jobs) | |
875 | { | |
9a0fb3e7 | 876 | config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs); |
05744997 | 877 | } |