Coverage Report

Created: 2023-11-19 07:08

/src/git/builtin/config.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "abspath.h"
3
#include "config.h"
4
#include "color.h"
5
#include "editor.h"
6
#include "environment.h"
7
#include "repository.h"
8
#include "gettext.h"
9
#include "ident.h"
10
#include "parse-options.h"
11
#include "urlmatch.h"
12
#include "path.h"
13
#include "quote.h"
14
#include "setup.h"
15
#include "strbuf.h"
16
#include "worktree.h"
17
18
static const char *const builtin_config_usage[] = {
19
  N_("git config [<options>]"),
20
  NULL
21
};
22
23
static char *key;
24
static regex_t *key_regexp;
25
static const char *value_pattern;
26
static regex_t *regexp;
27
static int show_keys;
28
static int omit_values;
29
static int use_key_regexp;
30
static int do_all;
31
static int do_not_match;
32
static char delim = '=';
33
static char key_delim = ' ';
34
static char term = '\n';
35
36
static int use_global_config, use_system_config, use_local_config;
37
static int use_worktree_config;
38
static struct git_config_source given_config_source;
39
static int actions, type;
40
static char *default_value;
41
static int end_nul;
42
static int respect_includes_opt = -1;
43
static struct config_options config_options;
44
static int show_origin;
45
static int show_scope;
46
static int fixed_value;
47
48
0
#define ACTION_GET (1<<0)
49
0
#define ACTION_GET_ALL (1<<1)
50
0
#define ACTION_GET_REGEXP (1<<2)
51
0
#define ACTION_REPLACE_ALL (1<<3)
52
0
#define ACTION_ADD (1<<4)
53
0
#define ACTION_UNSET (1<<5)
54
0
#define ACTION_UNSET_ALL (1<<6)
55
0
#define ACTION_RENAME_SECTION (1<<7)
56
0
#define ACTION_REMOVE_SECTION (1<<8)
57
0
#define ACTION_LIST (1<<9)
58
0
#define ACTION_EDIT (1<<10)
59
0
#define ACTION_SET (1<<11)
60
0
#define ACTION_SET_ALL (1<<12)
61
0
#define ACTION_GET_COLOR (1<<13)
62
0
#define ACTION_GET_COLORBOOL (1<<14)
63
0
#define ACTION_GET_URLMATCH (1<<15)
64
65
/*
66
 * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
67
 * one line of output and which should therefore be paged.
68
 */
69
0
#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
70
0
      ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
71
72
0
#define TYPE_BOOL   1
73
0
#define TYPE_INT    2
74
0
#define TYPE_BOOL_OR_INT  3
75
0
#define TYPE_PATH   4
76
0
#define TYPE_EXPIRY_DATE  5
77
0
#define TYPE_COLOR    6
78
0
#define TYPE_BOOL_OR_STR  7
79
80
#define OPT_CALLBACK_VALUE(s, l, v, h, i) \
81
  { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \
82
  PARSE_OPT_NONEG, option_parse_type, (i) }
83
84
static NORETURN void usage_builtin_config(void);
85
86
static int option_parse_type(const struct option *opt, const char *arg,
87
           int unset)
