Coverage Report

Created: 2026-02-14 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/submodule-config.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
#define DISABLE_SIGN_COMPARE_WARNINGS
3
4
#include "git-compat-util.h"
5
#include "dir.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "hex.h"
9
#include "path.h"
10
#include "repository.h"
11
#include "config.h"
12
#include "submodule-config.h"
13
#include "submodule.h"
14
#include "strbuf.h"
15
#include "object-name.h"
16
#include "odb.h"
17
#include "parse-options.h"
18
#include "thread-utils.h"
19
#include "tree-walk.h"
20
#include "url.h"
21
#include "urlmatch.h"
22
23
/*
24
 * submodule cache lookup structure
25
 * There is one shared set of 'struct submodule' entries which can be
26
 * looked up by their sha1 blob id of the .gitmodules file and either
27
 * using path or name as key.
28
 * for_path stores submodule entries with path as key
29
 * for_name stores submodule entries with name as key
30
 */
31
struct submodule_cache {
32
  struct hashmap for_path;
33
  struct hashmap for_name;
34
  unsigned initialized:1;
35
  unsigned gitmodules_read:1;
36
};
37
38
/*
39
 * thin wrapper struct needed to insert 'struct submodule' entries to
40
 * the hashmap
41
 */
42
struct submodule_entry {
43
  struct hashmap_entry ent;
44
  struct submodule *config;
45
};
46
47
enum lookup_type {
48
  lookup_name,
49
  lookup_path
50
};
51
52
static int config_path_cmp(const void *cmp_data UNUSED,
53
         const struct hashmap_entry *eptr,
54
         const struct hashmap_entry *entry_or_key,
55
         const void *keydata UNUSED)
56
0
{
57
0
  const struct submodule_entry *a, *b;
58
59
0
  a = container_of(eptr, const struct submodule_entry, ent);
60
0
  b = container_of(entry_or_key, const struct submodule_entry, ent);
61
62
0
  return strcmp(a->config->path, b->config->path) ||
63
0
         !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
64
0
}
65
66
static int config_name_cmp(const void *cmp_data UNUSED,
67
         const struct hashmap_entry *eptr,
68
         const struct hashmap_entry *entry_or_key,
69
         const void *keydata UNUSED)
70
0
{
71
0
  const struct submodule_entry *a, *b;
72
73
0
  a = container_of(eptr, const struct submodule_entry, ent);
74
0
  b = container_of(entry_or_key, const struct submodule_entry, ent);
75
76
0
  return strcmp(a->config->name, b->config->name) ||
77
0
         !oideq(&a->config->gitmodules_oid, &b->config->gitmodules_oid);
78
0
}
79
80
static struct submodule_cache *submodule_cache_alloc(void)
81
0
{
82
0
  return xcalloc(1, sizeof(struct submodule_cache));
83
0
}
84
85
static void submodule_cache_init(struct submodule_cache *cache)
86
0
{
87
0
  hashmap_init(&cache->for_path, config_path_cmp, NULL, 0);
88
0
  hashmap_init(&cache->for_name, config_name_cmp, NULL, 0);
89
0
  cache->initialized = 1;
90
0
}
91
92
static void free_one_config(struct submodule_entry *entry)
93
0
{
94
0
  free((void *) entry->config->path);
95
0
  free((void *) entry->config->name);
96
0
  free((void *) entry->config->branch);
97
0
  free((void *) entry->config->url);
98
0
  free((void *) entry->config->ignore);
99
0
  submodule_update_strategy_release(&entry->config->update_strategy);
100
0
  free(entry->config);
101
0
}
102
103
static void submodule_cache_clear(struct submodule_cache *cache)
104
0
{
105
0
  struct hashmap_iter iter;
106
0
  struct submodule_entry *entry;
107
108
0
  if (!cache->initialized)
109
0
    return;
110
111
  /*
112
   * We iterate over the name hash here to be symmetric with the
113
   * allocation of struct submodule entries. Each is allocated by
114
   * their .gitmodules blob sha1 and submodule name.
115
   */
116
0
  hashmap_for_each_entry(&cache->for_name, &iter, entry,
117
0
        ent /* member name */)
118
0
    free_one_config(entry);
119
120
0
  hashmap_clear_and_free(&cache->for_path, struct submodule_entry, ent);
121
0
  hashmap_clear_and_free(&cache->for_name, struct submodule_entry, ent);
122
0
  cache->initialized = 0;
123
0
  cache->gitmodules_read = 0;
124
0
}
125
126
void submodule_cache_free(struct submodule_cache *cache)
127
0
{
128
0
  submodule_cache_clear(cache);
129
0
  free(cache);
130
0
}
131
132
static unsigned int hash_oid_string(const struct object_id *oid,
133
            const char *string)
134
0
{
135
0
  return memhash(oid->hash, the_hash_algo->rawsz) + strhash(string);
136
0
}
137
138
static void cache_put_path(struct submodule_cache *cache,
139
         struct submodule *submodule)
