]>
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" |
c339932b | 7 | #include "path.h" |
bf12fcdf | 8 | #include "repository.h" |
b2141fc1 | 9 | #include "config.h" |
959b5455 HV |
10 | #include "submodule-config.h" |
11 | #include "submodule.h" | |
12 | #include "strbuf.h" | |
dabab1d6 | 13 | #include "object-name.h" |
a034e910 | 14 | #include "object-store-ll.h" |
886dc154 | 15 | #include "parse-options.h" |
e3d2f20e | 16 | #include "thread-utils.h" |
961b130d | 17 | #include "tree-walk.h" |
959b5455 HV |
18 | |
19 | /* | |
20 | * submodule cache lookup structure | |
21 | * There is one shared set of 'struct submodule' entries which can be | |
5aea9fe6 | 22 | * looked up by their sha1 blob id of the .gitmodules file and either |
959b5455 HV |
23 | * using path or name as key. |
24 | * for_path stores submodule entries with path as key | |
25 | * for_name stores submodule entries with name as key | |
26 | */ | |
27 | struct submodule_cache { | |
28 | struct hashmap for_path; | |
29 | struct hashmap for_name; | |
bf12fcdf | 30 | unsigned initialized:1; |
ff6f1f56 | 31 | unsigned gitmodules_read:1; |
959b5455 HV |
32 | }; |
33 | ||
34 | /* | |
35 | * thin wrapper struct needed to insert 'struct submodule' entries to | |
36 | * the hashmap | |
37 | */ | |
38 | struct submodule_entry { | |
39 | struct hashmap_entry ent; | |
40 | struct submodule *config; | |
41 | }; | |
42 | ||
43 | enum lookup_type { | |
44 | lookup_name, | |
45 | lookup_path | |
46 | }; | |
47 | ||
5cf88fd8 | 48 | static int config_path_cmp(const void *cmp_data UNUSED, |
939af16e EW |
49 | const struct hashmap_entry *eptr, |
50 | const struct hashmap_entry *entry_or_key, | |
5cf88fd8 | 51 | const void *keydata UNUSED) |
959b5455 | 52 | { |
939af16e EW |
53 | const struct submodule_entry *a, *b; |
54 | ||
55 | a = container_of(eptr, const struct submodule_entry, ent); | |
56 | b = container_of(entry_or_key, const struct submodule_entry, ent); | |
152cbdc6 | 57 | |
959b5455 | 58 | return strcmp(a->config->path, b->config->path) || |
9001dc2a | 59 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); |
959b5455 HV |
60 | } |
61 | ||
5cf88fd8 | 62 | static int config_name_cmp(const void *cmp_data UNUSED, |
939af16e EW |
63 | const struct hashmap_entry *eptr, |
64 | const struct hashmap_entry *entry_or_key, | |
5cf88fd8 | 65 | const void *keydata UNUSED) |
959b5455 | 66 | { |
939af16e EW |
67 | const struct submodule_entry *a, *b; |
68 | ||
69 | a = container_of(eptr, const struct submodule_entry, ent); | |
70 | b = container_of(entry_or_key, const struct submodule_entry, ent); | |
152cbdc6 | 71 | |
959b5455 | 72 | return strcmp(a->config->name, b->config->name) || |
9001dc2a | 73 | !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid); |
959b5455 HV |
74 | } |
75 | ||
bf12fcdf BW |
76 | static struct submodule_cache *submodule_cache_alloc(void) |
77 | { | |
78 | return xcalloc(1, sizeof(struct submodule_cache)); | |
79 | } | |
80 | ||
81 | static void submodule_cache_init(struct submodule_cache *cache) | |
959b5455 | 82 | { |
152cbdc6 SB |
83 | hashmap_init(&cache->for_path, config_path_cmp, NULL, 0); |
84 | hashmap_init(&cache->for_name, config_name_cmp, NULL, 0); | |
bf12fcdf | 85 | cache->initialized = 1; |
959b5455 HV |
86 | } |
87 | ||
88 | static void free_one_config(struct submodule_entry *entry) | |
89 | { | |
90 | free((void *) entry->config->path); | |
91 | free((void *) entry->config->name); | |
b5944f34 | 92 | free((void *) entry->config->branch); |
ea2fa5a3 | 93 | free((void *) entry->config->update_strategy.command); |
959b5455 HV |
94 | free(entry->config); |
95 | } | |
96 | ||
bf12fcdf | 97 | static void submodule_cache_clear(struct submodule_cache *cache) |
959b5455 HV |
98 | { |
99 | struct hashmap_iter iter; | |
100 | struct submodule_entry *entry; | |
101 | ||
bf12fcdf BW |
102 | if (!cache->initialized) |
103 | return; | |
104 | ||
959b5455 HV |
105 | /* |
106 | * We iterate over the name hash here to be symmetric with the | |
107 | * allocation of struct submodule entries. Each is allocated by | |
5aea9fe6 | 108 | * their .gitmodules blob sha1 and submodule name. |
959b5455 | 109 | */ |
87571c3f | 110 | hashmap_for_each_entry(&cache->for_name, &iter, entry, |
23dee69f | 111 | ent /* member name */) |
959b5455 HV |
112 | free_one_config(entry); |
113 | ||
6da1a258 EN |
114 | hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent); |
115 | hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent); | |
bf12fcdf | 116 | cache->initialized = 0; |
ff6f1f56 | 117 | cache->gitmodules_read = 0; |
bf12fcdf BW |
118 | } |
119 | ||
120 | void submodule_cache_free(struct submodule_cache *cache) | |
121 | { | |
122 | submodule_cache_clear(cache); | |
123 | free(cache); | |
959b5455 HV |
124 | } |
125 | ||
34caab02 | 126 | static unsigned int hash_oid_string(const struct object_id *oid, |
127 | const char *string) | |
959b5455 | 128 | { |
34caab02 | 129 | return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string); |
959b5455 HV |
130 | } |
131 | ||
132 | static void cache_put_path(struct submodule_cache *cache, | |
133 | struct submodule *submodule) | |
134 | { | |
34caab02 | 135 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
136 | submodule->path); | |
959b5455 | 137 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
d22245a2 | 138 | hashmap_entry_init(&e->ent, hash); |
959b5455 | 139 | e->config = submodule; |
26b455f2 | 140 | hashmap_put(&cache->for_path, &e->ent); |
959b5455 HV |
141 | } |
142 | ||
143 | static void cache_remove_path(struct submodule_cache *cache, | |
144 | struct submodule *submodule) | |
145 | { | |
34caab02 | 146 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
147 | submodule->path); | |
959b5455 HV |
148 | struct submodule_entry e; |
149 | struct submodule_entry *removed; | |
d22245a2 | 150 | hashmap_entry_init(&e.ent, hash); |
959b5455 | 151 | e.config = submodule; |
404ab78e | 152 | removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL); |
959b5455 HV |
153 | free(removed); |
154 | } | |
155 | ||
156 | static void cache_add(struct submodule_cache *cache, | |
157 | struct submodule *submodule) | |
158 | { | |
34caab02 | 159 | unsigned int hash = hash_oid_string(&submodule->gitmodules_oid, |
160 | submodule->name); | |
959b5455 | 161 | struct submodule_entry *e = xmalloc(sizeof(*e)); |
d22245a2 | 162 | hashmap_entry_init(&e->ent, hash); |
959b5455 | 163 | e->config = submodule; |
b94e5c1d | 164 | hashmap_add(&cache->for_name, &e->ent); |
959b5455 HV |
165 | } |
166 | ||
167 | static const struct submodule *cache_lookup_path(struct submodule_cache *cache, | |
34caab02 | 168 | const struct object_id *gitmodules_oid, const char *path) |
959b5455 HV |
169 | { |
170 | struct submodule_entry *entry; | |
34caab02 | 171 | unsigned int hash = hash_oid_string(gitmodules_oid, path); |
959b5455 HV |
172 | struct submodule_entry key; |
173 | struct submodule key_config; | |
174 | ||
34caab02 | 175 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); |
959b5455 HV |
176 | key_config.path = path; |
177 | ||
d22245a2 | 178 | hashmap_entry_init(&key.ent, hash); |
959b5455 HV |
179 | key.config = &key_config; |
180 | ||
404ab78e | 181 | entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL); |
959b5455 HV |
182 | if (entry) |
183 | return entry->config; | |
184 | return NULL; | |
185 | } | |
186 | ||
187 | static struct submodule *cache_lookup_name(struct submodule_cache *cache, | |
34caab02 | 188 | const struct object_id *gitmodules_oid, const char *name) |
959b5455 HV |
189 | { |
190 | struct submodule_entry *entry; | |
34caab02 | 191 | unsigned int hash = hash_oid_string(gitmodules_oid, name); |
959b5455 HV |
192 | struct submodule_entry key; |
193 | struct submodule key_config; | |
194 | ||
34caab02 | 195 | oidcpy(&key_config.gitmodules_oid, gitmodules_oid); |
959b5455 HV |
196 | key_config.name = name; |
197 | ||
d22245a2 | 198 | hashmap_entry_init(&key.ent, hash); |
959b5455 HV |
199 | key.config = &key_config; |
200 | ||
404ab78e | 201 | entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL); |
959b5455 HV |
202 | if (entry) |
203 | return entry->config; | |
204 | return NULL; | |
205 | } | |
206 | ||
0383bbb9 JK |
207 | int check_submodule_name(const char *name) |
208 | { | |
209 | /* Disallow empty names */ | |
210 | if (!*name) | |
211 | return -1; | |
212 | ||
213 | /* | |
9fd512c8 | 214 | * Look for '..' as a path component. Check is_xplatform_dir_sep() as |
0383bbb9 JK |
215 | * separators rather than is_dir_sep(), because we want the name rules |
216 | * to be consistent across platforms. | |
217 | */ | |
218 | goto in_component; /* always start inside component */ | |
219 | while (*name) { | |
220 | char c = *name++; | |
9fd512c8 | 221 | if (is_xplatform_dir_sep(c)) { |
0383bbb9 JK |
222 | in_component: |
223 | if (name[0] == '.' && name[1] == '.' && | |
9fd512c8 | 224 | (!name[2] || is_xplatform_dir_sep(name[2]))) |
0383bbb9 JK |
225 | return -1; |
226 | } | |
227 | } | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
959b5455 HV |
232 | static int name_and_item_from_var(const char *var, struct strbuf *name, |
233 | struct strbuf *item) | |
234 | { | |
235 | const char *subsection, *key; | |
f5914f4b JK |
236 | size_t subsection_len; |
237 | int parse; | |
959b5455 HV |
238 | parse = parse_config_key(var, "submodule", &subsection, |
239 | &subsection_len, &key); | |
240 | if (parse < 0 || !subsection) | |
241 | return 0; | |
242 | ||
243 | strbuf_add(name, subsection, subsection_len); | |
0383bbb9 JK |
244 | if (check_submodule_name(name->buf) < 0) { |
245 | warning(_("ignoring suspicious submodule name: %s"), name->buf); | |
246 | strbuf_release(name); | |
247 | return 0; | |
248 | } | |
249 | ||
959b5455 HV |
250 | strbuf_addstr(item, key); |
251 | ||
252 | return 1; | |
253 | } | |
254 | ||
255 | static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache, | |
34caab02 | 256 | const struct object_id *gitmodules_oid, const char *name) |
959b5455 HV |
257 | { |
258 | struct submodule *submodule; | |
259 | struct strbuf name_buf = STRBUF_INIT; | |
260 | ||
34caab02 | 261 | submodule = cache_lookup_name(cache, gitmodules_oid, name); |
959b5455 HV |
262 | if (submodule) |
263 | return submodule; | |
264 | ||
265 | submodule = xmalloc(sizeof(*submodule)); | |
266 | ||
267 | strbuf_addstr(&name_buf, name); | |
268 | submodule->name = strbuf_detach(&name_buf, NULL); | |
269 | ||
270 | submodule->path = NULL; | |
271 | submodule->url = NULL; | |
ea2fa5a3 SB |
272 | submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED; |
273 | submodule->update_strategy.command = NULL; | |
959b5455 HV |
274 | submodule->fetch_recurse = RECURSE_SUBMODULES_NONE; |
275 | submodule->ignore = NULL; | |
b5944f34 | 276 | submodule->branch = NULL; |
37f52e93 | 277 | submodule->recommend_shallow = -1; |
959b5455 | 278 | |
34caab02 | 279 | oidcpy(&submodule->gitmodules_oid, gitmodules_oid); |
959b5455 HV |
280 | |
281 | cache_add(cache, submodule); | |
282 | ||
283 | return submodule; | |
284 | } | |
285 | ||
027771fc HV |
286 | static int parse_fetch_recurse(const char *opt, const char *arg, |
287 | int die_on_error) | |
288 | { | |
89576613 | 289 | switch (git_parse_maybe_bool(arg)) { |
027771fc HV |
290 | case 1: |
291 | return RECURSE_SUBMODULES_ON; | |
292 | case 0: | |
293 | return RECURSE_SUBMODULES_OFF; | |
294 | default: | |
295 | if (!strcmp(arg, "on-demand")) | |
296 | return RECURSE_SUBMODULES_ON_DEMAND; | |
5a59a230 NTND |
297 | /* |
298 | * Please update $__git_fetch_recurse_submodules in | |
299 | * git-completion.bash when you add new options. | |
300 | */ | |
027771fc HV |
301 | if (die_on_error) |
302 | die("bad %s argument: %s", opt, arg); | |
303 | else | |
304 | return RECURSE_SUBMODULES_ERROR; | |
305 | } | |
306 | } | |
307 | ||
f20e7c1e BW |
308 | int parse_submodule_fetchjobs(const char *var, const char *value) |
309 | { | |
310 | int fetchjobs = git_config_int(var, value); | |
311 | if (fetchjobs < 0) | |
b4eda05d | 312 | die(_("negative values not allowed for submodule.fetchJobs")); |
51243f9f ÆAB |
313 | if (!fetchjobs) |
314 | fetchjobs = online_cpus(); | |
f20e7c1e BW |
315 | return fetchjobs; |
316 | } | |
317 | ||
027771fc HV |
318 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
319 | { | |
320 | return parse_fetch_recurse(opt, arg, 1); | |
321 | } | |
322 | ||
886dc154 SB |
323 | int option_fetch_parse_recurse_submodules(const struct option *opt, |
324 | const char *arg, int unset) | |
325 | { | |
326 | int *v; | |
327 | ||
328 | if (!opt->value) | |
329 | return -1; | |
330 | ||
331 | v = opt->value; | |
332 | ||
333 | if (unset) { | |
334 | *v = RECURSE_SUBMODULES_OFF; | |
335 | } else { | |
336 | if (arg) | |
337 | *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg); | |
338 | else | |
339 | *v = RECURSE_SUBMODULES_ON; | |
340 | } | |
341 | return 0; | |
342 | } | |
343 | ||
d601fd09 SB |
344 | static int parse_update_recurse(const char *opt, const char *arg, |
345 | int die_on_error) | |
346 | { | |
bdfcdefd | 347 | switch (git_parse_maybe_bool(arg)) { |
d601fd09 SB |
348 | case 1: |
349 | return RECURSE_SUBMODULES_ON; | |
350 | case 0: | |
351 | return RECURSE_SUBMODULES_OFF; | |
352 | default: | |
353 | if (die_on_error) | |
354 | die("bad %s argument: %s", opt, arg); | |
355 | return RECURSE_SUBMODULES_ERROR; | |
356 | } | |
357 | } | |
358 | ||
359 | int parse_update_recurse_submodules_arg(const char *opt, const char *arg) | |
360 | { | |
361 | return parse_update_recurse(opt, arg, 1); | |
362 | } | |
363 | ||
b33a15b0 MC |
364 | static int parse_push_recurse(const char *opt, const char *arg, |
365 | int die_on_error) | |
366 | { | |
89576613 | 367 | switch (git_parse_maybe_bool(arg)) { |
b33a15b0 MC |
368 | case 1: |
369 | /* There's no simple "on" value when pushing */ | |
370 | if (die_on_error) | |
371 | die("bad %s argument: %s", opt, arg); | |
372 | else | |
373 | return RECURSE_SUBMODULES_ERROR; | |
374 | case 0: | |
375 | return RECURSE_SUBMODULES_OFF; | |
376 | default: | |
377 | if (!strcmp(arg, "on-demand")) | |
378 | return RECURSE_SUBMODULES_ON_DEMAND; | |
379 | else if (!strcmp(arg, "check")) | |
380 | return RECURSE_SUBMODULES_CHECK; | |
6c656c3f BW |
381 | else if (!strcmp(arg, "only")) |
382 | return RECURSE_SUBMODULES_ONLY; | |
5a59a230 NTND |
383 | /* |
384 | * Please update $__git_push_recurse_submodules in | |
385 | * git-completion.bash when you add new modes. | |
386 | */ | |
b33a15b0 MC |
387 | else if (die_on_error) |
388 | die("bad %s argument: %s", opt, arg); | |
389 | else | |
390 | return RECURSE_SUBMODULES_ERROR; | |
391 | } | |
392 | } | |
393 | ||
394 | int parse_push_recurse_submodules_arg(const char *opt, const char *arg) | |
395 | { | |
396 | return parse_push_recurse(opt, arg, 1); | |
397 | } | |
398 | ||
34caab02 | 399 | static void warn_multiple_config(const struct object_id *treeish_name, |
959b5455 HV |
400 | const char *name, const char *option) |
401 | { | |
402 | const char *commit_string = "WORKTREE"; | |
73c293bb | 403 | if (treeish_name) |
34caab02 | 404 | commit_string = oid_to_hex(treeish_name); |
959b5455 HV |
405 | warning("%s:.gitmodules, multiple configurations found for " |
406 | "'submodule.%s.%s'. Skipping second one!", | |
407 | commit_string, name, option); | |
408 | } | |
409 | ||
f6adec4e JK |
410 | static void warn_command_line_option(const char *var, const char *value) |
411 | { | |
412 | warning(_("ignoring '%s' which may be interpreted as" | |
413 | " a command-line option: %s"), var, value); | |
414 | } | |
415 | ||
959b5455 HV |
416 | struct parse_config_parameter { |
417 | struct submodule_cache *cache; | |
34caab02 | 418 | const struct object_id *treeish_name; |
419 | const struct object_id *gitmodules_oid; | |
959b5455 HV |
420 | int overwrite; |
421 | }; | |
422 | ||
e904deb8 JN |
423 | /* |
424 | * Parse a config item from .gitmodules. | |
425 | * | |
426 | * This does not handle submodule-related configuration from the main | |
427 | * config store (.git/config, etc). Callers are responsible for | |
428 | * checking for overrides in the main config store when appropriate. | |
429 | */ | |
959b5455 HV |
430 | static int parse_config(const char *var, const char *value, void *data) |
431 | { | |
432 | struct parse_config_parameter *me = data; | |
433 | struct submodule *submodule; | |
434 | struct strbuf name = STRBUF_INIT, item = STRBUF_INIT; | |
435 | int ret = 0; | |
436 | ||
437 | /* this also ensures that we only parse submodule entries */ | |
438 | if (!name_and_item_from_var(var, &name, &item)) | |
439 | return 0; | |
440 | ||
147875fd | 441 | submodule = lookup_or_create_by_name(me->cache, |
34caab02 | 442 | me->gitmodules_oid, |
147875fd | 443 | name.buf); |
959b5455 HV |
444 | |
445 | if (!strcmp(item.buf, "path")) { | |
147875fd | 446 | if (!value) |
959b5455 | 447 | ret = config_error_nonbool(var); |
273c6149 JK |
448 | else if (looks_like_command_line_option(value)) |
449 | warn_command_line_option(var, value); | |
f73da110 | 450 | else if (!me->overwrite && submodule->path) |
73c293bb | 451 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 452 | "path"); |
147875fd SB |
453 | else { |
454 | if (submodule->path) | |
455 | cache_remove_path(me->cache, submodule); | |
456 | free((void *) submodule->path); | |
457 | submodule->path = xstrdup(value); | |
458 | cache_put_path(me->cache, submodule); | |
959b5455 | 459 | } |
959b5455 | 460 | } else if (!strcmp(item.buf, "fetchrecursesubmodules")) { |
027771fc | 461 | /* when parsing worktree configurations we can die early */ |
34caab02 | 462 | int die_on_error = is_null_oid(me->gitmodules_oid); |
959b5455 | 463 | if (!me->overwrite && |
147875fd | 464 | submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) |
73c293bb | 465 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 466 | "fetchrecursesubmodules"); |
147875fd SB |
467 | else |
468 | submodule->fetch_recurse = parse_fetch_recurse( | |
469 | var, value, | |
027771fc | 470 | die_on_error); |
959b5455 | 471 | } else if (!strcmp(item.buf, "ignore")) { |
147875fd SB |
472 | if (!value) |
473 | ret = config_error_nonbool(var); | |
f73da110 | 474 | else if (!me->overwrite && submodule->ignore) |
73c293bb | 475 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 476 | "ignore"); |
147875fd SB |
477 | else if (strcmp(value, "untracked") && |
478 | strcmp(value, "dirty") && | |
479 | strcmp(value, "all") && | |
480 | strcmp(value, "none")) | |
959b5455 | 481 | warning("Invalid parameter '%s' for config option " |
5ea30489 | 482 | "'submodule.%s.ignore'", value, name.buf); |
147875fd SB |
483 | else { |
484 | free((void *) submodule->ignore); | |
485 | submodule->ignore = xstrdup(value); | |
959b5455 | 486 | } |
959b5455 | 487 | } else if (!strcmp(item.buf, "url")) { |
959b5455 HV |
488 | if (!value) { |
489 | ret = config_error_nonbool(var); | |
f6adec4e JK |
490 | } else if (looks_like_command_line_option(value)) { |
491 | warn_command_line_option(var, value); | |
f73da110 | 492 | } else if (!me->overwrite && submodule->url) { |
73c293bb | 493 | warn_multiple_config(me->treeish_name, submodule->name, |
959b5455 | 494 | "url"); |
147875fd SB |
495 | } else { |
496 | free((void *) submodule->url); | |
497 | submodule->url = xstrdup(value); | |
959b5455 | 498 | } |
ea2fa5a3 SB |
499 | } else if (!strcmp(item.buf, "update")) { |
500 | if (!value) | |
501 | ret = config_error_nonbool(var); | |
502 | else if (!me->overwrite && | |
503 | submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED) | |
73c293bb | 504 | warn_multiple_config(me->treeish_name, submodule->name, |
ea2fa5a3 SB |
505 | "update"); |
506 | else if (parse_submodule_update_strategy(value, | |
e904deb8 JN |
507 | &submodule->update_strategy) < 0 || |
508 | submodule->update_strategy.type == SM_UPDATE_COMMAND) | |
1a8aea85 | 509 | die(_("invalid value for '%s'"), var); |
37f52e93 SB |
510 | } else if (!strcmp(item.buf, "shallow")) { |
511 | if (!me->overwrite && submodule->recommend_shallow != -1) | |
73c293bb | 512 | warn_multiple_config(me->treeish_name, submodule->name, |
37f52e93 | 513 | "shallow"); |
b5944f34 | 514 | else |
37f52e93 SB |
515 | submodule->recommend_shallow = |
516 | git_config_bool(var, value); | |
b5944f34 SB |
517 | } else if (!strcmp(item.buf, "branch")) { |
518 | if (!me->overwrite && submodule->branch) | |
73c293bb | 519 | warn_multiple_config(me->treeish_name, submodule->name, |
b5944f34 SB |
520 | "branch"); |
521 | else { | |
522 | free((void *)submodule->branch); | |
523 | submodule->branch = xstrdup(value); | |
37f52e93 | 524 | } |
959b5455 HV |
525 | } |
526 | ||
959b5455 HV |
527 | strbuf_release(&name); |
528 | strbuf_release(&item); | |
529 | ||
530 | return ret; | |
531 | } | |
532 | ||
1b796ace BW |
533 | static int gitmodule_oid_from_commit(const struct object_id *treeish_name, |
534 | struct object_id *gitmodules_oid, | |
535 | struct strbuf *rev) | |
959b5455 | 536 | { |
959b5455 HV |
537 | int ret = 0; |
538 | ||
cd73de47 | 539 | if (is_null_oid(treeish_name)) { |
540 | oidclr(gitmodules_oid); | |
959b5455 HV |
541 | return 1; |
542 | } | |
543 | ||
cd73de47 | 544 | strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name)); |
d850b7a5 | 545 | if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0) |
959b5455 HV |
546 | ret = 1; |
547 | ||
959b5455 HV |
548 | return ret; |
549 | } | |
550 | ||
551 | /* This does a lookup of a submodule configuration by name or by path | |
552 | * (key) with on-demand reading of the appropriate .gitmodules from | |
553 | * revisions. | |
554 | */ | |
555 | static const struct submodule *config_from(struct submodule_cache *cache, | |
cd73de47 | 556 | const struct object_id *treeish_name, const char *key, |
959b5455 HV |
557 | enum lookup_type lookup_type) |
558 | { | |
559 | struct strbuf rev = STRBUF_INIT; | |
560 | unsigned long config_size; | |
0918e250 | 561 | char *config = NULL; |
cd73de47 | 562 | struct object_id oid; |
959b5455 HV |
563 | enum object_type type; |
564 | const struct submodule *submodule = NULL; | |
565 | struct parse_config_parameter parameter; | |
566 | ||
567 | /* | |
568 | * If any parameter except the cache is a NULL pointer just | |
569 | * return the first submodule. Can be used to check whether | |
570 | * there are any submodules parsed. | |
571 | */ | |
73c293bb | 572 | if (!treeish_name || !key) { |
959b5455 HV |
573 | struct hashmap_iter iter; |
574 | struct submodule_entry *entry; | |
575 | ||
87571c3f EW |
576 | entry = hashmap_iter_first_entry(&cache->for_name, &iter, |
577 | struct submodule_entry, | |
578 | ent /* member name */); | |
959b5455 HV |
579 | if (!entry) |
580 | return NULL; | |
581 | return entry->config; | |
582 | } | |
583 | ||
cd73de47 | 584 | if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev)) |
0918e250 | 585 | goto out; |
959b5455 HV |
586 | |
587 | switch (lookup_type) { | |
588 | case lookup_name: | |
34caab02 | 589 | submodule = cache_lookup_name(cache, &oid, key); |
959b5455 HV |
590 | break; |
591 | case lookup_path: | |
34caab02 | 592 | submodule = cache_lookup_path(cache, &oid, key); |
959b5455 HV |
593 | break; |
594 | } | |
595 | if (submodule) | |
0918e250 | 596 | goto out; |
959b5455 | 597 | |
bc726bd0 ÆAB |
598 | config = repo_read_object_file(the_repository, &oid, &type, |
599 | &config_size); | |
0918e250 HV |
600 | if (!config || type != OBJ_BLOB) |
601 | goto out; | |
959b5455 HV |
602 | |
603 | /* fill the submodule config into the cache */ | |
604 | parameter.cache = cache; | |
34caab02 | 605 | parameter.treeish_name = treeish_name; |
606 | parameter.gitmodules_oid = &oid; | |
959b5455 | 607 | parameter.overwrite = 0; |
1b8132d9 | 608 | git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf, |
4574f1aa | 609 | config, config_size, ¶meter, NULL); |
514dea90 | 610 | strbuf_release(&rev); |
959b5455 HV |
611 | free(config); |
612 | ||
613 | switch (lookup_type) { | |
614 | case lookup_name: | |
34caab02 | 615 | return cache_lookup_name(cache, &oid, key); |
959b5455 | 616 | case lookup_path: |
34caab02 | 617 | return cache_lookup_path(cache, &oid, key); |
959b5455 HV |
618 | default: |
619 | return NULL; | |
620 | } | |
0918e250 HV |
621 | |
622 | out: | |
623 | strbuf_release(&rev); | |
624 | free(config); | |
625 | return submodule; | |
959b5455 HV |
626 | } |
627 | ||
bf12fcdf | 628 | static void submodule_cache_check_init(struct repository *repo) |
959b5455 | 629 | { |
bf12fcdf | 630 | if (repo->submodule_cache && repo->submodule_cache->initialized) |
959b5455 HV |
631 | return; |
632 | ||
bf12fcdf BW |
633 | if (!repo->submodule_cache) |
634 | repo->submodule_cache = submodule_cache_alloc(); | |
635 | ||
636 | submodule_cache_init(repo->submodule_cache); | |
959b5455 HV |
637 | } |
638 | ||
db64d112 AO |
639 | /* |
640 | * Note: This function is private for a reason, the '.gitmodules' file should | |
571fb965 | 641 | * not be used as a mechanism to retrieve arbitrary configuration stored in |
db64d112 AO |
642 | * the repository. |
643 | * | |
644 | * Runs the provided config function on the '.gitmodules' file found in the | |
645 | * working directory. | |
646 | */ | |
647 | static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data) | |
648 | { | |
649 | if (repo->worktree) { | |
9a83d088 MR |
650 | struct git_config_source config_source = { |
651 | 0, .scope = CONFIG_SCOPE_SUBMODULE | |
652 | }; | |
76e9bdc4 AO |
653 | const struct config_options opts = { 0 }; |
654 | struct object_id oid; | |
655 | char *file; | |
d9b8b8f8 | 656 | char *oidstr = NULL; |
76e9bdc4 AO |
657 | |
658 | file = repo_worktree_path(repo, GITMODULES_FILE); | |
659 | if (file_exists(file)) { | |
660 | config_source.file = file; | |
d9b8b8f8 NTND |
661 | } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 || |
662 | repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) { | |
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 | ||
9b6b06c1 | 670 | config_with_options(fn, data, &config_source, repo, &opts); |
76e9bdc4 AO |
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 | } |