Coverage Report

Created: 2026-01-09 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/config.c
Line
Count
Source
1
/*
2
 * GIT - The information manager from hell
3
 *
4
 * Copyright (C) Linus Torvalds, 2005
5
 * Copyright (C) Johannes Schindelin, 2005
6
 *
7
 */
8
9
#include "git-compat-util.h"
10
#include "abspath.h"
11
#include "advice.h"
12
#include "date.h"
13
#include "branch.h"
14
#include "config.h"
15
#include "dir.h"
16
#include "parse.h"
17
#include "convert.h"
18
#include "environment.h"
19
#include "gettext.h"
20
#include "git-zlib.h"
21
#include "repository.h"
22
#include "lockfile.h"
23
#include "exec-cmd.h"
24
#include "strbuf.h"
25
#include "quote.h"
26
#include "hashmap.h"
27
#include "string-list.h"
28
#include "object-name.h"
29
#include "odb.h"
30
#include "path.h"
31
#include "utf8.h"
32
#include "color.h"
33
#include "refs.h"
34
#include "setup.h"
35
#include "strvec.h"
36
#include "trace2.h"
37
#include "wildmatch.h"
38
#include "write-or-die.h"
39
40
struct config_source {
41
  struct config_source *prev;
42
  union {
43
    FILE *file;
44
    struct config_buf {
45
      const char *buf;
46
      size_t len;
47
      size_t pos;
48
    } buf;
49
  } u;
50
  enum config_origin_type origin_type;
51
  const char *name;
52
  enum config_error_action default_error_action;
53
  int linenr;
54
  int eof;
55
  size_t total_len;
56
  struct strbuf value;
57
  struct strbuf var;
58
  unsigned subsection_case_sensitive : 1;
59
60
  int (*do_fgetc)(struct config_source *c);
61
  int (*do_ungetc)(int c, struct config_source *conf);
62
  long (*do_ftell)(struct config_source *c);
63
};
64
0
#define CONFIG_SOURCE_INIT { 0 }
65
66
/*
67
 * Config that comes from trusted scopes, namely:
68
 * - CONFIG_SCOPE_SYSTEM (e.g. /etc/gitconfig)
69
 * - CONFIG_SCOPE_GLOBAL (e.g. $HOME/.gitconfig, $XDG_CONFIG_HOME/git)
70
 * - CONFIG_SCOPE_COMMAND (e.g. "-c" option, environment variables)
71
 *
72
 * This is declared here for code cleanliness, but unlike the other
73
 * static variables, this does not hold config parser state.
74
 */
75
static struct config_set protected_config;
76
77
static int config_file_fgetc(struct config_source *conf)
78
0
{
79
0
  return getc_unlocked(conf->u.file);
80
0
}
81
82
static int config_file_ungetc(int c, struct config_source *conf)
83
0
{
84
0
  return ungetc(c, conf->u.file);
85
0
}
86
87
static long config_file_ftell(struct config_source *conf)
88
0
{
89
0
  return ftell(conf->u.file);
90
0
}
91
92
static int config_buf_fgetc(struct config_source *conf)
93
0
{
94
0
  if (conf->u.buf.pos < conf->u.buf.len)
95
0
    return conf->u.buf.buf[conf->u.buf.pos++];
96
97
0
  return EOF;
98
0
}
99
100
static int config_buf_ungetc(int c, struct config_source *conf)
101
0
{
102
0
  if (conf->u.buf.pos > 0) {
103
0
    conf->u.buf.pos--;
104
0
    if (conf->u.buf.buf[conf->u.buf.pos] != c)
105
0
      BUG("config_buf can only ungetc the same character");
106
0
    return c;
107
0
  }
108
109
0
  return EOF;
110
0
}
111
112
static long config_buf_ftell(struct config_source *conf)
113
0
{
114
0
  return conf->u.buf.pos;
115
0
}
116
117
struct config_include_data {
118
  int depth;
119
  config_fn_t fn;
120
  void *data;
121
  const struct config_options *opts;
122
  const struct git_config_source *config_source;
123
  struct repository *repo;
124
125
  /*
126
   * All remote URLs discovered when reading all config files.
127
   */
128
  struct string_list *remote_urls;
129
};
130
0
#define CONFIG_INCLUDE_INIT { 0 }
131
132
static int git_config_include(const char *var, const char *value,
133
            const struct config_context *ctx, void *data);
134
135
0
#define MAX_INCLUDE_DEPTH 10
136
static const char include_depth_advice[] = N_(
137
"exceeded maximum include depth (%d) while including\n"
138
" %s\n"
139
"from\n"
140
" %s\n"
141
"This might be due to circular includes.");
142
static int handle_path_include(const struct key_value_info *kvi,
143
             const char *path,
144
             struct config_include_data *inc)
145
0
{
146
0
  int ret = 0;
147
0
  struct strbuf buf = STRBUF_INIT;
148
0
  char *expanded;
149
150
0
  if (!path)
151
0
    return config_error_nonbool("include.path");
152
153
0
  expanded = interpolate_path(path, 0);
154
0
  if (!expanded)
155
0
    return error(_("could not expand include path '%s'"), path);
156
0
  path = expanded;
157
158
  /*
159
   * Use an absolute path as-is, but interpret relative paths
160
   * based on the including config file.
161
   */
162
0
  if (!is_absolute_path(path)) {
163
0
    char *slash;
164
165
0
    if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) {
166
0
      ret = error(_("relative config includes must come from files"));
167
0
      goto cleanup;
168
0
    }
169
170
0
    slash = find_last_dir_sep(kvi->filename);
171
0
    if (slash)
172
0
      strbuf_add(&buf, kvi->filename, slash - kvi->filename + 1);
173
0
    strbuf_addstr(&buf, path);
174
0
    path = buf.buf;
175
0
  }
176
177
0
  if (!access_or_die(path, R_OK, 0)) {
178
0
    if (++inc->depth > MAX_INCLUDE_DEPTH)
179
0
      die(_(include_depth_advice), MAX_INCLUDE_DEPTH, path,
180
0
          !kvi ? "<unknown>" :
181
0
          kvi->filename ? kvi->filename :
182
0
          "the command line");
183
0
    ret = git_config_from_file_with_options(git_config_include, path, inc,
184
0
              kvi->scope, NULL);
185
0
    inc->depth--;
186
0
  }
187
0
cleanup:
188
0
  strbuf_release(&buf);
189
0
  free(expanded);
190
0
  return ret;
191
0
}
192
193
static void add_trailing_starstar_for_dir(struct strbuf *pat)
194
0
{
195
0
  if (pat->len && is_dir_sep(pat->buf[pat->len - 1]))
196
0
    strbuf_addstr(pat, "**");
197
0
}
198
199
static int prepare_include_condition_pattern(const struct key_value_info *kvi,
200
               struct strbuf *pat,
201
               size_t *out)
202
0
{
203
0
  struct strbuf path = STRBUF_INIT;
204
0
  char *expanded;
205
0
  size_t prefix = 0;
206
207
0
  expanded = interpolate_path(pat->buf, 1);
208
0
  if (expanded) {
209
0
    strbuf_reset(pat);
210
0
    strbuf_addstr(pat, expanded);
211
0
    free(expanded);
212
0
  }
213
214
0
  if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
215
0
    const char *slash;
216
217
0
    if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE)
218
0
      return error(_("relative config include "
219
0
               "conditionals must come from files"));
220
221
0
    strbuf_realpath(&path, kvi->filename, 1);
222
0
    slash = find_last_dir_sep(path.buf);
223
0
    if (!slash)
224
0
      BUG("how is this possible?");
225
0
    strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
226
0
    prefix = slash - path.buf + 1 /* slash */;
227
0
  } else if (!is_absolute_path(pat->buf))
228
0
    strbuf_insertstr(pat, 0, "**/");
229
230
0
  add_trailing_starstar_for_dir(pat);
231
232
0
  *out = prefix;
233
234
0
  strbuf_release(&path);
235
0
  return 0;
236
0
}
237
238
static int include_by_gitdir(const struct key_value_info *kvi,
239
           const struct config_options *opts,
240
           const char *cond, size_t cond_len, int icase)
241
0
{
242
0
  struct strbuf text = STRBUF_INIT;
243
0
  struct strbuf pattern = STRBUF_INIT;
244
0
  size_t prefix;
245
0
  int ret = 0;
246
0
  const char *git_dir;
247
0
  int already_tried_absolute = 0;
248
249
0
  if (opts->git_dir)
250
0
    git_dir = opts->git_dir;
251
0
  else
252
0
    goto done;
253
254
0
  strbuf_realpath(&text, git_dir, 1);
255
0
  strbuf_add(&pattern, cond, cond_len);
256
0
  ret = prepare_include_condition_pattern(kvi, &pattern, &prefix);
257
0
  if (ret < 0)
258
0
    goto done;
259
260
0
again:
261
0
  if (prefix > 0) {
262
    /*
263
     * perform literal matching on the prefix part so that
264
     * any wildcard character in it can't create side effects.
265
     */
266
0
    if (text.len < prefix)
267
0
      goto done;
268
0
    if (!icase && strncmp(pattern.buf, text.buf, prefix))
269
0
      goto done;
270
0
    if (icase && strncasecmp(pattern.buf, text.buf, prefix))
271
0
      goto done;
272
0
  }
273
274
0
  ret = !wildmatch(pattern.buf + prefix, text.buf + prefix,
275
0
       WM_PATHNAME | (icase ? WM_CASEFOLD : 0));
276
277
0
  if (!ret && !already_tried_absolute) {
278
    /*
279
     * We've tried e.g. matching gitdir:~/work, but if
280
     * ~/work is a symlink to /mnt/storage/work
281
     * strbuf_realpath() will expand it, so the rule won't
282
     * match. Let's match against a
283
     * strbuf_add_absolute_path() version of the path,
284
     * which'll do the right thing
285
     */
286
0
    strbuf_reset(&text);
287
0
    strbuf_add_absolute_path(&text, git_dir);
288
0
    already_tried_absolute = 1;
289
0
    goto again;
290
0
  }
291
0
done:
292
0
  strbuf_release(&pattern);
293
0
  strbuf_release(&text);
294
0
  return ret;
295
0
}
296
297
static int include_by_branch(struct config_include_data *data,
298
           const char *cond, size_t cond_len)
299
0
{
300
0
  int flags;
301
0
  int ret;
302
0
  struct strbuf pattern = STRBUF_INIT;
303
0
  const char *refname, *shortname;
304
305
0
  if (!data->repo || data->repo->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
306
0
    return 0;
307
308
0
  refname = refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
309
0
            "HEAD", 0, NULL, &flags);
310
0
  if (!refname ||
311
0
      !(flags & REF_ISSYMREF) ||
312
0
      !skip_prefix(refname, "refs/heads/", &shortname))
313
0
    return 0;
314
315
0
  strbuf_add(&pattern, cond, cond_len);
316
0
  add_trailing_starstar_for_dir(&pattern);
317
0
  ret = !wildmatch(pattern.buf, shortname, WM_PATHNAME);
318
0
  strbuf_release(&pattern);
319
0
  return ret;
320
0
}
321
322
static int add_remote_url(const char *var, const char *value,
323
        const struct config_context *ctx UNUSED, void *data)
324
0
{
325
0
  struct string_list *remote_urls = data;
326
0
  const char *remote_name;
327
0
  size_t remote_name_len;
328
0
  const char *key;
329
330
0
  if (!parse_config_key(var, "remote", &remote_name, &remote_name_len,
331
0
            &key) &&
332
0
      remote_name &&
333
0
      !strcmp(key, "url"))
334
0
    string_list_append(remote_urls, value);
335
0
  return 0;
336
0
}
337
338
static void populate_remote_urls(struct config_include_data *inc)
339
0
{
340
0
  struct config_options opts;
341
342
0
  opts = *inc->opts;
343
0
  opts.unconditional_remote_url = 1;
344
345
0
  inc->remote_urls = xmalloc(sizeof(*inc->remote_urls));
346
0
  string_list_init_dup(inc->remote_urls);
347
0
  config_with_options(add_remote_url, inc->remote_urls,
348
0
          inc->config_source, inc->repo, &opts);
349
0
}
350
351
static int forbid_remote_url(const char *var, const char *value UNUSED,
352
           const struct config_context *ctx UNUSED,
353
           void *data UNUSED)
354
0
{
355
0
  const char *remote_name;
356
0
  size_t remote_name_len;
357
0
  const char *key;
358
359
0
  if (!parse_config_key(var, "remote", &remote_name, &remote_name_len,
360
0
            &key) &&
361
0
      remote_name &&
362
0
      !strcmp(key, "url"))
363
0
    die(_("remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url"));
364
0
  return 0;
365
0
}
366
367
static int at_least_one_url_matches_glob(const char *glob, int glob_len,
368
           struct string_list *remote_urls)
369
0
{
370
0
  struct strbuf pattern = STRBUF_INIT;
371
0
  struct string_list_item *url_item;
372
0
  int found = 0;
373
374
0
  strbuf_add(&pattern, glob, glob_len);
375
0
  for_each_string_list_item(url_item, remote_urls) {
376
0
    if (!wildmatch(pattern.buf, url_item->string, WM_PATHNAME)) {
377
0
      found = 1;
378
0
      break;
379
0
    }
380
0
  }
381
0
  strbuf_release(&pattern);
382
0
  return found;
383
0
}
384
385
static int include_by_remote_url(struct config_include_data *inc,
386
    const char *cond, size_t cond_len)
387
0
{
388
0
  if (inc->opts->unconditional_remote_url)
389
0
    return 1;
390
0
  if (!inc->remote_urls)
391
0
    populate_remote_urls(inc);
392
0
  return at_least_one_url_matches_glob(cond, cond_len,
393
0
               inc->remote_urls);
394
0
}
395
396
static int include_condition_is_true(const struct key_value_info *kvi,
397
             struct config_include_data *inc,
398
             const char *cond, size_t cond_len)
399
0
{
400
0
  const struct config_options *opts = inc->opts;
401
402
0
  if (skip_prefix_mem(cond, cond_len, "gitdir:", &cond, &cond_len))
403
0
    return include_by_gitdir(kvi, opts, cond, cond_len, 0);
404
0
  else if (skip_prefix_mem(cond, cond_len, "gitdir/i:", &cond, &cond_len))
405
0
    return include_by_gitdir(kvi, opts, cond, cond_len, 1);
406
0
  else if (skip_prefix_mem(cond, cond_len, "onbranch:", &cond, &cond_len))
407
0
    return include_by_branch(inc, cond, cond_len);
408
0
  else if (skip_prefix_mem(cond, cond_len, "hasconfig:remote.*.url:", &cond,
409
0
           &cond_len))
410
0
    return include_by_remote_url(inc, cond, cond_len);
411
412
  /* unknown conditionals are always false */
413
0
  return 0;
414
0
}
415
416
static int git_config_include(const char *var, const char *value,
417
            const struct config_context *ctx,
418
            void *data)