140
0
{
141
0
  unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
142
0
              submodule->path);
143
0
  struct submodule_entry *e = xmalloc(sizeof(*e));
144
0
  hashmap_entry_init(&e->ent, hash);
145
0
  e->config = submodule;
146
0
  hashmap_put(&cache->for_path, &e->ent);
147
0
}
148
149
static void cache_remove_path(struct submodule_cache *cache,
150
            struct submodule *submodule)
151
0
{
152
0
  unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
153
0
              submodule->path);
154
0
  struct submodule_entry e;
155
0
  struct submodule_entry *removed;
156
0
  hashmap_entry_init(&e.ent, hash);
157
0
  e.config = submodule;
158
0
  removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
159
0
  free(removed);
160
0
}
161
162
static void cache_add(struct submodule_cache *cache,
163
          struct submodule *submodule)
164
0
{
165
0
  unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
166
0
              submodule->name);
167
0
  struct submodule_entry *e = xmalloc(sizeof(*e));
168
0
  hashmap_entry_init(&e->ent, hash);
169
0
  e->config = submodule;
170
0
  hashmap_add(&cache->for_name, &e->ent);
171
0
}
172
173
static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
174
    const struct object_id *gitmodules_oid, const char *path)
175
0
{
176
0
  struct submodule_entry *entry;
177
0
  unsigned int hash = hash_oid_string(gitmodules_oid, path);
178
0
  struct submodule_entry key;
179
0
  struct submodule key_config;
180
181
0
  oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
182
0
  key_config.path = path;
183
184
0
  hashmap_entry_init(&key.ent, hash);
185
0
  key.config = &key_config;
186
187
0
  entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
188
0
  if (entry)
189
0
    return entry->config;
190
0
  return NULL;
191
0
}
192
193
static struct submodule *cache_lookup_name(struct submodule_cache *cache,
194
    const struct object_id *gitmodules_oid, const char *name)
195
0
{
196
0
  struct submodule_entry *entry;
197
0
  unsigned int hash = hash_oid_string(gitmodules_oid, name);
198
0
  struct submodule_entry key;
199
0
  struct submodule key_config;
200
201
0
  oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
202
0
  key_config.name = name;
203
204
0
  hashmap_entry_init(&key.ent, hash);
205
0
  key.config = &key_config;
206
207
0
  entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
208
0
  if (entry)
209
0
    return entry->config;
210
0
  return NULL;
211
0
}
212
213
int check_submodule_name(const char *name)
214
0
{
215
  /* Disallow empty names */
216
0
  if (!*name)
217
0
    return -1;
218
219
  /*
220
   * Look for '..' as a path component. Check is_xplatform_dir_sep() as
221
   * separators rather than is_dir_sep(), because we want the name rules
222
   * to be consistent across platforms.
223
   */
224
0
  goto in_component; /* always start inside component */
225
0
  while (*name) {
226
0
    char c = *name++;
227
0
    if (is_xplatform_dir_sep(c)) {
228
0
in_component:
229
0
      if (name[0] == '.' && name[1] == '.' &&
230
0
          (!name[2] || is_xplatform_dir_sep(name[2])))
231
0
        return -1;
232
0
    }
233
0
  }
234
235
0
  return 0;
236
0
}
237
238
static int submodule_url_is_relative(const char *url)
239
0
{
240
0
  return starts_with_dot_slash(url) || starts_with_dot_dot_slash(url);
241
0
}
242
243
/*
244
 * Count directory components that a relative submodule URL should chop
245
 * from the remote_url it is to be resolved against.
246
 *
247
 * In other words, this counts "../" components at the start of a
248
 * submodule URL.
249
 *
250
 * Returns the number of directory components to chop and writes a
251
 * pointer to the next character of url after all leading "./" and
252
 * "../" components to out.
253
 */
254
static int count_leading_dotdots(const char *url, const char **out)
255
0
{
256
0
  int result = 0;
257
0
  while (1) {
258
0
    if (starts_with_dot_dot_slash(url)) {
259
0
      result++;
260
0
      url += strlen("../");
261
0
      continue;
262
0
    }
263
0
    if (starts_with_dot_slash(url)) {
264
0
      url += strlen("./");
265
0
      continue;
266
0
    }
267
0
    *out = url;
268
0
    return result;
269
0
  }
270
0
}
271
/*
272
 * Check whether a transport is implemented by git-remote-curl.
273
 *
274
 * If it is, returns 1 and writes the URL that would be passed to
275
 * git-remote-curl to the "out" parameter.
276
 *
277
 * Otherwise, returns 0 and leaves "out" untouched.
278
 *
279
 * Examples:
280
 *   http::https://example.com/repo.git -> 1, https://example.com/repo.git
281
 *   https://example.com/repo.git -> 1, https://example.com/repo.git
282
 *   git://example.com/repo.git -> 0
283
 *
284
 * This is for use in checking for previously exploitable bugs that
285
 * required a submodule URL to be passed to git-remote-curl.
286
 */
