Coverage Report

Created: 2026-12-01 06:58

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