419
0
{
420
0
  struct config_include_data *inc = data;
421
0
  const char *cond, *key;
422
0
  size_t cond_len;
423
0
  int ret;
424
425
  /*
426
   * Pass along all values, including "include" directives; this makes it
427
   * possible to query information on the includes themselves.
428
   */
429
0
  ret = inc->fn(var, value, ctx, inc->data);
430
0
  if (ret < 0)
431
0
    return ret;
432
433
0
  if (!strcmp(var, "include.path"))
434
0
    ret = handle_path_include(ctx->kvi, value, inc);
435
436
0
  if (!parse_config_key(var, "includeif", &cond, &cond_len, &key) &&
437
0
      cond && include_condition_is_true(ctx->kvi, inc, cond, cond_len) &&
438
0
      !strcmp(key, "path")) {
439
0
    config_fn_t old_fn = inc->fn;
440
441
0
    if (inc->opts->unconditional_remote_url)
442
0
      inc->fn = forbid_remote_url;
443
0
    ret = handle_path_include(ctx->kvi, value, inc);
444
0
    inc->fn = old_fn;
445
0
  }
446
447
0
  return ret;
448
0
}
449
450
static void git_config_push_split_parameter(const char *key, const char *value)
451
0
{
452
0
  struct strbuf env = STRBUF_INIT;
453
0
  const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
454
0
  if (old && *old) {
455
0
    strbuf_addstr(&env, old);
456
0
    strbuf_addch(&env, ' ');
457
0
  }
458
0
  sq_quote_buf(&env, key);
459
0
  strbuf_addch(&env, '=');
460
0
  if (value)
461
0
    sq_quote_buf(&env, value);
462
0
  setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
463
0
  strbuf_release(&env);
464
0
}
465
466
void git_config_push_parameter(const char *text)
467
0
{
468
0
  const char *value;
469
470
  /*
471
   * When we see:
472
   *
473
   *   section.subsection=with=equals.key=value
474
   *
475
   * we cannot tell if it means:
476
   *
477
   *   [section "subsection=with=equals"]
478
   *   key = value
479
   *
480
   * or:
481
   *
482
   *   [section]
483
   *   subsection = with=equals.key=value
484
   *
485
   * We parse left-to-right for the first "=", meaning we'll prefer to
486
   * keep the value intact over the subsection. This is historical, but
487
   * also sensible since values are more likely to contain odd or
488
   * untrusted input than a section name.
489
   *
490
   * A missing equals is explicitly allowed (as a bool-only entry).
491
   */
492
0
  value = strchr(text, '=');
493
0
  if (value) {
494
0
    char *key = xmemdupz(text, value - text);
495
0
    git_config_push_split_parameter(key, value + 1);
496
0
    free(key);
497
0
  } else {
498
0
    git_config_push_split_parameter(text, NULL);
499
0
  }
500
0
}
501
502
void git_config_push_env(const char *spec)
503
0
{
504
0
  char *key;
505
0
  const char *env_name;
506
0
  const char *env_value;
507
508
0
  env_name = strrchr(spec, '=');
509
0
  if (!env_name)
510
0
    die(_("invalid config format: %s"), spec);
511
0
  key = xmemdupz(spec, env_name - spec);
512
0
  env_name++;
513
0
  if (!*env_name)
514
0
    die(_("missing environment variable name for configuration '%.*s'"),
515
0
        (int)(env_name - spec - 1), spec);
516
517
0
  env_value = getenv(env_name);
518
0
  if (!env_value)
519
0
    die(_("missing environment variable '%s' for configuration '%.*s'"),
520
0
        env_name, (int)(env_name - spec - 1), spec);
521
522
0
  git_config_push_split_parameter(key, env_value);
523
0
  free(key);
524
0
}
525
526
static inline int iskeychar(int c)
527
0
{
528
0
  return isalnum(c) || c == '-';
529
0
}
530
531
/*
532
 * Auxiliary function to sanity-check and split the key into the section
533
 * identifier and variable name.
534
 *
535
 * Returns 0 on success, -1 when there is an invalid character in the key and
536
 * -2 if there is no section name in the key.
537
 *
538
 * store_key - pointer to char* which will hold a copy of the key with
539
 *             lowercase section and variable name
540
 * baselen - pointer to size_t which will hold the length of the
541
 *           section + subsection part, can be NULL
542
 */
543
int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
544
0
{
545
0
  size_t i, baselen;
546
0
  int dot;
547
0
  const char *last_dot = strrchr(key, '.');
548
549
  /*
550
   * Since "key" actually contains the section name and the real
551
   * key name separated by a dot, we have to know where the dot is.
552
   */
553
554
0
  if (last_dot == NULL || last_dot == key) {
555
0
    error(_("key does not contain a section: %s"), key);
556
0
    return -CONFIG_NO_SECTION_OR_NAME;
557
0
  }
558
559
0
  if (!last_dot[1]) {
560
0
    error(_("key does not contain variable name: %s"), key);
561
0
    return -CONFIG_NO_SECTION_OR_NAME;
562
0
  }
563
564
0
  baselen = last_dot - key;
565
0
  if (baselen_)
566
0
    *baselen_ = baselen;
567
568
  /*
569
   * Validate the key and while at it, lower case it for matching.
570
   */
571
0
  *store_key = xmallocz(strlen(key));
572
573
0
  dot = 0;
574
0
  for (i = 0; key[i]; i++) {
575
0
    unsigned char c = key[i];
576
0
    if (c == '.')
577
0
      dot = 1;
578
    /* Leave the extended basename untouched.. */
579
0
    if (!dot || i > baselen) {
580
0
      if (!iskeychar(c) ||
581
0
          (i == baselen + 1 && !isalpha(c))) {
582
0
        error(_("invalid key: %s"), key);
583
0
        goto out_free_ret_1;
584
0
      }
585
0
      c = tolower(c);
586
0
    } else if (c == '\n') {
587
0
      error(_("invalid key (newline): %s"), key);
588
0
      goto out_free_ret_1;
589
0
    }
590
0
    (*store_key)[i] = c;
591
0
  }
592
593
0
  return 0;
594
595
0
out_free_ret_1:
596
0
  FREE_AND_NULL(*store_key);
597
0
  return -CONFIG_INVALID_KEY;
598
0
}
599
600
static int config_parse_pair(const char *key, const char *value,
601
           struct key_value_info *kvi,
602
           config_fn_t fn, void *data)
603
0
{
604
0
  char *canonical_name;
605
0
  int ret;
606
0
  struct config_context ctx = {
607
0
    .kvi = kvi,
608
0
  };
609
610
0
  if (!strlen(key))
611
0
    return error(_("empty config key"));
612
0
  if (git_config_parse_key(key, &canonical_name, NULL))
613
0
    return -1;
614
615
0
  ret = (fn(canonical_name, value, &ctx, data) < 0) ? -1 : 0;
616
0
  free(canonical_name);
617
0
  return ret;
618
0
}
619
620
621
/* for values read from `git_config_from_parameters()` */
622
void kvi_from_param(struct key_value_info *out)
623
0
{
624
0
  out->filename = NULL;
625
0
  out->linenr = -1;
626
0
  out->origin_type = CONFIG_ORIGIN_CMDLINE;
627
0
  out->scope = CONFIG_SCOPE_COMMAND;
628
0
}
629
630
int git_config_parse_parameter(const char *text,
631
             config_fn_t fn, void *data)
632
0
{
633
0
  const char *value;
634
0
  struct string_list pair = STRING_LIST_INIT_DUP;
635
0
  int ret;
636
0
  struct key_value_info kvi = KVI_INIT;
637
638
0
  kvi_from_param(&kvi);
639
640
0
  string_list_split(&pair, text, "=", 1);
641
0
  if (!pair.nr)
642
0
    return error(_("bogus config parameter: %s"), text);
643
644
0
  if (pair.nr == 1)
645
0
    value = NULL;
646
0
  else
647
0
    value = pair.items[1].string;
648
649
0
  if (!*pair.items[0].string) {
650
0
    string_list_clear(&pair, 0);
651
0
    return error(_("bogus config parameter: %s"), text);
652
0
  }
653
654
0
  ret = config_parse_pair(pair.items[0].string, value, &kvi, fn, data);
655
0
  string_list_clear(&pair, 0);
656
0
  return ret;
657
0
}
658
659
static int parse_config_env_list(char *env, struct key_value_info *kvi,
660
         config_fn_t fn, void *data)
661
0
{
662
0
  char *cur = env;
663
0
  while (cur && *cur) {
664
0
    const char *key = sq_dequote_step(cur, &cur);
665
0
    if (!key)
666
0
      return error(_("bogus format in %s"),
667
0
             CONFIG_DATA_ENVIRONMENT);
668
669
0
    if (!cur || isspace(*cur)) {
670
      /* old-style 'key=value' */
671
0
      if (git_config_parse_parameter(key, fn, data) < 0)
672
0
        return -1;
673
0
    }
674
0
    else if (*cur == '=') {
675
      /* new-style 'key'='value' */
676
0
      const char *value;
677
678
0
      cur++;
679
0
      if (*cur == '\'') {
680
        /* quoted value */
681
0
        value = sq_dequote_step(cur, &cur);
682
0
        if (!value || (cur && !isspace(*cur))) {
683
0
          return error(_("bogus format in %s"),
684
0
                 CONFIG_DATA_ENVIRONMENT);
685
0
        }
686
0
      } else if (!*cur || isspace(*cur)) {
687
        /* implicit bool: 'key'= */
688
0
        value = NULL;
689
0
      } else {
690
0
        return error(_("bogus format in %s"),
691
0
               CONFIG_DATA_ENVIRONMENT);
692
0
      }
693
694
0
      if (config_parse_pair(key, value, kvi, fn, data) < 0)
695
0
        return -1;
696
0
    }
697
0
    else {
698
      /* unknown format */
699
0
      return error(_("bogus format in %s"),
700
0
             CONFIG_DATA_ENVIRONMENT);
701
0
    }
702
703
0
    if (cur) {
704
0
      while (isspace(*cur))
705
0
        cur++;
706
0
    }
707
0
  }
708
0
  return 0;
709
0
}
710
711
int git_config_from_parameters(config_fn_t fn, void *data)
712
0
{
713
0
  const char *env;
714
0
  struct strbuf envvar = STRBUF_INIT;
715
0
  struct strvec to_free = STRVEC_INIT;
716
0
  int ret = 0;
717
0
  char *envw = NULL;
718
0
  struct key_value_info kvi = KVI_INIT;
719
720
0
  kvi_from_param(&kvi);
721
0
  env = getenv(CONFIG_COUNT_ENVIRONMENT);
722
0
  if (env) {
723
0
    unsigned long count;
724
0
    char *endp;
725
726
0
    count = strtoul(env, &endp, 10);
727
0
    if (*endp) {
728
0
      ret = error(_("bogus count in %s"), CONFIG_COUNT_ENVIRONMENT);
729
0
      goto out;
730
0
    }
731
0
    if (count > INT_MAX) {
732
0
      ret = error(_("too many entries in %s"), CONFIG_COUNT_ENVIRONMENT);
733
0
      goto out;
734
0
    }
735
736
0
    for (unsigned long i = 0; i < count; i++) {
737
0
      const char *key, *value;
738
739
0
      strbuf_addf(&envvar, "GIT_CONFIG_KEY_%lu", i);
740
0
      key = getenv_safe(&to_free, envvar.buf);
741
0
      if (!key) {
742
0
        ret = error(_("missing config key %s"), envvar.buf);
743
0
        goto out;
744
0
      }
745
0
      strbuf_reset(&envvar);
746
747
0
      strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%lu", i);
748
0
      value = getenv_safe(&to_free, envvar.buf);
749
0
      if (!value) {
750
0
        ret = error(_("missing config value %s"), envvar.buf);
751
0
        goto out;
752
0
      }
753
0
      strbuf_reset(&envvar);
754
755
0
      if (config_parse_pair(key, value, &kvi, fn, data) < 0) {
756
0
        ret = -1;
757
0
        goto out;
758
0
      }
759
0
    }
760
0
  }
761
762
0
  env = getenv(CONFIG_DATA_ENVIRONMENT);
763
0
  if (env) {
764
    /* sq_dequote will write over it */
765
0
    envw = xstrdup(env);
766
0
    if (parse_config_env_list(envw, &kvi, fn, data) < 0) {
767
0
      ret = -1;
768
0
      goto out;
769
0
    }
770
0
  }
771
772
0
out:
773
0
  strbuf_release(&envvar);
774
0
  strvec_clear(&to_free);
775
0
  free(envw);
776
0
  return ret;
777
0
}
778
779
static int get_next_char(struct config_source *cs)
780
0
{
781
0
  int c = cs->do_fgetc(cs);
782
783
0
  if (c == '\r') {
784
    /* DOS like systems */
785
0
    c = cs->do_fgetc(cs);
786
0
    if (c != '\n') {
787
0
      if (c != EOF)
788
0
        cs->do_ungetc(c, cs);
789
0
      c = '\r';
790
0
    }
791
0
  }
792
793
0
  if (c != EOF && ++cs->total_len > INT_MAX) {
794
    /*
795
     * This is an absurdly long config file; refuse to parse
796
     * further in order to protect downstream code from integer
797
     * overflows. Note that we can't return an error specifically,
798
     * but we can mark EOF and put trash in the return value,
799
     * which will trigger a parse error.
800
     */
801
0
    cs->eof = 1;
802
0
    return 0;
803
0
  }
804
805
0
  if (c == '\n')
806
0
    cs->linenr++;
807
0
  if (c == EOF) {
808
0
    cs->eof = 1;
809
0
    cs->linenr++;
810
0
    c = '\n';
811
0
  }
812
0
  return c;
813
0
}
814
815
static char *parse_value(struct config_source *cs)
816
0
{
817
0
  int quote = 0, comment = 0;
818
0
  size_t trim_len = 0;
819
820
0
  strbuf_reset(&cs->value);
821
0
  for (;;) {
822
0
    int c = get_next_char(cs);
823
0
    if (c == '\n') {
824
0
      if (quote) {
825
0
        cs->linenr--;
826
0
        return NULL;
827
0
      }
828
0
      if (trim_len)
829
0
        strbuf_setlen(&cs->value, trim_len);
830
0
      return cs->value.buf;
831
0
    }
832
0
    if (comment)
833
0
      continue;
834
0
    if (isspace(c) && !quote) {
835
0
      if (!trim_len)
836
0
        trim_len = cs->value.len;
837
0
      if (cs->value.len)
838
0
        strbuf_addch(&cs->value, c);
839
0
      continue;
840
0
    }
841
0
    if (!quote) {
842
0
      if (c == ';' || c == '#') {
843
0
        comment = 1;
844
0
        continue;
845
0
      }
846
0
    }
847
0
    if (trim_len)
848
0
      trim_len = 0;
849
0
    if (c == '\\') {
850
0
      c = get_next_char(cs);
851
0
      switch (c) {
852
0
      case '\n':
853
0
        continue;
854
0
      case 't':
855
0
        c = '\t';
856
0
        break;
857
0
      case 'b':
858
0
        c = '\b';
859
0
        break;
860
0
      case 'n':
861
0
        c = '\n';
862
0
        break;
863
      /* Some characters escape as themselves */
864
0
      case '\\': case '"':
865
0
        break;
866
      /* Reject unknown escape sequences */
867
0
      default:
868
0
        return NULL;
869
0
      }
870
0
      strbuf_addch(&cs->value, c);
871
0
      continue;
872
0
    }
873
0
    if (c == '"') {
874
0
      quote = 1 - quote;
875
0
      continue;
876
0
    }
877
0
    strbuf_addch(&cs->value, c);
878
0
  }
879
0
}
880
881
static int get_value(struct config_source *cs, struct key_value_info *kvi,
882
         config_fn_t fn, void *data, struct strbuf *name)
883
0
{
884
0
  int c;
885
0
  char *value;
886
0
  int ret;
887
0
  struct config_context ctx = {
888
0
    .kvi = kvi,
889
0
  };
890
891
  /* Get the full name */
892
0
  for (;;) {
893
0
    c = get_next_char(cs);
894
0
    if (cs->eof)
895
0
      break;
896
0
    if (!iskeychar(c))
897
0
      break;
898
0
    strbuf_addch(name, tolower(c));
899
0
  }
900
901
0
  while (c == ' ' || c == '\t')
902
0
    c = get_next_char(cs);
903
904
0
  value = NULL;
905
0
  if (c != '\n') {
906
0
    if (c != '=')
907
0
      return -1;
908
0
    value = parse_value(cs);
909
0
    if (!value)
910
0
      return -1;
911
0
  }
912
  /*
913
   * We already consumed the \n, but we need linenr to point to
914
   * the line we just parsed during the call to fn to get
915
   * accurate line number in error messages.
916
   */
917
0
  cs->linenr--;
918
0
  kvi->linenr = cs->linenr;
919
0
  ret = fn(name->buf, value, &ctx, data);
920
0
  if (ret >= 0)
921
0
    cs->linenr++;
922
0
  return ret;
923
0
}
924
925
static int get_extended_base_var(struct config_source *cs, struct strbuf *name,
926
         int c)
927
0
{
928
0
  cs->subsection_case_sensitive = 0;
929
0
  do {
930
0
    if (c == '\n')
931
0
      goto error_incomplete_line;
932
0
    c = get_next_char(cs);
933
0
  } while (isspace(c));
934
935
  /* We require the format to be '[base "extension"]' */
936
0
  if (c != '"')
937
0
    return -1;
938
0
  strbuf_addch(name, '.');
939
940
0
  for (;;) {
941
0
    int c = get_next_char(cs);
942
0
    if (c == '\n')
943
0
      goto error_incomplete_line;
944
0
    if (c == '"')
945
0
      break;
946
0
    if (c == '\\') {
947
0
      c = get_next_char(cs);
948
0
      if (c == '\n')
949
0
        goto error_incomplete_line;
950
0
    }
951
0
    strbuf_addch(name, c);
952
0
  }
953
954
  /* Final ']' */
955
0
  if (get_next_char(cs) != ']')
956
0
    return -1;
957
0
  return 0;
958
0
error_incomplete_line:
959
0
  cs->linenr--;
960
0
  return -1;
961
0
}
962
963
static int get_base_var(struct config_source *cs, struct strbuf *name)
964
0
{
965
0
  cs->subsection_case_sensitive = 1;
966
0
  for (;;) {
967
0
    int c = get_next_char(cs);
968
0
    if (cs->eof)
969
0
      return -1;
970
0
    if (c == ']')
971
0
      return 0;
972
0
    if (isspace(c))
973
0
      return get_extended_base_var(cs, name, c);
974
0
    if (!iskeychar(c) && c != '.')
975
0
      return -1;
976
0
    strbuf_addch(name, tolower(c));
977
0
  }
978
0
}
979
980
struct parse_event_data {
981
  enum config_event_t previous_type;
982
  size_t previous_offset;
983
  const struct config_options *opts;
984
};
985
986
static int do_event(struct config_source *cs, enum config_event_t type,
987
        struct parse_event_data *data)