287
static int url_to_curl_url(const char *url, const char **out)
288
0
{
289
  /*
290
   * We don't need to check for case-aliases, "http.exe", and so
291
   * on because in the default configuration, is_transport_allowed
292
   * prevents URLs with those schemes from being cloned
293
   * automatically.
294
   */
295
0
  if (skip_prefix(url, "http::", out) ||
296
0
      skip_prefix(url, "https::", out) ||
297
0
      skip_prefix(url, "ftp::", out) ||
298
0
      skip_prefix(url, "ftps::", out))
299
0
    return 1;
300
0
  if (starts_with(url, "http://") ||
301
0
      starts_with(url, "https://") ||
302
0
      starts_with(url, "ftp://") ||
303
0
      starts_with(url, "ftps://")) {
304
0
    *out = url;
305
0
    return 1;
306
0
  }
307
0
  return 0;
308
0
}
309
310
int check_submodule_url(const char *url)
311
0
{
312
0
  const char *curl_url;
313
314
0
  if (looks_like_command_line_option(url))
315
0
    return -1;
316
317
0
  if (submodule_url_is_relative(url) || starts_with(url, "git://")) {
318
0
    char *decoded;
319
0
    const char *next;
320
0
    int has_nl;
321
322
    /*
323
     * This could be appended to an http URL and url-decoded;
324
     * check for malicious characters.
325
     */
326
0
    decoded = url_decode(url);
327
0
    has_nl = !!strchr(decoded, '\n');
328
329
0
    free(decoded);
330
0
    if (has_nl)
331
0
      return -1;
332
333
    /*
334
     * URLs which escape their root via "../" can overwrite
335
     * the host field and previous components, resolving to
336
     * URLs like https::example.com/submodule.git and
337
     * https:///example.com/submodule.git that were
338
     * susceptible to CVE-2020-11008.
339
     */
340
0
    if (count_leading_dotdots(url, &next) > 0 &&
341
0
        (*next == ':' || *next == '/'))
342
0
      return -1;
343
0
  }
344
345
0
  else if (url_to_curl_url(url, &curl_url)) {
346
0
    int ret = 0;
347
0
    char *normalized = url_normalize(curl_url, NULL);
348
0
    if (normalized) {
349
0
      char *decoded = url_decode(normalized);
350
0
      if (strchr(decoded, '\n'))
351
0
        ret = -1;
352
0
      free(normalized);
353
0
      free(decoded);
354
0
    } else {
355
0
      ret = -1;
356
0
    }
357
358
0
    return ret;
359
0
  }
360
361
0
  return 0;
362
0
}
363
364
static int name_and_item_from_var(const char *var, struct strbuf *name,
365
          struct strbuf *item)
366
0
{
367
0
  const char *subsection, *key;
368
0
  size_t subsection_len;
369
0
  int parse;
370
0
  parse = parse_config_key(var, "submodule", &subsection,
371
0
      &subsection_len, &key);
372
0
  if (parse < 0 || !subsection)
373
0
    return 0;
374
375
0
  strbuf_add(name, subsection, subsection_len);
376
0
  if (check_submodule_name(name->buf) < 0) {
377
0
    warning(_("ignoring suspicious submodule name: %s"), name->buf);
378
0
    strbuf_release(name);
379
0
    return 0;
380
0
  }
381
382
0
  strbuf_addstr(item, key);
383
384
0
  return 1;
385
0
}
386
387
static struct submodule *lookup_or_create_by_name(struct submodule_cache *cache,
388
    const struct object_id *gitmodules_oid, const char *name)
389
0
{
390
0
  struct submodule *submodule;
391
0
  struct strbuf name_buf = STRBUF_INIT;
392
393
0
  submodule = cache_lookup_name(cache, gitmodules_oid, name);
394
0
  if (submodule)
395
0
    return submodule;
396
397
0
  submodule = xmalloc(sizeof(*submodule));
398
399
0
  strbuf_addstr(&name_buf, name);
400
0
  submodule->name = strbuf_detach(&name_buf, NULL);
401
402
0
  submodule->path = NULL;
403
0
  submodule->url = NULL;
404
0
  submodule->update_strategy.type = SM_UPDATE_UNSPECIFIED;
405
0
  submodule->update_strategy.command = NULL;
406
0
  submodule->fetch_recurse = RECURSE_SUBMODULES_NONE;
407
0
  submodule->ignore = NULL;
408
0
  submodule->branch = NULL;
409
0
  submodule->recommend_shallow = -1;
410
411
0
  oidcpy(&submodule->gitmodules_oid, gitmodules_oid);
412
413
0
  cache_add(cache, submodule);
414
415
0
  return submodule;
416
0
}
417
418
static int parse_fetch_recurse(const char *opt, const char *arg,
419
             int die_on_error)