88
0
{
89
0
  int new_type, *to_type;
90
91
0
  if (unset) {
92
0
    *((int *) opt->value) = 0;
93
0
    return 0;
94
0
  }
95
96
  /*
97
   * To support '--<type>' style flags, begin with new_type equal to
98
   * opt->defval.
99
   */
100
0
  new_type = opt->defval;
101
0
  if (!new_type) {
102
0
    if (!strcmp(arg, "bool"))
103
0
      new_type = TYPE_BOOL;
104
0
    else if (!strcmp(arg, "int"))
105
0
      new_type = TYPE_INT;
106
0
    else if (!strcmp(arg, "bool-or-int"))
107
0
      new_type = TYPE_BOOL_OR_INT;
108
0
    else if (!strcmp(arg, "bool-or-str"))
109
0
      new_type = TYPE_BOOL_OR_STR;
110
0
    else if (!strcmp(arg, "path"))
111
0
      new_type = TYPE_PATH;
112
0
    else if (!strcmp(arg, "expiry-date"))
113
0
      new_type = TYPE_EXPIRY_DATE;
114
0
    else if (!strcmp(arg, "color"))
115
0
      new_type = TYPE_COLOR;
116
0
    else
117
0
      die(_("unrecognized --type argument, %s"), arg);
118
0
  }
119
120
0
  to_type = opt->value;
121
0
  if (*to_type && *to_type != new_type) {
122
    /*
123
     * Complain when there is a new type not equal to the old type.
124
     * This allows for combinations like '--int --type=int' and
125
     * '--type=int --type=int', but disallows ones like '--type=bool
126
     * --int' and '--type=bool
127
     * --type=int'.
128
     */
129
0
    error(_("only one type at a time"));
130
0
    usage_builtin_config();
131
0
  }
132
0
  *to_type = new_type;
133
134
0
  return 0;
135
0
}
136
137
static struct option builtin_config_options[] = {
138
  OPT_GROUP(N_("Config file location")),
139
  OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
140
  OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
141
  OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
142
  OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
143
  OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
144
  OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
145
  OPT_GROUP(N_("Action")),
146
  OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
147
  OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
148
  OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
149
  OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
150
  OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
151
  OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
152
  OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
153
  OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
154
  OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
155
  OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
156
  OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
157
  OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
158
  OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
159
  OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
160
  OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
161
  OPT_GROUP(N_("Type")),
162
  OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
163
  OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
164
  OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
165
  OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
166
  OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
167
  OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
168
  OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
169
  OPT_GROUP(N_("Other")),
170
  OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
171
  OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
172
  OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
173
  OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
174
  OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
175
  OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
176
  OPT_END(),
177
};
178
179
static NORETURN void usage_builtin_config(void)
180
0
{
181
0
  usage_with_options(builtin_config_usage, builtin_config_options);
182
0
}
183
184
static void check_argc(int argc, int min, int max)
185
0
{
186
0
  if (argc >= min && argc <= max)
187
0
    return;
188
0
  if (min == max)
189
0
    error(_("wrong number of arguments, should be %d"), min);
190
0
  else
191
0
    error(_("wrong number of arguments, should be from %d to %d"),
192
0
          min, max);
193
0
  usage_builtin_config();
194
0
}
195
196
static void show_config_origin(const struct key_value_info *kvi,
197
             struct strbuf *buf)
198
0
{
199
0
  const char term = end_nul ? '\0' : '\t';
200
201
0
  strbuf_addstr(buf, config_origin_type_name(kvi->origin_type));
202
0
  strbuf_addch(buf, ':');
203
0
  if (end_nul)
204
0
    strbuf_addstr(buf, kvi->filename ? kvi->filename : "");
205
0
  else
206
0
    quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0);
207
0
  strbuf_addch(buf, term);
208
0
}
209
210
static void show_config_scope(const struct key_value_info *kvi,
211
            struct strbuf *buf)
212
0
{
213
0
  const char term = end_nul ? '\0' : '\t';
214
0
  const char *scope = config_scope_name(kvi->scope);
215
216
0
  strbuf_addstr(buf, N_(scope));
217
0
  strbuf_addch(buf, term);
218
0
}
219
220
static int show_all_config(const char *key_, const char *value_,
221
         const struct config_context *ctx,
222
         void *cb UNUSED)
223
0
{
224
0
  const struct key_value_info *kvi = ctx->kvi;
225
226
0
  if (show_origin || show_scope) {
227
0
    struct strbuf buf = STRBUF_INIT;
228
0
    if (show_scope)
229
0
      show_config_scope(kvi, &buf);
230
0
    if (show_origin)
231
0
      show_config_origin(kvi, &buf);
232
    /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
233
0
    fwrite(buf.buf, 1, buf.len, stdout);
234
0
    strbuf_release(&buf);
235
0
  }
236
0
  if (!omit_values && value_)
237
0
    printf("%s%c%s%c", key_, delim, value_, term);
238
0
  else
239
0
    printf("%s%c", key_, term);
240
0
  return 0;
241
0
}
242
243
struct strbuf_list {
244
  struct strbuf *items;
245
  int nr;
246
  int alloc;
247
};
248
249
static int format_config(struct strbuf *buf, const char *key_,
250
       const char *value_, const struct key_value_info *kvi)