988
0
{
989
0
  size_t offset;
990
991
0
  if (!data->opts || !data->opts->event_fn)
992
0
    return 0;
993
994
0
  if (type == CONFIG_EVENT_WHITESPACE &&
995
0
      data->previous_type == type)
996
0
    return 0;
997
998
0
  offset = cs->do_ftell(cs);
999
  /*
1000
   * At EOF, the parser always "inserts" an extra '\n', therefore
1001
   * the end offset of the event is the current file position, otherwise
1002
   * we will already have advanced to the next event.
1003
   */
1004
0
  if (type != CONFIG_EVENT_EOF)
1005
0
    offset--;
1006
1007
0
  if (data->previous_type != CONFIG_EVENT_EOF &&
1008
0
      data->opts->event_fn(data->previous_type, data->previous_offset,
1009
0
         offset, cs, data->opts->event_fn_data) < 0)
1010
0
    return -1;
1011
1012
0
  data->previous_type = type;
1013
0
  data->previous_offset = offset;
1014
1015
0
  return 0;
1016
0
}
1017
1018
static void kvi_from_source(struct config_source *cs,
1019
          enum config_scope scope,
1020
          struct key_value_info *out)
1021
0
{
1022
0
  out->filename = strintern(cs->name);
1023
0
  out->origin_type = cs->origin_type;
1024
0
  out->linenr = cs->linenr;
1025
0
  out->scope = scope;
1026
0
}
1027
1028
static int git_parse_source(struct config_source *cs, config_fn_t fn,
1029
          struct key_value_info *kvi, void *data,
1030
          const struct config_options *opts)
1031
0
{
1032
0
  int comment = 0;
1033
0
  size_t baselen = 0;
1034
0
  struct strbuf *var = &cs->var;
1035
0
  int error_return = 0;
1036
0
  char *error_msg = NULL;
1037
1038
  /* U+FEFF Byte Order Mark in UTF8 */
1039
0
  const char *bomptr = utf8_bom;
1040
1041
  /* For the parser event callback */
1042
0
  struct parse_event_data event_data = {
1043
0
    CONFIG_EVENT_EOF, 0, opts
1044
0
  };
1045
1046
0
  for (;;) {
1047
0
    int c;
1048
1049
0
    c = get_next_char(cs);
1050
0
    if (bomptr && *bomptr) {
1051
      /* We are at the file beginning; skip UTF8-encoded BOM
1052
       * if present. Sane editors won't put this in on their
1053
       * own, but e.g. Windows Notepad will do it happily. */
1054
0
      if (c == (*bomptr & 0377)) {
1055
0
        bomptr++;
1056
0
        continue;
1057
0
      } else {
1058
        /* Do not tolerate partial BOM. */
1059
0
        if (bomptr != utf8_bom)
1060
0
          break;
1061
        /* No BOM at file beginning. Cool. */
1062
0
        bomptr = NULL;
1063
0
      }
1064
0
    }
1065
0
    if (c == '\n') {
1066
0
      if (cs->eof) {
1067
0
        if (do_event(cs, CONFIG_EVENT_EOF, &event_data) < 0)
1068
0
          return -1;
1069
0
        return 0;
1070
0
      }
1071
0
      if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0)
1072
0
        return -1;
1073
0
      comment = 0;
1074
0
      continue;
1075
0
    }
1076
0
    if (comment)
1077
0
      continue;
1078
0
    if (isspace(c)) {
1079
0
      if (do_event(cs, CONFIG_EVENT_WHITESPACE, &event_data) < 0)
1080
0
          return -1;
1081
0
      continue;
1082
0
    }
1083
0
    if (c == '#' || c == ';') {
1084
0
      if (do_event(cs, CONFIG_EVENT_COMMENT, &event_data) < 0)
1085
0
          return -1;
1086
0
      comment = 1;
1087
0
      continue;
1088
0
    }
1089
0
    if (c == '[') {
1090
0
      if (do_event(cs, CONFIG_EVENT_SECTION, &event_data) < 0)
1091
0
          return -1;
1092
1093
      /* Reset prior to determining a new stem */
1094
0
      strbuf_reset(var);
1095
0
      if (get_base_var(cs, var) < 0 || var->len < 1)
1096
0
        break;
1097
0
      strbuf_addch(var, '.');
1098
0
      baselen = var->len;
1099
0
      continue;
1100
0
    }
1101
0
    if (!isalpha(c))
1102
0
      break;
1103
1104
0
    if (do_event(cs, CONFIG_EVENT_ENTRY, &event_data) < 0)
1105
0
      return -1;
1106
1107
    /*
1108
     * Truncate the var name back to the section header
1109
     * stem prior to grabbing the suffix part of the name
1110
     * and the value.
1111
     */
1112
0
    strbuf_setlen(var, baselen);
1113
0
    strbuf_addch(var, tolower(c));
1114
0
    if (get_value(cs, kvi, fn, data, var) < 0)
1115
0
      break;
1116
0
  }
1117
1118
0
  if (do_event(cs, CONFIG_EVENT_ERROR, &event_data) < 0)
1119
0
    return -1;
1120
1121
0
  switch (cs->origin_type) {
1122
0
  case CONFIG_ORIGIN_BLOB:
1123
0
    error_msg = xstrfmt(_("bad config line %d in blob %s"),
1124
0
              cs->linenr, cs->name);
1125
0
    break;
1126
0
  case CONFIG_ORIGIN_FILE:
1127
0
    error_msg = xstrfmt(_("bad config line %d in file %s"),
1128
0
              cs->linenr, cs->name);
1129
0
    break;
1130
0
  case CONFIG_ORIGIN_STDIN:
1131
0
    error_msg = xstrfmt(_("bad config line %d in standard input"),
1132
0
              cs->linenr);
1133
0
    break;
1134
0
  case CONFIG_ORIGIN_SUBMODULE_BLOB:
1135
0
    error_msg = xstrfmt(_("bad config line %d in submodule-blob %s"),
1136
0
               cs->linenr, cs->name);
1137
0
    break;
1138
0
  case CONFIG_ORIGIN_CMDLINE:
1139
0
    error_msg = xstrfmt(_("bad config line %d in command line %s"),
1140
0
               cs->linenr, cs->name);
1141
0
    break;
1142
0
  default:
1143
0
    error_msg = xstrfmt(_("bad config line %d in %s"),
1144
0
              cs->linenr, cs->name);
1145
0
  }
1146
1147
0
  switch (opts && opts->error_action ?
1148
0
    opts->error_action :
1149
0
    cs->default_error_action) {
1150
0
  case CONFIG_ERROR_DIE:
1151
0
    die("%s", error_msg);
1152
0
    break;
1153
0
  case CONFIG_ERROR_ERROR:
1154
0
    error_return = error("%s", error_msg);
1155
0
    break;
1156
0
  case CONFIG_ERROR_SILENT:
1157
0
    error_return = -1;
1158
0
    break;
1159
0
  case CONFIG_ERROR_UNSET:
1160
0
    BUG("config error action unset");
1161
0
  }
1162
1163
0
  free(error_msg);
1164
0
  return error_return;
1165
0
}
1166
1167
NORETURN
1168
static void die_bad_number(const char *name, const char *value,
1169
         const struct key_value_info *kvi)
1170
0
{
1171
0
  const char *error_type = (errno == ERANGE) ?
1172
0
    N_("out of range") : N_("invalid unit");
1173
0
  const char *bad_numeric = N_("bad numeric config value '%s' for '%s': %s");
1174
1175
0
  if (!kvi)
1176
0
    BUG("kvi should not be NULL");
1177
1178
0
  if (!value)
1179
0
    value = "";
1180
1181
0
  if (!kvi->filename)
1182
0
    die(_(bad_numeric), value, name, _(error_type));
1183
1184
0
  switch (kvi->origin_type) {
1185
0
  case CONFIG_ORIGIN_BLOB:
1186
0
    die(_("bad numeric config value '%s' for '%s' in blob %s: %s"),
1187
0
        value, name, kvi->filename, _(error_type));
1188
0
  case CONFIG_ORIGIN_FILE:
1189
0
    die(_("bad numeric config value '%s' for '%s' in file %s: %s"),
1190
0
        value, name, kvi->filename, _(error_type));
1191
0
  case CONFIG_ORIGIN_STDIN:
1192
0
    die(_("bad numeric config value '%s' for '%s' in standard input: %s"),
1193
0
        value, name, _(error_type));
1194
0
  case CONFIG_ORIGIN_SUBMODULE_BLOB:
1195
0
    die(_("bad numeric config value '%s' for '%s' in submodule-blob %s: %s"),
1196
0
        value, name, kvi->filename, _(error_type));
1197
0
  case CONFIG_ORIGIN_CMDLINE:
1198
0
    die(_("bad numeric config value '%s' for '%s' in command line %s: %s"),
1199
0
        value, name, kvi->filename, _(error_type));
1200
0
  default:
1201
0
    die(_("bad numeric config value '%s' for '%s' in %s: %s"),
1202
0
        value, name, kvi->filename, _(error_type));
1203
0
  }
1204
0
}
1205
1206
int git_config_int(const char *name, const char *value,
1207
       const struct key_value_info *kvi)
1208
0
{
1209
0
  int ret;
1210
0
  if (!git_parse_int(value, &ret))
1211
0
    die_bad_number(name, value, kvi);
1212
0
  return ret;
1213
0
}
1214
1215
int64_t git_config_int64(const char *name, const char *value,
1216
       const struct key_value_info *kvi)
1217
0
{
1218
0
  int64_t ret;
1219
0
  if (!git_parse_int64(value, &ret))
1220
0
    die_bad_number(name, value, kvi);
1221
0
  return ret;
1222
0
}
1223
1224
unsigned long git_config_ulong(const char *name, const char *value,
1225
             const struct key_value_info *kvi)
1226
0
{
1227
0
  unsigned long ret;
1228
0
  if (!git_parse_ulong(value, &ret))
1229
0
    die_bad_number(name, value, kvi);
1230
0
  return ret;
1231
0
}
1232
1233
ssize_t git_config_ssize_t(const char *name, const char *value,
1234
         const struct key_value_info *kvi)
1235
0
{
1236
0
  ssize_t ret;
1237
0
  if (!git_parse_ssize_t(value, &ret))
1238
0
    die_bad_number(name, value, kvi);
1239
0
  return ret;
1240
0
}
1241
1242
double git_config_double(const char *name, const char *value,
1243
       const struct key_value_info *kvi)
1244
0
{
1245
0
  double ret;
1246
0
  if (!git_parse_double(value, &ret))
1247
0
    die_bad_number(name, value, kvi);
1248
0
  return ret;
1249
0
}
1250
1251
int git_config_bool_or_int(const char *name, const char *value,
1252
         const struct key_value_info *kvi, int *is_bool)
1253
0
{
1254
0
  int v = git_parse_maybe_bool_text(value);
1255
0
  if (0 <= v) {
1256
0
    *is_bool = 1;
1257
0
    return v;
1258
0
  }
1259
0
  *is_bool = 0;
1260
0
  return git_config_int(name, value, kvi);
1261
0
}
1262
1263
int git_config_bool(const char *name, const char *value)
1264
0
{
1265
0
  int v = git_parse_maybe_bool(value);
1266
0
  if (v < 0)
1267
0
    die(_("bad boolean config value '%s' for '%s'"), value, name);
1268
0
  return v;
1269
0
}
1270
1271
int git_config_string(char **dest, const char *var, const char *value)
1272
0
{
1273
0
  if (!value)
1274
0
    return config_error_nonbool(var);
1275
0
  *dest = xstrdup(value);
1276
0
  return 0;
1277
0
}
1278
1279
int git_config_pathname(char **dest, const char *var, const char *value)
1280
0
{
1281
0
  bool is_optional;
1282
0
  char *path;
1283
1284
0
  if (!value)
1285
0
    return config_error_nonbool(var);
1286
1287
0
  is_optional = skip_prefix(value, ":(optional)", &value);
1288
0
  path = interpolate_path(value, 0);
1289
0
  if (!path)
1290
0
    die(_("failed to expand user dir in: '%s'"), value);
1291
1292
0
  if (is_optional && is_missing_file(path)) {
1293
0
    free(path);
1294
0
    *dest = NULL;
1295
0
    return 0;
1296
0
  }
1297
1298
0
  *dest = path;
1299
0
  return 0;
1300
0
}
1301
1302
int git_config_expiry_date(timestamp_t *timestamp, const char *var, const char *value)
1303
0
{
1304
0
  if (!value)
1305
0
    return config_error_nonbool(var);
1306
0
  if (parse_expiry_date(value, timestamp))
1307
0
    return error(_("'%s' for '%s' is not a valid timestamp"),
1308
0
           value, var);
1309
0
  return 0;
1310
0
}
1311
1312
int git_config_color(char *dest, const char *var, const char *value)
1313
0
{
1314
0
  if (!value)
1315
0
    return config_error_nonbool(var);
1316
0
  if (color_parse(value, dest) < 0)
1317
0
    return -1;
1318
0
  return 0;
1319
0
}
1320
1321
/*
1322
 * All source specific fields in the union, die_on_error, name and the callbacks
1323
 * fgetc, ungetc, ftell of top need to be initialized before calling
1324
 * this function.
1325
 */
1326
static int do_config_from(struct config_source *top, config_fn_t fn,
1327
        void *data, enum config_scope scope,
1328
        const struct config_options *opts)
1329
0
{
1330
0
  struct key_value_info kvi = KVI_INIT;
1331
0
  int ret;
1332
1333
  /* push config-file parsing state stack */
1334
0
  top->linenr = 1;
1335
0
  top->eof = 0;
1336
0
  top->total_len = 0;
1337
0
  strbuf_init(&top->value, 1024);
1338
0
  strbuf_init(&top->var, 1024);
1339
0
  kvi_from_source(top, scope, &kvi);
1340
1341
0
  ret = git_parse_source(top, fn, &kvi, data, opts);
1342
1343
0
  strbuf_release(&top->value);
1344
0
  strbuf_release(&top->var);
1345
1346
0
  return ret;
1347
0
}
1348
1349
static int do_config_from_file(config_fn_t fn,
1350
             const enum config_origin_type origin_type,
1351
             const char *name, FILE *f, void *data,
1352
             enum config_scope scope,
1353
             const struct config_options *opts)
1354
0
{
1355
0
  struct config_source top = CONFIG_SOURCE_INIT;
1356
0
  int ret;
1357
1358
0
  if (origin_type == CONFIG_ORIGIN_FILE && (!name || !*name))
1359
0
    BUG("missing filename for CONFIG_ORIGIN_FILE");
1360
1361
0
  top.u.file = f;
1362
0
  top.origin_type = origin_type;
1363
0
  top.name = name;
1364
0
  top.default_error_action = CONFIG_ERROR_DIE;
1365
0
  top.do_fgetc = config_file_fgetc;
1366
0
  top.do_ungetc = config_file_ungetc;
1367
0
  top.do_ftell = config_file_ftell;
1368
1369
0
  flockfile(f);
1370
0
  ret = do_config_from(&top, fn, data, scope, opts);
1371
0
  funlockfile(f);
1372
0
  return ret;
1373
0
}
1374
1375
static int git_config_from_stdin(config_fn_t fn, void *data,
1376
         enum config_scope scope)
1377
0
{
1378
0
  return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", stdin, data,
1379
0
           scope, NULL);
1380
0
}
1381
1382
int git_config_from_file_with_options(config_fn_t fn, const char *filename,
1383
              void *data, enum config_scope scope,
1384
              const struct config_options *opts)
1385
0
{
1386
0
  int ret = -1;
1387
0
  FILE *f;
1388
1389
0
  if (!filename)
1390
0
    BUG("filename cannot be NULL");
1391
0
  f = fopen_or_warn(filename, "r");
1392
0
  if (f) {
1393
0
    ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
1394
0
            f, data, scope, opts);
1395
0
    fclose(f);
1396
0
  }
1397
0
  return ret;
1398
0
}
1399
1400
int git_config_from_file(config_fn_t fn, const char *filename, void *data)
1401
0
{
1402
0
  return git_config_from_file_with_options(fn, filename, data,
1403
0
             CONFIG_SCOPE_UNKNOWN, NULL);
1404
0
}
1405
1406
int git_config_from_mem(config_fn_t fn,
1407
      const enum config_origin_type origin_type,
1408
      const char *name, const char *buf, size_t len,
1409
      void *data, enum config_scope scope,
1410
      const struct config_options *opts)