420
0
{
421
0
  switch (git_parse_maybe_bool(arg)) {
422
0
  case 1:
423
0
    return RECURSE_SUBMODULES_ON;
424
0
  case 0:
425
0
    return RECURSE_SUBMODULES_OFF;
426
0
  default:
427
0
    if (!strcmp(arg, "on-demand"))
428
0
      return RECURSE_SUBMODULES_ON_DEMAND;
429
    /*
430
     * Please update $__git_fetch_recurse_submodules in
431
     * git-completion.bash when you add new options.
432
     */
433
0
    if (die_on_error)
434
0
      die("bad %s argument: %s", opt, arg);
435
0
    else
436
0
      return RECURSE_SUBMODULES_ERROR;
437
0
  }
438
0
}
439
440
int parse_submodule_fetchjobs(const char *var, const char *value,
441
            const struct key_value_info *kvi)
442
0
{
443
0
  int fetchjobs = git_config_int(var, value, kvi);
444
0
  if (fetchjobs < 0)
445
0
    die(_("negative values not allowed for submodule.fetchJobs"));
446
0
  if (!fetchjobs)
447
0
    fetchjobs = online_cpus();
448
0
  return fetchjobs;
449
0
}
450
451
int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
452
0
{
453
0
  return parse_fetch_recurse(opt, arg, 1);
454
0
}
455
456
int option_fetch_parse_recurse_submodules(const struct option *opt,
457
            const char *arg, int unset)
458
0
{
459
0
  int *v;
460
461
0
  if (!opt->value)
462
0
    return -1;
463
464
0
  v = opt->value;
465
466
0
  if (unset) {
467
0
    *v = RECURSE_SUBMODULES_OFF;
468
0
  } else {
469
0
    if (arg)
470
0
      *v = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
471
0
    else
472
0
      *v = RECURSE_SUBMODULES_ON;
473
0
  }
474
0
  return 0;
475
0
}
476
477
static int parse_update_recurse(const char *opt, const char *arg,
478
        int die_on_error)
479
0
{
480
0
  switch (git_parse_maybe_bool(arg)) {
481
0
  case 1:
482
0
    return RECURSE_SUBMODULES_ON;
483
0
  case 0:
484
0
    return RECURSE_SUBMODULES_OFF;
485
0
  default:
486
0
    if (die_on_error)
487
0
      die("bad %s argument: %s", opt, arg);
488
0
    return RECURSE_SUBMODULES_ERROR;
489
0
  }
490
0
}
491
492
int parse_update_recurse_submodules_arg(const char *opt, const char *arg)
493
0
{
494
0
  return parse_update_recurse(opt, arg, 1);
495
0
}
496
497
static int parse_push_recurse(const char *opt, const char *arg,
498
             int die_on_error)
499
0
{
500
0
  switch (git_parse_maybe_bool(arg)) {
501
0
  case 1:
502
    /* There's no simple "on" value when pushing */
503
0
    if (die_on_error)
504
0
      die("bad %s argument: %s", opt, arg);
505
0
    else
506
0
      return RECURSE_SUBMODULES_ERROR;
507
0
  case 0:
508
0
    return RECURSE_SUBMODULES_OFF;
509
0
  default:
510
0
    if (!strcmp(arg, "on-demand"))
511
0
      return RECURSE_SUBMODULES_ON_DEMAND;
512
0
    else if (!strcmp(arg, "check"))
513
0
      return RECURSE_SUBMODULES_CHECK;
514
0
    else if (!strcmp(arg, "only"))
515
0
      return RECURSE_SUBMODULES_ONLY;
516
    /*
517
     * Please update $__git_push_recurse_submodules in
518
     * git-completion.bash when you add new modes.
519
     */
520
0
    else if (die_on_error)
521
0
      die("bad %s argument: %s", opt, arg);
522
0
    else
523
0
      return RECURSE_SUBMODULES_ERROR;
524
0
  }
525
0
}
526
527
int parse_push_recurse_submodules_arg(const char *opt, const char *arg)
528
0
{
529
0
  return parse_push_recurse(opt, arg, 1);
530
0
}
531
532
static void warn_multiple_config(const struct object_id *treeish_name,
533
         const char *name, const char *option)
534
0
{
535
0
  const char *commit_string = "WORKTREE";
536
0
  if (treeish_name)
537
0
    commit_string = oid_to_hex(treeish_name);
538
0
  warning("%s:.gitmodules, multiple configurations found for "
539
0
      "'submodule.%s.%s'. Skipping second one!",
540
0
      commit_string, name, option);
541
0
}
542
543
static void warn_command_line_option(const char *var, const char *value)
544
0
{
545
0
  warning(_("ignoring '%s' which may be interpreted as"
546
0
      " a command-line option: %s"), var, value);
547
0
}
548
549
struct parse_config_parameter {
550
  struct submodule_cache *cache;
551
  const struct object_id *treeish_name;
552
  const struct object_id *gitmodules_oid;
553
  int overwrite;
554
};
555
556
/*
557
 * Parse a config item from .gitmodules.
558
 *
559
 * This does not handle submodule-related configuration from the main
560
 * config store (.git/config, etc).  Callers are responsible for
561
 * checking for overrides in the main config store when appropriate.
562
 */