251
0
{
252
0
  if (show_scope)
253
0
    show_config_scope(kvi, buf);
254
0
  if (show_origin)
255
0
    show_config_origin(kvi, buf);
256
0
  if (show_keys)
257
0
    strbuf_addstr(buf, key_);
258
0
  if (!omit_values) {
259
0
    if (show_keys)
260
0
      strbuf_addch(buf, key_delim);
261
262
0
    if (type == TYPE_INT)
263
0
      strbuf_addf(buf, "%"PRId64,
264
0
            git_config_int64(key_, value_ ? value_ : "", kvi));
265
0
    else if (type == TYPE_BOOL)
266
0
      strbuf_addstr(buf, git_config_bool(key_, value_) ?
267
0
              "true" : "false");
268
0
    else if (type == TYPE_BOOL_OR_INT) {
269
0
      int is_bool, v;
270
0
      v = git_config_bool_or_int(key_, value_, kvi,
271
0
               &is_bool);
272
0
      if (is_bool)
273
0
        strbuf_addstr(buf, v ? "true" : "false");
274
0
      else
275
0
        strbuf_addf(buf, "%d", v);
276
0
    } else if (type == TYPE_BOOL_OR_STR) {
277
0
      int v = git_parse_maybe_bool(value_);
278
0
      if (v < 0)
279
0
        strbuf_addstr(buf, value_);
280
0
      else
281
0
        strbuf_addstr(buf, v ? "true" : "false");
282
0
    } else if (type == TYPE_PATH) {
283
0
      const char *v;
284
0
      if (git_config_pathname(&v, key_, value_) < 0)
285
0
        return -1;
286
0
      strbuf_addstr(buf, v);
287
0
      free((char *)v);
288
0
    } else if (type == TYPE_EXPIRY_DATE) {
289
0
      timestamp_t t;
290
0
      if (git_config_expiry_date(&t, key_, value_) < 0)
291
0
        return -1;
292
0
      strbuf_addf(buf, "%"PRItime, t);
293
0
    } else if (type == TYPE_COLOR) {
294
0
      char v[COLOR_MAXLEN];
295
0
      if (git_config_color(v, key_, value_) < 0)
296
0
        return -1;
297
0
      strbuf_addstr(buf, v);
298
0
    } else if (value_) {
299
0
      strbuf_addstr(buf, value_);
300
0
    } else {
301
      /* Just show the key name; back out delimiter */
302
0
      if (show_keys)
303
0
        strbuf_setlen(buf, buf->len - 1);
304
0
    }
305
0
  }
306
0
  strbuf_addch(buf, term);
307
0
  return 0;
308
0
}
309
310
static int collect_config(const char *key_, const char *value_,
311
        const struct config_context *ctx, void *cb)
