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