563
static int parse_config(const char *var, const char *value,
564
      const struct config_context *ctx UNUSED, void *data)
565
0
{
566
0
  struct parse_config_parameter *me = data;
567
0
  struct submodule *submodule;
568
0
  struct strbuf name = STRBUF_INIT, item = STRBUF_INIT;
569
0
  int ret = 0;
570
571
  /* this also ensures that we only parse submodule entries */
572
0
  if (!name_and_item_from_var(var, &name, &item))
573
0
    return 0;
574
575
0
  submodule = lookup_or_create_by_name(me->cache,
576
0
               me->gitmodules_oid,
577
0
               name.buf);
578
579
0
  if (!strcmp(item.buf, "path")) {
580
0
    if (!value)
581
0
      ret = config_error_nonbool(var);
582
0
    else if (looks_like_command_line_option(value))
583
0
      warn_command_line_option(var, value);
584
0
    else if (!me->overwrite && submodule->path)
585
0
      warn_multiple_config(me->treeish_name, submodule->name,
586
0
          "path");
587
0
    else {
588
0
      if (submodule->path)
589
0
        cache_remove_path(me->cache, submodule);
590
0
      free((void *) submodule->path);
591
0
      submodule->path = xstrdup(value);
592
0
      cache_put_path(me->cache, submodule);
593
0
    }
594
0
  } else if (!strcmp(item.buf, "fetchrecursesubmodules")) {
595
    /* when parsing worktree configurations we can die early */
596
0
    int die_on_error = is_null_oid(me->gitmodules_oid);
597
0
    if (!me->overwrite &&
598
0
        submodule->fetch_recurse != RECURSE_SUBMODULES_NONE)
599
0
      warn_multiple_config(me->treeish_name, submodule->name,
600
0
          "fetchrecursesubmodules");
601
0
    else
602
0
      submodule->fetch_recurse = parse_fetch_recurse(
603
0
                var, value,
604
0
                die_on_error);
605
0
  } else if (!strcmp(item.buf, "ignore")) {
606
0
    if (!value)
607
0
      ret = config_error_nonbool(var);
608
0
    else if (!me->overwrite && submodule->ignore)
609
0
      warn_multiple_config(me->treeish_name, submodule->name,
610
0
          "ignore");
611
0
    else if (strcmp(value, "untracked") &&
612
0
       strcmp(value, "dirty") &&
613
0
       strcmp(value, "all") &&
614
0
       strcmp(value, "none"))
615
0
      warning("Invalid parameter '%s' for config option "
616
0
          "'submodule.%s.ignore'", value, name.buf);
617
0
    else {
618
0
      free((void *) submodule->ignore);
619
0
      submodule->ignore = xstrdup(value);
620
0
    }
621
0
  } else if (!strcmp(item.buf, "url")) {
622
0
    if (!value) {
623
0
      ret = config_error_nonbool(var);
624
0
    } else if (looks_like_command_line_option(value)) {
625
0
      warn_command_line_option(var, value);
626
0
    } else if (!me->overwrite && submodule->url) {
627
0
      warn_multiple_config(me->treeish_name, submodule->name,
628
0
          "url");
629
0
    } else {
630
0
      free((void *) submodule->url);
631
0
      submodule->url = xstrdup(value);
632
0
    }
633
0
  } else if (!strcmp(item.buf, "update")) {
634
0
    if (!value)
635
0
      ret = config_error_nonbool(var);
636
0
    else if (!me->overwrite &&
637
0
       submodule->update_strategy.type != SM_UPDATE_UNSPECIFIED)
638
0
      warn_multiple_config(me->treeish_name, submodule->name,
639
0
               "update");
640
0
    else if (parse_submodule_update_strategy(value,
641
0
       &submodule->update_strategy) < 0 ||
642
0
       submodule->update_strategy.type == SM_UPDATE_COMMAND)
643
0
      die(_("invalid value for '%s'"), var);
644
0
  } else if (!strcmp(item.buf, "shallow")) {
645
0
    if (!me->overwrite && submodule->recommend_shallow != -1)
646
0
      warn_multiple_config(me->treeish_name, submodule->name,
647
0
               "shallow");
648
0
    else
649
0
      submodule->recommend_shallow =
650
0
        git_config_bool(var, value);
651
0
  } else if (!strcmp(item.buf, "branch")) {
652
0
    if (!value)
653
0
      ret = config_error_nonbool(var);
654
0
    else if (!me->overwrite && submodule->branch)
655
0
      warn_multiple_config(me->treeish_name, submodule->name,
656
0
               "branch");
657
0
    else {
658
0
      free((void *)submodule->branch);
659
0
      submodule->branch = xstrdup(value);
660
0
    }
661
0
  }
662
663
0
  strbuf_release(&name);
664
0
  strbuf_release(&item);
665
666
0
  return ret;
667
0
}
668
669
static int gitmodule_oid_from_commit(const struct object_id *treeish_name,
670
             struct object_id *gitmodules_oid,
671
             struct strbuf *rev)
