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