1411
0
{
1412
0
  struct config_source top = CONFIG_SOURCE_INIT;
1413
1414
0
  top.u.buf.buf = buf;
1415
0
  top.u.buf.len = len;
1416
0
  top.u.buf.pos = 0;
1417
0
  top.origin_type = origin_type;
1418
0
  top.name = name;
1419
0
  top.default_error_action = CONFIG_ERROR_ERROR;
1420
0
  top.do_fgetc = config_buf_fgetc;
1421
0
  top.do_ungetc = config_buf_ungetc;
1422
0
  top.do_ftell = config_buf_ftell;
1423
1424
0
  return do_config_from(&top, fn, data, scope, opts);
1425
0
}
1426
1427
int git_config_from_blob_oid(config_fn_t fn,
1428
            const char *name,
1429
            struct repository *repo,
1430
            const struct object_id *oid,
1431
            void *data,
1432
            enum config_scope scope)
1433
0
{
1434
0
  enum object_type type;
1435
0
  char *buf;
1436
0
  unsigned long size;
1437
0
  int ret;
1438
1439
0
  buf = odb_read_object(repo->objects, oid, &type, &size);
1440
0
  if (!buf)
1441
0
    return error(_("unable to load config blob object '%s'"), name);
1442
0
  if (type != OBJ_BLOB) {
1443
0
    free(buf);
1444
0
    return error(_("reference '%s' does not point to a blob"), name);
1445
0
  }
1446
1447
0
  ret = git_config_from_mem(fn, CONFIG_ORIGIN_BLOB, name, buf, size,
1448
0
          data, scope, NULL);
1449
0
  free(buf);
1450
1451
0
  return ret;
1452
0
}
1453
1454
static int git_config_from_blob_ref(config_fn_t fn,
1455
            struct repository *repo,
1456
            const char *name,
1457
            void *data,
1458
            enum config_scope scope)
1459
0
{
1460
0
  struct object_id oid;
1461
1462
0
  if (repo_get_oid(repo, name, &oid) < 0)
1463
0
    return error(_("unable to resolve config blob '%s'"), name);
1464
0
  return git_config_from_blob_oid(fn, name, repo, &oid, data, scope);
1465
0
}
1466
1467
char *git_system_config(void)
1468
0
{
1469
0
  char *system_config = xstrdup_or_null(getenv("GIT_CONFIG_SYSTEM"));
1470
0
  if (!system_config)
1471
0
    system_config = system_path(ETC_GITCONFIG);
1472
0
  normalize_path_copy(system_config, system_config);
1473
0
  return system_config;
1474
0
}
1475
1476
char *git_global_config(void)
1477
0
{
1478
0
  char *user_config, *xdg_config;
1479
1480
0
  git_global_config_paths(&user_config, &xdg_config);
1481
0
  if (!user_config) {
1482
0
    free(xdg_config);
1483
0
    return NULL;
1484
0
  }
1485
1486
0
  if (access_or_warn(user_config, R_OK, 0) && xdg_config &&
1487
0
      !access_or_warn(xdg_config, R_OK, 0)) {
1488
0
    free(user_config);
1489
0
    return xdg_config;
1490
0
  } else {
1491
0
    free(xdg_config);
1492
0
    return user_config;
1493
0
  }
1494
0
}
1495
1496
void git_global_config_paths(char **user_out, char **xdg_out)
1497
0
{
1498
0
  char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
1499
0
  char *xdg_config = NULL;
1500
1501
0
  if (!user_config) {
1502
0
    user_config = interpolate_path("~/.gitconfig", 0);
1503
0
    xdg_config = xdg_config_home("config");
1504
0
  }
1505
1506
0
  *user_out = user_config;
1507
0
  *xdg_out = xdg_config;
1508
0
}
1509
1510
int git_config_system(void)
1511
0
{
1512
0
  return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
1513
0
}
1514
1515
static int do_git_config_sequence(const struct config_options *opts,
1516
          const struct repository *repo,
1517
          config_fn_t fn, void *data)
1518
0
{
1519
0
  int ret = 0;
1520
0
  char *system_config = git_system_config();
1521
0
  char *xdg_config = NULL;
1522
0
  char *user_config = NULL;
1523
0
  char *repo_config;
1524
0
  char *worktree_config;
1525
1526
  /*
1527
   * Ensure that either:
1528
   * - the git_dir and commondir are both set, or
1529
   * - the git_dir and commondir are both NULL
1530
   */
1531
0
  if (!opts->git_dir != !opts->commondir)
1532
0
    BUG("only one of commondir and git_dir is non-NULL");
1533
1534
0
  if (opts->commondir) {
1535
0
    repo_config = mkpathdup("%s/config", opts->commondir);
1536
0
    worktree_config = mkpathdup("%s/config.worktree", opts->git_dir);
1537
0
  } else {
1538
0
    repo_config = NULL;
1539
0
    worktree_config = NULL;
1540
0
  }
1541
1542
0
  if (git_config_system() && system_config &&
1543
0
      !access_or_die(system_config, R_OK,
1544
0
         opts->system_gently ? ACCESS_EACCES_OK : 0))
1545
0
    ret += git_config_from_file_with_options(fn, system_config,
1546
0
               data, CONFIG_SCOPE_SYSTEM,
1547
0
               NULL);
1548
1549
0
  git_global_config_paths(&user_config, &xdg_config);
1550
1551
0
  if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
1552
0
    ret += git_config_from_file_with_options(fn, xdg_config, data,
1553
0
               CONFIG_SCOPE_GLOBAL, NULL);
1554
1555
0
  if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
1556
0
    ret += git_config_from_file_with_options(fn, user_config, data,
1557
0
               CONFIG_SCOPE_GLOBAL, NULL);
1558
1559
0
  if (!opts->ignore_repo && repo_config &&
1560
0
      !access_or_die(repo_config, R_OK, 0))
1561
0
    ret += git_config_from_file_with_options(fn, repo_config, data,
1562
0
               CONFIG_SCOPE_LOCAL, NULL);
1563
1564
0
  if (!opts->ignore_worktree && worktree_config &&
1565
0
      repo && repo->repository_format_worktree_config &&
1566
0
      !access_or_die(worktree_config, R_OK, 0)) {
1567
0
      ret += git_config_from_file_with_options(fn, worktree_config, data,
1568
0
                 CONFIG_SCOPE_WORKTREE,
1569
0
                 NULL);
1570
0
  }
1571
1572
0
  if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
1573
0
    die(_("unable to parse command-line config"));
1574
1575
0
  free(system_config);
1576
0
  free(xdg_config);
1577
0
  free(user_config);
1578
0
  free(repo_config);
1579
0
  free(worktree_config);
1580
0
  return ret;
1581
0
}
1582
1583
int config_with_options(config_fn_t fn, void *data,
1584
      const struct git_config_source *config_source,
1585
      struct repository *repo,
1586
      const struct config_options *opts)
1587
0
{
1588
0
  struct config_include_data inc = CONFIG_INCLUDE_INIT;
1589
0
  int ret;
1590
1591
0
  if (opts->respect_includes) {
1592
0
    inc.fn = fn;
1593
0
    inc.data = data;
1594
0
    inc.opts = opts;
1595
0
    inc.repo = repo;
1596
0
    inc.config_source = config_source;
1597
0
    fn = git_config_include;
1598
0
    data = &inc;
1599
0
  }
1600
1601
  /*
1602
   * If we have a specific filename, use it. Otherwise, follow the
1603
   * regular lookup sequence.
1604
   */
1605
0
  if (config_source && config_source->use_stdin) {
1606
0
    ret = git_config_from_stdin(fn, data, config_source->scope);
1607
0
  } else if (config_source && config_source->file) {
1608
0
    ret = git_config_from_file_with_options(fn, config_source->file,
1609
0
              data, config_source->scope,
1610
0
              NULL);
1611
0
  } else if (config_source && config_source->blob) {
1612
0
    ret = git_config_from_blob_ref(fn, repo, config_source->blob,
1613
0
                 data, config_source->scope);
1614
0
  } else {
1615
0
    ret = do_git_config_sequence(opts, repo, fn, data);
1616
0
  }
1617
1618
0
  if (inc.remote_urls) {
1619
0
    string_list_clear(inc.remote_urls, 0);
1620
0
    FREE_AND_NULL(inc.remote_urls);
1621
0
  }
1622
0
  return ret;
1623
0
}
1624
1625
static void configset_iter(struct config_set *set, config_fn_t fn, void *data)
1626
0
{
1627
0
  int value_index;
1628
0
  struct string_list *values;
1629
0
  struct config_set_element *entry;
1630
0
  struct configset_list *list = &set->list;
1631
0
  struct config_context ctx = CONFIG_CONTEXT_INIT;
1632
1633
0
  for (size_t i = 0; i < list->nr; i++) {
1634
0
    entry = list->items[i].e;
1635
0
    value_index = list->items[i].value_index;
1636
0
    values = &entry->value_list;
1637
1638
0
    ctx.kvi = values->items[value_index].util;
1639
0
    if (fn(entry->key, values->items[value_index].string, &ctx, data) < 0)
1640
0
      git_die_config_linenr(entry->key,
1641
0
                ctx.kvi->filename,
1642
0
                ctx.kvi->linenr);
1643
0
  }
1644
0
}
1645
1646
void read_early_config(struct repository *repo, config_fn_t cb, void *data)
1647
0
{
1648
0
  struct config_options opts = {0};
1649
0
  struct strbuf commondir = STRBUF_INIT;
1650
0
  struct strbuf gitdir = STRBUF_INIT;
1651
1652
0
  opts.respect_includes = 1;
1653
1654
0
  if (repo && repo->gitdir) {
1655
0
    opts.commondir = repo_get_common_dir(repo);
1656
0
    opts.git_dir = repo_get_git_dir(repo);
1657
  /*
1658
   * When setup_git_directory() was not yet asked to discover the
1659
   * GIT_DIR, we ask discover_git_directory() to figure out whether there
1660
   * is any repository config we should use (but unlike
1661
   * setup_git_directory_gently(), no global state is changed, most
1662
   * notably, the current working directory is still the same after the
1663
   * call).
1664
   */
1665
0
  } else if (!discover_git_directory(&commondir, &gitdir)) {
1666
0
    opts.commondir = commondir.buf;
1667
0
    opts.git_dir = gitdir.buf;
1668
0
  }
1669
1670
0
  config_with_options(cb, data, NULL, NULL, &opts);
1671
1672
0
  strbuf_release(&commondir);
1673
0
  strbuf_release(&gitdir);
1674
0
}
1675
1676
void read_very_early_config(config_fn_t cb, void *data)
1677
0
{
1678
0
  struct config_options opts = { 0 };
1679
1680
0
  opts.respect_includes = 1;
1681
0
  opts.ignore_repo = 1;
1682
0
  opts.ignore_worktree = 1;
1683
0
  opts.ignore_cmdline = 1;
1684
0
  opts.system_gently = 1;
1685
1686
0
  config_with_options(cb, data, NULL, NULL, &opts);
1687
0
}
1688
1689
RESULT_MUST_BE_USED
1690
static int configset_find_element(struct config_set *set, const char *key,
1691
          struct config_set_element **dest)
1692
0
{
1693
0
  struct config_set_element k;
1694
0
  struct config_set_element *found_entry;
1695
0
  char *normalized_key;
1696
0
  int ret;
1697
1698
  /*
1699
   * `key` may come from the user, so normalize it before using it
1700
   * for querying entries from the hashmap.
1701
   */
1702
0
  ret = git_config_parse_key(key, &normalized_key, NULL);
1703
0
  if (ret)
1704
0
    return ret;
1705
1706
0
  hashmap_entry_init(&k.ent, strhash(normalized_key));
1707
0
  k.key = normalized_key;
1708
0
  found_entry = hashmap_get_entry(&set->config_hash, &k, ent, NULL);
1709
0
  free(normalized_key);
1710
0
  *dest = found_entry;
1711
0
  return 0;
1712
0
}
1713
1714
static int configset_add_value(const struct key_value_info *kvi_p,
1715
             struct config_set *set, const char *key,
1716
             const char *value)
1717
0
{
1718
0
  struct config_set_element *e;
1719
0
  struct string_list_item *si;
1720
0
  struct configset_list_item *l_item;
1721
0
  struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
1722
0
  int ret;
1723
1724
0
  ret = configset_find_element(set, key, &e);
1725
0
  if (ret)
1726
0
    return ret;
1727
  /*
1728
   * Since the keys are being fed by git_config*() callback mechanism, they
1729
   * are already normalized. So simply add them without any further munging.
1730
   */
1731
0
  if (!e) {
1732
0
    e = xmalloc(sizeof(*e));
1733
0
    hashmap_entry_init(&e->ent, strhash(key));
1734
0
    e->key = xstrdup(key);
1735
0
    string_list_init_dup(&e->value_list);
1736
0
    hashmap_add(&set->config_hash, &e->ent);
1737
0
  }
1738
0
  si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
1739
1740
0
  ALLOC_GROW(set->list.items, set->list.nr + 1, set->list.alloc);
1741
0
  l_item = &set->list.items[set->list.nr++];
1742
0
  l_item->e = e;
1743
0
  l_item->value_index = e->value_list.nr - 1;
1744
1745
0
  *kv_info = *kvi_p;
1746
0
  si->util = kv_info;
1747
1748
0
  return 0;
1749
0
}
1750
1751
static int config_set_element_cmp(const void *cmp_data UNUSED,
1752
          const struct hashmap_entry *eptr,
1753
          const struct hashmap_entry *entry_or_key,
1754
          const void *keydata UNUSED)
1755
0
{
1756
0
  const struct config_set_element *e1, *e2;
1757
1758
0
  e1 = container_of(eptr, const struct config_set_element, ent);
1759
0
  e2 = container_of(entry_or_key, const struct config_set_element, ent);
1760
1761
0
  return strcmp(e1->key, e2->key);
1762
0
}
1763
1764
void git_configset_init(struct config_set *set)
1765
0
{
1766
0
  hashmap_init(&set->config_hash, config_set_element_cmp, NULL, 0);
1767
0
  set->hash_initialized = 1;
1768
0
  set->list.nr = 0;
1769
0
  set->list.alloc = 0;
1770
0
  set->list.items = NULL;
1771
0
}
1772
1773
void git_configset_clear(struct config_set *set)
1774
0
{
1775
0
  struct config_set_element *entry;
1776
0
  struct hashmap_iter iter;
1777
0
  if (!set->hash_initialized)
1778
0
    return;
1779
1780
0
  hashmap_for_each_entry(&set->config_hash, &iter, entry,
1781
0
        ent /* member name */) {
1782
0
    free(entry->key);
1783
0
    string_list_clear(&entry->value_list, 1);
1784
0
  }
1785
0
  hashmap_clear_and_free(&set->config_hash, struct config_set_element, ent);
1786
0
  set->hash_initialized = 0;
1787
0
  free(set->list.items);
1788
0
  set->list.nr = 0;
1789
0
  set->list.alloc = 0;
1790
0
  set->list.items = NULL;
1791
0
}
1792
1793
static int config_set_callback(const char *key, const char *value,
1794
             const struct config_context *ctx,
1795
             void *cb)
1796
0
{
1797
0
  struct config_set *set = cb;
1798
0
  configset_add_value(ctx->kvi, set, key, value);
1799
0
  return 0;
1800
0
}
1801
1802
int git_configset_add_file(struct config_set *set, const char *filename)
1803
0
{
1804
0
  return git_config_from_file(config_set_callback, filename, set);
1805
0
}
1806
1807
int git_configset_get_value(struct config_set *set, const char *key,
1808
          const char **value, struct key_value_info *kvi)
1809
0
{
1810
0
  const struct string_list *values = NULL;
1811
0
  int ret;
1812
0
  struct string_list_item item;
1813
  /*
1814
   * Follows "last one wins" semantic, i.e., if there are multiple matches for the
1815
   * queried key in the files of the configset, the value returned will be the last
1816
   * value in the value list for that key.
1817
   */
1818
0
  if ((ret = git_configset_get_value_multi(set, key, &values)))
1819
0
    return ret;
1820
1821
0
  assert(values->nr > 0);
1822
0
  item = values->items[values->nr - 1];
1823
0
  *value = item.string;
1824
0
  if (kvi)
1825
0
    *kvi = *((struct key_value_info *)item.util);
1826
0
  return 0;
1827
0
}
1828
1829
int git_configset_get_value_multi(struct config_set *set, const char *key,
1830
          const struct string_list **dest)