672
0
{
673
0
  int ret = 0;
674
675
0
  if (is_null_oid(treeish_name)) {
676
0
    oidclr(gitmodules_oid, the_repository->hash_algo);
677
0
    return 1;
678
0
  }
679
680
0
  strbuf_addf(rev, "%s:.gitmodules", oid_to_hex(treeish_name));
681
0
  if (repo_get_oid(the_repository, rev->buf, gitmodules_oid) >= 0)
682
0
    ret = 1;
683
684
0
  return ret;
685
0
}
686
687
/* This does a lookup of a submodule configuration by name or by path
688
 * (key) with on-demand reading of the appropriate .gitmodules from
689
 * revisions.
690
 */
691
static const struct submodule *config_from(struct submodule_cache *cache,
692
    const struct object_id *treeish_name, const char *key,
693
    enum lookup_type lookup_type)
694
0
{
695
0
  struct strbuf rev = STRBUF_INIT;
696
0
  unsigned long config_size;
697
0
  char *config = NULL;
698
0
  struct object_id oid;
699
0
  enum object_type type;
700
0
  const struct submodule *submodule = NULL;
701
0
  struct parse_config_parameter parameter;
702
703
  /*
704
   * If any parameter except the cache is a NULL pointer just
705
   * return the first submodule. Can be used to check whether
706
   * there are any submodules parsed.
707
   */
708
0
  if (!treeish_name || !key) {
709
0
    struct hashmap_iter iter;
710
0
    struct submodule_entry *entry;
711
712
0
    entry = hashmap_iter_first_entry(&cache->for_name, &iter,
713
0
            struct submodule_entry,
714
0
            ent /* member name */);
715
0
    if (!entry)
716
0
      return NULL;
717
0
    return entry->config;
718
0
  }
719
720
0
  if (!gitmodule_oid_from_commit(treeish_name, &oid, &rev))
721
0
    goto out;
722
723
0
  switch (lookup_type) {
724
0
  case lookup_name:
725
0
    submodule = cache_lookup_name(cache, &oid, key);
726
0
    break;
727
0
  case lookup_path:
728
0
    submodule = cache_lookup_path(cache, &oid, key);
729
0
    break;
730
0
  }
731
0
  if (submodule)
732
0
    goto out;
733
734
0
  config = odb_read_object(the_repository->objects, &oid,
735
0
         &type, &config_size);
736
0
  if (!config || type != OBJ_BLOB)
737
0
    goto out;
738
739
  /* fill the submodule config into the cache */
740
0
  parameter.cache = cache;
741
0
  parameter.treeish_name = treeish_name;
742
0
  parameter.gitmodules_oid = &oid;
743
0
  parameter.overwrite = 0;
744
0
  git_config_from_mem(parse_config, CONFIG_ORIGIN_SUBMODULE_BLOB, rev.buf,
745
0
          config, config_size, &parameter, CONFIG_SCOPE_UNKNOWN, NULL);
746
0
  strbuf_release(&rev);
747
0
  free(config);
748
749
0
  switch (lookup_type) {
750
0
  case lookup_name:
751
0
    return cache_lookup_name(cache, &oid, key);
752
0
  case lookup_path:
753
0
    return cache_lookup_path(cache, &oid, key);
754
0
  default:
755
0
    return NULL;
756
0
  }
757
758
0
out:
759
0
  strbuf_release(&rev);
760
0
  free(config);
761
0
  return submodule;
762
0
}
763
764
static void submodule_cache_check_init(struct repository *repo)
765
0
{
766
0
  if (repo->submodule_cache && repo->submodule_cache->initialized)
767
0
    return;
768
769
0
  if (!repo->submodule_cache)
770
0
    repo->submodule_cache = submodule_cache_alloc();
771
772
0
  submodule_cache_init(repo->submodule_cache);
773
0
}
774
775
/*
776
 * Note: This function is private for a reason, the '.gitmodules' file should
777
 * not be used as a mechanism to retrieve arbitrary configuration stored in
778
 * the repository.
779
 *
780
 * Runs the provided config function on the '.gitmodules' file found in the
781
 * working directory.
782
 */
