Coverage Report

Created: 2026-03-31 06:24

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