blob: 2026120fb3891ba53a8bb676e2da5a6248984cf7 [file] [log] [blame]
Heiko Voigt959b5452015-08-17 17:21:57 -07001#include "cache.h"
Antonio Ospite76e9bdc2018-10-25 18:18:12 +02002#include "dir.h"
Brandon Williamsbf12fcd2017-06-22 11:43:44 -07003#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07004#include "config.h"
Heiko Voigt959b5452015-08-17 17:21:57 -07005#include "submodule-config.h"
6#include "submodule.h"
7#include "strbuf.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07008#include "object-store.h"
Stefan Beller886dc152017-06-23 12:13:00 -07009#include "parse-options.h"
Heiko Voigt959b5452015-08-17 17:21:57 -070010
11/*
12 * submodule cache lookup structure
13 * There is one shared set of 'struct submodule' entries which can be
Robert P. J. Day5aea9fe2018-02-13 19:09:31 -050014 * looked up by their sha1 blob id of the .gitmodules file and either
Heiko Voigt959b5452015-08-17 17:21:57 -070015 * using path or name as key.
16 * for_path stores submodule entries with path as key
17 * for_name stores submodule entries with name as key
18 */
19struct submodule_cache {
20 struct hashmap for_path;
21 struct hashmap for_name;
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070022 unsigned initialized:1;
Brandon Williamsff6f1f52017-08-03 11:19:58 -070023 unsigned gitmodules_read:1;
Heiko Voigt959b5452015-08-17 17:21:57 -070024};
25
26/*
27 * thin wrapper struct needed to insert 'struct submodule' entries to
28 * the hashmap
29 */
30struct submodule_entry {
31 struct hashmap_entry ent;
32 struct submodule *config;
33};
34
35enum lookup_type {
36 lookup_name,
37 lookup_path
38};
39
Stefan Beller7663cdc2017-06-30 12:14:05 -070040static int config_path_cmp(const void *unused_cmp_data,
Eric Wong939af162019-10-06 23:30:37 +000041 const struct hashmap_entry *eptr,
42 const struct hashmap_entry *entry_or_key,
Stefan Beller7663cdc2017-06-30 12:14:05 -070043 const void *unused_keydata)
Heiko Voigt959b5452015-08-17 17:21:57 -070044{
Eric Wong939af162019-10-06 23:30:37 +000045 const struct submodule_entry *a, *b;
46
47 a = container_of(eptr, const struct submodule_entry, ent);
48 b = container_of(entry_or_key, const struct submodule_entry, ent);
Stefan Beller152cbdc2017-06-30 17:28:36 -070049
Heiko Voigt959b5452015-08-17 17:21:57 -070050 return strcmp(a->config->path, b->config->path) ||
Jeff King9001dc22018-08-28 17:22:48 -040051 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -070052}
53
Stefan Beller7663cdc2017-06-30 12:14:05 -070054static int config_name_cmp(const void *unused_cmp_data,
Eric Wong939af162019-10-06 23:30:37 +000055 const struct hashmap_entry *eptr,
56 const struct hashmap_entry *entry_or_key,
Stefan Beller7663cdc2017-06-30 12:14:05 -070057 const void *unused_keydata)
Heiko Voigt959b5452015-08-17 17:21:57 -070058{
Eric Wong939af162019-10-06 23:30:37 +000059 const struct submodule_entry *a, *b;
60
61 a = container_of(eptr, const struct submodule_entry, ent);
62 b = container_of(entry_or_key, const struct submodule_entry, ent);
Stefan Beller152cbdc2017-06-30 17:28:36 -070063
Heiko Voigt959b5452015-08-17 17:21:57 -070064 return strcmp(a->config->name, b->config->name) ||
Jeff King9001dc22018-08-28 17:22:48 -040065 !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -070066}
67
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070068static struct submodule_cache *submodule_cache_alloc(void)
69{
70 return xcalloc(1, sizeof(struct submodule_cache));
71}
72
73static void submodule_cache_init(struct submodule_cache *cache)
Heiko Voigt959b5452015-08-17 17:21:57 -070074{
Stefan Beller152cbdc2017-06-30 17:28:36 -070075 hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
76 hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070077 cache->initialized = 1;
Heiko Voigt959b5452015-08-17 17:21:57 -070078}
79
80static void free_one_config(struct submodule_entry *entry)
81{
82 free((void *) entry->config->path);
83 free((void *) entry->config->name);
Stefan Bellerb5944f32016-07-28 17:44:07 -070084 free((void *) entry->config->branch);
Stefan Bellerea2fa5a2016-02-29 18:07:11 -080085 free((void *) entry->config->update_strategy.command);
Heiko Voigt959b5452015-08-17 17:21:57 -070086 free(entry->config);
87}
88
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070089static void submodule_cache_clear(struct submodule_cache *cache)
Heiko Voigt959b5452015-08-17 17:21:57 -070090{
91 struct hashmap_iter iter;
92 struct submodule_entry *entry;
93
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070094 if (!cache->initialized)
95 return;
96
Heiko Voigt959b5452015-08-17 17:21:57 -070097 /*
98 * We iterate over the name hash here to be symmetric with the
99 * allocation of struct submodule entries. Each is allocated by
Robert P. J. Day5aea9fe2018-02-13 19:09:31 -0500100 * their .gitmodules blob sha1 and submodule name.
Heiko Voigt959b5452015-08-17 17:21:57 -0700101 */
Eric Wong87571c32019-10-06 23:30:38 +0000102 hashmap_for_each_entry(&cache->for_name, &iter, entry,
Eric Wong23dee692019-10-06 23:30:41 +0000103 ent /* member name */)
Heiko Voigt959b5452015-08-17 17:21:57 -0700104 free_one_config(entry);
105
Elijah Newren6da1a252020-11-02 18:55:05 +0000106 hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
107 hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700108 cache->initialized = 0;
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700109 cache->gitmodules_read = 0;
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700110}
111
112void submodule_cache_free(struct submodule_cache *cache)
113{
114 submodule_cache_clear(cache);
115 free(cache);
Heiko Voigt959b5452015-08-17 17:21:57 -0700116}
117
brian m. carlson34caab02018-05-02 00:25:42 +0000118static unsigned int hash_oid_string(const struct object_id *oid,
119 const char *string)
Heiko Voigt959b5452015-08-17 17:21:57 -0700120{
brian m. carlson34caab02018-05-02 00:25:42 +0000121 return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
Heiko Voigt959b5452015-08-17 17:21:57 -0700122}
123
124static void cache_put_path(struct submodule_cache *cache,
125 struct submodule *submodule)
126{
brian m. carlson34caab02018-05-02 00:25:42 +0000127 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
128 submodule->path);
Heiko Voigt959b5452015-08-17 17:21:57 -0700129 struct submodule_entry *e = xmalloc(sizeof(*e));
Eric Wongd22245a2019-10-06 23:30:27 +0000130 hashmap_entry_init(&e->ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700131 e->config = submodule;
Eric Wong26b455f2019-10-06 23:30:32 +0000132 hashmap_put(&cache->for_path, &e->ent);
Heiko Voigt959b5452015-08-17 17:21:57 -0700133}
134
135static void cache_remove_path(struct submodule_cache *cache,
136 struct submodule *submodule)
137{
brian m. carlson34caab02018-05-02 00:25:42 +0000138 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
139 submodule->path);
Heiko Voigt959b5452015-08-17 17:21:57 -0700140 struct submodule_entry e;
141 struct submodule_entry *removed;
Eric Wongd22245a2019-10-06 23:30:27 +0000142 hashmap_entry_init(&e.ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700143 e.config = submodule;
Eric Wong404ab782019-10-06 23:30:42 +0000144 removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
Heiko Voigt959b5452015-08-17 17:21:57 -0700145 free(removed);
146}
147
148static void cache_add(struct submodule_cache *cache,
149 struct submodule *submodule)
150{
brian m. carlson34caab02018-05-02 00:25:42 +0000151 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
152 submodule->name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700153 struct submodule_entry *e = xmalloc(sizeof(*e));
Eric Wongd22245a2019-10-06 23:30:27 +0000154 hashmap_entry_init(&e->ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700155 e->config = submodule;
Eric Wongb94e5c12019-10-06 23:30:29 +0000156 hashmap_add(&cache->for_name, &e->ent);
Heiko Voigt959b5452015-08-17 17:21:57 -0700157}
158
159static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000160 const struct object_id *gitmodules_oid, const char *path)
Heiko Voigt959b5452015-08-17 17:21:57 -0700161{
162 struct submodule_entry *entry;
brian m. carlson34caab02018-05-02 00:25:42 +0000163 unsigned int hash = hash_oid_string(gitmodules_oid, path);
Heiko Voigt959b5452015-08-17 17:21:57 -0700164 struct submodule_entry key;
165 struct submodule key_config;
166
brian m. carlson34caab02018-05-02 00:25:42 +0000167 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700168 key_config.path = path;
169
Eric Wongd22245a2019-10-06 23:30:27 +0000170 hashmap_entry_init(&key.ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700171 key.config = &key_config;
172
Eric Wong404ab782019-10-06 23:30:42 +0000173 entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
Heiko Voigt959b5452015-08-17 17:21:57 -0700174 if (entry)
175 return entry->config;
176 return NULL;
177}
178
179static struct submodule *cache_lookup_name(struct submodule_cache *cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000180 const struct object_id *gitmodules_oid, const char *name)
Heiko Voigt959b5452015-08-17 17:21:57 -0700181{
182 struct submodule_entry *entry;
brian m. carlson34caab02018-05-02 00:25:42 +0000183 unsigned int hash = hash_oid_string(gitmodules_oid, name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700184 struct submodule_entry key;
185 struct submodule key_config;
186
brian m. carlson34caab02018-05-02 00:25:42 +0000187 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700188 key_config.name = name;
189
Eric Wongd22245a2019-10-06 23:30:27 +0000190 hashmap_entry_init(&key.ent, hash);
Heiko Voigt959b5452015-08-17 17:21:57 -0700191 key.config = &key_config;
192
Eric Wong404ab782019-10-06 23:30:42 +0000193 entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
Heiko Voigt959b5452015-08-17 17:21:57 -0700194 if (entry)
195 return entry->config;
196 return NULL;
197}
198
Jeff King0383bbb2018-04-30 03:25:25 -0400199int check_submodule_name(const char *name)
200{
201 /* Disallow empty names */
202 if (!*name)
203 return -1;
204
205 /*
206 * Look for '..' as a path component. Check both '/' and '\\' as
207 * separators rather than is_dir_sep(), because we want the name rules
208 * to be consistent across platforms.
209 */
210 goto in_component; /* always start inside component */
211 while (*name) {
212 char c = *name++;
213 if (c == '/' || c == '\\') {
214in_component:
215 if (name[0] == '.' && name[1] == '.' &&
216 (!name[2] || name[2] == '/' || name[2] == '\\'))
217 return -1;
218 }
219 }
220
221 return 0;
222}
223
Heiko Voigt959b5452015-08-17 17:21:57 -0700224static int name_and_item_from_var(const char *var, struct strbuf *name,
225 struct strbuf *item)
226{
227 const char *subsection, *key;
Jeff Kingf5914f42020-04-10 15:44:28 -0400228 size_t subsection_len;
229 int parse;
Heiko Voigt959b5452015-08-17 17:21:57 -0700230 parse = parse_config_key(var, "submodule", &subsection,
231 &subsection_len, &key);
232 if (parse < 0 || !subsection)
233 return 0;
234
235 strbuf_add(name, subsection, subsection_len);
Jeff King0383bbb2018-04-30 03:25:25 -0400236 if (check_submodule_name(name->buf) < 0) {
237 warning(_("ignoring suspicious submodule name: %s"), name->buf);
238 strbuf_release(name);
239 return 0;
240 }
241
Heiko Voigt959b5452015-08-17 17:21:57 -0700242 strbuf_addstr(item, key);
243
244 return 1;
245}
246
247static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000248 const struct object_id *gitmodules_oid, const char *name)
Heiko Voigt959b5452015-08-17 17:21:57 -0700249{
250 struct submodule *submodule;
251 struct strbuf name_buf = STRBUF_INIT;
252
brian m. carlson34caab02018-05-02 00:25:42 +0000253 submodule = cache_lookup_name(cache, gitmodules_oid, name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700254 if (submodule)
255 return submodule;
256
257 submodule = xmalloc(sizeof(*submodule));
258
259 strbuf_addstr(&name_buf, name);
260 submodule->name = strbuf_detach(&name_buf, NULL);
261
262 submodule->path = NULL;
263 submodule->url = NULL;
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800264 submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
265 submodule->update_strategy.command = NULL;
Heiko Voigt959b5452015-08-17 17:21:57 -0700266 submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
267 submodule->ignore = NULL;
Stefan Bellerb5944f32016-07-28 17:44:07 -0700268 submodule->branch = NULL;
Stefan Beller37f52e92016-05-26 14:59:42 -0700269 submodule->recommend_shallow = -1;
Heiko Voigt959b5452015-08-17 17:21:57 -0700270
brian m. carlson34caab02018-05-02 00:25:42 +0000271 oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700272
273 cache_add(cache, submodule);
274
275 return submodule;
276}
277
Heiko Voigt027771f2015-08-17 17:22:00 -0700278static int parse_fetch_recurse(const char *opt, const char *arg,
279 int die_on_error)
280{
Martin Ågren89576612017-08-07 20:20:49 +0200281 switch (git_parse_maybe_bool(arg)) {
Heiko Voigt027771f2015-08-17 17:22:00 -0700282 case 1:
283 return RECURSE_SUBMODULES_ON;
284 case 0:
285 return RECURSE_SUBMODULES_OFF;
286 default:
287 if (!strcmp(arg, "on-demand"))
288 return RECURSE_SUBMODULES_ON_DEMAND;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700289 /*
290 * Please update $__git_fetch_recurse_submodules in
291 * git-completion.bash when you add new options.
292 */
Heiko Voigt027771f2015-08-17 17:22:00 -0700293 if (die_on_error)
294 die("bad %s argument: %s", opt, arg);
295 else
296 return RECURSE_SUBMODULES_ERROR;
297 }
298}
299
Brandon Williamsf20e7c12017-08-02 12:49:18 -0700300int parse_submodule_fetchjobs(const char *var, const char *value)
301{
302 int fetchjobs = git_config_int(var, value);
303 if (fetchjobs < 0)
304 die(_("negative values not allowed for submodule.fetchjobs"));
305 return fetchjobs;
306}
307
Heiko Voigt027771f2015-08-17 17:22:00 -0700308int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
309{
310 return parse_fetch_recurse(opt, arg, 1);
311}
312
Stefan Beller886dc152017-06-23 12:13:00 -0700313int option_fetch_parse_recurse_submodules(const struct option *opt,
314 const char *arg, int unset)
315{
316 int *v;
317
318 if (!opt->value)
319 return -1;
320
321 v = opt->value;
322
323 if (unset) {
324 *v = RECURSE_SUBMODULES_OFF;
325 } else {
326 if (arg)
327 *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
328 else
329 *v = RECURSE_SUBMODULES_ON;
330 }
331 return 0;
332}
333
Stefan Bellerd601fd02017-03-14 14:46:32 -0700334static int parse_update_recurse(const char *opt, const char *arg,
335 int die_on_error)
336{
Junio C Hamanobdfcdef2017-08-22 10:29:03 -0700337 switch (git_parse_maybe_bool(arg)) {
Stefan Bellerd601fd02017-03-14 14:46:32 -0700338 case 1:
339 return RECURSE_SUBMODULES_ON;
340 case 0:
341 return RECURSE_SUBMODULES_OFF;
342 default:
343 if (die_on_error)
344 die("bad %s argument: %s", opt, arg);
345 return RECURSE_SUBMODULES_ERROR;
346 }
347}
348
349int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
350{
351 return parse_update_recurse(opt, arg, 1);
352}
353
Mike Croweb33a15b2015-11-17 11:05:56 +0000354static int parse_push_recurse(const char *opt, const char *arg,
355 int die_on_error)
356{
Martin Ågren89576612017-08-07 20:20:49 +0200357 switch (git_parse_maybe_bool(arg)) {
Mike Croweb33a15b2015-11-17 11:05:56 +0000358 case 1:
359 /* There's no simple "on" value when pushing */
360 if (die_on_error)
361 die("bad %s argument: %s", opt, arg);
362 else
363 return RECURSE_SUBMODULES_ERROR;
364 case 0:
365 return RECURSE_SUBMODULES_OFF;
366 default:
367 if (!strcmp(arg, "on-demand"))
368 return RECURSE_SUBMODULES_ON_DEMAND;
369 else if (!strcmp(arg, "check"))
370 return RECURSE_SUBMODULES_CHECK;
Brandon Williams6c656c32016-12-19 10:25:32 -0800371 else if (!strcmp(arg, "only"))
372 return RECURSE_SUBMODULES_ONLY;
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700373 /*
374 * Please update $__git_push_recurse_submodules in
375 * git-completion.bash when you add new modes.
376 */
Mike Croweb33a15b2015-11-17 11:05:56 +0000377 else if (die_on_error)
378 die("bad %s argument: %s", opt, arg);
379 else
380 return RECURSE_SUBMODULES_ERROR;
381 }
382}
383
384int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
385{
386 return parse_push_recurse(opt, arg, 1);
387}
388
brian m. carlson34caab02018-05-02 00:25:42 +0000389static void warn_multiple_config(const struct object_id *treeish_name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700390 const char *name, const char *option)
391{
392 const char *commit_string = "WORKTREE";
Stefan Beller73c293b2016-11-22 12:14:37 -0800393 if (treeish_name)
brian m. carlson34caab02018-05-02 00:25:42 +0000394 commit_string = oid_to_hex(treeish_name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700395 warning("%s:.gitmodules, multiple configurations found for "
396 "'submodule.%s.%s'. Skipping second one!",
397 commit_string, name, option);
398}
399
Jeff Kingf6adec42018-09-24 04:36:30 -0400400static void warn_command_line_option(const char *var, const char *value)
401{
402 warning(_("ignoring '%s' which may be interpreted as"
403 " a command-line option: %s"), var, value);
404}
405
Heiko Voigt959b5452015-08-17 17:21:57 -0700406struct parse_config_parameter {
407 struct submodule_cache *cache;
brian m. carlson34caab02018-05-02 00:25:42 +0000408 const struct object_id *treeish_name;
409 const struct object_id *gitmodules_oid;
Heiko Voigt959b5452015-08-17 17:21:57 -0700410 int overwrite;
411};
412
Jonathan Niedere904deb2019-12-05 01:28:28 -0800413/*
414 * Parse a config item from .gitmodules.
415 *
416 * This does not handle submodule-related configuration from the main
417 * config store (.git/config, etc). Callers are responsible for
418 * checking for overrides in the main config store when appropriate.
419 */
Heiko Voigt959b5452015-08-17 17:21:57 -0700420static int parse_config(const char *var, const char *value, void *data)
421{
422 struct parse_config_parameter *me = data;
423 struct submodule *submodule;
424 struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
425 int ret = 0;
426
427 /* this also ensures that we only parse submodule entries */
428 if (!name_and_item_from_var(var, &name, &item))
429 return 0;
430
Stefan Beller147875f2015-10-12 10:58:58 -0700431 submodule = lookup_or_create_by_name(me->cache,
brian m. carlson34caab02018-05-02 00:25:42 +0000432 me->gitmodules_oid,
Stefan Beller147875f2015-10-12 10:58:58 -0700433 name.buf);
Heiko Voigt959b5452015-08-17 17:21:57 -0700434
435 if (!strcmp(item.buf, "path")) {
Stefan Beller147875f2015-10-12 10:58:58 -0700436 if (!value)
Heiko Voigt959b5452015-08-17 17:21:57 -0700437 ret = config_error_nonbool(var);
Jeff King273c6142018-09-24 04:39:55 -0400438 else if (looks_like_command_line_option(value))
439 warn_command_line_option(var, value);
Stefan Bellerf73da112016-02-29 18:07:12 -0800440 else if (!me->overwrite && submodule->path)
Stefan Beller73c293b2016-11-22 12:14:37 -0800441 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700442 "path");
Stefan Beller147875f2015-10-12 10:58:58 -0700443 else {
444 if (submodule->path)
445 cache_remove_path(me->cache, submodule);
446 free((void *) submodule->path);
447 submodule->path = xstrdup(value);
448 cache_put_path(me->cache, submodule);
Heiko Voigt959b5452015-08-17 17:21:57 -0700449 }
Heiko Voigt959b5452015-08-17 17:21:57 -0700450 } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
Heiko Voigt027771f2015-08-17 17:22:00 -0700451 /* when parsing worktree configurations we can die early */
brian m. carlson34caab02018-05-02 00:25:42 +0000452 int die_on_error = is_null_oid(me->gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700453 if (!me->overwrite &&
Stefan Beller147875f2015-10-12 10:58:58 -0700454 submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
Stefan Beller73c293b2016-11-22 12:14:37 -0800455 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700456 "fetchrecursesubmodules");
Stefan Beller147875f2015-10-12 10:58:58 -0700457 else
458 submodule->fetch_recurse = parse_fetch_recurse(
459 var, value,
Heiko Voigt027771f2015-08-17 17:22:00 -0700460 die_on_error);
Heiko Voigt959b5452015-08-17 17:21:57 -0700461 } else if (!strcmp(item.buf, "ignore")) {
Stefan Beller147875f2015-10-12 10:58:58 -0700462 if (!value)
463 ret = config_error_nonbool(var);
Stefan Bellerf73da112016-02-29 18:07:12 -0800464 else if (!me->overwrite && submodule->ignore)
Stefan Beller73c293b2016-11-22 12:14:37 -0800465 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700466 "ignore");
Stefan Beller147875f2015-10-12 10:58:58 -0700467 else if (strcmp(value, "untracked") &&
468 strcmp(value, "dirty") &&
469 strcmp(value, "all") &&
470 strcmp(value, "none"))
Heiko Voigt959b5452015-08-17 17:21:57 -0700471 warning("Invalid parameter '%s' for config option "
Stefan Beller5ea30482017-03-14 15:14:40 -0700472 "'submodule.%s.ignore'", value, name.buf);
Stefan Beller147875f2015-10-12 10:58:58 -0700473 else {
474 free((void *) submodule->ignore);
475 submodule->ignore = xstrdup(value);
Heiko Voigt959b5452015-08-17 17:21:57 -0700476 }
Heiko Voigt959b5452015-08-17 17:21:57 -0700477 } else if (!strcmp(item.buf, "url")) {
Heiko Voigt959b5452015-08-17 17:21:57 -0700478 if (!value) {
479 ret = config_error_nonbool(var);
Jeff Kingf6adec42018-09-24 04:36:30 -0400480 } else if (looks_like_command_line_option(value)) {
481 warn_command_line_option(var, value);
Stefan Bellerf73da112016-02-29 18:07:12 -0800482 } else if (!me->overwrite && submodule->url) {
Stefan Beller73c293b2016-11-22 12:14:37 -0800483 warn_multiple_config(me->treeish_name, submodule->name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700484 "url");
Stefan Beller147875f2015-10-12 10:58:58 -0700485 } else {
486 free((void *) submodule->url);
487 submodule->url = xstrdup(value);
Heiko Voigt959b5452015-08-17 17:21:57 -0700488 }
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800489 } else if (!strcmp(item.buf, "update")) {
490 if (!value)
491 ret = config_error_nonbool(var);
492 else if (!me->overwrite &&
493 submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
Stefan Beller73c293b2016-11-22 12:14:37 -0800494 warn_multiple_config(me->treeish_name, submodule->name,
Stefan Bellerea2fa5a2016-02-29 18:07:11 -0800495 "update");
496 else if (parse_submodule_update_strategy(value,
Jonathan Niedere904deb2019-12-05 01:28:28 -0800497 &submodule->update_strategy) < 0 ||
498 submodule->update_strategy.type == SM_UPDATE_COMMAND)
499 die(_("invalid value for %s"), var);
Stefan Beller37f52e92016-05-26 14:59:42 -0700500 } else if (!strcmp(item.buf, "shallow")) {
501 if (!me->overwrite && submodule->recommend_shallow != -1)
Stefan Beller73c293b2016-11-22 12:14:37 -0800502 warn_multiple_config(me->treeish_name, submodule->name,
Stefan Beller37f52e92016-05-26 14:59:42 -0700503 "shallow");
Stefan Bellerb5944f32016-07-28 17:44:07 -0700504 else
Stefan Beller37f52e92016-05-26 14:59:42 -0700505 submodule->recommend_shallow =
506 git_config_bool(var, value);
Stefan Bellerb5944f32016-07-28 17:44:07 -0700507 } else if (!strcmp(item.buf, "branch")) {
508 if (!me->overwrite && submodule->branch)
Stefan Beller73c293b2016-11-22 12:14:37 -0800509 warn_multiple_config(me->treeish_name, submodule->name,
Stefan Bellerb5944f32016-07-28 17:44:07 -0700510 "branch");
511 else {
512 free((void *)submodule->branch);
513 submodule->branch = xstrdup(value);
Stefan Beller37f52e92016-05-26 14:59:42 -0700514 }
Heiko Voigt959b5452015-08-17 17:21:57 -0700515 }
516
Heiko Voigt959b5452015-08-17 17:21:57 -0700517 strbuf_release(&name);
518 strbuf_release(&item);
519
520 return ret;
521}
522
Brandon Williams1b796ac2017-08-03 11:19:57 -0700523static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
524 struct object_id *gitmodules_oid,
525 struct strbuf *rev)
Heiko Voigt959b5452015-08-17 17:21:57 -0700526{
Heiko Voigt959b5452015-08-17 17:21:57 -0700527 int ret = 0;
528
brian m. carlsoncd73de42017-07-13 23:49:20 +0000529 if (is_null_oid(treeish_name)) {
530 oidclr(gitmodules_oid);
Heiko Voigt959b5452015-08-17 17:21:57 -0700531 return 1;
532 }
533
brian m. carlsoncd73de42017-07-13 23:49:20 +0000534 strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
535 if (get_oid(rev->buf, gitmodules_oid) >= 0)
Heiko Voigt959b5452015-08-17 17:21:57 -0700536 ret = 1;
537
Heiko Voigt959b5452015-08-17 17:21:57 -0700538 return ret;
539}
540
541/* This does a lookup of a submodule configuration by name or by path
542 * (key) with on-demand reading of the appropriate .gitmodules from
543 * revisions.
544 */
545static const struct submodule *config_from(struct submodule_cache *cache,
brian m. carlsoncd73de42017-07-13 23:49:20 +0000546 const struct object_id *treeish_name, const char *key,
Heiko Voigt959b5452015-08-17 17:21:57 -0700547 enum lookup_type lookup_type)
548{
549 struct strbuf rev = STRBUF_INIT;
550 unsigned long config_size;
Heiko Voigt0918e252016-07-28 14:49:47 +0200551 char *config = NULL;
brian m. carlsoncd73de42017-07-13 23:49:20 +0000552 struct object_id oid;
Heiko Voigt959b5452015-08-17 17:21:57 -0700553 enum object_type type;
554 const struct submodule *submodule = NULL;
555 struct parse_config_parameter parameter;
556
557 /*
558 * If any parameter except the cache is a NULL pointer just
559 * return the first submodule. Can be used to check whether
560 * there are any submodules parsed.
561 */
Stefan Beller73c293b2016-11-22 12:14:37 -0800562 if (!treeish_name || !key) {
Heiko Voigt959b5452015-08-17 17:21:57 -0700563 struct hashmap_iter iter;
564 struct submodule_entry *entry;
565
Eric Wong87571c32019-10-06 23:30:38 +0000566 entry = hashmap_iter_first_entry(&cache->for_name, &iter,
567 struct submodule_entry,
568 ent /* member name */);
Heiko Voigt959b5452015-08-17 17:21:57 -0700569 if (!entry)
570 return NULL;
571 return entry->config;
572 }
573
brian m. carlsoncd73de42017-07-13 23:49:20 +0000574 if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
Heiko Voigt0918e252016-07-28 14:49:47 +0200575 goto out;
Heiko Voigt959b5452015-08-17 17:21:57 -0700576
577 switch (lookup_type) {
578 case lookup_name:
brian m. carlson34caab02018-05-02 00:25:42 +0000579 submodule = cache_lookup_name(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700580 break;
581 case lookup_path:
brian m. carlson34caab02018-05-02 00:25:42 +0000582 submodule = cache_lookup_path(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700583 break;
584 }
585 if (submodule)
Heiko Voigt0918e252016-07-28 14:49:47 +0200586 goto out;
Heiko Voigt959b5452015-08-17 17:21:57 -0700587
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000588 config = read_object_file(&oid, &type, &config_size);
Heiko Voigt0918e252016-07-28 14:49:47 +0200589 if (!config || type != OBJ_BLOB)
590 goto out;
Heiko Voigt959b5452015-08-17 17:21:57 -0700591
592 /* fill the submodule config into the cache */
593 parameter.cache = cache;
brian m. carlson34caab02018-05-02 00:25:42 +0000594 parameter.treeish_name = treeish_name;
595 parameter.gitmodules_oid = &oid;
Heiko Voigt959b5452015-08-17 17:21:57 -0700596 parameter.overwrite = 0;
Vasco Almeida1b8132d2016-07-28 13:14:03 +0000597 git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
Jeff King4574f1a2018-06-28 18:05:24 -0400598 config, config_size, &parameter, NULL);
Heiko Voigt514dea92016-07-28 14:49:11 +0200599 strbuf_release(&rev);
Heiko Voigt959b5452015-08-17 17:21:57 -0700600 free(config);
601
602 switch (lookup_type) {
603 case lookup_name:
brian m. carlson34caab02018-05-02 00:25:42 +0000604 return cache_lookup_name(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700605 case lookup_path:
brian m. carlson34caab02018-05-02 00:25:42 +0000606 return cache_lookup_path(cache, &oid, key);
Heiko Voigt959b5452015-08-17 17:21:57 -0700607 default:
608 return NULL;
609 }
Heiko Voigt0918e252016-07-28 14:49:47 +0200610
611out:
612 strbuf_release(&rev);
613 free(config);
614 return submodule;
Heiko Voigt959b5452015-08-17 17:21:57 -0700615}
616
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700617static void submodule_cache_check_init(struct repository *repo)
Heiko Voigt959b5452015-08-17 17:21:57 -0700618{
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700619 if (repo->submodule_cache && repo->submodule_cache->initialized)
Heiko Voigt959b5452015-08-17 17:21:57 -0700620 return;
621
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700622 if (!repo->submodule_cache)
623 repo->submodule_cache = submodule_cache_alloc();
624
625 submodule_cache_init(repo->submodule_cache);
Heiko Voigt959b5452015-08-17 17:21:57 -0700626}
627
Antonio Ospitedb64d112018-06-26 12:47:10 +0200628/*
629 * Note: This function is private for a reason, the '.gitmodules' file should
ryenus571fb962019-12-15 15:12:24 +0000630 * not be used as a mechanism to retrieve arbitrary configuration stored in
Antonio Ospitedb64d112018-06-26 12:47:10 +0200631 * the repository.
632 *
633 * Runs the provided config function on the '.gitmodules' file found in the
634 * working directory.
635 */
636static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
637{
638 if (repo->worktree) {
Matthew Rogers9a83d082020-02-10 00:30:58 +0000639 struct git_config_source config_source = {
640 0, .scope = CONFIG_SCOPE_SUBMODULE
641 };
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200642 const struct config_options opts = { 0 };
643 struct object_id oid;
644 char *file;
Nguyễn Thái Ngọc Duyd9b8b8f2019-04-16 16:33:38 +0700645 char *oidstr = NULL;
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200646
647 file = repo_worktree_path(repo, GITMODULES_FILE);
648 if (file_exists(file)) {
649 config_source.file = file;
Nguyễn Thái Ngọc Duyd9b8b8f2019-04-16 16:33:38 +0700650 } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
651 repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
652 config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
653 if (repo != the_repository)
654 add_to_alternates_memory(repo->objects->odb->path);
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200655 } else {
656 goto out;
657 }
658
659 config_with_options(fn, data, &config_source, &opts);
660
661out:
Nguyễn Thái Ngọc Duyd9b8b8f2019-04-16 16:33:38 +0700662 free(oidstr);
Antonio Ospitedb64d112018-06-26 12:47:10 +0200663 free(file);
664 }
665}
666
Brandon Williams1b796ac2017-08-03 11:19:57 -0700667static int gitmodules_cb(const char *var, const char *value, void *data)
Heiko Voigt851e18c2015-08-17 17:21:59 -0700668{
Brandon Williams1b796ac2017-08-03 11:19:57 -0700669 struct repository *repo = data;
Heiko Voigt851e18c2015-08-17 17:21:59 -0700670 struct parse_config_parameter parameter;
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700671
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700672 parameter.cache = repo->submodule_cache;
Stefan Beller73c293b2016-11-22 12:14:37 -0800673 parameter.treeish_name = NULL;
brian m. carlson14228442021-04-26 01:02:56 +0000674 parameter.gitmodules_oid = null_oid();
Heiko Voigt851e18c2015-08-17 17:21:59 -0700675 parameter.overwrite = 1;
676
Heiko Voigt851e18c2015-08-17 17:21:59 -0700677 return parse_config(var, value, &parameter);
678}
679
Matheus Tavaresd7992422020-01-15 23:39:55 -0300680void repo_read_gitmodules(struct repository *repo, int skip_if_read)
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700681{
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700682 submodule_cache_check_init(repo);
683
Matheus Tavaresd7992422020-01-15 23:39:55 -0300684 if (repo->submodule_cache->gitmodules_read && skip_if_read)
685 return;
686
Antonio Ospitedb64d112018-06-26 12:47:10 +0200687 if (repo_read_index(repo) < 0)
688 return;
Brandon Williams1b796ac2017-08-03 11:19:57 -0700689
Antonio Ospitedb64d112018-06-26 12:47:10 +0200690 if (!is_gitmodules_unmerged(repo->index))
691 config_from_gitmodules(gitmodules_cb, repo, repo);
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700692
693 repo->submodule_cache->gitmodules_read = 1;
Brandon Williams1b796ac2017-08-03 11:19:57 -0700694}
695
696void gitmodules_config_oid(const struct object_id *commit_oid)
697{
698 struct strbuf rev = STRBUF_INIT;
699 struct object_id oid;
700
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700701 submodule_cache_check_init(the_repository);
702
Brandon Williams1b796ac2017-08-03 11:19:57 -0700703 if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
704 git_config_from_blob_oid(gitmodules_cb, rev.buf,
705 &oid, the_repository);
706 }
707 strbuf_release(&rev);
Brandon Williamsff6f1f52017-08-03 11:19:58 -0700708
709 the_repository->submodule_cache->gitmodules_read = 1;
710}
711
Stefan Beller3b8fb392018-03-28 15:35:29 -0700712const struct submodule *submodule_from_name(struct repository *r,
713 const struct object_id *treeish_name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700714 const char *name)
715{
Matheus Tavaresd7992422020-01-15 23:39:55 -0300716 repo_read_gitmodules(r, 1);
Stefan Beller3b8fb392018-03-28 15:35:29 -0700717 return config_from(r->submodule_cache, treeish_name, name, lookup_name);
Heiko Voigt959b5452015-08-17 17:21:57 -0700718}
719
Stefan Beller3b8fb392018-03-28 15:35:29 -0700720const struct submodule *submodule_from_path(struct repository *r,
721 const struct object_id *treeish_name,
Heiko Voigt959b5452015-08-17 17:21:57 -0700722 const char *path)
723{
Matheus Tavaresd7992422020-01-15 23:39:55 -0300724 repo_read_gitmodules(r, 1);
Stefan Beller3b8fb392018-03-28 15:35:29 -0700725 return config_from(r->submodule_cache, treeish_name, path, lookup_path);
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700726}
727
Stefan Bellerf793b892018-03-28 15:35:28 -0700728void submodule_free(struct repository *r)
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700729{
Stefan Bellerf793b892018-03-28 15:35:28 -0700730 if (r->submodule_cache)
731 submodule_cache_clear(r->submodule_cache);
Heiko Voigt959b5452015-08-17 17:21:57 -0700732}
Antonio Ospitead136372018-06-26 12:47:05 +0200733
Antonio Ospitebcbc7802018-10-05 15:05:52 +0200734static int config_print_callback(const char *var, const char *value, void *cb_data)
735{
736 char *wanted_key = cb_data;
737
738 if (!strcmp(wanted_key, var))
739 printf("%s\n", value);
740
741 return 0;
742}
743
744int print_config_from_gitmodules(struct repository *repo, const char *key)
745{
746 int ret;
747 char *store_key;
748
749 ret = git_config_parse_key(key, &store_key, NULL);
750 if (ret < 0)
751 return CONFIG_INVALID_KEY;
752
753 config_from_gitmodules(config_print_callback, repo, store_key);
754
755 free(store_key);
756 return 0;
757}
758
Antonio Ospite45f5ef32018-10-05 15:05:53 +0200759int config_set_in_gitmodules_file_gently(const char *key, const char *value)
760{
761 int ret;
762
763 ret = git_config_set_in_file_gently(GITMODULES_FILE, key, value);
764 if (ret < 0)
765 /* Maybe the user already did that, don't error out here */
766 warning(_("Could not update .gitmodules entry %s"), key);
767
768 return ret;
769}
770
Antonio Ospite71a69532018-06-26 12:47:06 +0200771struct fetch_config {
772 int *max_children;
773 int *recurse_submodules;
774};
775
776static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
777{
778 struct fetch_config *config = cb;
779 if (!strcmp(var, "submodule.fetchjobs")) {
Jonathan Tane5b94212020-08-17 21:01:33 -0700780 if (config->max_children)
781 *(config->max_children) =
782 parse_submodule_fetchjobs(var, value);
Antonio Ospite71a69532018-06-26 12:47:06 +0200783 return 0;
784 } else if (!strcmp(var, "fetch.recursesubmodules")) {
Jonathan Tane5b94212020-08-17 21:01:33 -0700785 if (config->recurse_submodules)
786 *(config->recurse_submodules) =
787 parse_fetch_recurse_submodules_arg(var, value);
Antonio Ospite71a69532018-06-26 12:47:06 +0200788 return 0;
789 }
790
791 return 0;
792}
793
794void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
795{
796 struct fetch_config config = {
797 .max_children = max_children,
798 .recurse_submodules = recurse_submodules
799 };
Antonio Ospite9a0fb3e2018-06-26 12:47:09 +0200800 config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
Antonio Ospite71a69532018-06-26 12:47:06 +0200801}
Antonio Ospite05744992018-06-26 12:47:07 +0200802
803static int gitmodules_update_clone_config(const char *var, const char *value,
804 void *cb)
805{
806 int *max_jobs = cb;
807 if (!strcmp(var, "submodule.fetchjobs"))
808 *max_jobs = parse_submodule_fetchjobs(var, value);
809 return 0;
810}
811
812void update_clone_config_from_gitmodules(int *max_jobs)
813{
Antonio Ospite9a0fb3e2018-06-26 12:47:09 +0200814 config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
Antonio Ospite05744992018-06-26 12:47:07 +0200815}