blob: 54130f6a38572b613d4b7ee8ae1cf3bc6035055d [file] [log] [blame]
Elijah Newrend812c3b2023-04-11 00:41:56 -07001#include "git-compat-util.h"
Antonio Ospite76e9bdc2018-10-25 18:18:12 +02002#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00003#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00005#include "hex.h"
Elijah Newrenc3399322023-05-16 06:33:59 +00006#include "path.h"
Brandon Williamsbf12fcd2017-06-22 11:43:44 -07007#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Heiko Voigt959b5452015-08-17 17:21:57 -07009#include "submodule-config.h"
10#include "submodule.h"
11#include "strbuf.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070012#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000013#include "object-store-ll.h"
Stefan Beller886dc152017-06-23 12:13:00 -070014#include "parse-options.h"
Elijah Newrene3d2f202023-04-22 20:17:27 +000015#include "thread-utils.h"
Glen Choo961b1302022-01-28 16:04:45 -080016#include "tree-walk.h"
Victoria Dye13320ff2024-01-18 01:55:15 +000017#include "url.h"
Victoria Dye8430b432024-01-18 01:55:18 +000018#include "urlmatch.h"
Heiko Voigt959b5452015-08-17 17:21:57 -070019
20/*
21 * submodule cache lookup structure
22 * There is one shared set of 'struct submodule' entries which can be
Robert P. J. Day5aea9fe2018-02-13 19:09:31 -050023 * looked up by their sha1 blob id of the .gitmodules file and either
Heiko Voigt959b5452015-08-17 17:21:57 -070024 * using path or name as key.
25 * for_path stores submodule entries with path as key
26 * for_name stores submodule entries with name as key
27 */
28struct submodule_cache {
29 struct hashmap for_path;
30 struct hashmap for_name;
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070031 unsigned initialized:1;
Brandon Williamsff6f1f52017-08-03 11:19:58 -070032 unsigned gitmodules_read:1;
Heiko Voigt959b5452015-08-17 17:21:57 -070033};
34
35/*
36 * thin wrapper struct needed to insert 'struct submodule' entries to
37 * the hashmap
38 */
39struct submodule_entry {
40 struct hashmap_entry ent;
41 struct submodule *config;
42};
43
44enum lookup_type {
45 lookup_name,
46 lookup_path
47};
48
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020049static int config_path_cmp(const void *cmp_data UNUSED,
Eric Wong939af162019-10-06 23:30:37 +000050 const struct hashmap_entry *eptr,
51 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020052 const void *keydata UNUSED)
Heiko Voigt959b5452015-08-17 17:21:57 -070053{
Eric Wong939af162019-10-06 23:30:37 +000054 const struct submodule_entry *a, *b;
55
56 a = container_of(eptr, const struct submodule_entry, ent);
57 b = container_of(entry_or_key, const struct submodule_entry, ent);
Stefan Beller152cbdc2017-06-30 17:28:36 -070058
Heiko Voigt959b5452015-08-17 17:21:57 -070059 return strcmp(a->config->path, b->config->path) ||
Jeff King9001dc22018-08-28 17:22:48 -040060 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -070061}
62
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020063static int config_name_cmp(const void *cmp_data UNUSED,
Eric Wong939af162019-10-06 23:30:37 +000064 const struct hashmap_entry *eptr,
65 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020066 const void *keydata UNUSED)
Heiko Voigt959b5452015-08-17 17:21:57 -070067{
Eric Wong939af162019-10-06 23:30:37 +000068 const struct submodule_entry *a, *b;
69
70 a = container_of(eptr, const struct submodule_entry, ent);
71 b = container_of(entry_or_key, const struct submodule_entry, ent);
Stefan Beller152cbdc2017-06-30 17:28:36 -070072
Heiko Voigt959b5452015-08-17 17:21:57 -070073 return strcmp(a->config->name, b->config->name) ||
Jeff King9001dc22018-08-28 17:22:48 -040074 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -070075}
76
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070077static struct submodule_cache *submodule_cache_alloc(void)
78{
79 return xcalloc(1, sizeof(struct submodule_cache));
80}
81
82static void submodule_cache_init(struct submodule_cache *cache)
Heiko Voigt959b5452015-08-17 17:21:57 -070083{
Stefan Beller152cbdc2017-06-30 17:28:36 -070084 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
85 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070086 cache->initialized = 1;
Heiko Voigt959b5452015-08-17 17:21:57 -070087}
88
89static void free_one_config(struct submodule_entry *entry)
90{
91 free((void *) entry->config->path);
92 free((void *) entry->config->name);
Stefan Bellerb5944f32016-07-28 17:44:07 -070093 free((void *) entry->config->branch);
Stefan Bellerea2fa5a2016-02-29 18:07:11 -080094 free((void *) entry->config->update_strategy.command);
Heiko Voigt959b5452015-08-17 17:21:57 -070095 free(entry->config);
96}
97
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070098static void submodule_cache_clear(struct submodule_cache *cache)
Heiko Voigt959b5452015-08-17 17:21:57 -070099{
100 struct hashmap_iter iter;
101 struct submodule_entry *entry;
102
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700103 if (!cache->initialized)
104 return;
105
Heiko Voigt959b5452015-08-17 17:21:57 -0700106 /*
107 * We iterate over the name hash here to be symmetric with the
108 * allocation of struct submodule entries. Each is allocated by
Robert P. J. Day5aea9fe2018-02-13 19:09:31 -0500109 * their .gitmodules blob sha1 and submodule name.
Heiko Voigt959b5452015-08-17 17:21:57 -0700110 */
Eric Wong87571c32019-10-06 23:30:38 +0000111 hashmap_for_each_entry(&cache->for_name, &iter, entry,
Eric Wong23dee692019-10-06 23:30:41 +0000112 ent /* member name */)
Heiko Voigt959b5452015-08-17 17:21:57 -0700113 free_one_config(entry);
114
Elijah Newren6da1a252020-11-02 18:55:05 +0000115 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
116 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700117 cache->initialized = 0;
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700118 cache->gitmodules_read = 0;
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700119}
120
121void submodule_cache_free(struct submodule_cache *cache)
122{
123 submodule_cache_clear(cache);
124 free(cache);
Heiko Voigt959b5452015-08-17 17:21:57 -0700125}
126
brian m. carlson34caab02018-05-02 00:25:42 +0000127static unsigned int hash_oid_string(const struct object_id *oid,
128 const char *string)
Heiko Voigt959b5452015-08-17 17:21:57 -0700129{
brian m. carlson34caab02018-05-02 00:25:42 +0000130 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
Heiko Voigt959b5452015-08-17 17:21:57 -0700131}
132
133static void cache_put_path(struct submodule_cache *cache,
134 struct submodule *submodule)
135{
brian m. carlson34caab02018-05-02 00:25:42 +0000136 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
137 submodule->path);
Heiko Voigt959b5452015-08-17 17:21:57 -0700138 struct submodule_entry *e = xmalloc(sizeof(*e));
Eric Wongd22245a2019-10-06 23:30:27 +0000139 hashmap_entry_init(&e->ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700140 e->config = submodule;
Eric Wong26b455f2019-10-06 23:30:32 +0000141 hashmap_put(&cache->for_path, &e->ent);
Heiko Voigt959b5452015-08-17 17:21:57 -0700142}
143
144static void cache_remove_path(struct submodule_cache *cache,
145 struct submodule *submodule)
146{
brian m. carlson34caab02018-05-02 00:25:42 +0000147 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
148 submodule->path);
Heiko Voigt959b5452015-08-17 17:21:57 -0700149 struct submodule_entry e;
150 struct submodule_entry *removed;
Eric Wongd22245a2019-10-06 23:30:27 +0000151 hashmap_entry_init(&e.ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700152 e.config = submodule;
Eric Wong404ab782019-10-06 23:30:42 +0000153 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
Heiko Voigt959b5452015-08-17 17:21:57 -0700154 free(removed);
155}
156
157static void cache_add(struct submodule_cache *cache,
158 struct submodule *submodule)
159{
brian m. carlson34caab02018-05-02 00:25:42 +0000160 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
161 submodule->name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700162 struct submodule_entry *e = xmalloc(sizeof(*e));
Eric Wongd22245a2019-10-06 23:30:27 +0000163 hashmap_entry_init(&e->ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700164 e->config = submodule;
Eric Wongb94e5c12019-10-06 23:30:29 +0000165 hashmap_add(&cache->for_name, &e->ent);
Heiko Voigt959b5452015-08-17 17:21:57 -0700166}
167
168static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000169 const struct object_id *gitmodules_oid, const char *path)
Heiko Voigt959b5452015-08-17 17:21:57 -0700170{
171 struct submodule_entry *entry;
brian m. carlson34caab02018-05-02 00:25:42 +0000172 unsigned int hash = hash_oid_string(gitmodules_oid, path);
Heiko Voigt959b5452015-08-17 17:21:57 -0700173 struct submodule_entry key;
174 struct submodule key_config;
175
brian m. carlson34caab02018-05-02 00:25:42 +0000176 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700177 key_config.path = path;
178
Eric Wongd22245a2019-10-06 23:30:27 +0000179 hashmap_entry_init(&key.ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700180 key.config = &key_config;
181
Eric Wong404ab782019-10-06 23:30:42 +0000182 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
Heiko Voigt959b5452015-08-17 17:21:57 -0700183 if (entry)
184 return entry->config;
185 return NULL;
186}
187
188static struct submodule *cache_lookup_name(struct submodule_cache *cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000189 const struct object_id *gitmodules_oid, const char *name)
Heiko Voigt959b5452015-08-17 17:21:57 -0700190{
191 struct submodule_entry *entry;
brian m. carlson34caab02018-05-02 00:25:42 +0000192 unsigned int hash = hash_oid_string(gitmodules_oid, name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700193 struct submodule_entry key;
194 struct submodule key_config;
195
brian m. carlson34caab02018-05-02 00:25:42 +0000196 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700197 key_config.name = name;
198
Eric Wongd22245a2019-10-06 23:30:27 +0000199 hashmap_entry_init(&key.ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700200 key.config = &key_config;
201
Eric Wong404ab782019-10-06 23:30:42 +0000202 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
Heiko Voigt959b5452015-08-17 17:21:57 -0700203 if (entry)
204 return entry->config;
205 return NULL;
206}
207
Jeff King0383bbb2018-04-30 03:25:25 -0400208int check_submodule_name(const char *name)
209{
210 /* Disallow empty names */
211 if (!*name)
212 return -1;
213
214 /*
Ævar Arnfjörð Bjarmason9fd512c2022-05-16 20:10:59 +0000215 * Look for '..' as a path component. Check is_xplatform_dir_sep() as
Jeff King0383bbb2018-04-30 03:25:25 -0400216 * separators rather than is_dir_sep(), because we want the name rules
217 * to be consistent across platforms.
218 */
219 goto in_component; /* always start inside component */
220 while (*name) {
221 char c = *name++;
Ævar Arnfjörð Bjarmason9fd512c2022-05-16 20:10:59 +0000222 if (is_xplatform_dir_sep(c)) {
Jeff King0383bbb2018-04-30 03:25:25 -0400223in_component:
224 if (name[0] == '.' && name[1] == '.' &&
Ævar Arnfjörð Bjarmason9fd512c2022-05-16 20:10:59 +0000225 (!name[2] || is_xplatform_dir_sep(name[2])))
Jeff King0383bbb2018-04-30 03:25:25 -0400226 return -1;
227 }
228 }
229
230 return 0;
231}
232
Victoria Dye13320ff2024-01-18 01:55:15 +0000233static int starts_with_dot_slash(const char *const path)
234{
235 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
236 PATH_MATCH_XPLATFORM);
237}
238
239static int starts_with_dot_dot_slash(const char *const path)
240{
241 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
242 PATH_MATCH_XPLATFORM);
243}
244
245static int submodule_url_is_relative(const char *url)
246{
247 return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
248}
249
250/*
251 * Count directory components that a relative submodule URL should chop
252 * from the remote_url it is to be resolved against.
253 *
254 * In other words, this counts "../" components at the start of a
255 * submodule URL.
256 *
257 * Returns the number of directory components to chop and writes a
258 * pointer to the next character of url after all leading "./" and
259 * "../" components to out.
260 */
261static int count_leading_dotdots(const char *url, const char **out)
262{
263 int result = 0;
264 while (1) {
265 if (starts_with_dot_dot_slash(url)) {
266 result++;
267 url += strlen("../");
268 continue;
269 }
270 if (starts_with_dot_slash(url)) {
271 url += strlen("./");
272 continue;
273 }
274 *out = url;
275 return result;
276 }
277}
278/*
279 * Check whether a transport is implemented by git-remote-curl.
280 *
281 * If it is, returns 1 and writes the URL that would be passed to
282 * git-remote-curl to the "out" parameter.
283 *
284 * Otherwise, returns 0 and leaves "out" untouched.
285 *
286 * Examples:
287 * http::https://example.com/repo.git -> 1, https://example.com/repo.git
288 * https://example.com/repo.git -> 1, https://example.com/repo.git
289 * git://example.com/repo.git -> 0
290 *
291 * This is for use in checking for previously exploitable bugs that
292 * required a submodule URL to be passed to git-remote-curl.
293 */
294static int url_to_curl_url(const char *url, const char **out)
295{
296 /*
297 * We don't need to check for case-aliases, "http.exe", and so
298 * on because in the default configuration, is_transport_allowed
299 * prevents URLs with those schemes from being cloned
300 * automatically.
301 */
302 if (skip_prefix(url, "http::", out) ||
303 skip_prefix(url, "https::", out) ||
304 skip_prefix(url, "ftp::", out) ||
305 skip_prefix(url, "ftps::", out))
306 return 1;
307 if (starts_with(url, "http://") ||
308 starts_with(url, "https://") ||
309 starts_with(url, "ftp://") ||
310 starts_with(url, "ftps://")) {
311 *out = url;
312 return 1;
313 }
314 return 0;
315}
316
317int check_submodule_url(const char *url)
318{
319 const char *curl_url;
320
321 if (looks_like_command_line_option(url))
322 return -1;
323
324 if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
325 char *decoded;
326 const char *next;
327 int has_nl;
328
329 /*
330 * This could be appended to an http URL and url-decoded;
331 * check for malicious characters.
332 */
333 decoded = url_decode(url);
334 has_nl = !!strchr(decoded, '\n');
335
336 free(decoded);
337 if (has_nl)
338 return -1;
339
340 /*
341 * URLs which escape their root via "../" can overwrite
342 * the host field and previous components, resolving to
343 * URLs like https::example.com/submodule.git and
344 * https:///example.com/submodule.git that were
345 * susceptible to CVE-2020-11008.
346 */
347 if (count_leading_dotdots(url, &next) > 0 &&
348 (*next == ':' || *next == '/'))
349 return -1;
350 }
351
352 else if (url_to_curl_url(url, &curl_url)) {
Victoria Dye13320ff2024-01-18 01:55:15 +0000353 int ret = 0;
Victoria Dye8430b432024-01-18 01:55:18 +0000354 char *normalized = url_normalize(curl_url, NULL);
355 if (normalized) {
356 char *decoded = url_decode(normalized);
357 if (strchr(decoded, '\n'))
358 ret = -1;
359 free(normalized);
360 free(decoded);
361 } else {
Victoria Dye13320ff2024-01-18 01:55:15 +0000362 ret = -1;
Victoria Dye8430b432024-01-18 01:55:18 +0000363 }
364
Victoria Dye13320ff2024-01-18 01:55:15 +0000365 return ret;
366 }
367
368 return 0;
369}
370
Heiko Voigt959b5452015-08-17 17:21:57 -0700371static int name_and_item_from_var(const char *var, struct strbuf *name,
372 struct strbuf *item)
373{
374 const char *subsection, *key;
Jeff Kingf5914f42020-04-10 15:44:28 -0400375 size_t subsection_len;
376 int parse;
Heiko Voigt959b5452015-08-17 17:21:57 -0700377 parse = parse_config_key(var, "submodule", &subsection,
378 &subsection_len, &key);
379 if (parse < 0 || !subsection)
380 return 0;
381
382 strbuf_add(name, subsection, subsection_len);
Jeff King0383bbb2018-04-30 03:25:25 -0400383 if (check_submodule_name(name->buf) < 0) {
384 warning(_("ignoring suspicious submodule name: %s"), name->buf);
385 strbuf_release(name);
386 return 0;
387 }
388
Heiko Voigt959b5452015-08-17 17:21:57 -0700389 strbuf_addstr(item, key);
390
391 return 1;
392}
393
394static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000395 const struct object_id *gitmodules_oid, const char *name)
Heiko Voigt959b5452015-08-17 17:21:57 -0700396{
397 struct submodule *submodule;
398 struct strbuf name_buf = STRBUF_INIT;
399
brian m. carlson34caab02018-05-02 00:25:42 +0000400 submodule = cache_lookup_name(cache, gitmodules_oid, name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700401 if (submodule)
402 return submodule;
403
404 submodule = xmalloc(sizeof(*submodule));
405
406 strbuf_addstr(&name_buf, name);
407 submodule->name = strbuf_detach(&name_buf, NULL);
408
409 submodule->path = NULL;
410 submodule->url = NULL;
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800411 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
412 submodule->update_strategy.command = NULL;
Heiko Voigt959b5452015-08-17 17:21:57 -0700413 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
414 submodule->ignore = NULL;
Stefan Bellerb5944f32016-07-28 17:44:07 -0700415 submodule->branch = NULL;
Stefan Beller37f52e92016-05-26 14:59:42 -0700416 submodule->recommend_shallow = -1;
Heiko Voigt959b5452015-08-17 17:21:57 -0700417
brian m. carlson34caab02018-05-02 00:25:42 +0000418 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700419
420 cache_add(cache, submodule);
421
422 return submodule;
423}
424
Heiko Voigt027771f2015-08-17 17:22:00 -0700425static int parse_fetch_recurse(const char *opt, const char *arg,
426 int die_on_error)
427{
Martin Ågren89576612017-08-07 20:20:49 +0200428 switch (git_parse_maybe_bool(arg)) {
Heiko Voigt027771f2015-08-17 17:22:00 -0700429 case 1:
430 return RECURSE_SUBMODULES_ON;
431 case 0:
432 return RECURSE_SUBMODULES_OFF;
433 default:
434 if (!strcmp(arg, "on-demand"))
435 return RECURSE_SUBMODULES_ON_DEMAND;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700436 /*
437 * Please update $__git_fetch_recurse_submodules in
438 * git-completion.bash when you add new options.
439 */
Heiko Voigt027771f2015-08-17 17:22:00 -0700440 if (die_on_error)
441 die("bad %s argument: %s", opt, arg);
442 else
443 return RECURSE_SUBMODULES_ERROR;
444 }
445}
446
Glen Choo8868b1e2023-06-28 19:26:27 +0000447int parse_submodule_fetchjobs(const char *var, const char *value,
448 const struct key_value_info *kvi)
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700449{
Glen Choo8868b1e2023-06-28 19:26:27 +0000450 int fetchjobs = git_config_int(var, value, kvi);
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700451 if (fetchjobs < 0)
Jiang Xinb4eda052022-06-17 18:03:09 +0800452 die(_("negative values not allowed for submodule.fetchJobs"));
Ævar Arnfjörð Bjarmason51243f92022-10-12 23:02:24 +0200453 if (!fetchjobs)
454 fetchjobs = online_cpus();
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700455 return fetchjobs;
456}
457
Heiko Voigt027771f2015-08-17 17:22:00 -0700458int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
459{
460 return parse_fetch_recurse(opt, arg, 1);
461}
462
Stefan Beller886dc152017-06-23 12:13:00 -0700463int option_fetch_parse_recurse_submodules(const struct option *opt,
464 const char *arg, int unset)
465{
466 int *v;
467
468 if (!opt->value)
469 return -1;
470
471 v = opt->value;
472
473 if (unset) {
474 *v = RECURSE_SUBMODULES_OFF;
475 } else {
476 if (arg)
477 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
478 else
479 *v = RECURSE_SUBMODULES_ON;
480 }
481 return 0;
482}
483
Stefan Bellerd601fd02017-03-14 14:46:32 -0700484static int parse_update_recurse(const char *opt, const char *arg,
485 int die_on_error)
486{
Junio C Hamanobdfcdef2017-08-22 10:29:03 -0700487 switch (git_parse_maybe_bool(arg)) {
Stefan Bellerd601fd02017-03-14 14:46:32 -0700488 case 1:
489 return RECURSE_SUBMODULES_ON;
490 case 0:
491 return RECURSE_SUBMODULES_OFF;
492 default:
493 if (die_on_error)
494 die("bad %s argument: %s", opt, arg);
495 return RECURSE_SUBMODULES_ERROR;
496 }
497}
498
499int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
500{
501 return parse_update_recurse(opt, arg, 1);
502}
503
Mike Croweb33a15b2015-11-17 11:05:56 +0000504static int parse_push_recurse(const char *opt, const char *arg,
505 int die_on_error)
506{
Martin Ågren89576612017-08-07 20:20:49 +0200507 switch (git_parse_maybe_bool(arg)) {
Mike Croweb33a15b2015-11-17 11:05:56 +0000508 case 1:
509 /* There's no simple "on" value when pushing */
510 if (die_on_error)
511 die("bad %s argument: %s", opt, arg);
512 else
513 return RECURSE_SUBMODULES_ERROR;
514 case 0:
515 return RECURSE_SUBMODULES_OFF;
516 default:
517 if (!strcmp(arg, "on-demand"))
518 return RECURSE_SUBMODULES_ON_DEMAND;
519 else if (!strcmp(arg, "check"))
520 return RECURSE_SUBMODULES_CHECK;
Brandon Williams6c656c32016-12-19 10:25:32 -0800521 else if (!strcmp(arg, "only"))
522 return RECURSE_SUBMODULES_ONLY;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700523 /*
524 * Please update $__git_push_recurse_submodules in
525 * git-completion.bash when you add new modes.
526 */
Mike Croweb33a15b2015-11-17 11:05:56 +0000527 else if (die_on_error)
528 die("bad %s argument: %s", opt, arg);
529 else
530 return RECURSE_SUBMODULES_ERROR;
531 }
532}
533
534int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
535{
536 return parse_push_recurse(opt, arg, 1);
537}
538
brian m. carlson34caab02018-05-02 00:25:42 +0000539static void warn_multiple_config(const struct object_id *treeish_name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700540 const char *name, const char *option)
541{
542 const char *commit_string = "WORKTREE";
Stefan Beller73c293b2016-11-22 12:14:37 -0800543 if (treeish_name)
brian m. carlson34caab02018-05-02 00:25:42 +0000544 commit_string = oid_to_hex(treeish_name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700545 warning("%s:.gitmodules, multiple configurations found for "
546 "'submodule.%s.%s'. Skipping second one!",
547 commit_string, name, option);
548}
549
Jeff Kingf6adec42018-09-24 04:36:30 -0400550static void warn_command_line_option(const char *var, const char *value)
551{
552 warning(_("ignoring '%s' which may be interpreted as"
553 " a command-line option: %s"), var, value);
554}
555
Heiko Voigt959b5452015-08-17 17:21:57 -0700556struct parse_config_parameter {
557 struct submodule_cache *cache;
brian m. carlson34caab02018-05-02 00:25:42 +0000558 const struct object_id *treeish_name;
559 const struct object_id *gitmodules_oid;
Heiko Voigt959b5452015-08-17 17:21:57 -0700560 int overwrite;
561};
562
Jonathan Niedere904deb2019-12-05 01:28:28 -0800563/*
564 * Parse a config item from .gitmodules.
565 *
566 * This does not handle submodule-related configuration from the main
567 * config store (.git/config, etc). Callers are responsible for
568 * checking for overrides in the main config store when appropriate.
569 */
Glen Chooa4e7e312023-06-28 19:26:22 +0000570static int parse_config(const char *var, const char *value,
571 const struct config_context *ctx UNUSED, void *data)
Heiko Voigt959b5452015-08-17 17:21:57 -0700572{
573 struct parse_config_parameter *me = data;
574 struct submodule *submodule;
575 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
576 int ret = 0;
577
578 /* this also ensures that we only parse submodule entries */
579 if (!name_and_item_from_var(var, &name, &item))
580 return 0;
581
Stefan Beller147875f2015-10-12 10:58:58 -0700582 submodule = lookup_or_create_by_name(me->cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000583 me->gitmodules_oid,
Stefan Beller147875f2015-10-12 10:58:58 -0700584 name.buf);
Heiko Voigt959b5452015-08-17 17:21:57 -0700585
586 if (!strcmp(item.buf, "path")) {
Stefan Beller147875f2015-10-12 10:58:58 -0700587 if (!value)
Heiko Voigt959b5452015-08-17 17:21:57 -0700588 ret = config_error_nonbool(var);
Jeff King273c6142018-09-24 04:39:55 -0400589 else if (looks_like_command_line_option(value))
590 warn_command_line_option(var, value);
Stefan Bellerf73da112016-02-29 18:07:12 -0800591 else if (!me->overwrite && submodule->path)
Stefan Beller73c293b2016-11-22 12:14:37 -0800592 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700593 "path");
Stefan Beller147875f2015-10-12 10:58:58 -0700594 else {
595 if (submodule->path)
596 cache_remove_path(me->cache, submodule);
597 free((void *) submodule->path);
598 submodule->path = xstrdup(value);
599 cache_put_path(me->cache, submodule);
Heiko Voigt959b5452015-08-17 17:21:57 -0700600 }
Heiko Voigt959b5452015-08-17 17:21:57 -0700601 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
Heiko Voigt027771f2015-08-17 17:22:00 -0700602 /* when parsing worktree configurations we can die early */
brian m. carlson34caab02018-05-02 00:25:42 +0000603 int die_on_error = is_null_oid(me->gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700604 if (!me->overwrite &&
Stefan Beller147875f2015-10-12 10:58:58 -0700605 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
Stefan Beller73c293b2016-11-22 12:14:37 -0800606 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700607 "fetchrecursesubmodules");
Stefan Beller147875f2015-10-12 10:58:58 -0700608 else
609 submodule->fetch_recurse = parse_fetch_recurse(
610 var, value,
Heiko Voigt027771f2015-08-17 17:22:00 -0700611 die_on_error);
Heiko Voigt959b5452015-08-17 17:21:57 -0700612 } else if (!strcmp(item.buf, "ignore")) {
Stefan Beller147875f2015-10-12 10:58:58 -0700613 if (!value)
614 ret = config_error_nonbool(var);
Stefan Bellerf73da112016-02-29 18:07:12 -0800615 else if (!me->overwrite && submodule->ignore)
Stefan Beller73c293b2016-11-22 12:14:37 -0800616 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700617 "ignore");
Stefan Beller147875f2015-10-12 10:58:58 -0700618 else if (strcmp(value, "untracked") &&
619 strcmp(value, "dirty") &&
620 strcmp(value, "all") &&
621 strcmp(value, "none"))
Heiko Voigt959b5452015-08-17 17:21:57 -0700622 warning("Invalid parameter '%s' for config option "
Stefan Beller5ea30482017-03-14 15:14:40 -0700623 "'submodule.%s.ignore'", value, name.buf);
Stefan Beller147875f2015-10-12 10:58:58 -0700624 else {
625 free((void *) submodule->ignore);
626 submodule->ignore = xstrdup(value);
Heiko Voigt959b5452015-08-17 17:21:57 -0700627 }
Heiko Voigt959b5452015-08-17 17:21:57 -0700628 } else if (!strcmp(item.buf, "url")) {
Heiko Voigt959b5452015-08-17 17:21:57 -0700629 if (!value) {
630 ret = config_error_nonbool(var);
Jeff Kingf6adec42018-09-24 04:36:30 -0400631 } else if (looks_like_command_line_option(value)) {
632 warn_command_line_option(var, value);
Stefan Bellerf73da112016-02-29 18:07:12 -0800633 } else if (!me->overwrite && submodule->url) {
Stefan Beller73c293b2016-11-22 12:14:37 -0800634 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700635 "url");
Stefan Beller147875f2015-10-12 10:58:58 -0700636 } else {
637 free((void *) submodule->url);
638 submodule->url = xstrdup(value);
Heiko Voigt959b5452015-08-17 17:21:57 -0700639 }
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800640 } else if (!strcmp(item.buf, "update")) {
641 if (!value)
642 ret = config_error_nonbool(var);
643 else if (!me->overwrite &&
644 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
Stefan Beller73c293b2016-11-22 12:14:37 -0800645 warn_multiple_config(me->treeish_name, submodule->name,
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800646 "update");
647 else if (parse_submodule_update_strategy(value,
Jonathan Niedere904deb2019-12-05 01:28:28 -0800648 &submodule->update_strategy) < 0 ||
649 submodule->update_strategy.type == SM_UPDATE_COMMAND)
Jean-Noël Avila1a8aea82022-01-31 22:07:47 +0000650 die(_("invalid value for '%s'"), var);
Stefan Beller37f52e92016-05-26 14:59:42 -0700651 } else if (!strcmp(item.buf, "shallow")) {
652 if (!me->overwrite && submodule->recommend_shallow != -1)
Stefan Beller73c293b2016-11-22 12:14:37 -0800653 warn_multiple_config(me->treeish_name, submodule->name,
Stefan Beller37f52e92016-05-26 14:59:42 -0700654 "shallow");
Stefan Bellerb5944f32016-07-28 17:44:07 -0700655 else
Stefan Beller37f52e92016-05-26 14:59:42 -0700656 submodule->recommend_shallow =
657 git_config_bool(var, value);
Stefan Bellerb5944f32016-07-28 17:44:07 -0700658 } else if (!strcmp(item.buf, "branch")) {
Jeff King34b1a0d2023-12-07 02:11:29 -0500659 if (!value)
660 ret = config_error_nonbool(var);
661 else if (!me->overwrite && submodule->branch)
Stefan Beller73c293b2016-11-22 12:14:37 -0800662 warn_multiple_config(me->treeish_name, submodule->name,
Stefan Bellerb5944f32016-07-28 17:44:07 -0700663 "branch");
664 else {
665 free((void *)submodule->branch);
666 submodule->branch = xstrdup(value);
Stefan Beller37f52e92016-05-26 14:59:42 -0700667 }
Heiko Voigt959b5452015-08-17 17:21:57 -0700668 }
669
Heiko Voigt959b5452015-08-17 17:21:57 -0700670 strbuf_release(&name);
671 strbuf_release(&item);
672
673 return ret;
674}
675
Brandon Williams1b796ac2017-08-03 11:19:57 -0700676static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
677 struct object_id *gitmodules_oid,
678 struct strbuf *rev)
Heiko Voigt959b5452015-08-17 17:21:57 -0700679{
Heiko Voigt959b5452015-08-17 17:21:57 -0700680 int ret = 0;
681
brian m. carlsoncd73de42017-07-13 23:49:20 +0000682 if (is_null_oid(treeish_name)) {
683 oidclr(gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700684 return 1;
685 }
686
brian m. carlsoncd73de42017-07-13 23:49:20 +0000687 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200688 if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
Heiko Voigt959b5452015-08-17 17:21:57 -0700689 ret = 1;
690
Heiko Voigt959b5452015-08-17 17:21:57 -0700691 return ret;
692}
693
694/* This does a lookup of a submodule configuration by name or by path
695 * (key) with on-demand reading of the appropriate .gitmodules from
696 * revisions.
697 */
698static const struct submodule *config_from(struct submodule_cache *cache,
brian m. carlsoncd73de42017-07-13 23:49:20 +0000699 const struct object_id *treeish_name, const char *key,
Heiko Voigt959b5452015-08-17 17:21:57 -0700700 enum lookup_type lookup_type)
701{
702 struct strbuf rev = STRBUF_INIT;
703 unsigned long config_size;
Heiko Voigt0918e252016-07-28 14:49:47 +0200704 char *config = NULL;
brian m. carlsoncd73de42017-07-13 23:49:20 +0000705 struct object_id oid;
Heiko Voigt959b5452015-08-17 17:21:57 -0700706 enum object_type type;
707 const struct submodule *submodule = NULL;
708 struct parse_config_parameter parameter;
709
710 /*
711 * If any parameter except the cache is a NULL pointer just
712 * return the first submodule. Can be used to check whether
713 * there are any submodules parsed.
714 */
Stefan Beller73c293b2016-11-22 12:14:37 -0800715 if (!treeish_name || !key) {
Heiko Voigt959b5452015-08-17 17:21:57 -0700716 struct hashmap_iter iter;
717 struct submodule_entry *entry;
718
Eric Wong87571c32019-10-06 23:30:38 +0000719 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
720 struct submodule_entry,
721 ent /* member name */);
Heiko Voigt959b5452015-08-17 17:21:57 -0700722 if (!entry)
723 return NULL;
724 return entry->config;
725 }
726
brian m. carlsoncd73de42017-07-13 23:49:20 +0000727 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
Heiko Voigt0918e252016-07-28 14:49:47 +0200728 goto out;
Heiko Voigt959b5452015-08-17 17:21:57 -0700729
730 switch (lookup_type) {
731 case lookup_name:
brian m. carlson34caab02018-05-02 00:25:42 +0000732 submodule = cache_lookup_name(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700733 break;
734 case lookup_path:
brian m. carlson34caab02018-05-02 00:25:42 +0000735 submodule = cache_lookup_path(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700736 break;
737 }
738 if (submodule)
Heiko Voigt0918e252016-07-28 14:49:47 +0200739 goto out;
Heiko Voigt959b5452015-08-17 17:21:57 -0700740
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200741 config = repo_read_object_file(the_repository, &oid, &type,
742 &config_size);
Heiko Voigt0918e252016-07-28 14:49:47 +0200743 if (!config || type != OBJ_BLOB)
744 goto out;
Heiko Voigt959b5452015-08-17 17:21:57 -0700745
746 /* fill the submodule config into the cache */
747 parameter.cache = cache;
brian m. carlson34caab02018-05-02 00:25:42 +0000748 parameter.treeish_name = treeish_name;
749 parameter.gitmodules_oid = &oid;
Heiko Voigt959b5452015-08-17 17:21:57 -0700750 parameter.overwrite = 0;
Vasco Almeida1b8132d2016-07-28 13:14:03 +0000751 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
Glen Choo809d8682023-06-28 19:26:24 +0000752 config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
Heiko Voigt514dea92016-07-28 14:49:11 +0200753 strbuf_release(&rev);
Heiko Voigt959b5452015-08-17 17:21:57 -0700754 free(config);
755
756 switch (lookup_type) {
757 case lookup_name:
brian m. carlson34caab02018-05-02 00:25:42 +0000758 return cache_lookup_name(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700759 case lookup_path:
brian m. carlson34caab02018-05-02 00:25:42 +0000760 return cache_lookup_path(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700761 default:
762 return NULL;
763 }
Heiko Voigt0918e252016-07-28 14:49:47 +0200764
765out:
766 strbuf_release(&rev);
767 free(config);
768 return submodule;
Heiko Voigt959b5452015-08-17 17:21:57 -0700769}
770
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700771static void submodule_cache_check_init(struct repository *repo)
Heiko Voigt959b5452015-08-17 17:21:57 -0700772{
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700773 if (repo->submodule_cache && repo->submodule_cache->initialized)
Heiko Voigt959b5452015-08-17 17:21:57 -0700774 return;
775
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700776 if (!repo->submodule_cache)
777 repo->submodule_cache = submodule_cache_alloc();
778
779 submodule_cache_init(repo->submodule_cache);
Heiko Voigt959b5452015-08-17 17:21:57 -0700780}
781
Antonio Ospitedb64d112018-06-26 12:47:10 +0200782/*
783 * Note: This function is private for a reason, the '.gitmodules' file should
ryenus571fb962019-12-15 15:12:24 +0000784 * not be used as a mechanism to retrieve arbitrary configuration stored in
Antonio Ospitedb64d112018-06-26 12:47:10 +0200785 * the repository.
786 *
787 * Runs the provided config function on the '.gitmodules' file found in the
788 * working directory.
789 */
790static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
791{
792 if (repo->worktree) {
Matthew Rogers9a83d082020-02-10 00:30:58 +0000793 struct git_config_source config_source = {
794 0, .scope = CONFIG_SCOPE_SUBMODULE
795 };
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200796 const struct config_options opts = { 0 };
797 struct object_id oid;
798 char *file;
Nguyễn Thái Ngọc Duyd9b8b8f2019-04-16 16:33:38 +0700799 char *oidstr = NULL;
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200800
801 file = repo_worktree_path(repo, GITMODULES_FILE);
802 if (file_exists(file)) {
803 config_source.file = file;
Nguyễn Thái Ngọc Duyd9b8b8f2019-04-16 16:33:38 +0700804 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
805 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
806 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
807 if (repo != the_repository)
Jonathan Tane3e8bf02021-08-16 14:09:57 -0700808 add_submodule_odb_by_path(repo->objects->odb->path);
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200809 } else {
810 goto out;
811 }
812
Victoria Dye9b6b06c2023-05-26 01:32:59 +0000813 config_with_options(fn, data, &config_source, repo, &opts);
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200814
815out:
Nguyễn Thái Ngọc Duyd9b8b8f2019-04-16 16:33:38 +0700816 free(oidstr);
Antonio Ospitedb64d112018-06-26 12:47:10 +0200817 free(file);
818 }
819}
820
Glen Chooa4e7e312023-06-28 19:26:22 +0000821static int gitmodules_cb(const char *var, const char *value,
822 const struct config_context *ctx, void *data)
Heiko Voigt851e18c2015-08-17 17:21:59 -0700823{
Brandon Williams1b796ac2017-08-03 11:19:57 -0700824 struct repository *repo = data;
Heiko Voigt851e18c2015-08-17 17:21:59 -0700825 struct parse_config_parameter parameter;
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700826
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700827 parameter.cache = repo->submodule_cache;
Stefan Beller73c293b2016-11-22 12:14:37 -0800828 parameter.treeish_name = NULL;
brian m. carlson14228442021-04-26 01:02:56 +0000829 parameter.gitmodules_oid = null_oid();
Heiko Voigt851e18c2015-08-17 17:21:59 -0700830 parameter.overwrite = 1;
831
Glen Chooa4e7e312023-06-28 19:26:22 +0000832 return parse_config(var, value, ctx, &parameter);
Heiko Voigt851e18c2015-08-17 17:21:59 -0700833}
834
Matheus Tavaresd7992422020-01-15 23:39:55 -0300835void repo_read_gitmodules(struct repository *repo, int skip_if_read)
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700836{
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700837 submodule_cache_check_init(repo);
838
Matheus Tavaresd7992422020-01-15 23:39:55 -0300839 if (repo->submodule_cache->gitmodules_read && skip_if_read)
840 return;
841
Antonio Ospitedb64d112018-06-26 12:47:10 +0200842 if (repo_read_index(repo) < 0)
843 return;
Brandon Williams1b796ac2017-08-03 11:19:57 -0700844
Antonio Ospitedb64d112018-06-26 12:47:10 +0200845 if (!is_gitmodules_unmerged(repo->index))
846 config_from_gitmodules(gitmodules_cb, repo, repo);
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700847
848 repo->submodule_cache->gitmodules_read = 1;
Brandon Williams1b796ac2017-08-03 11:19:57 -0700849}
850
851void gitmodules_config_oid(const struct object_id *commit_oid)
852{
853 struct strbuf rev = STRBUF_INIT;
854 struct object_id oid;
855
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700856 submodule_cache_check_init(the_repository);
857
Brandon Williams1b796ac2017-08-03 11:19:57 -0700858 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
859 git_config_from_blob_oid(gitmodules_cb, rev.buf,
Glen Choo809d8682023-06-28 19:26:24 +0000860 the_repository, &oid, the_repository,
861 CONFIG_SCOPE_UNKNOWN);
Brandon Williams1b796ac2017-08-03 11:19:57 -0700862 }
863 strbuf_release(&rev);
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700864
865 the_repository->submodule_cache->gitmodules_read = 1;
866}
867
Stefan Beller3b8fb392018-03-28 15:35:29 -0700868const struct submodule *submodule_from_name(struct repository *r,
869 const struct object_id *treeish_name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700870 const char *name)
871{
Matheus Tavaresd7992422020-01-15 23:39:55 -0300872 repo_read_gitmodules(r, 1);
Stefan Beller3b8fb392018-03-28 15:35:29 -0700873 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700874}
875
Stefan Beller3b8fb392018-03-28 15:35:29 -0700876const struct submodule *submodule_from_path(struct repository *r,
877 const struct object_id *treeish_name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700878 const char *path)
879{
Matheus Tavaresd7992422020-01-15 23:39:55 -0300880 repo_read_gitmodules(r, 1);
Stefan Beller3b8fb392018-03-28 15:35:29 -0700881 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700882}
883
Glen Choo961b1302022-01-28 16:04:45 -0800884/**
885 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
886 * and appends submodule entries to 'out'. The submodule_cache expects
887 * a root-level treeish_name and paths, so keep track of these values
888 * with 'root_tree' and 'prefix'.
889 */
890static void traverse_tree_submodules(struct repository *r,
891 const struct object_id *root_tree,
892 char *prefix,
893 const struct object_id *treeish_name,
894 struct submodule_entry_list *out)
895{
896 struct tree_desc tree;
897 struct submodule_tree_entry *st_entry;
898 struct name_entry *name_entry;
899 char *tree_path = NULL;
900
901 name_entry = xmalloc(sizeof(*name_entry));
902
903 fill_tree_descriptor(r, &tree, treeish_name);
904 while (tree_entry(&tree, name_entry)) {
905 if (prefix)
906 tree_path =
907 mkpathdup("%s/%s", prefix, name_entry->path);
908 else
909 tree_path = xstrdup(name_entry->path);
910
911 if (S_ISGITLINK(name_entry->mode) &&
912 is_tree_submodule_active(r, root_tree, tree_path)) {
Johannes Schindelinf5355922022-06-15 23:35:39 +0000913 ALLOC_GROW(out->entries, out->entry_nr + 1,
914 out->entry_alloc);
915 st_entry = &out->entries[out->entry_nr++];
916
Glen Choo961b1302022-01-28 16:04:45 -0800917 st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
918 *st_entry->name_entry = *name_entry;
919 st_entry->submodule =
920 submodule_from_path(r, root_tree, tree_path);
921 st_entry->repo = xmalloc(sizeof(*st_entry->repo));
922 if (repo_submodule_init(st_entry->repo, r, tree_path,
923 root_tree))
924 FREE_AND_NULL(st_entry->repo);
925
Glen Choo961b1302022-01-28 16:04:45 -0800926 } else if (S_ISDIR(name_entry->mode))
927 traverse_tree_submodules(r, root_tree, tree_path,
928 &name_entry->oid, out);
929 free(tree_path);
930 }
931}
932
933void submodules_of_tree(struct repository *r,
934 const struct object_id *treeish_name,
935 struct submodule_entry_list *out)
936{
937 CALLOC_ARRAY(out->entries, 0);
938 out->entry_nr = 0;
939 out->entry_alloc = 0;
940
941 traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
942}
943
Stefan Bellerf793b892018-03-28 15:35:28 -0700944void submodule_free(struct repository *r)
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700945{
Stefan Bellerf793b892018-03-28 15:35:28 -0700946 if (r->submodule_cache)
947 submodule_cache_clear(r->submodule_cache);
Heiko Voigt959b5452015-08-17 17:21:57 -0700948}
Antonio Ospitead136372018-06-26 12:47:05 +0200949
Glen Chooa4e7e312023-06-28 19:26:22 +0000950static int config_print_callback(const char *var, const char *value,
951 const struct config_context *ctx UNUSED,
952 void *cb_data)
Antonio Ospitebcbc7802018-10-05 15:05:52 +0200953{
954 char *wanted_key = cb_data;
955
956 if (!strcmp(wanted_key, var))
957 printf("%s\n", value);
958
959 return 0;
960}
961
962int print_config_from_gitmodules(struct repository *repo, const char *key)
963{
964 int ret;
965 char *store_key;
966
967 ret = git_config_parse_key(key, &store_key, NULL);
968 if (ret < 0)
969 return CONFIG_INVALID_KEY;
970
971 config_from_gitmodules(config_print_callback, repo, store_key);
972
973 free(store_key);
974 return 0;
975}
976
Antonio Ospite45f5ef32018-10-05 15:05:53 +0200977int config_set_in_gitmodules_file_gently(const char *key, const char *value)
978{
979 int ret;
980
981 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
982 if (ret < 0)
983 /* Maybe the user already did that, don't error out here */
984 warning(_("Could not update .gitmodules entry %s"), key);
985
986 return ret;
987}
988
Antonio Ospite71a69532018-06-26 12:47:06 +0200989struct fetch_config {
990 int *max_children;
991 int *recurse_submodules;
992};
993
Glen Chooa4e7e312023-06-28 19:26:22 +0000994static int gitmodules_fetch_config(const char *var, const char *value,
Glen Choo8868b1e2023-06-28 19:26:27 +0000995 const struct config_context *ctx,
Glen Chooa4e7e312023-06-28 19:26:22 +0000996 void *cb)
Antonio Ospite71a69532018-06-26 12:47:06 +0200997{
998 struct fetch_config *config = cb;
999 if (!strcmp(var, "submodule.fetchjobs")) {
Jonathan Tane5b94212020-08-17 21:01:33 -07001000 if (config->max_children)
1001 *(config->max_children) =
Glen Choo8868b1e2023-06-28 19:26:27 +00001002 parse_submodule_fetchjobs(var, value, ctx->kvi);
Antonio Ospite71a69532018-06-26 12:47:06 +02001003 return 0;
1004 } else if (!strcmp(var, "fetch.recursesubmodules")) {
Jonathan Tane5b94212020-08-17 21:01:33 -07001005 if (config->recurse_submodules)
1006 *(config->recurse_submodules) =
1007 parse_fetch_recurse_submodules_arg(var, value);
Antonio Ospite71a69532018-06-26 12:47:06 +02001008 return 0;
1009 }
1010
1011 return 0;
1012}
1013
1014void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
1015{
1016 struct fetch_config config = {
1017 .max_children = max_children,
1018 .recurse_submodules = recurse_submodules
1019 };
Antonio Ospite9a0fb3e2018-06-26 12:47:09 +02001020 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
Antonio Ospite71a69532018-06-26 12:47:06 +02001021}
Antonio Ospite05744992018-06-26 12:47:07 +02001022
1023static int gitmodules_update_clone_config(const char *var, const char *value,
Glen Choo8868b1e2023-06-28 19:26:27 +00001024 const struct config_context *ctx,
Antonio Ospite05744992018-06-26 12:47:07 +02001025 void *cb)
1026{
1027 int *max_jobs = cb;
1028 if (!strcmp(var, "submodule.fetchjobs"))
Glen Choo8868b1e2023-06-28 19:26:27 +00001029 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
Antonio Ospite05744992018-06-26 12:47:07 +02001030 return 0;
1031}
1032
1033void update_clone_config_from_gitmodules(int *max_jobs)
1034{
Antonio Ospite9a0fb3e2018-06-26 12:47:09 +02001035 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
Antonio Ospite05744992018-06-26 12:47:07 +02001036}