312
0
{
313
0
  struct strbuf_list *values = cb;
314
0
  const struct key_value_info *kvi = ctx->kvi;
315
316
0
  if (!use_key_regexp && strcmp(key_, key))
317
0
    return 0;
318
0
  if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
319
0
    return 0;
320
0
  if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
321
0
    return 0;
322
0
  if (regexp != NULL &&
323
0
      (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
324
0
    return 0;
325
326
0
  ALLOC_GROW(values->items, values->nr + 1, values->alloc);
327
0
  strbuf_init(&values->items[values->nr], 0);
328
329
0
  return format_config(&values->items[values->nr++], key_, value_, kvi);
330
0
}
331
332
static int get_value(const char *key_, const char *regex_, unsigned flags)
333
{
334
  int ret = CONFIG_GENERIC_ERROR;
335
  struct strbuf_list values = {NULL};
336
  int i;
337
338
  if (use_key_regexp) {
339
    char *tl;
340
341
    /*
342
     * NEEDSWORK: this naive pattern lowercasing obviously does not
343
     * work for more complex patterns like "^[^.]*Foo.*bar".
344
     * Perhaps we should deprecate this altogether someday.
345
     */
346
347
    key = xstrdup(key_);
348
    for (tl = key + strlen(key) - 1;
349
         tl >= key && *tl != '.';
350
         tl--)
351
      *tl = tolower(*tl);
352
    for (tl = key; *tl && *tl != '.'; tl++)
353
      *tl = tolower(*tl);
354
355
    key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
356
    if (regcomp(key_regexp, key, REG_EXTENDED)) {
357
      error(_("invalid key pattern: %s"), key_);
358
      FREE_AND_NULL(key_regexp);
359
      ret = CONFIG_INVALID_PATTERN;
360
      goto free_strings;
361
    }
362
  } else {
363
    if (git_config_parse_key(key_, &key, NULL)) {
364
      ret = CONFIG_INVALID_KEY;
365
      goto free_strings;
366
    }
367
  }
368
369
  if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
370
    value_pattern = regex_;
371
  else if (regex_) {
372
    if (regex_[0] == '!') {
373
      do_not_match = 1;
374
      regex_++;
375
    }
376
377
    regexp = (regex_t*)xmalloc(sizeof(regex_t));
378
    if (regcomp(regexp, regex_, REG_EXTENDED)) {
379
      error(_("invalid pattern: %s"), regex_);
380
      FREE_AND_NULL(regexp);
381
      ret = CONFIG_INVALID_PATTERN;
382
      goto free_strings;
383
    }
384
  }
385
386
  config_with_options(collect_config, &values,
387
          &given_config_source, the_repository,
388
          &config_options);
389
390
  if (!values.nr && default_value) {
391
    struct key_value_info kvi = KVI_INIT;
392
    struct strbuf *item;
393
394
    kvi_from_param(&kvi);
395
    ALLOC_GROW(values.items, values.nr + 1, values.alloc);
396
    item = &values.items[values.nr++];
397
    strbuf_init(item, 0);
398
    if (format_config(item, key_, default_value, &kvi) < 0)
399
      die(_("failed to format default config value: %s"),
400
        default_value);
401
  }
402
403
  ret = !values.nr;
404
405
  for (i = 0; i < values.nr; i++) {
406
    struct strbuf *buf = values.items + i;
407
    if (do_all || i == values.nr - 1)
408
      fwrite(buf->buf, 1, buf->len, stdout);
409
    strbuf_release(buf);
410
  }
411
  free(values.items);
412
413
free_strings:
414
  free(key);
415
  if (key_regexp) {
416
    regfree(key_regexp);
417
    free(key_regexp);
418
  }
419
  if (regexp) {
420
    regfree(regexp);
421
    free(regexp);
422
  }
423
424
  return ret;
425
}
426
427
static char *normalize_value(const char *key, const char *value,
428
           struct key_value_info *kvi)
429
0
{
430
0
  if (!value)
431
0
    return NULL;
432
433
0
  if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE)
434
    /*
435
     * We don't do normalization for TYPE_PATH here: If
436
     * the path is like ~/foobar/, we prefer to store
437
     * "~/foobar/" in the config file, and to expand the ~
438
     * when retrieving the value.
439
     * Also don't do normalization for expiry dates.
440
     */
441
0
    return xstrdup(value);
442
0
  if (type == TYPE_INT)
443
0
    return xstrfmt("%"PRId64, git_config_int64(key, value, kvi));
444
0
  if (type == TYPE_BOOL)
445
0
    return xstrdup(git_config_bool(key, value) ?  "true" : "false");
446
0
  if (type == TYPE_BOOL_OR_INT) {
447
0
    int is_bool, v;
448
0
    v = git_config_bool_or_int(key, value, kvi, &is_bool);
449
0
    if (!is_bool)
450
0
      return xstrfmt("%d", v);
451
0
    else
452
0
      return xstrdup(v ? "true" : "false");
453
0
  }
454
0
  if (type == TYPE_BOOL_OR_STR) {
455
0
    int v = git_parse_maybe_bool(value);
456
0
    if (v < 0)
457
0
      return xstrdup(value);
458
0
    else
459
0
      return xstrdup(v ? "true" : "false");
460
0
  }
461
0
  if (type == TYPE_COLOR) {
462
0
    char v[COLOR_MAXLEN];
463
0
    if (git_config_color(v, key, value))
464
0
      die(_("cannot parse color '%s'"), value);
465
466
    /*
467
     * The contents of `v` now contain an ANSI escape
468
     * sequence, not suitable for including within a
469
     * configuration file. Treat the above as a
470
     * "sanity-check", and return the given value, which we
471
     * know is representable as valid color code.
472
     */
473
0
    return xstrdup(value);
474
0
  }
475
476
0
  BUG("cannot normalize type %d", type);
477
0
}
478
479
static int get_color_found;
480
static const char *get_color_slot;
481
static const char *get_colorbool_slot;
482
static char parsed_color[COLOR_MAXLEN];
483
484
static int git_get_color_config(const char *var, const char *value,
485
        const struct config_context *ctx UNUSED,
486
        void *cb UNUSED)
487
0
{
488
0
  if (!strcmp(var, get_color_slot)) {
489
0
    if (!value)
490
0
      config_error_nonbool(var);
491
0
    if (color_parse(value, parsed_color) < 0)
492
0
      return -1;
493
0
    get_color_found = 1;
494
0
  }
495
0
  return 0;
496
0
}
497
498
static void get_color(const char *var, const char *def_color)
499
0
{
500
0
  get_color_slot = var;
501
0
  get_color_found = 0;
502
0
  parsed_color[0] = '\0';
503
0
  config_with_options(git_get_color_config, NULL,
504
0
          &given_config_source, the_repository,
505
0
          &config_options);
506
507
0
  if (!get_color_found && def_color) {
508
0
    if (color_parse(def_color, parsed_color) < 0)
509
0
      die(_("unable to parse default color value"));
510
0
  }
511
512
0
  fputs(parsed_color, stdout);
513
0
}
514
515
static int get_colorbool_found;
516
static int get_diff_color_found;
517
static int get_color_ui_found;
518
static int git_get_colorbool_config(const char *var, const char *value,
519
            const struct config_context *ctx UNUSED,
520
            void *data UNUSED)
521
0
{
522
0
  if (!strcmp(var, get_colorbool_slot))
523
0
    get_colorbool_found = git_config_colorbool(var, value);
524
0
  else if (!strcmp(var, "diff.color"))
525
0
    get_diff_color_found = git_config_colorbool(var, value);
526
0
  else if (!strcmp(var, "color.ui"))
527
0
    get_color_ui_found = git_config_colorbool(var, value);
528
0
  return 0;
529
0
}
530
531
static int get_colorbool(const char *var, int print)
532
0
{
533
0
  get_colorbool_slot = var;
534
0
  get_colorbool_found = -1;
535
0
  get_diff_color_found = -1;
536
0
  get_color_ui_found = -1;
537
0
  config_with_options(git_get_colorbool_config, NULL,
538
0
          &given_config_source, the_repository,
539
0
          &config_options);
540
541
0
  if (get_colorbool_found < 0) {
542
0
    if (!strcmp(get_colorbool_slot, "color.diff"))
543
0
      get_colorbool_found = get_diff_color_found;
544
0
    if (get_colorbool_found < 0)
545
0
      get_colorbool_found = get_color_ui_found;
546
0
  }
547
548
0
  if (get_colorbool_found < 0)
549
    /* default value if none found in config */
550
0
    get_colorbool_found = GIT_COLOR_AUTO;
551
552
0
  get_colorbool_found = want_color(get_colorbool_found);
553
554
0
  if (print) {
555
0
    printf("%s\n", get_colorbool_found ? "true" : "false");
556
0
    return 0;
557
0
  } else
558
0
    return get_colorbool_found ? 0 : 1;
559
0
}
560
561
static void check_write(void)
562
0
{
563
0
  if (!given_config_source.file && !startup_info->have_repository)
564
0
    die(_("not in a git directory"));
565
566
0
  if (given_config_source.use_stdin)
567
0
    die(_("writing to stdin is not supported"));
568
569
0
  if (given_config_source.blob)
570
0
    die(_("writing config blobs is not supported"));
571
0
}
572
573
struct urlmatch_current_candidate_value {
574
  char value_is_null;
575
  struct strbuf value;
576
  struct key_value_info kvi;
577
};
578
579
static int urlmatch_collect_fn(const char *var, const char *value,
580
             const struct config_context *ctx,
581
             void *cb)
582
0
{
583
0
  struct string_list *values = cb;
584
0
  struct string_list_item *item = string_list_insert(values, var);
585
0
  struct urlmatch_current_candidate_value *matched = item->util;
586
0
  const struct key_value_info *kvi = ctx->kvi;
587
588
0
  if (!matched) {
589
0
    matched = xmalloc(sizeof(*matched));
590
0
    strbuf_init(&matched->value, 0);
591
0
    item->util = matched;
592
0
  } else {
593
0
    strbuf_reset(&matched->value);
594
0
  }
595
0
  matched->kvi = *kvi;
596
597
0
  if (value) {
598
0
    strbuf_addstr(&matched->value, value);
599
0
    matched->value_is_null = 0;
600
0
  } else {
601
0
    matched->value_is_null = 1;
602
0
  }
603
0
  return 0;
604
0
}
605
606
static int get_urlmatch(const char *var, const char *url)
607
0
{
608
0
  int ret;
609
0
  char *section_tail;
610
0
  struct string_list_item *item;
611
0
  struct urlmatch_config config = URLMATCH_CONFIG_INIT;
612
0
  struct string_list values = STRING_LIST_INIT_DUP;
613
614
0
  config.collect_fn = urlmatch_collect_fn;
615
0
  config.cascade_fn = NULL;
616
0
  config.cb = &values;
617
618
0
  if (!url_normalize(url, &config.url))
619
0
    die("%s", config.url.err);
620
621
0
  config.section = xstrdup_tolower(var);
622
0
  section_tail = strchr(config.section, '.');
623
0
  if (section_tail) {
624
0
    *section_tail = '\0';
625
0
    config.key = section_tail + 1;
626
0
    show_keys = 0;
627
0
  } else {
628
0
    config.key = NULL;
629
0
    show_keys = 1;
630
0
  }
631
632
0
  config_with_options(urlmatch_config_entry, &config,
633
0
          &given_config_source, the_repository,
634
0
          &config_options);
635
636
0
  ret = !values.nr;
637
638
0
  for_each_string_list_item(item, &values) {
639
0
    struct urlmatch_current_candidate_value *matched = item->util;
640
0
    struct strbuf buf = STRBUF_INIT;
641
642
0
    format_config(&buf, item->string,
643
0
            matched->value_is_null ? NULL : matched->value.buf,
644
0
            &matched->kvi);
645
0
    fwrite(buf.buf, 1, buf.len, stdout);
646
0
    strbuf_release(&buf);
647
648
0
    strbuf_release(&matched->value);
649
0
  }
650
0
  urlmatch_config_release(&config);
651
0
  string_list_clear(&values, 1);
652
0
  free(config.url.url);
653
654
0
  free((void *)config.section);
655
0
  return ret;
656
0
}
657
658
static char *default_user_config(void)
659
0
{
660
0
  struct strbuf buf = STRBUF_INIT;
661
0
  strbuf_addf(&buf,
662
0
        _("# This is Git's per-user configuration file.\n"
663
0
          "[user]\n"
664
0
          "# Please adapt and uncomment the following lines:\n"
665
0
          "#  name = %s\n"
666
0
          "#  email = %s\n"),
667
0
        ident_default_name(),
668
0
        ident_default_email());
669
0
  return strbuf_detach(&buf, NULL);
670
0
}
671
672
int cmd_config(int argc, const char **argv, const char *prefix)
673
0
{
674
0
  int nongit = !startup_info->have_repository;
675
0
  char *value = NULL;
676
0
  int flags = 0;
677
0
  int ret = 0;
678
0
  struct key_value_info default_kvi = KVI_INIT;
679
680
0
  given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
681
682
0
  argc = parse_options(argc, argv, prefix, builtin_config_options,
683
0
           builtin_config_usage,
684
0
           PARSE_OPT_STOP_AT_NON_OPTION);
685
686
0
  if (use_global_config + use_system_config + use_local_config +
687
0
      use_worktree_config +
688
0
      !!given_config_source.file + !!given_config_source.blob > 1) {
689
0
    error(_("only one config file at a time"));
690
0
    usage_builtin_config();
691
0
  }
692
693
0
  if (nongit) {
694
0
    if (use_local_config)
695
0
      die(_("--local can only be used inside a git repository"));
696
0
    if (given_config_source.blob)
697
0
      die(_("--blob can only be used inside a git repository"));
698
0
    if (use_worktree_config)
699
0
      die(_("--worktree can only be used inside a git repository"));
700
701
0
  }
702
703
0
  if (given_config_source.file &&
704
0
      !strcmp(given_config_source.file, "-")) {
705
0
    given_config_source.file = NULL;
706
0
    given_config_source.use_stdin = 1;
707
0
    given_config_source.scope = CONFIG_SCOPE_COMMAND;
708
0
  }
709
710
0
  if (use_global_config) {
711
0
    char *user_config, *xdg_config;
712
713
0
    git_global_config(&user_config, &xdg_config);
714
0
    if (!user_config)
715
      /*
716
       * It is unknown if HOME/.gitconfig exists, so
717
       * we do not know if we should write to XDG
718
       * location; error out even if XDG_CONFIG_HOME
719
       * is set and points at a sane location.
720
       */
721
0
      die(_("$HOME not set"));
722
723
0
    given_config_source.scope = CONFIG_SCOPE_GLOBAL;
724
725
0
    if (access_or_warn(user_config, R_OK, 0) &&
726
0
        xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
727
0
      given_config_source.file = xdg_config;
728
0
      free(user_config);
729
0
    } else {
730
0
      given_config_source.file = user_config;
731
0
      free(xdg_config);
732
0
    }
733
0
  }
734
0
  else if (use_system_config) {
735
0
    given_config_source.file = git_system_config();
736
0
    given_config_source.scope = CONFIG_SCOPE_SYSTEM;
737
0
  } else if (use_local_config) {
738
0
    given_config_source.file = git_pathdup("config");
739
0
    given_config_source.scope = CONFIG_SCOPE_LOCAL;
740
0
  } else if (use_worktree_config) {
741
0
    struct worktree **worktrees = get_worktrees();
742
0
    if (the_repository->repository_format_worktree_config)
743
0
      given_config_source.file = git_pathdup("config.worktree");
744
0
    else if (worktrees[0] && worktrees[1])
745
0
      die(_("--worktree cannot be used with multiple "
746
0
            "working trees unless the config\n"
747
0
            "extension worktreeConfig is enabled. "
748
0
            "Please read \"CONFIGURATION FILE\"\n"
749
0
            "section in \"git help worktree\" for details"));
750
0
    else
751
0
      given_config_source.file = git_pathdup("config");
752
0
    given_config_source.scope = CONFIG_SCOPE_LOCAL;
753
0
    free_worktrees(worktrees);
754
0
  } else if (given_config_source.file) {
755
0
    if (!is_absolute_path(given_config_source.file) && prefix)
756
0
      given_config_source.file =
757
0
        prefix_filename(prefix, given_config_source.file);
758
0
    given_config_source.scope = CONFIG_SCOPE_COMMAND;
759
0
  } else if (given_config_source.blob) {
760
0
    given_config_source.scope = CONFIG_SCOPE_COMMAND;
761
0
  }
762
763
764
0
  if (respect_includes_opt == -1)
765
0
    config_options.respect_includes = !given_config_source.file;
766
0
  else
767
0
    config_options.respect_includes = respect_includes_opt;
768
0
  if (!nongit) {
769
0
    config_options.commondir = get_git_common_dir();
770
0
    config_options.git_dir = get_git_dir();
771
0
  }
772
773
0
  if (end_nul) {
774
0
    term = '\0';
775
0
    delim = '\n';
776
0
    key_delim = '\n';
777
0
  }
778
779
0
  if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
780
0
    error(_("--get-color and variable type are incoherent"));
781
0
    usage_builtin_config();
782
0
  }
783
784
0
  if (HAS_MULTI_BITS(actions)) {
785
0
    error(_("only one action at a time"));
786
0
    usage_builtin_config();
787
0
  }
788
0
  if (actions == 0)
789
0
    switch (argc) {
790
0
    case 1: actions = ACTION_GET; break;
791
0
    case 2: actions = ACTION_SET; break;
792
0
    case 3: actions = ACTION_SET_ALL; break;
793
0
    default:
794
0
      usage_builtin_config();
795
0
    }
796
0
  if (omit_values &&
797
0
      !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
798
0
    error(_("--name-only is only applicable to --list or --get-regexp"));
799
0
    usage_builtin_config();
800
0
  }
801
802
0
  if (show_origin && !(actions &
803
0
    (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
804
0
    error(_("--show-origin is only applicable to --get, --get-all, "
805
0
      "--get-regexp, and --list"));
806
0
    usage_builtin_config();
807
0
  }
808
809
0
  if (default_value && !(actions & ACTION_GET)) {
810
0
    error(_("--default is only applicable to --get"));
811
0
    usage_builtin_config();
812
0
  }
813
814
  /* check usage of --fixed-value */
815
0
  if (fixed_value) {
816
0
    int allowed_usage = 0;
817
818
0
    switch (actions) {
819
    /* git config --get <name> <value-pattern> */
820
0
    case ACTION_GET:
821
    /* git config --get-all <name> <value-pattern> */
822
0
    case ACTION_GET_ALL:
823
    /* git config --get-regexp <name-pattern> <value-pattern> */
824
0
    case ACTION_GET_REGEXP:
825
    /* git config --unset <name> <value-pattern> */
826
0
    case ACTION_UNSET:
827
    /* git config --unset-all <name> <value-pattern> */
828
0
    case ACTION_UNSET_ALL:
829
0
      allowed_usage = argc > 1 && !!argv[1];
830
0
      break;
831
832
    /* git config <name> <value> <value-pattern> */
833
0
    case ACTION_SET_ALL:
834
    /* git config --replace-all <name> <value> <value-pattern> */
835
0
    case ACTION_REPLACE_ALL:
836
0
      allowed_usage = argc > 2 && !!argv[2];
837
0
      break;
838
839
    /* other options don't allow --fixed-value */
840
0
    }
841
842
0
    if (!allowed_usage) {
843
0
      error(_("--fixed-value only applies with 'value-pattern'"));
844
0
      usage_builtin_config();
845
0
    }
846
847
0
    flags |= CONFIG_FLAGS_FIXED_VALUE;
848
0
  }
849
850
0
  if (actions & PAGING_ACTIONS)
851
0
    setup_auto_pager("config", 1);
852
853
0
  if (actions == ACTION_LIST) {
854
0
    check_argc(argc, 0, 0);
855
0
    if (config_with_options(show_all_config, NULL,
856
0
          &given_config_source, the_repository,
857
0
          &config_options) < 0) {
858
0
      if (given_config_source.file)
859
0
        die_errno(_("unable to read config file '%s'"),
860
0
            given_config_source.file);
861
0
      else
862
0
        die(_("error processing config file(s)"));
863
0
    }
864
0
  }
865
0
  else if (actions == ACTION_EDIT) {
866
0
    char *config_file;
867
868
0
    check_argc(argc, 0, 0);
869
0
    if (!given_config_source.file && nongit)
870
0
      die(_("not in a git directory"));
871
0
    if (given_config_source.use_stdin)
872
0
      die(_("editing stdin is not supported"));
873
0
    if (given_config_source.blob)
874
0
      die(_("editing blobs is not supported"));
875
0
    git_config(git_default_config, NULL);
876
0
    config_file = given_config_source.file ?
877
0
        xstrdup(given_config_source.file) :
878
0
        git_pathdup("config");
879
0
    if (use_global_config) {
880
0
      int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
881
0
      if (fd >= 0) {
882
0
        char *content = default_user_config();
883
0
        write_str_in_full(fd, content);
884
0
        free(content);
885
0
        close(fd);
886
0
      }
887
0
      else if (errno != EEXIST)
888
0
        die_errno(_("cannot create configuration file %s"), config_file);
889
0
    }
890
0
    launch_editor(config_file, NULL, NULL);
891
0
    free(config_file);
892
0
  }
893
0
  else if (actions == ACTION_SET) {
894
0
    check_write();
895
0
    check_argc(argc, 2, 2);
896
0
    value = normalize_value(argv[0], argv[1], &default_kvi);
897
0
    ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
898
0
    if (ret == CONFIG_NOTHING_SET)
899
0
      error(_("cannot overwrite multiple values with a single value\n"
900
0
      "       Use a regexp, --add or --replace-all to change %s."), argv[0]);
901
0
  }
902
0
  else if (actions == ACTION_SET_ALL) {
903
0
    check_write();
904
0
    check_argc(argc, 2, 3);
905
0
    value = normalize_value(argv[0], argv[1], &default_kvi);
906
0
    ret = git_config_set_multivar_in_file_gently(given_config_source.file,
907
0
                   argv[0], value, argv[2],
908
0
                   flags);
909
0
  }
910
0
  else if (actions == ACTION_ADD) {
911
0
    check_write();
912
0
    check_argc(argc, 2, 2);
913
0
    value = normalize_value(argv[0], argv[1], &default_kvi);
914
0
    ret = git_config_set_multivar_in_file_gently(given_config_source.file,
915
0
                   argv[0], value,
916
0
                   CONFIG_REGEX_NONE,
917
0
                   flags);
918
0
  }
919
0
  else if (actions == ACTION_REPLACE_ALL) {
920
0
    check_write();
921
0
    check_argc(argc, 2, 3);
922
0
    value = normalize_value(argv[0], argv[1], &default_kvi);
923
0
    ret = git_config_set_multivar_in_file_gently(given_config_source.file,
924
0
                   argv[0], value, argv[2],
925
0
                   flags | CONFIG_FLAGS_MULTI_REPLACE);
926
0
  }
927
0
  else if (actions == ACTION_GET) {
928
0
    check_argc(argc, 1, 2);
929
0
    return get_value(argv[0], argv[1], flags);
930
0
  }
931
0
  else if (actions == ACTION_GET_ALL) {
932
0
    do_all = 1;
933
0
    check_argc(argc, 1, 2);
934
0
    return get_value(argv[0], argv[1], flags);
935
0
  }
936
0
  else if (actions == ACTION_GET_REGEXP) {
937
0
    show_keys = 1;
938
0
    use_key_regexp = 1;
939
0
    do_all = 1;
940
0
    check_argc(argc, 1, 2);
941
0
    return get_value(argv[0], argv[1], flags);
942
0
  }
943
0
  else if (actions == ACTION_GET_URLMATCH) {
944
0
    check_argc(argc, 2, 2);
945
0
    return get_urlmatch(argv[0], argv[1]);
946
0
  }
947
0
  else if (actions == ACTION_UNSET) {
948
0
    check_write();
949
0
    check_argc(argc, 1, 2);
950
0
    if (argc == 2)
951
0
      return git_config_set_multivar_in_file_gently(given_config_source.file,
952
0
                      argv[0], NULL, argv[1],
953
0
                      flags);
954
0
    else
955
0
      return git_config_set_in_file_gently(given_config_source.file,
956
0
                   argv[0], NULL);
957
0
  }
958
0
  else if (actions == ACTION_UNSET_ALL) {
959
0
    check_write();
960
0
    check_argc(argc, 1, 2);
961
0
    return git_config_set_multivar_in_file_gently(given_config_source.file,
962
0
                    argv[0], NULL, argv[1],
963
0
                    flags | CONFIG_FLAGS_MULTI_REPLACE);
964
0
  }
965
0
  else if (actions == ACTION_RENAME_SECTION) {
966
0
    check_write();
967
0
    check_argc(argc, 2, 2);
968
0
    ret = git_config_rename_section_in_file(given_config_source.file,
969
0
              argv[0], argv[1]);
970
0
    if (ret < 0)
971
0
      return ret;
972
0
    else if (!ret)
973
0
      die(_("no such section: %s"), argv[0]);
974
0
    else
975
0
      ret = 0;
976
0
  }
977
0
  else if (actions == ACTION_REMOVE_SECTION) {
978
0
    check_write();
979
0
    check_argc(argc, 1, 1);
980
0
    ret = git_config_rename_section_in_file(given_config_source.file,
981
0
              argv[0], NULL);
982
0
    if (ret < 0)
983
0
      return ret;
984
0
    else if (!ret)
985
0
      die(_("no such section: %s"), argv[0]);
986
0
    else
987
0
      ret = 0;
988
0
  }
989
0
  else if (actions == ACTION_GET_COLOR) {
990
0
    check_argc(argc, 1, 2);
991
0
    get_color(argv[0], argv[1]);
992
0
  }
993
0
  else if (actions == ACTION_GET_COLORBOOL) {
994
0
    check_argc(argc, 1, 2);
995
0
    if (argc == 2)
996
0
      color_stdout_is_tty = git_config_bool("command line", argv[1]);
997
0
    return get_colorbool(argv[0], argc == 2);
998
0
  }
999
1000
0
  free(value);
1001
0
  return ret;
1002
0
}