783
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
784
0
{
785
0
  if (repo->worktree) {
786
0
    struct git_config_source config_source = {
787
0
      0, .scope = CONFIG_SCOPE_SUBMODULE
788
0
    };
789
0
    const struct config_options opts = { 0 };
790
0
    struct object_id oid;
791
0
    char *file;
792
0
    char *oidstr = NULL;
793
794
0
    file = repo_worktree_path(repo, GITMODULES_FILE);
795
0
    if (file_exists(file)) {
796
0
      config_source.file = file;
797
0
    } else if (repo_get_oid(repo, GITMODULES_INDEX, &oid) >= 0 ||
798
0
         repo_get_oid(repo, GITMODULES_HEAD, &oid) >= 0) {
799
0
      config_source.blob = oidstr = xstrdup(oid_to_hex(&oid));
800
0
      if (repo != the_repository)
801
0
        odb_add_submodule_source_by_path(the_repository->objects,
802
0
                 repo->objects->sources->path);
803
0
    } else {
804
0
      goto out;
805
0
    }
806
807
0
    config_with_options(fn, data, &config_source, repo, &opts);
808
809
0
out:
810
0
    free(oidstr);
811
0
    free(file);
812
0
  }
813
0
}
814
815
static int gitmodules_cb(const char *var, const char *value,
816
       const struct config_context *ctx, void *data)
817
0
{
818
0
  struct repository *repo = data;
819
0
  struct parse_config_parameter parameter;
820
821
0
  parameter.cache = repo->submodule_cache;
822
0
  parameter.treeish_name = NULL;
823
0
  parameter.gitmodules_oid = null_oid(the_hash_algo);
824
0
  parameter.overwrite = 1;
825
826
0
  return parse_config(var, value, ctx, &parameter);
827
0
}
828
829
void repo_read_gitmodules(struct repository *repo, int skip_if_read)
830
0
{
831
0
  submodule_cache_check_init(repo);
832
833
0
  if (repo->submodule_cache->gitmodules_read && skip_if_read)
834
0
    return;
835
836
0
  if (repo_read_index(repo) < 0)
837
0
    return;
838
839
0
  if (!is_gitmodules_unmerged(repo->index))
840
0
    config_from_gitmodules(gitmodules_cb, repo, repo);
841
842
0
  repo->submodule_cache->gitmodules_read = 1;
843
0
}
844
845
void gitmodules_config_oid(const struct object_id *commit_oid)
846
0
{
847
0
  struct strbuf rev = STRBUF_INIT;
848
0
  struct object_id oid;
849
850
0
  submodule_cache_check_init(the_repository);
851
852
0
  if (gitmodule_oid_from_commit(commit_oid, &oid, &rev)) {
853
0
    git_config_from_blob_oid(gitmodules_cb, rev.buf,
854
0
           the_repository, &oid, the_repository,
855
0
           CONFIG_SCOPE_UNKNOWN);
856
0
  }
857
0
  strbuf_release(&rev);
858
859
0
  the_repository->submodule_cache->gitmodules_read = 1;
860
0
}
861
862
const struct submodule *submodule_from_name(struct repository *r,
863
              const struct object_id *treeish_name,
864
    const char *name)
865
0
{
866
0
  repo_read_gitmodules(r, 1);
867
0
  return config_from(r->submodule_cache, treeish_name, name, lookup_name);
868
0
}
869
870
const struct submodule *submodule_from_path(struct repository *r,
871
              const struct object_id *treeish_name,
872
    const char *path)
873
0
{
874
0
  repo_read_gitmodules(r, 1);
875
0
  return config_from(r->submodule_cache, treeish_name, path, lookup_path);
876
0
}
877
878
/**
879
 * Used internally by submodules_of_tree(). Recurses into 'treeish_name'
880
 * and appends submodule entries to 'out'. The submodule_cache expects
881
 * a root-level treeish_name and paths, so keep track of these values
882
 * with 'root_tree' and 'prefix'.
883
 */
884
static void traverse_tree_submodules(struct repository *r,
885
             const struct object_id *root_tree,
886
             char *prefix,
887
             const struct object_id *treeish_name,
888
             struct submodule_entry_list *out)
889
0
{
890
0
  struct tree_desc tree;
891
0
  struct submodule_tree_entry *st_entry;
892
0
  struct name_entry name_entry;
893
0
  char *tree_path = NULL;
894
0
  char *tree_buf;
895
896
0
  tree_buf = fill_tree_descriptor(r, &tree, treeish_name);
897
0
  while (tree_entry(&tree, &name_entry)) {
898
0
    if (prefix)
899
0
      tree_path =
900
0
        mkpathdup("%s/%s", prefix, name_entry.path);
901
0
    else
902
0
      tree_path = xstrdup(name_entry.path);
903
904
0
    if (S_ISGITLINK(name_entry.mode) &&
905
0
        is_tree_submodule_active(r, root_tree, tree_path)) {
906
0
      ALLOC_GROW(out->entries, out->entry_nr + 1,
907
0
           out->entry_alloc);
908
0
      st_entry = &out->entries[out->entry_nr++];
909
910
0
      st_entry->name_entry = xmalloc(sizeof(*st_entry->name_entry));
911
0
      *st_entry->name_entry = name_entry;
912
0
      st_entry->submodule =
913
0
        submodule_from_path(r, root_tree, tree_path);
914
0
      st_entry->repo = xmalloc(sizeof(*st_entry->repo));
915
0
      if (repo_submodule_init(st_entry->repo, r, tree_path,
916
0
            root_tree))
917
0
        FREE_AND_NULL(st_entry->repo);
918
919
0
    } else if (S_ISDIR(name_entry.mode))
920
0
      traverse_tree_submodules(r, root_tree, tree_path,
921
0
             &name_entry.oid, out);
922
0
    free(tree_path);
923
0
  }
924
925
0
  free(tree_buf);
926
0
}
927
928
void submodules_of_tree(struct repository *r,
929
      const struct object_id *treeish_name,
930
      struct submodule_entry_list *out)
