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