1831
0
{
1832
0
  struct config_set_element *e;
1833
0
  int ret;
1834
1835
0
  if ((ret = configset_find_element(set, key, &e)))
1836
0
    return ret;
1837
0
  else if (!e)
1838
0
    return 1;
1839
0
  *dest = &e->value_list;
1840
1841
0
  return 0;
1842
0
}
1843
1844
static int check_multi_string(struct string_list_item *item, void *util)
1845
0
{
1846
0
  return item->string ? 0 : config_error_nonbool(util);
1847
0
}
1848
1849
int git_configset_get_string_multi(struct config_set *cs, const char *key,
1850
           const struct string_list **dest)
1851
0
{
1852
0
  int ret;
1853
1854
0
  if ((ret = git_configset_get_value_multi(cs, key, dest)))
1855
0
    return ret;
1856
0
  if ((ret = for_each_string_list((struct string_list *)*dest,
1857
0
          check_multi_string, (void *)key)))
1858
0
    return ret;
1859
1860
0
  return 0;
1861
0
}
1862
1863
int git_configset_get(struct config_set *set, const char *key)
1864
0
{
1865
0
  struct config_set_element *e;
1866
0
  int ret;
1867
1868
0
  if ((ret = configset_find_element(set, key, &e)))
1869
0
    return ret;
1870
0
  else if (!e)
1871
0
    return 1;
1872
0
  return 0;
1873
0
}
1874
1875
int git_configset_get_string(struct config_set *set, const char *key, char **dest)
1876
0
{
1877
0
  const char *value;
1878
0
  if (!git_configset_get_value(set, key, &value, NULL))
1879
0
    return git_config_string(dest, key, value);
1880
0
  else
1881
0
    return 1;
1882
0
}
1883
1884
static int git_configset_get_string_tmp(struct config_set *set, const char *key,
1885
          const char **dest)
1886
0
{
1887
0
  const char *value;
1888
0
  if (!git_configset_get_value(set, key, &value, NULL)) {
1889
0
    if (!value)
1890
0
      return config_error_nonbool(key);
1891
0
    *dest = value;
1892
0
    return 0;
1893
0
  } else {
1894
0
    return 1;
1895
0
  }
1896
0
}
1897
1898
int git_configset_get_int(struct config_set *set, const char *key, int *dest)
1899
0
{
1900
0
  const char *value;
1901
0
  struct key_value_info kvi;
1902
1903
0
  if (!git_configset_get_value(set, key, &value, &kvi)) {
1904
0
    *dest = git_config_int(key, value, &kvi);
1905
0
    return 0;
1906
0
  } else
1907
0
    return 1;
1908
0
}
1909
1910
int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest)
1911
0
{
1912
0
  const char *value;
1913
0
  struct key_value_info kvi;
1914
1915
0
  if (!git_configset_get_value(set, key, &value, &kvi)) {
1916
0
    *dest = git_config_ulong(key, value, &kvi);
1917
0
    return 0;
1918
0
  } else
1919
0
    return 1;
1920
0
}
1921
1922
int git_configset_get_bool(struct config_set *set, const char *key, int *dest)
1923
0
{
1924
0
  const char *value;
1925
0
  if (!git_configset_get_value(set, key, &value, NULL)) {
1926
0
    *dest = git_config_bool(key, value);
1927
0
    return 0;
1928
0
  } else
1929
0
    return 1;
1930
0
}
1931
1932
int git_configset_get_bool_or_int(struct config_set *set, const char *key,
1933
        int *is_bool, int *dest)
1934
0
{
1935
0
  const char *value;
1936
0
  struct key_value_info kvi;
1937
1938
0
  if (!git_configset_get_value(set, key, &value, &kvi)) {
1939
0
    *dest = git_config_bool_or_int(key, value, &kvi, is_bool);
1940
0
    return 0;
1941
0
  } else
1942
0
    return 1;
1943
0
}
1944
1945
int git_configset_get_maybe_bool(struct config_set *set, const char *key, int *dest)
1946
0
{
1947
0
  const char *value;
1948
0
  if (!git_configset_get_value(set, key, &value, NULL)) {
1949
0
    *dest = git_parse_maybe_bool(value);
1950
0
    if (*dest == -1)
1951
0
      return -1;
1952
0
    return 0;
1953
0
  } else
1954
0
    return 1;
1955
0
}
1956
1957
static int git_configset_get_pathname(struct config_set *set, const char *key, char **dest)
1958
0
{
1959
0
  const char *value;
1960
0
  if (!git_configset_get_value(set, key, &value, NULL))
1961
0
    return git_config_pathname(dest, key, value);
1962
0
  else
1963
0
    return 1;
1964
0
}
1965
1966
struct comment_char_config {
1967
  unsigned last_key_id;
1968
  bool auto_set;
1969
  bool auto_set_in_file;
1970
  struct strintmap key_flags;
1971
  size_t alloc, nr;
1972
  struct comment_char_config_item {
1973
    unsigned key_id;
1974
    char *path;
1975
    enum config_scope scope;
1976
  } *item;
1977
};
1978
1979
0
#define COMMENT_CHAR_CFG_INIT {     \
1980
0
    .key_flags = STRINTMAP_INIT, \
1981
0
  }
1982
1983
static void comment_char_config_release(struct comment_char_config *config)
1984
0
{
1985
0
  strintmap_clear(&config->key_flags);
1986
0
  for (size_t i = 0; i < config->nr; i++)
1987
0
    free(config->item[i].path);
1988
0
  free(config->item);
1989
0
}
1990
1991
/* Used to track whether the key occurs more than once in a given file */
1992
0
#define KEY_SEEN_ONCE 1u
1993
0
#define KEY_SEEN_TWICE 2u
1994
0
#define COMMENT_KEY_SHIFT(id) (2 * (id))
1995
0
#define COMMENT_KEY_MASK(id) (3u << COMMENT_KEY_SHIFT(id))
1996
1997
static void set_comment_key_flags(struct comment_char_config *config,
1998
          const char *path, unsigned id, unsigned value)
1999
0
{
2000
0
  unsigned old = strintmap_get(&config->key_flags, path);
2001
0
  unsigned new = (old & ~COMMENT_KEY_MASK(id)) |
2002
0
        value << COMMENT_KEY_SHIFT(id);
2003
2004
0
  strintmap_set(&config->key_flags, path, new);
2005
0
}
2006
2007
static unsigned get_comment_key_flags(struct comment_char_config *config,
2008
              const char *path, unsigned id)
2009
0
{
2010
0
  unsigned value = strintmap_get(&config->key_flags, path);
2011
2012
0
  return (value & COMMENT_KEY_MASK(id)) >> COMMENT_KEY_SHIFT(id);
2013
0
}
2014
2015
static const char *comment_key_name(unsigned id)
2016
0
{
2017
0
  static const char *name[] = {
2018
0
    "core.commentChar",
2019
0
    "core.commentString",
2020
0
  };
2021
2022
0
  if (id >= ARRAY_SIZE(name))
2023
0
    BUG("invalid comment key id");
2024
2025
0
  return name[id];
2026
0
}
2027
2028
static void comment_char_callback(const char *key, const char *value,
2029
          const struct config_context *ctx, void *data)
2030
0
{
2031
0
  struct comment_char_config *config = data;
2032
0
  const struct key_value_info *kvi = ctx->kvi;
2033
0
  unsigned key_id;
2034
2035
0
  if (!strcmp(key, "core.commentchar"))
2036
0
    key_id = 0;
2037
0
  else if (!strcmp(key, "core.commentstring"))
2038
0
    key_id = 1;
2039
0
  else
2040
0
    return;
2041
2042
0
  config->last_key_id = key_id;
2043
0
  config->auto_set = value && !strcmp(value, "auto");
2044
0
  if (kvi->origin_type != CONFIG_ORIGIN_FILE) {
2045
0
    return;
2046
0
  } else if (get_comment_key_flags(config, kvi->filename, key_id)) {
2047
0
    set_comment_key_flags(config, kvi->filename, key_id,
2048
0
              KEY_SEEN_TWICE);
2049
0
  } else {
2050
0
    struct comment_char_config_item *item;
2051
2052
0
    ALLOC_GROW_BY(config->item, config->nr, 1, config->alloc);
2053
0
    item = &config->item[config->nr - 1];
2054
0
    item->key_id = key_id;
2055
0
    item->scope = kvi->scope;
2056
0
    item->path = xstrdup(kvi->filename);
2057
0
    set_comment_key_flags(config, kvi->filename, key_id,
2058
0
              KEY_SEEN_ONCE);
2059
0
  }
2060
0
  config->auto_set_in_file = config->auto_set;
2061
0
}
2062
2063
static void add_config_scope_arg(struct repository *repo, struct strbuf *buf,
2064
         struct comment_char_config_item *item)
2065
0
{
2066
0
  char *global_config = git_global_config();
2067
0
  char *system_config = git_system_config();
2068
2069
0
  if (item->scope == CONFIG_SCOPE_SYSTEM && access(item->path, W_OK)) {
2070
    /*
2071
     * If the user cannot write to the system config recommend
2072
     * setting the global config instead.
2073
     */
2074
0
    strbuf_addstr(buf, "--global ");
2075
0
  } else if (fspatheq(item->path, system_config)) {
2076
0
    strbuf_addstr(buf, "--system ");
2077
0
  } else if (fspatheq(item->path, global_config)) {
2078
0
    strbuf_addstr(buf, "--global ");
2079
0
  } else if (fspatheq(item->path,
2080
0
          mkpath("%s/config",
2081
0
           repo_get_git_dir(repo)))) {
2082
0
    ; /* --local is the default */
2083
0
  } else if (fspatheq(item->path,
2084
0
          mkpath("%s/config.worktree",
2085
0
           repo_get_common_dir(repo)))) {
2086
0
    strbuf_addstr(buf, "--worktree ");
2087
0
  } else {
2088
0
    const char *path = item->path;
2089
0
    const char *home = getenv("HOME");
2090
2091
0
    strbuf_addstr(buf, "--file ");
2092
0
    if (home && !fspathncmp(path, home, strlen(home))) {
2093
0
      path += strlen(home);
2094
0
      if (!fspathncmp(path, "/", 1))
2095
0
        path++;
2096
0
      strbuf_addstr(buf, "~/");
2097
0
    }
2098
0
    sq_quote_buf_pretty(buf, path);
2099
0
    strbuf_addch(buf, ' ');
2100
0
  }
2101
2102
0
  free(global_config);
2103
0
  free(system_config);
2104
0
}
2105
2106
static bool can_unset_comment_char_config(struct comment_char_config *config)
2107
0
{
2108
0
  for (size_t i = 0; i < config->nr; i++) {
2109
0
    struct comment_char_config_item *item = &config->item[i];
2110
2111
0
    if (item->scope == CONFIG_SCOPE_SYSTEM &&
2112
0
        access(item->path, W_OK))
2113
0
      return false;
2114
0
  }
2115
2116
0
  return true;
2117
0
}
2118
2119
static void add_unset_auto_comment_char_advice(struct repository *repo,
2120
                 struct comment_char_config *config)
2121
0
{
2122
0
  struct strbuf buf = STRBUF_INIT;
2123
2124
0
  if (!can_unset_comment_char_config(config))
2125
0
    return;
2126
2127
0
  for (size_t i = 0; i < config->nr; i++) {
2128
0
    struct comment_char_config_item *item = &config->item[i];
2129
2130
0
    strbuf_addstr(&buf, "    git config unset ");
2131
0
    add_config_scope_arg(repo, &buf, item);
2132
0
    if (get_comment_key_flags(config, item->path, item->key_id) == KEY_SEEN_TWICE)
2133
0
      strbuf_addstr(&buf, "--all ");
2134
0
    strbuf_addf(&buf, "%s\n", comment_key_name(item->key_id));
2135
0
  }
2136
0
  advise(_("\nTo use the default comment string (#) please run\n\n%s"),
2137
0
         buf.buf);
2138
0
  strbuf_release(&buf);
2139
0
}
2140
2141
static void add_comment_char_advice(struct repository *repo,
2142
            struct comment_char_config *config)
2143
0
{
2144
0
  struct strbuf buf = STRBUF_INIT;
2145
0
  struct comment_char_config_item *item;
2146
  /* TRANSLATORS this is a place holder for the value of core.commentString */
2147
0
  const char *placeholder = _("<comment string>");
2148
2149
  /*
2150
   * If auto is set in the last file that we saw advise the user how to
2151
   * update their config.
2152
   */
2153
0
  if (!config->auto_set_in_file)
2154
0
    return;
2155
2156
0
  add_unset_auto_comment_char_advice(repo, config);
2157
0
  item = &config->item[config->nr - 1];
2158
0
  strbuf_reset(&buf);
2159
0
  strbuf_addstr(&buf, "    git config set ");
2160
0
  add_config_scope_arg(repo, &buf, item);
2161
0
  strbuf_addf(&buf, "%s %s\n", comment_key_name(item->key_id),
2162
0
        placeholder);
2163
0
  advise(_("\nTo set a custom comment string please run\n\n"
2164
0
     "%s\nwhere '%s' is the string you wish to use.\n"),
2165
0
         buf.buf, placeholder);
2166
0
  strbuf_release(&buf);
2167
0
}
2168
2169
#undef KEY_SEEN_ONCE
2170
#undef KEY_SEEN_TWICE
2171
#undef COMMENT_KEY_SHIFT
2172
#undef COMMENT_KEY_MASK
2173
2174
struct repo_config {
2175
  struct repository *repo;
2176
  struct comment_char_config comment_char_config;
2177
};
2178
2179
0
#define REPO_CONFIG_INIT(repo_) {       \
2180
0
    .comment_char_config = COMMENT_CHAR_CFG_INIT, \
2181
0
    .repo = repo_,          \
2182
0
  };
2183
2184
static void repo_config_release(struct repo_config *config)
2185
0
{
2186
0
  comment_char_config_release(&config->comment_char_config);
2187
0
}
2188
2189
#ifdef WITH_BREAKING_CHANGES
2190
static void check_auto_comment_char_config(struct repository *repo,
2191
             struct comment_char_config *config)
2192
{
2193
  if (!config->auto_set)
2194
    return;
2195
2196
  die_message(_("Support for '%s=auto' has been removed in Git 3.0"),
2197
        comment_key_name(config->last_key_id));
2198
  add_comment_char_advice(repo, config);
2199
  die(NULL);
2200
}
2201
#else
2202
static void check_auto_comment_char_config(struct repository *repo,
2203
             struct comment_char_config *config)
2204
0
{
2205
0
  extern bool warn_on_auto_comment_char;
2206
0
  const char *DEPRECATED_CONFIG_ENV =
2207
0
        "GIT_AUTO_COMMENT_CHAR_CONFIG_WARNING_GIVEN";
2208
2209
0
  if (!config->auto_set || !warn_on_auto_comment_char)
2210
0
    return;
2211
2212
  /*
2213
   * Use an environment variable to ensure that subprocesses do not repeat
2214
   * the warning.
2215
   */
2216
0
  if (git_env_bool(DEPRECATED_CONFIG_ENV, false))
2217
0
    return;
2218
2219
0
  setenv(DEPRECATED_CONFIG_ENV, "true", true);
2220
2221
0
  warning(_("Support for '%s=auto' is deprecated and will be removed in "
2222
0
      "Git 3.0"), comment_key_name(config->last_key_id));
2223
0
  add_comment_char_advice(repo, config);
2224
0
}
2225
#endif /* WITH_BREAKING_CHANGES */
2226
2227
static void check_deprecated_config(struct repo_config *config)
2228
0
{
2229
0
  if (!config->repo->check_deprecated_config)
2230
0
      return;
2231
2232
0
  check_auto_comment_char_config(config->repo,
2233
0
               &config->comment_char_config);
2234
0
}
2235
2236
static int repo_config_callback(const char *key, const char *value,
2237
        const struct config_context *ctx, void *data)
