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