931
0
{
932
0
  CALLOC_ARRAY(out->entries, 0);
933
0
  out->entry_nr = 0;
934
0
  out->entry_alloc = 0;
935
936
0
  traverse_tree_submodules(r, treeish_name, NULL, treeish_name, out);
937
0
}
938
939
void submodule_entry_list_release(struct submodule_entry_list *list)
940
0
{
941
0
  for (size_t i = 0; i < list->entry_nr; i++) {
942
0
    free(list->entries[i].name_entry);
943
0
    repo_clear(list->entries[i].repo);
944
0
    free(list->entries[i].repo);
945
0
  }
946
0
  free(list->entries);
947
0
}
948
949
void submodule_free(struct repository *r)
950
0
{
951
0
  if (r->submodule_cache)
952
0
    submodule_cache_clear(r->submodule_cache);
953
0
}
954
955
static int config_print_callback(const char *var, const char *value,
956
         const struct config_context *ctx UNUSED,
957
         void *cb_data)
958
0
{
959
0
  char *wanted_key = cb_data;
960
961
0
  if (!strcmp(wanted_key, var))
962
0
    printf("%s\n", value);
963
964
0
  return 0;
965
0
}
966
967
int print_config_from_gitmodules(struct repository *repo, const char *key)
968
0
{
969
0
  int ret;
970
0
  char *store_key;
971
972
0
  ret = git_config_parse_key(key, &store_key, NULL);
973
0
  if (ret < 0)
974
0
    return CONFIG_INVALID_KEY;
975
976
0
  config_from_gitmodules(config_print_callback, repo, store_key);
977
978
0
  free(store_key);
979
0
  return 0;
980
0
}
981
982
int config_set_in_gitmodules_file_gently(const char *key, const char *value)
983
0
{
984
0
  int ret;
985
986
0
  ret = repo_config_set_in_file_gently(the_repository, GITMODULES_FILE, key, NULL, value);
987
0
  if (ret < 0)
988
    /* Maybe the user already did that, don't error out here */
989
0
    warning(_("Could not update .gitmodules entry %s"), key);
990
991
0
  return ret;
992
0
}
993
994
struct fetch_config {
995
  int *max_children;
996
  int *recurse_submodules;
997
};
998
999
static int gitmodules_fetch_config(const char *var, const char *value,
1000
           const struct config_context *ctx,
1001
           void *cb)
1002
0
{
1003
0
  struct fetch_config *config = cb;
1004
0
  if (!strcmp(var, "submodule.fetchjobs")) {
1005
0
    if (config->max_children)
1006
0
      *(config->max_children) =
1007
0
        parse_submodule_fetchjobs(var, value, ctx->kvi);
1008
0
    return 0;
1009
0
  } else if (!strcmp(var, "fetch.recursesubmodules")) {
1010
0
    if (config->recurse_submodules)
1011
0
      *(config->recurse_submodules) =
1012
0
        parse_fetch_recurse_submodules_arg(var, value);
1013
0
    return 0;
1014
0
  }
1015
1016
0
  return 0;
1017
0
}
1018
1019
void fetch_config_from_gitmodules(int *max_children, int *recurse_submodules)
1020
0
{
1021
0
  struct fetch_config config = {
1022
0
    .max_children = max_children,
1023
0
    .recurse_submodules = recurse_submodules
1024
0
  };
1025
0
  config_from_gitmodules(gitmodules_fetch_config, the_repository, &config);
1026
0
}
1027
1028
static int gitmodules_update_clone_config(const char *var, const char *value,
1029
            const struct config_context *ctx,
1030
            void *cb)
1031
0
{
1032
0
  int *max_jobs = cb;
1033
0
  if (!strcmp(var, "submodule.fetchjobs"))
1034
0
    *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
1035
0
  return 0;
1036
0
}
1037
1038
void update_clone_config_from_gitmodules(int *max_jobs)
1039
0
{
1040
0
  config_from_gitmodules(gitmodules_update_clone_config, the_repository, &max_jobs);
1041
0
}