2238
0
{
2239
0
  struct repo_config *config = data;
2240
2241
0
  comment_char_callback(key, value, ctx, &config->comment_char_config);
2242
0
  return config_set_callback(key, value, ctx, config->repo->config);
2243
0
}
2244
2245
/* Functions use to read configuration from a repository */
2246
static void repo_read_config(struct repository *repo)
2247
0
{
2248
0
  struct config_options opts = { 0 };
2249
0
  struct repo_config config = REPO_CONFIG_INIT(repo);
2250
2251
0
  opts.respect_includes = 1;
2252
0
  opts.commondir = repo->commondir;
2253
0
  opts.git_dir = repo->gitdir;
2254
2255
0
  if (!repo->config)
2256
0
    CALLOC_ARRAY(repo->config, 1);
2257
0
  else
2258
0
    git_configset_clear(repo->config);
2259
2260
0
  git_configset_init(repo->config);
2261
0
  if (config_with_options(repo_config_callback, &config, NULL, repo,
2262
0
        &opts) < 0)
2263
    /*
2264
     * config_with_options() normally returns only
2265
     * zero, as most errors are fatal, and
2266
     * non-fatal potential errors are guarded by "if"
2267
     * statements that are entered only when no error is
2268
     * possible.
2269
     *
2270
     * If we ever encounter a non-fatal error, it means
2271
     * something went really wrong and we should stop
2272
     * immediately.
2273
     */
2274
0
    die(_("unknown error occurred while reading the configuration files"));
2275
0
  check_deprecated_config(&config);
2276
0
  repo_config_release(&config);
2277
0
}
2278
2279
static void git_config_check_init(struct repository *repo)
2280
0
{
2281
0
  if (repo->config && repo->config->hash_initialized)
2282
0
    return;
2283
0
  repo_read_config(repo);
2284
0
}
2285
2286
void repo_config_clear(struct repository *repo)
2287
0
{
2288
0
  if (!repo->config || !repo->config->hash_initialized)
2289
0
    return;
2290
0
  git_configset_clear(repo->config);
2291
0
}
2292
2293
void repo_config(struct repository *repo, config_fn_t fn, void *data)
2294
0
{
2295
0
  if (!repo) {
2296
0
    read_very_early_config(fn, data);
2297
0
    return;
2298
0
  }
2299
0
  git_config_check_init(repo);
2300
0
  configset_iter(repo->config, fn, data);
2301
0
}
2302
2303
int repo_config_get(struct repository *repo, const char *key)
2304
0
{
2305
0
  git_config_check_init(repo);
2306
0
  return git_configset_get(repo->config, key);
2307
0
}
2308
2309
int repo_config_get_value(struct repository *repo,
2310
        const char *key, const char **value)
2311
0
{
2312
0
  git_config_check_init(repo);
2313
0
  return git_configset_get_value(repo->config, key, value, NULL);
2314
0
}
2315
2316
int repo_config_get_value_multi(struct repository *repo, const char *key,
2317
        const struct string_list **dest)
2318
0
{
2319
0
  git_config_check_init(repo);
2320
0
  return git_configset_get_value_multi(repo->config, key, dest);
2321
0
}
2322
2323
int repo_config_get_string_multi(struct repository *repo, const char *key,
2324
         const struct string_list **dest)
2325
0
{
2326
0
  git_config_check_init(repo);
2327
0
  return git_configset_get_string_multi(repo->config, key, dest);
2328
0
}
2329
2330
int repo_config_get_string(struct repository *repo,
2331
         const char *key, char **dest)
2332
0
{
2333
0
  int ret;
2334
0
  git_config_check_init(repo);
2335
0
  ret = git_configset_get_string(repo->config, key, dest);
2336
0
  if (ret < 0)
2337
0
    git_die_config(repo, key, NULL);
2338
0
  return ret;
2339
0
}
2340
2341
int repo_config_get_string_tmp(struct repository *repo,
2342
             const char *key, const char **dest)
2343
0
{
2344
0
  int ret;
2345
0
  git_config_check_init(repo);
2346
0
  ret = git_configset_get_string_tmp(repo->config, key, dest);
2347
0
  if (ret < 0)
2348
0
    git_die_config(repo, key, NULL);
2349
0
  return ret;
2350
0
}
2351
2352
int repo_config_get_int(struct repository *repo,
2353
      const char *key, int *dest)
2354
0
{
2355
0
  git_config_check_init(repo);
2356
0
  return git_configset_get_int(repo->config, key, dest);
2357
0
}
2358
2359
int repo_config_get_ulong(struct repository *repo,
2360
        const char *key, unsigned long *dest)
2361
0
{
2362
0
  git_config_check_init(repo);
2363
0
  return git_configset_get_ulong(repo->config, key, dest);
2364
0
}
2365
2366
int repo_config_get_bool(struct repository *repo,
2367
       const char *key, int *dest)
2368
0
{
2369
0
  git_config_check_init(repo);
2370
0
  return git_configset_get_bool(repo->config, key, dest);
2371
0
}
2372
2373
int repo_config_get_bool_or_int(struct repository *repo,
2374
        const char *key, int *is_bool, int *dest)
2375
0
{
2376
0
  git_config_check_init(repo);
2377
0
  return git_configset_get_bool_or_int(repo->config, key, is_bool, dest);
2378
0
}
2379
2380
int repo_config_get_maybe_bool(struct repository *repo,
2381
             const char *key, int *dest)
2382
0
{
2383
0
  git_config_check_init(repo);
2384
0
  return git_configset_get_maybe_bool(repo->config, key, dest);
2385
0
}
2386
2387
int repo_config_get_pathname(struct repository *repo,
2388
           const char *key, char **dest)
2389
0
{
2390
0
  int ret;
2391
0
  git_config_check_init(repo);
2392
0
  ret = git_configset_get_pathname(repo->config, key, dest);
2393
0
  if (ret < 0)
2394
0
    git_die_config(repo, key, NULL);
2395
0
  return ret;
2396
0
}
2397
2398
/* Read values into protected_config. */
2399
static void read_protected_config(void)
2400
0
{
2401
0
  struct config_options opts = {
2402
0
    .respect_includes = 1,
2403
0
    .ignore_repo = 1,
2404
0
    .ignore_worktree = 1,
2405
0
    .system_gently = 1,
2406
0
  };
2407
2408
0
  git_configset_init(&protected_config);
2409
0
  config_with_options(config_set_callback, &protected_config, NULL,
2410
0
          NULL, &opts);
2411
0
}
2412
2413
void git_protected_config(config_fn_t fn, void *data)
2414
0
{
2415
0
  if (!protected_config.hash_initialized)
2416
0
    read_protected_config();
2417
0
  configset_iter(&protected_config, fn, data);
2418
0
}
2419
2420
int repo_config_get_expiry(struct repository *r, const char *key, char **output)
2421
0
{
2422
0
  int ret = repo_config_get_string(r, key, output);
2423
2424
0
  if (ret)
2425
0
    return ret;
2426
0
  if (strcmp(*output, "now")) {
2427
0
    timestamp_t now = approxidate("now");
2428
0
    if (approxidate(*output) >= now)
2429
0
      git_die_config(r, key, _("Invalid %s: '%s'"), key, *output);
2430
0
  }
2431
0
  return ret;
2432
0
}
2433
2434
int repo_config_get_expiry_in_days(struct repository *r, const char *key,
2435
           timestamp_t *expiry, timestamp_t now)
2436
0
{
2437
0
  const char *expiry_string;
2438
0
  int days;
2439
0
  timestamp_t when;
2440
2441
0
  if (repo_config_get_string_tmp(r, key, &expiry_string))
2442
0
    return 1; /* no such thing */
2443
2444
0
  if (git_parse_int(expiry_string, &days)) {
2445
0
    const intmax_t scale = 86400;
2446
0
    *expiry = now - days * scale;
2447
0
    return 0;
2448
0
  }
2449
2450
0
  if (!parse_expiry_date(expiry_string, &when)) {
2451
0
    *expiry = when;
2452
0
    return 0;
2453
0
  }
2454
0
  return -1; /* thing exists but cannot be parsed */
2455
0
}
2456
2457
int repo_config_get_split_index(struct repository *r)
2458
0
{
2459
0
  int val;
2460
2461
0
  if (!repo_config_get_maybe_bool(r, "core.splitindex", &val))
2462
0
    return val;
2463
2464
0
  return -1; /* default value */
2465
0
}
2466
2467
int repo_config_get_max_percent_split_change(struct repository *r)
2468
0
{
2469
0
  int val = -1;
2470
2471
0
  if (!repo_config_get_int(r, "splitindex.maxpercentchange", &val)) {
2472
0
    if (0 <= val && val <= 100)
2473
0
      return val;
2474
2475
0
    return error(_("splitIndex.maxPercentChange value '%d' "
2476
0
             "should be between 0 and 100"), val);
2477
0
  }
2478
2479
0
  return -1; /* default value */
2480
0
}
2481
2482
int repo_config_get_index_threads(struct repository *r, int *dest)
2483
0
{
2484
0
  int is_bool, val;
2485
2486
0
  val = git_env_ulong("GIT_TEST_INDEX_THREADS", 0);
2487
0
  if (val) {
2488
0
    *dest = val;
2489
0
    return 0;
2490
0
  }
2491
2492
0
  if (!repo_config_get_bool_or_int(r, "index.threads", &is_bool, &val)) {
2493
0
    if (is_bool)
2494
0
      *dest = val ? 0 : 1;
2495
0
    else
2496
0
      *dest = val;
2497
0
    return 0;
2498
0
  }
2499
2500
0
  return 1;
2501
0
}
2502
2503
NORETURN
2504
void git_die_config_linenr(const char *key, const char *filename, int linenr)
2505
0
{
2506
0
  if (!filename)
2507
0
    die(_("unable to parse '%s' from command-line config"), key);
2508
0
  else
2509
0
    die(_("bad config variable '%s' in file '%s' at line %d"),
2510
0
        key, filename, linenr);
2511
0
}
2512
2513
void git_die_config(struct repository *r, const char *key, const char *err, ...)
2514
0
{
2515
0
  const struct string_list *values;
2516
0
  struct key_value_info *kv_info;
2517
0
  report_fn error_fn = get_error_routine();
2518
2519
0
  if (err) {
2520
0
    va_list params;
2521
0
    va_start(params, err);
2522
0
    error_fn(err, params);
2523
0
    va_end(params);
2524
0
  }
2525
0
  if (repo_config_get_value_multi(r, key, &values))
2526
0
    BUG("for key '%s' we must have a value to report on", key);
2527
0
  kv_info = values->items[values->nr - 1].util;
2528
0
  git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
2529
0
}
2530
2531
/*
2532
 * Find all the stuff for repo_config_set() below.
2533
 */
2534
2535
struct config_store_data {
2536
  size_t baselen;
2537
  char *key;
2538
  int do_not_match;
2539
  const char *fixed_value;
2540
  regex_t *value_pattern;
2541
  int multi_replace;
2542
  struct {
2543
    size_t begin, end;
2544
    enum config_event_t type;
2545
    int is_keys_section;
2546
  } *parsed;
2547
  unsigned int parsed_nr, parsed_alloc, *seen, seen_nr, seen_alloc;
2548
  unsigned int key_seen:1, section_seen:1, is_keys_section:1;
2549
};
2550
0
#define CONFIG_STORE_INIT { 0 }
2551
2552
static void config_store_data_clear(struct config_store_data *store)
2553
0
{
2554
0
  free(store->key);
2555
0
  if (store->value_pattern != NULL &&
2556
0
      store->value_pattern != CONFIG_REGEX_NONE) {
2557
0
    regfree(store->value_pattern);
2558
0
    free(store->value_pattern);
2559
0
  }
2560
0
  free(store->parsed);
2561
0
  free(store->seen);
2562
0
  memset(store, 0, sizeof(*store));
2563
0
}
2564
2565
static int matches(const char *key, const char *value,
2566
       const struct config_store_data *store)
2567
0
{
2568
0
  if (strcmp(key, store->key))
2569
0
    return 0; /* not ours */
2570
0
  if (store->fixed_value && value)
2571
0
    return !strcmp(store->fixed_value, value);
2572
0
  if (!store->value_pattern)
2573
0
    return 1; /* always matches */
2574
0
  if (store->value_pattern == CONFIG_REGEX_NONE)
2575
0
    return 0; /* never matches */
2576
2577
0
  return store->do_not_match ^
2578
0
    (value && !regexec(store->value_pattern, value, 0, NULL, 0));
2579
0
}
2580
2581
static int store_aux_event(enum config_event_t type, size_t begin, size_t end,
2582
         struct config_source *cs, void *data)
2583
0
{
2584
0
  struct config_store_data *store = data;
2585
2586
0
  ALLOC_GROW(store->parsed, store->parsed_nr + 1, store->parsed_alloc);
2587
0
  store->parsed[store->parsed_nr].begin = begin;
2588
0
  store->parsed[store->parsed_nr].end = end;
2589
0
  store->parsed[store->parsed_nr].type = type;
2590
2591
0
  if (type == CONFIG_EVENT_SECTION) {
2592
0
    int (*cmpfn)(const char *, const char *, size_t);
2593
2594
0
    if (cs->var.len < 2 || cs->var.buf[cs->var.len - 1] != '.')
2595
0
      return error(_("invalid section name '%s'"), cs->var.buf);
2596
2597
0
    if (cs->subsection_case_sensitive)
2598
0
      cmpfn = strncasecmp;
2599
0
    else
2600
0
      cmpfn = strncmp;
2601
2602
    /* Is this the section we were looking for? */
2603
0
    store->is_keys_section =
2604
0
      store->parsed[store->parsed_nr].is_keys_section =
2605
0
      cs->var.len - 1 == store->baselen &&
2606
0
      !cmpfn(cs->var.buf, store->key, store->baselen);
2607
0
    if (store->is_keys_section) {
2608
0
      store->section_seen = 1;
2609
0
      ALLOC_GROW(store->seen, store->seen_nr + 1,
2610
0
           store->seen_alloc);
2611
0
      store->seen[store->seen_nr] = store->parsed_nr;
2612
0
    }
2613
0
  }
2614
2615
0
  store->parsed_nr++;
2616
2617
0
  return 0;
2618
0
}
2619
2620
static int store_aux(const char *key, const char *value,
2621
         const struct config_context *ctx UNUSED, void *cb)
2622
0
{
2623
0
  struct config_store_data *store = cb;
2624
2625
0
  if (store->key_seen) {
2626
0
    if (matches(key, value, store)) {
2627
0
      if (store->seen_nr == 1 && store->multi_replace == 0) {
2628
0
        warning(_("%s has multiple values"), key);
2629
0
      }
2630
2631
0
      ALLOC_GROW(store->seen, store->seen_nr + 1,
2632
0
           store->seen_alloc);
2633
2634
0
      store->seen[store->seen_nr] = store->parsed_nr;
2635
0
      store->seen_nr++;
2636
0
    }
2637
0
  } else if (store->is_keys_section) {
2638
    /*
2639
     * Do not increment matches yet: this may not be a match, but we
2640
     * are in the desired section.
2641
     */
2642
0
    ALLOC_GROW(store->seen, store->seen_nr + 1, store->seen_alloc);
2643
0
    store->seen[store->seen_nr] = store->parsed_nr;
2644
0
    store->section_seen = 1;
2645
2646
0
    if (matches(key, value, store)) {
2647
0
      store->seen_nr++;
2648
0
      store->key_seen = 1;
2649
0
    }
2650
0
  }
2651
2652
0
  return 0;
2653
0
}
2654
2655
static int write_error(const char *filename)
2656
0
{
2657
0
  error(_("failed to write new configuration file %s"), filename);
2658
2659
  /* Same error code as "failed to rename". */
2660
0
  return 4;
2661
0
}
2662
2663
static struct strbuf store_create_section(const char *key,
2664
            const struct config_store_data *store)
2665
0
{
2666
0
  const char *dot;
2667
0
  size_t i;
2668
0
  struct strbuf sb = STRBUF_INIT;
2669
2670
0
  dot = memchr(key, '.', store->baselen);
2671
0
  if (dot) {
2672
0
    strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
2673
0
    for (i = dot - key + 1; i < store->baselen; i++) {
2674
0
      if (key[i] == '"' || key[i] == '\\')
2675
0
        strbuf_addch(&sb, '\\');
2676
0
      strbuf_addch(&sb, key[i]);
2677
0
    }
2678
0
    strbuf_addstr(&sb, "\"]\n");
2679
0
  } else {
2680
0
    strbuf_addch(&sb, '[');
2681
0
    strbuf_add(&sb, key, store->baselen);
2682
0
    strbuf_addstr(&sb, "]\n");
2683
0
  }
2684
2685
0
  return sb;
2686
0
}
2687
2688
static ssize_t write_section(int fd, const char *key,
2689
           const struct config_store_data *store)
2690
0
{
2691
0
  struct strbuf sb = store_create_section(key, store);
2692
0
  ssize_t ret;
2693
2694
0
  ret = write_in_full(fd, sb.buf, sb.len);
2695
0
  strbuf_release(&sb);
2696
2697
0
  return ret;
2698
0
}
2699
2700
static ssize_t write_pair(int fd, const char *key, const char *value,
2701
        const char *comment,
2702
        const struct config_store_data *store)
2703
0
{
2704
0
  int i;
2705
0
  ssize_t ret;
2706
0
  const char *quote = "";
2707
0
  struct strbuf sb = STRBUF_INIT;
2708
2709
  /*
2710
   * Check to see if the value needs to be surrounded with a dq pair.
2711
   * Note that problematic characters are always backslash-quoted; this
2712
   * check is about not losing leading or trailing SP and strings that
2713
   * follow beginning-of-comment characters (i.e. ';' and '#') by the
2714
   * configuration parser.
2715
   */
2716
0
  if (value[0] == ' ')
2717
0
    quote = "\"";
2718
0
  for (i = 0; value[i]; i++)
2719
0
    if (value[i] == ';' || value[i] == '#' || value[i] == '\r')
2720
0
      quote = "\"";
2721
0
  if (i && value[i - 1] == ' ')
2722
0
    quote = "\"";
2723
2724
0
  strbuf_addf(&sb, "\t%s = %s", key + store->baselen + 1, quote);
2725
2726
0
  for (i = 0; value[i]; i++)
2727
0
    switch (value[i]) {
2728
0
    case '\n':
2729
0
      strbuf_addstr(&sb, "\\n");
2730
0
      break;
2731
0
    case '\t':
2732
0
      strbuf_addstr(&sb, "\\t");
2733
0
      break;
2734
0
    case '"':
2735
0
    case '\\':
2736
0
      strbuf_addch(&sb, '\\');
2737
      /* fallthrough */
2738
0
    default:
2739
0
      strbuf_addch(&sb, value[i]);
2740
0
      break;
2741
0
    }
2742
2743
0
  if (comment)
2744
0
    strbuf_addf(&sb, "%s%s\n", quote, comment);
2745
0
  else
2746
0
    strbuf_addf(&sb, "%s\n", quote);
2747
2748
0
  ret = write_in_full(fd, sb.buf, sb.len);
2749
0
  strbuf_release(&sb);
2750
2751
0
  return ret;
2752
0
}
2753
2754
/*
2755
 * If we are about to unset the last key(s) in a section, and if there are
2756
 * no comments surrounding (or included in) the section, we will want to
2757
 * extend begin/end to remove the entire section.
2758
 *
2759
 * Note: the parameter `seen_ptr` points to the index into the store.seen
2760
 * array.  * This index may be incremented if a section has more than one
2761
 * entry (which all are to be removed).
2762
 */
2763
static void maybe_remove_section(struct config_store_data *store,
2764
         size_t *begin_offset, size_t *end_offset,
2765
         unsigned *seen_ptr)
2766
0
{
2767
0
  size_t begin;
2768
0
  int section_seen = 0;
2769
0
  unsigned int i, seen;
2770
2771
  /*
2772
   * First, ensure that this is the first key, and that there are no
2773
   * comments before the entry nor before the section header.
2774
   */
2775
0
  seen = *seen_ptr;
2776
0
  for (i = store->seen[seen]; i > 0; i--) {
2777
0
    enum config_event_t type = store->parsed[i - 1].type;
2778
2779
0
    if (type == CONFIG_EVENT_COMMENT)
2780
      /* There is a comment before this entry or section */
2781
0
      return;
2782
0
    if (type == CONFIG_EVENT_ENTRY) {
2783
0
      if (!section_seen)
2784
        /* This is not the section's first entry. */
2785
0
        return;
2786
      /* We encountered no comment before the section. */
2787
0
      break;
2788
0
    }
2789
0
    if (type == CONFIG_EVENT_SECTION) {
2790
0
      if (!store->parsed[i - 1].is_keys_section)
2791
0
        break;
2792
0
      section_seen = 1;
2793
0
    }
2794
0
  }
2795
0
  begin = store->parsed[i].begin;
2796
2797
  /*
2798
   * Next, make sure that we are removing the last key(s) in the section,
2799
   * and that there are no comments that are possibly about the current
2800
   * section.
2801
   */
2802
0
  for (i = store->seen[seen] + 1; i < store->parsed_nr; i++) {
2803
0
    enum config_event_t type = store->parsed[i].type;
2804
2805
0
    if (type == CONFIG_EVENT_COMMENT)
2806
0
      return;
2807
0
    if (type == CONFIG_EVENT_SECTION) {
2808
0
      if (store->parsed[i].is_keys_section)
2809
0
        continue;
2810
0
      break;
2811
0
    }
2812
0
    if (type == CONFIG_EVENT_ENTRY) {
2813
0
      if (++seen < store->seen_nr &&
2814
0
          i == store->seen[seen])
2815
        /* We want to remove this entry, too */
2816
0
        continue;
2817
      /* There is another entry in this section. */
2818
0
      return;
2819
0
    }
2820
0
  }
2821
2822
  /*
2823
   * We are really removing the last entry/entries from this section, and
2824
   * there are no enclosed or surrounding comments. Remove the entire,
2825
   * now-empty section.
2826
   */
2827
0
  *seen_ptr = seen;
2828
0
  *begin_offset = begin;
2829
0
  if (i < store->parsed_nr)
2830
0
    *end_offset = store->parsed[i].begin;
2831
0
  else
2832
0
    *end_offset = store->parsed[store->parsed_nr - 1].end;
2833
0
}
2834
2835
int repo_config_set_in_file_gently(struct repository *r, const char *config_filename,
2836
           const char *key, const char *comment, const char *value)
2837
0
{
2838
0
  return repo_config_set_multivar_in_file_gently(r, config_filename, key, value, NULL, comment, 0);
2839
0
}
2840
2841
void repo_config_set_in_file(struct repository *r, const char *config_filename,
2842
           const char *key, const char *value)
2843
0
{
2844
0
  repo_config_set_multivar_in_file(r, config_filename, key, value, NULL, 0);
2845
0
}
2846
2847
int repo_config_set_gently(struct repository *r, const char *key, const char *value)
2848
0
{
2849
0
  return repo_config_set_multivar_gently(r, key, value, NULL, 0);
2850
0
}
2851
2852
int repo_config_set_worktree_gently(struct repository *r,
2853
            const char *key, const char *value)
2854
0
{
2855
  /* Only use worktree-specific config if it is already enabled. */
2856
0
  if (r->repository_format_worktree_config) {
2857
0
    char *file = repo_git_path(r, "config.worktree");
2858
0
    int ret = repo_config_set_multivar_in_file_gently(
2859
0
          r, file, key, value, NULL, NULL, 0);
2860
0
    free(file);
2861
0
    return ret;
2862
0
  }
2863
0
  return repo_config_set_multivar_gently(r, key, value, NULL, 0);
2864
0
}
2865
2866
void repo_config_set(struct repository *r, const char *key, const char *value)
2867
0
{
2868
0
  repo_config_set_multivar(r, key, value, NULL, 0);
2869
2870
0
  trace2_cmd_set_config(key, value);
2871
0
}
2872
2873
char *git_config_prepare_comment_string(const char *comment)
2874
0
{
2875
0
  size_t leading_blanks;
2876
0
  char *prepared;
2877
2878
0
  if (!comment)
2879
0
    return NULL;
2880
2881
0
  if (strchr(comment, '\n'))
2882
0
    die(_("no multi-line comment allowed: '%s'"), comment);
2883
2884
  /*
2885
   * If it begins with one or more leading whitespace characters
2886
   * followed by '#", the comment string is used as-is.
2887
   *
2888
   * If it begins with '#', a SP is inserted between the comment
2889
   * and the value the comment is about.
2890
   *
2891
   * Otherwise, the value is followed by a SP followed by '#'
2892
   * followed by SP and then the comment string comes.
2893
   */
2894
2895
0
  leading_blanks = strspn(comment, " \t");
2896
0
  if (leading_blanks && comment[leading_blanks] == '#')
2897
0
    prepared = xstrdup(comment); /* use it as-is */
2898
0
  else if (comment[0] == '#')
2899
0
    prepared = xstrfmt(" %s", comment);
2900
0
  else
2901
0
    prepared = xstrfmt(" # %s", comment);
2902
2903
0
  return prepared;
2904
0
}
2905
2906
static void validate_comment_string(const char *comment)
2907
0
{
2908
0
  size_t leading_blanks;
2909
2910
0
  if (!comment)
2911
0
    return;
2912
  /*
2913
   * The front-end must have massaged the comment string
2914
   * properly before calling us.
2915
   */
2916
0
  if (strchr(comment, '\n'))
2917
0
    BUG("multi-line comments are not permitted: '%s'", comment);
2918
2919
0
  leading_blanks = strspn(comment, " \t");
2920
0
  if (!leading_blanks || comment[leading_blanks] != '#')
2921
0
    BUG("comment must begin with one or more SP followed by '#': '%s'",
2922
0
        comment);
2923
0
}
2924
2925
/*
2926
 * If value==NULL, unset in (remove from) config,
2927
 * if value_pattern!=NULL, disregard key/value pairs where value does not match.
2928
 * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values
2929
 *     (only add a new one)
2930
 * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
2931
 *     key/values are removed before a single new pair is written. If the
2932
 *     flag is not present, then replace only the first match.
2933
 *
2934
 * Returns 0 on success.
2935
 *
2936
 * This function does this:
2937
 *
2938
 * - it locks the config file by creating ".git/config.lock"
2939
 *
2940
 * - it then parses the config using store_aux() as validator to find
2941
 *   the position on the key/value pair to replace. If it is to be unset,
2942
 *   it must be found exactly once.
2943
 *
2944
 * - the config file is mmap()ed and the part before the match (if any) is
2945
 *   written to the lock file, then the changed part and the rest.
2946
 *
2947
 * - the config file is removed and the lock file rename()d to it.
2948
 *
2949
 */
2950
int repo_config_set_multivar_in_file_gently(struct repository *r,
2951
              const char *config_filename,
2952
              const char *key, const char *value,
2953
              const char *value_pattern,
2954
              const char *comment,
2955
              unsigned flags)
2956
0
{
2957
0
  int fd = -1, in_fd = -1;
2958
0
  int ret;
2959
0
  struct lock_file lock = LOCK_INIT;
2960
0
  char *filename_buf = NULL;
2961
0
  char *contents = NULL;
2962
0
  size_t contents_sz;
2963
0
  struct config_store_data store = CONFIG_STORE_INIT;
2964
0
  bool saved_check_deprecated_config = r->check_deprecated_config;
2965
2966
  /*
2967
   * Do not warn or die if there are deprecated config settings as
2968
   * we want the user to be able to change those settings by running
2969
   * "git config".
2970
   */
2971
0
  r->check_deprecated_config = false;
2972
2973
0
  validate_comment_string(comment);
2974
2975
  /* parse-key returns negative; flip the sign to feed exit(3) */
2976
0
  ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
2977
0
  if (ret)
2978
0
    goto out_free;
2979
2980
0
  store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
2981
2982
0
  if (!config_filename)
2983
0
    config_filename = filename_buf = repo_git_path(r, "config");
2984
2985
  /*
2986
   * The lock serves a purpose in addition to locking: the new
2987
   * contents of .git/config will be written into it.
2988
   */
2989
0
  fd = hold_lock_file_for_update(&lock, config_filename, 0);
2990
0
  if (fd < 0) {
2991
0
    error_errno(_("could not lock config file %s"), config_filename);
2992
0
    ret = CONFIG_NO_LOCK;
2993
0
    goto out_free;
2994
0
  }
2995
2996
  /*
2997
   * If .git/config does not exist yet, write a minimal version.
2998
   */
2999
0
  in_fd = open(config_filename, O_RDONLY);
3000
0
  if ( in_fd < 0 ) {
3001
0
    if ( ENOENT != errno ) {
3002
0
      error_errno(_("opening %s"), config_filename);
3003
0
      ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
3004
0
      goto out_free;
3005
0
    }
3006
    /* if nothing to unset, error out */
3007
0
    if (!value) {
3008
0
      ret = CONFIG_NOTHING_SET;
3009
0
      goto out_free;
3010
0
    }
3011
3012
0
    free(store.key);
3013
0
    store.key = xstrdup(key);
3014
0
    if (write_section(fd, key, &store) < 0 ||
3015
0
        write_pair(fd, key, value, comment, &store) < 0)
3016
0
      goto write_err_out;
3017
0
  } else {
3018
0
    struct stat st;
3019
0
    size_t copy_begin, copy_end;
3020
0
    unsigned i;
3021
0
    int new_line = 0;
3022
0
    struct config_options opts;
3023
3024
0
    if (!value_pattern)
3025
0
      store.value_pattern = NULL;
3026
0
    else if (value_pattern == CONFIG_REGEX_NONE)
3027
0
      store.value_pattern = CONFIG_REGEX_NONE;
3028
0
    else if (flags & CONFIG_FLAGS_FIXED_VALUE)
3029
0
      store.fixed_value = value_pattern;
3030
0
    else {
3031
0
      if (value_pattern[0] == '!') {
3032
0
        store.do_not_match = 1;
3033
0
        value_pattern++;
3034
0
      } else
3035
0
        store.do_not_match = 0;
3036
3037
0
      store.value_pattern = (regex_t*)xmalloc(sizeof(regex_t));
3038
0
      if (regcomp(store.value_pattern, value_pattern,
3039
0
          REG_EXTENDED)) {
3040
0
        error(_("invalid pattern: %s"), value_pattern);
3041
0
        FREE_AND_NULL(store.value_pattern);
3042
0
        ret = CONFIG_INVALID_PATTERN;
3043
0
        goto out_free;
3044
0
      }
3045
0
    }
3046
3047
0
    ALLOC_GROW(store.parsed, 1, store.parsed_alloc);
3048
0
    store.parsed[0].end = 0;
3049
3050
0
    memset(&opts, 0, sizeof(opts));
3051
0
    opts.event_fn = store_aux_event;
3052
0
    opts.event_fn_data = &store;
3053
3054
    /*
3055
     * After this, store.parsed will contain offsets of all the
3056
     * parsed elements, and store.seen will contain a list of
3057
     * matches, as indices into store.parsed.
3058
     *
3059
     * As a side effect, we make sure to transform only a valid
3060
     * existing config file.
3061
     */
3062
0
    if (git_config_from_file_with_options(store_aux,
3063
0
                  config_filename,
3064
0
                  &store, CONFIG_SCOPE_UNKNOWN,
3065
0
                  &opts)) {
3066
0
      error(_("invalid config file %s"), config_filename);
3067
0
      ret = CONFIG_INVALID_FILE;
3068
0
      goto out_free;
3069
0
    }
3070
3071
    /* if nothing to unset, or too many matches, error out */
3072
0
    if ((store.seen_nr == 0 && value == NULL) ||
3073
0
        (store.seen_nr > 1 && !store.multi_replace)) {
3074
0
      ret = CONFIG_NOTHING_SET;
3075
0
      goto out_free;
3076
0
    }
3077
3078
0
    if (fstat(in_fd, &st) == -1) {
3079
0
      error_errno(_("fstat on %s failed"), config_filename);
3080
0
      ret = CONFIG_INVALID_FILE;
3081
0
      goto out_free;
3082
0
    }
3083
3084
0
    contents_sz = xsize_t(st.st_size);
3085
0
    contents = xmmap_gently(NULL, contents_sz, PROT_READ,
3086
0
          MAP_PRIVATE, in_fd, 0);
3087
0
    if (contents == MAP_FAILED) {
3088
0
      if (errno == ENODEV && S_ISDIR(st.st_mode))
3089
0
        errno = EISDIR;
3090
0
      error_errno(_("unable to mmap '%s'%s"),
3091
0
          config_filename, mmap_os_err());
3092
0
      ret = CONFIG_INVALID_FILE;
3093
0
      contents = NULL;
3094
0
      goto out_free;
3095
0
    }
3096
0
    close(in_fd);
3097
0
    in_fd = -1;
3098
3099
0
    if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
3100
0
      error_errno(_("chmod on %s failed"), get_lock_file_path(&lock));
3101
0
      ret = CONFIG_NO_WRITE;
3102
0
      goto out_free;
3103
0
    }
3104
3105
0
    if (store.seen_nr == 0) {
3106
0
      if (!store.seen_alloc) {
3107
        /* Did not see key nor section */
3108
0
        ALLOC_GROW(store.seen, 1, store.seen_alloc);
3109
0
        store.seen[0] = store.parsed_nr
3110
0
          - !!store.parsed_nr;
3111
0
      }
3112
0
      store.seen_nr = 1;
3113
0
    }
3114
3115
0
    for (i = 0, copy_begin = 0; i < store.seen_nr; i++) {
3116
0
      size_t replace_end;
3117
0
      int j = store.seen[i];
3118
3119
0
      new_line = 0;
3120
0
      if (!store.key_seen) {
3121
0
        copy_end = store.parsed[j].end;
3122
        /* include '\n' when copying section header */
3123
0
        if (copy_end > 0 && copy_end < contents_sz &&
3124
0
            contents[copy_end - 1] != '\n' &&
3125
0
            contents[copy_end] == '\n')
3126
0
          copy_end++;
3127
0
        replace_end = copy_end;
3128
0
      } else {
3129
0
        replace_end = store.parsed[j].end;
3130
0
        copy_end = store.parsed[j].begin;
3131
0
        if (!value)
3132
0
          maybe_remove_section(&store,
3133
0
                   &copy_end,
3134
0
                   &replace_end, &i);
3135
        /*
3136
         * Swallow preceding white-space on the same
3137
         * line.
3138
         */
3139
0
        while (copy_end > 0 ) {
3140
0
          char c = contents[copy_end - 1];
3141
3142
0
          if (isspace(c) && c != '\n')
3143
0
            copy_end--;
3144
0
          else
3145
0
            break;
3146
0
        }
3147
0
      }
3148
3149
0
      if (copy_end > 0 && contents[copy_end-1] != '\n')
3150
0
        new_line = 1;
3151
3152
      /* write the first part of the config */
3153
0
      if (copy_end > copy_begin) {
3154
0
        if (write_in_full(fd, contents + copy_begin,
3155
0
              copy_end - copy_begin) < 0)
3156
0
          goto write_err_out;
3157
0
        if (new_line &&
3158
0
            write_str_in_full(fd, "\n") < 0)
3159
0
          goto write_err_out;
3160
0
      }
3161
0
      copy_begin = replace_end;
3162
0
    }
3163
3164
    /* write the pair (value == NULL means unset) */
3165
0
    if (value) {
3166
0
      if (!store.section_seen) {
3167
0
        if (write_section(fd, key, &store) < 0)
3168
0
          goto write_err_out;
3169
0
      }
3170
0
      if (write_pair(fd, key, value, comment, &store) < 0)
3171
0
        goto write_err_out;
3172
0
    }
3173
3174
    /* write the rest of the config */
3175
0
    if (copy_begin < contents_sz)
3176
0
      if (write_in_full(fd, contents + copy_begin,
3177
0
            contents_sz - copy_begin) < 0)
3178
0
        goto write_err_out;
3179
3180
0
    munmap(contents, contents_sz);
3181
0
    contents = NULL;
3182
0
  }
3183
3184
0
  if (commit_lock_file(&lock) < 0) {
3185
0
    error_errno(_("could not write config file %s"), config_filename);
3186
0
    ret = CONFIG_NO_WRITE;
3187
0
    goto out_free;
3188
0
  }
3189
3190
0
  ret = 0;
3191
3192
  /* Invalidate the config cache */
3193
0
  repo_config_clear(r);
3194
3195
0
out_free:
3196
0
  rollback_lock_file(&lock);
3197
0
  free(filename_buf);
3198
0
  if (contents)
3199
0
    munmap(contents, contents_sz);
3200
0
  if (in_fd >= 0)
3201
0
    close(in_fd);
3202
0
  config_store_data_clear(&store);
3203
0
  r->check_deprecated_config = saved_check_deprecated_config;
3204
0
  return ret;
3205
3206
0
write_err_out:
3207
0
  ret = write_error(get_lock_file_path(&lock));
3208
0
  goto out_free;
3209
0
}
3210
3211
void repo_config_set_multivar_in_file(struct repository *r,
3212
              const char *config_filename,
3213
              const char *key, const char *value,
3214
              const char *value_pattern, unsigned flags)
3215
0
{
3216
0
  if (!repo_config_set_multivar_in_file_gently(r, config_filename, key, value,
3217
0
                 value_pattern, NULL, flags))
3218
0
    return;
3219
0
  if (value)
3220
0
    die(_("could not set '%s' to '%s'"), key, value);
3221
0
  else
3222
0
    die(_("could not unset '%s'"), key);
3223
0
}
3224
3225
int repo_config_set_multivar_gently(struct repository *r, const char *key,
3226
            const char *value,
3227
            const char *value_pattern, unsigned flags)
3228
0
{
3229
0
  char *file = repo_git_path(r, "config");
3230
0
  int res = repo_config_set_multivar_in_file_gently(r, file,
3231
0
                key, value,
3232
0
                value_pattern,
3233
0
                NULL, flags);
3234
0
  free(file);
3235
0
  return res;
3236
0
}
3237
3238
void repo_config_set_multivar(struct repository *r,
3239
            const char *key, const char *value,
3240
            const char *value_pattern, unsigned flags)
3241
0
{
3242
0
  char *file = repo_git_path(r, "config");
3243
0
  repo_config_set_multivar_in_file(r, file, key, value,
3244
0
           value_pattern, flags);
3245
0
  free(file);
3246
0
}
3247
3248
static size_t section_name_match (const char *buf, const char *name)
3249
0
{
3250
0
  size_t i = 0, j = 0;
3251
0
  int dot = 0;
3252
0
  if (buf[i] != '[')
3253
0
    return 0;
3254
0
  for (i = 1; buf[i] && buf[i] != ']'; i++) {
3255
0
    if (!dot && isspace(buf[i])) {
3256
0
      dot = 1;
3257
0
      if (name[j++] != '.')
3258
0
        break;
3259
0
      for (i++; isspace(buf[i]); i++)
3260
0
        ; /* do nothing */
3261
0
      if (buf[i] != '"')
3262
0
        break;
3263
0
      continue;
3264
0
    }
3265
0
    if (buf[i] == '\\' && dot)
3266
0
      i++;
3267
0
    else if (buf[i] == '"' && dot) {
3268
0
      for (i++; isspace(buf[i]); i++)
3269
0
        ; /* do_nothing */
3270
0
      break;
3271
0
    }
3272
0
    if (buf[i] != name[j++])
3273
0
      break;
3274
0
  }
3275
0
  if (buf[i] == ']' && name[j] == 0) {
3276
    /*
3277
     * We match, now just find the right length offset by
3278
     * gobbling up any whitespace after it, as well
3279
     */
3280
0
    i++;
3281
0
    for (; buf[i] && isspace(buf[i]); i++)
3282
0
      ; /* do nothing */
3283
0
    return i;
3284
0
  }
3285
0
  return 0;
3286
0
}
3287
3288
static int section_name_is_ok(const char *name)
3289
0
{
3290
  /* Empty section names are bogus. */
3291
0
  if (!*name)
3292
0
    return 0;
3293
3294
  /*
3295
   * Before a dot, we must be alphanumeric or dash. After the first dot,
3296
   * anything goes, so we can stop checking.
3297
   */
3298
0
  for (; *name && *name != '.'; name++)
3299
0
    if (*name != '-' && !isalnum(*name))
3300
0
      return 0;
3301
0
  return 1;
3302
0
}
3303
3304
0
#define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
3305
3306
/* if new_name == NULL, the section is removed instead */
3307
static int repo_config_copy_or_rename_section_in_file(
3308
  struct repository *r,
3309
  const char *config_filename,
3310
  const char *old_name,
3311
  const char *new_name, int copy)
3312
0
{
3313
0
  int ret = 0, remove = 0;
3314
0
  char *filename_buf = NULL;
3315
0
  struct lock_file lock = LOCK_INIT;
3316
0
  int out_fd;
3317
0
  struct strbuf buf = STRBUF_INIT;
3318
0
  FILE *config_file = NULL;
3319
0
  struct stat st;
3320
0
  struct strbuf copystr = STRBUF_INIT;
3321
0
  struct config_store_data store;
3322
0
  uint32_t line_nr = 0;
3323
3324
0
  memset(&store, 0, sizeof(store));
3325
3326
0
  if (new_name && !section_name_is_ok(new_name)) {
3327
0
    ret = error(_("invalid section name: %s"), new_name);
3328
0
    goto out_no_rollback;
3329
0
  }
3330
3331
0
  if (!config_filename)
3332
0
    config_filename = filename_buf = repo_git_path(r, "config");
3333
3334
0
  out_fd = hold_lock_file_for_update(&lock, config_filename, 0);
3335
0
  if (out_fd < 0) {
3336
0
    ret = error(_("could not lock config file %s"), config_filename);
3337
0
    goto out;
3338
0
  }
3339
3340
0
  if (!(config_file = fopen(config_filename, "rb"))) {
3341
0
    ret = warn_on_fopen_errors(config_filename);
3342
0
    if (ret)
3343
0
      goto out;
3344
    /* no config file means nothing to rename, no error */
3345
0
    goto commit_and_out;
3346
0
  }
3347
3348
0
  if (fstat(fileno(config_file), &st) == -1) {
3349
0
    ret = error_errno(_("fstat on %s failed"), config_filename);
3350
0
    goto out;
3351
0
  }
3352
3353
0
  if (chmod(get_lock_file_path(&lock), st.st_mode & 07777) < 0) {
3354
0
    ret = error_errno(_("chmod on %s failed"),
3355
0
          get_lock_file_path(&lock));
3356
0
    goto out;
3357
0
  }
3358
3359
0
  while (!strbuf_getwholeline(&buf, config_file, '\n')) {
3360
0
    size_t i, length;
3361
0
    int is_section = 0;
3362
0
    char *output = buf.buf;
3363
3364
0
    line_nr++;
3365
3366
0
    if (buf.len >= GIT_CONFIG_MAX_LINE_LEN) {
3367
0
      ret = error(_("refusing to work with overly long line "
3368
0
              "in '%s' on line %"PRIuMAX),
3369
0
            config_filename, (uintmax_t)line_nr);
3370
0
      goto out;
3371
0
    }
3372
3373
0
    for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)
3374
0
      ; /* do nothing */
3375
0
    if (buf.buf[i] == '[') {
3376
      /* it's a section */
3377
0
      size_t offset;
3378
0
      is_section = 1;
3379
3380
      /*
3381
       * When encountering a new section under -c we
3382
       * need to flush out any section we're already
3383
       * coping and begin anew. There might be
3384
       * multiple [branch "$name"] sections.
3385
       */
3386
0
      if (copystr.len > 0) {
3387
0
        if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
3388
0
          ret = write_error(get_lock_file_path(&lock));
3389
0
          goto out;
3390
0
        }
3391
0
        strbuf_reset(&copystr);
3392
0
      }
3393
3394
0
      offset = section_name_match(&buf.buf[i], old_name);
3395
0
      if (offset > 0) {
3396
0
        ret++;
3397
0
        if (!new_name) {
3398
0
          remove = 1;
3399
0
          continue;
3400
0
        }
3401
0
        store.baselen = strlen(new_name);
3402
0
        if (!copy) {
3403
0
          if (write_section(out_fd, new_name, &store) < 0) {
3404
0
            ret = write_error(get_lock_file_path(&lock));
3405
0
            goto out;
3406
0
          }
3407
          /*
3408
           * We wrote out the new section, with
3409
           * a newline, now skip the old
3410
           * section's length
3411
           */
3412
0
          output += offset + i;
3413
0
          if (strlen(output) > 0) {
3414
            /*
3415
             * More content means there's
3416
             * a declaration to put on the
3417
             * next line; indent with a
3418
             * tab
3419
             */
3420
0
            output -= 1;
3421
0
            output[0] = '\t';
3422
0
          }
3423
0
        } else {
3424
0
          strbuf_release(&copystr);
3425
0
          copystr = store_create_section(new_name, &store);
3426
0
        }
3427
0
      }
3428
0
      remove = 0;
3429
0
    }
3430
0
    if (remove)
3431
0
      continue;
3432
0
    length = strlen(output);
3433
3434
0
    if (!is_section && copystr.len > 0) {
3435
0
      strbuf_add(&copystr, output, length);
3436
0
    }
3437
3438
0
    if (write_in_full(out_fd, output, length) < 0) {
3439
0
      ret = write_error(get_lock_file_path(&lock));
3440
0
      goto out;
3441
0
    }
3442
0
  }
3443
3444
  /*
3445
   * Copy a trailing section at the end of the config, won't be
3446
   * flushed by the usual "flush because we have a new section
3447
   * logic in the loop above.
3448
   */
3449
0
  if (copystr.len > 0) {
3450
0
    if (write_in_full(out_fd, copystr.buf, copystr.len) < 0) {
3451
0
      ret = write_error(get_lock_file_path(&lock));
3452
0
      goto out;
3453
0
    }
3454
0
    strbuf_reset(&copystr);
3455
0
  }
3456
3457
0
  fclose(config_file);
3458
0
  config_file = NULL;
3459
0
commit_and_out:
3460
0
  if (commit_lock_file(&lock) < 0)
3461
0
    ret = error_errno(_("could not write config file %s"),
3462
0
          config_filename);
3463
0
out:
3464
0
  if (config_file)
3465
0
    fclose(config_file);
3466
0
  rollback_lock_file(&lock);
3467
0
out_no_rollback:
3468
0
  free(filename_buf);
3469
0
  config_store_data_clear(&store);
3470
0
  strbuf_release(&buf);
3471
0
  strbuf_release(&copystr);
3472
0
  return ret;
3473
0
}
3474
3475
int repo_config_rename_section_in_file(struct repository *r, const char *config_filename,
3476
               const char *old_name, const char *new_name)
3477
0
{
3478
0
  return repo_config_copy_or_rename_section_in_file(r, config_filename,
3479
0
           old_name, new_name, 0);
3480
0
}
3481
3482
int repo_config_rename_section(struct repository *r, const char *old_name, const char *new_name)
3483
0
{
3484
0
  return repo_config_rename_section_in_file(r, NULL, old_name, new_name);
3485
0
}
3486
3487
int repo_config_copy_section_in_file(struct repository *r, const char *config_filename,
3488
             const char *old_name, const char *new_name)
3489
0
{
3490
0
  return repo_config_copy_or_rename_section_in_file(r, config_filename,
3491
0
           old_name, new_name, 1);
3492
0
}
3493
3494
int repo_config_copy_section(struct repository *r, const char *old_name, const char *new_name)
3495
0
{
3496
0
  return repo_config_copy_section_in_file(r, NULL, old_name, new_name);
3497
0
}
3498
3499
/*
3500
 * Call this to report error for your variable that should not
3501
 * get a boolean value (i.e. "[my] var" means "true").
3502
 */
3503
#undef config_error_nonbool
3504
int config_error_nonbool(const char *var)
3505
0
{
3506
0
  return error(_("missing value for '%s'"), var);
3507
0
}
3508
3509
int parse_config_key(const char *var,
3510
         const char *section,
3511
         const char **subsection, size_t *subsection_len,
3512
         const char **key)
3513
0
{
3514
0
  const char *dot;
3515
3516
  /* Does it start with "section." ? */
3517
0
  if (!skip_prefix(var, section, &var) || *var != '.')
3518
0
    return -1;
3519
3520
  /*
3521
   * Find the key; we don't know yet if we have a subsection, but we must
3522
   * parse backwards from the end, since the subsection may have dots in
3523
   * it, too.
3524
   */
3525
0
  dot = strrchr(var, '.');
3526
0
  *key = dot + 1;
3527
3528
  /* Did we have a subsection at all? */
3529
0
  if (dot == var) {
3530
0
    if (subsection) {
3531
0
      *subsection = NULL;
3532
0
      *subsection_len = 0;
3533
0
    }
3534
0
  }
3535
0
  else {
3536
0
    if (!subsection)
3537
0
      return -1;
3538
0
    *subsection = var + 1;
3539
0
    *subsection_len = dot - *subsection;
3540
0
  }
3541
3542
0
  return 0;
3543
0
}
3544
3545
const char *config_origin_type_name(enum config_origin_type type)
3546
0
{
3547
0
  switch (type) {
3548
0
  case CONFIG_ORIGIN_BLOB:
3549
0
    return "blob";
3550
0
  case CONFIG_ORIGIN_FILE:
3551
0
    return "file";
3552
0
  case CONFIG_ORIGIN_STDIN:
3553
0
    return "standard input";
3554
0
  case CONFIG_ORIGIN_SUBMODULE_BLOB:
3555
0
    return "submodule-blob";
3556
0
  case CONFIG_ORIGIN_CMDLINE:
3557
0
    return "command line";
3558
0
  default:
3559
0
    BUG("unknown config origin type");
3560
0
  }
3561
0
}
3562
3563
const char *config_scope_name(enum config_scope scope)
3564
0
{
3565
0
  switch (scope) {
3566
0
  case CONFIG_SCOPE_SYSTEM:
3567
0
    return "system";
3568
0
  case CONFIG_SCOPE_GLOBAL:
3569
0
    return "global";
3570
0
  case CONFIG_SCOPE_LOCAL:
3571
0
    return "local";
3572
0
  case CONFIG_SCOPE_WORKTREE:
3573
0
    return "worktree";
3574
0
  case CONFIG_SCOPE_COMMAND:
3575
0
    return "command";
3576
0
  case CONFIG_SCOPE_SUBMODULE:
3577
0
    return "submodule";
3578
0
  default:
3579
0
    return "unknown";
3580
0
  }
3581
0
}
3582
3583
int lookup_config(const char **mapping, int nr_mapping, const char *var)
3584
0
{
3585
0
  int i;
3586
3587
0
  for (i = 0; i < nr_mapping; i++) {
3588
0
    const char *name = mapping[i];
3589
3590
0
    if (name && !strcasecmp(var, name))
3591
0
      return i;
3592
0
  }
3593
0
  return -1;
3594
0
}