builtin / config.con commit coccinelle: make use of the "type" FREE_AND_NULL() rule (6a83d90)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "color.h"
   4#include "parse-options.h"
   5#include "urlmatch.h"
   6#include "quote.h"
   7
   8static const char *const builtin_config_usage[] = {
   9        N_("git config [<options>]"),
  10        NULL
  11};
  12
  13static char *key;
  14static regex_t *key_regexp;
  15static regex_t *regexp;
  16static int show_keys;
  17static int omit_values;
  18static int use_key_regexp;
  19static int do_all;
  20static int do_not_match;
  21static char delim = '=';
  22static char key_delim = ' ';
  23static char term = '\n';
  24
  25static int use_global_config, use_system_config, use_local_config;
  26static struct git_config_source given_config_source;
  27static int actions, types;
  28static int end_null;
  29static int respect_includes_opt = -1;
  30static struct config_options config_options;
  31static int show_origin;
  32
  33#define ACTION_GET (1<<0)
  34#define ACTION_GET_ALL (1<<1)
  35#define ACTION_GET_REGEXP (1<<2)
  36#define ACTION_REPLACE_ALL (1<<3)
  37#define ACTION_ADD (1<<4)
  38#define ACTION_UNSET (1<<5)
  39#define ACTION_UNSET_ALL (1<<6)
  40#define ACTION_RENAME_SECTION (1<<7)
  41#define ACTION_REMOVE_SECTION (1<<8)
  42#define ACTION_LIST (1<<9)
  43#define ACTION_EDIT (1<<10)
  44#define ACTION_SET (1<<11)
  45#define ACTION_SET_ALL (1<<12)
  46#define ACTION_GET_COLOR (1<<13)
  47#define ACTION_GET_COLORBOOL (1<<14)
  48#define ACTION_GET_URLMATCH (1<<15)
  49
  50#define TYPE_BOOL (1<<0)
  51#define TYPE_INT (1<<1)
  52#define TYPE_BOOL_OR_INT (1<<2)
  53#define TYPE_PATH (1<<3)
  54
  55static struct option builtin_config_options[] = {
  56        OPT_GROUP(N_("Config file location")),
  57        OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
  58        OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
  59        OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
  60        OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
  61        OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
  62        OPT_GROUP(N_("Action")),
  63        OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
  64        OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
  65        OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
  66        OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
  67        OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
  68        OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
  69        OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
  70        OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
  71        OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
  72        OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
  73        OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
  74        OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
  75        OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
  76        OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
  77        OPT_GROUP(N_("Type")),
  78        OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
  79        OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
  80        OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
  81        OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
  82        OPT_GROUP(N_("Other")),
  83        OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
  84        OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
  85        OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
  86        OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
  87        OPT_END(),
  88};
  89
  90static void check_argc(int argc, int min, int max) {
  91        if (argc >= min && argc <= max)
  92                return;
  93        error("wrong number of arguments");
  94        usage_with_options(builtin_config_usage, builtin_config_options);
  95}
  96
  97static void show_config_origin(struct strbuf *buf)
  98{
  99        const char term = end_null ? '\0' : '\t';
 100
 101        strbuf_addstr(buf, current_config_origin_type());
 102        strbuf_addch(buf, ':');
 103        if (end_null)
 104                strbuf_addstr(buf, current_config_name());
 105        else
 106                quote_c_style(current_config_name(), buf, NULL, 0);
 107        strbuf_addch(buf, term);
 108}
 109
 110static int show_all_config(const char *key_, const char *value_, void *cb)
 111{
 112        if (show_origin) {
 113                struct strbuf buf = STRBUF_INIT;
 114                show_config_origin(&buf);
 115                /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
 116                fwrite(buf.buf, 1, buf.len, stdout);
 117                strbuf_release(&buf);
 118        }
 119        if (!omit_values && value_)
 120                printf("%s%c%s%c", key_, delim, value_, term);
 121        else
 122                printf("%s%c", key_, term);
 123        return 0;
 124}
 125
 126struct strbuf_list {
 127        struct strbuf *items;
 128        int nr;
 129        int alloc;
 130};
 131
 132static int format_config(struct strbuf *buf, const char *key_, const char *value_)
 133{
 134        if (show_origin)
 135                show_config_origin(buf);
 136        if (show_keys)
 137                strbuf_addstr(buf, key_);
 138        if (!omit_values) {
 139                if (show_keys)
 140                        strbuf_addch(buf, key_delim);
 141
 142                if (types == TYPE_INT)
 143                        strbuf_addf(buf, "%"PRId64,
 144                                    git_config_int64(key_, value_ ? value_ : ""));
 145                else if (types == TYPE_BOOL)
 146                        strbuf_addstr(buf, git_config_bool(key_, value_) ?
 147                                      "true" : "false");
 148                else if (types == TYPE_BOOL_OR_INT) {
 149                        int is_bool, v;
 150                        v = git_config_bool_or_int(key_, value_, &is_bool);
 151                        if (is_bool)
 152                                strbuf_addstr(buf, v ? "true" : "false");
 153                        else
 154                                strbuf_addf(buf, "%d", v);
 155                } else if (types == TYPE_PATH) {
 156                        const char *v;
 157                        if (git_config_pathname(&v, key_, value_) < 0)
 158                                return -1;
 159                        strbuf_addstr(buf, v);
 160                        free((char *)v);
 161                } else if (value_) {
 162                        strbuf_addstr(buf, value_);
 163                } else {
 164                        /* Just show the key name; back out delimiter */
 165                        if (show_keys)
 166                                strbuf_setlen(buf, buf->len - 1);
 167                }
 168        }
 169        strbuf_addch(buf, term);
 170        return 0;
 171}
 172
 173static int collect_config(const char *key_, const char *value_, void *cb)
 174{
 175        struct strbuf_list *values = cb;
 176
 177        if (!use_key_regexp && strcmp(key_, key))
 178                return 0;
 179        if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
 180                return 0;
 181        if (regexp != NULL &&
 182            (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
 183                return 0;
 184
 185        ALLOC_GROW(values->items, values->nr + 1, values->alloc);
 186        strbuf_init(&values->items[values->nr], 0);
 187
 188        return format_config(&values->items[values->nr++], key_, value_);
 189}
 190
 191static int get_value(const char *key_, const char *regex_)
 192{
 193        int ret = CONFIG_GENERIC_ERROR;
 194        struct strbuf_list values = {NULL};
 195        int i;
 196
 197        if (use_key_regexp) {
 198                char *tl;
 199
 200                /*
 201                 * NEEDSWORK: this naive pattern lowercasing obviously does not
 202                 * work for more complex patterns like "^[^.]*Foo.*bar".
 203                 * Perhaps we should deprecate this altogether someday.
 204                 */
 205
 206                key = xstrdup(key_);
 207                for (tl = key + strlen(key) - 1;
 208                     tl >= key && *tl != '.';
 209                     tl--)
 210                        *tl = tolower(*tl);
 211                for (tl = key; *tl && *tl != '.'; tl++)
 212                        *tl = tolower(*tl);
 213
 214                key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
 215                if (regcomp(key_regexp, key, REG_EXTENDED)) {
 216                        error("invalid key pattern: %s", key_);
 217                        FREE_AND_NULL(key_regexp);
 218                        ret = CONFIG_INVALID_PATTERN;
 219                        goto free_strings;
 220                }
 221        } else {
 222                if (git_config_parse_key(key_, &key, NULL)) {
 223                        ret = CONFIG_INVALID_KEY;
 224                        goto free_strings;
 225                }
 226        }
 227
 228        if (regex_) {
 229                if (regex_[0] == '!') {
 230                        do_not_match = 1;
 231                        regex_++;
 232                }
 233
 234                regexp = (regex_t*)xmalloc(sizeof(regex_t));
 235                if (regcomp(regexp, regex_, REG_EXTENDED)) {
 236                        error("invalid pattern: %s", regex_);
 237                        FREE_AND_NULL(regexp);
 238                        ret = CONFIG_INVALID_PATTERN;
 239                        goto free_strings;
 240                }
 241        }
 242
 243        git_config_with_options(collect_config, &values,
 244                                &given_config_source, &config_options);
 245
 246        ret = !values.nr;
 247
 248        for (i = 0; i < values.nr; i++) {
 249                struct strbuf *buf = values.items + i;
 250                if (do_all || i == values.nr - 1)
 251                        fwrite(buf->buf, 1, buf->len, stdout);
 252                strbuf_release(buf);
 253        }
 254        free(values.items);
 255
 256free_strings:
 257        free(key);
 258        if (key_regexp) {
 259                regfree(key_regexp);
 260                free(key_regexp);
 261        }
 262        if (regexp) {
 263                regfree(regexp);
 264                free(regexp);
 265        }
 266
 267        return ret;
 268}
 269
 270static char *normalize_value(const char *key, const char *value)
 271{
 272        if (!value)
 273                return NULL;
 274
 275        if (types == 0 || types == TYPE_PATH)
 276                /*
 277                 * We don't do normalization for TYPE_PATH here: If
 278                 * the path is like ~/foobar/, we prefer to store
 279                 * "~/foobar/" in the config file, and to expand the ~
 280                 * when retrieving the value.
 281                 */
 282                return xstrdup(value);
 283        if (types == TYPE_INT)
 284                return xstrfmt("%"PRId64, git_config_int64(key, value));
 285        if (types == TYPE_BOOL)
 286                return xstrdup(git_config_bool(key, value) ?  "true" : "false");
 287        if (types == TYPE_BOOL_OR_INT) {
 288                int is_bool, v;
 289                v = git_config_bool_or_int(key, value, &is_bool);
 290                if (!is_bool)
 291                        return xstrfmt("%d", v);
 292                else
 293                        return xstrdup(v ? "true" : "false");
 294        }
 295
 296        die("BUG: cannot normalize type %d", types);
 297}
 298
 299static int get_color_found;
 300static const char *get_color_slot;
 301static const char *get_colorbool_slot;
 302static char parsed_color[COLOR_MAXLEN];
 303
 304static int git_get_color_config(const char *var, const char *value, void *cb)
 305{
 306        if (!strcmp(var, get_color_slot)) {
 307                if (!value)
 308                        config_error_nonbool(var);
 309                if (color_parse(value, parsed_color) < 0)
 310                        return -1;
 311                get_color_found = 1;
 312        }
 313        return 0;
 314}
 315
 316static void get_color(const char *var, const char *def_color)
 317{
 318        get_color_slot = var;
 319        get_color_found = 0;
 320        parsed_color[0] = '\0';
 321        git_config_with_options(git_get_color_config, NULL,
 322                                &given_config_source, &config_options);
 323
 324        if (!get_color_found && def_color) {
 325                if (color_parse(def_color, parsed_color) < 0)
 326                        die(_("unable to parse default color value"));
 327        }
 328
 329        fputs(parsed_color, stdout);
 330}
 331
 332static int get_colorbool_found;
 333static int get_diff_color_found;
 334static int get_color_ui_found;
 335static int git_get_colorbool_config(const char *var, const char *value,
 336                void *cb)
 337{
 338        if (!strcmp(var, get_colorbool_slot))
 339                get_colorbool_found = git_config_colorbool(var, value);
 340        else if (!strcmp(var, "diff.color"))
 341                get_diff_color_found = git_config_colorbool(var, value);
 342        else if (!strcmp(var, "color.ui"))
 343                get_color_ui_found = git_config_colorbool(var, value);
 344        return 0;
 345}
 346
 347static int get_colorbool(const char *var, int print)
 348{
 349        get_colorbool_slot = var;
 350        get_colorbool_found = -1;
 351        get_diff_color_found = -1;
 352        get_color_ui_found = -1;
 353        git_config_with_options(git_get_colorbool_config, NULL,
 354                                &given_config_source, &config_options);
 355
 356        if (get_colorbool_found < 0) {
 357                if (!strcmp(get_colorbool_slot, "color.diff"))
 358                        get_colorbool_found = get_diff_color_found;
 359                if (get_colorbool_found < 0)
 360                        get_colorbool_found = get_color_ui_found;
 361        }
 362
 363        if (get_colorbool_found < 0)
 364                /* default value if none found in config */
 365                get_colorbool_found = GIT_COLOR_AUTO;
 366
 367        get_colorbool_found = want_color(get_colorbool_found);
 368
 369        if (print) {
 370                printf("%s\n", get_colorbool_found ? "true" : "false");
 371                return 0;
 372        } else
 373                return get_colorbool_found ? 0 : 1;
 374}
 375
 376static void check_write(void)
 377{
 378        if (!given_config_source.file && !startup_info->have_repository)
 379                die("not in a git directory");
 380
 381        if (given_config_source.use_stdin)
 382                die("writing to stdin is not supported");
 383
 384        if (given_config_source.blob)
 385                die("writing config blobs is not supported");
 386}
 387
 388struct urlmatch_current_candidate_value {
 389        char value_is_null;
 390        struct strbuf value;
 391};
 392
 393static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
 394{
 395        struct string_list *values = cb;
 396        struct string_list_item *item = string_list_insert(values, var);
 397        struct urlmatch_current_candidate_value *matched = item->util;
 398
 399        if (!matched) {
 400                matched = xmalloc(sizeof(*matched));
 401                strbuf_init(&matched->value, 0);
 402                item->util = matched;
 403        } else {
 404                strbuf_reset(&matched->value);
 405        }
 406
 407        if (value) {
 408                strbuf_addstr(&matched->value, value);
 409                matched->value_is_null = 0;
 410        } else {
 411                matched->value_is_null = 1;
 412        }
 413        return 0;
 414}
 415
 416static int get_urlmatch(const char *var, const char *url)
 417{
 418        int ret;
 419        char *section_tail;
 420        struct string_list_item *item;
 421        struct urlmatch_config config = { STRING_LIST_INIT_DUP };
 422        struct string_list values = STRING_LIST_INIT_DUP;
 423
 424        config.collect_fn = urlmatch_collect_fn;
 425        config.cascade_fn = NULL;
 426        config.cb = &values;
 427
 428        if (!url_normalize(url, &config.url))
 429                die("%s", config.url.err);
 430
 431        config.section = xstrdup_tolower(var);
 432        section_tail = strchr(config.section, '.');
 433        if (section_tail) {
 434                *section_tail = '\0';
 435                config.key = section_tail + 1;
 436                show_keys = 0;
 437        } else {
 438                config.key = NULL;
 439                show_keys = 1;
 440        }
 441
 442        git_config_with_options(urlmatch_config_entry, &config,
 443                                &given_config_source, &config_options);
 444
 445        ret = !values.nr;
 446
 447        for_each_string_list_item(item, &values) {
 448                struct urlmatch_current_candidate_value *matched = item->util;
 449                struct strbuf buf = STRBUF_INIT;
 450
 451                format_config(&buf, item->string,
 452                              matched->value_is_null ? NULL : matched->value.buf);
 453                fwrite(buf.buf, 1, buf.len, stdout);
 454                strbuf_release(&buf);
 455
 456                strbuf_release(&matched->value);
 457        }
 458        string_list_clear(&config.vars, 1);
 459        string_list_clear(&values, 1);
 460        free(config.url.url);
 461
 462        free((void *)config.section);
 463        return ret;
 464}
 465
 466static char *default_user_config(void)
 467{
 468        struct strbuf buf = STRBUF_INIT;
 469        strbuf_addf(&buf,
 470                    _("# This is Git's per-user configuration file.\n"
 471                      "[user]\n"
 472                      "# Please adapt and uncomment the following lines:\n"
 473                      "#        name = %s\n"
 474                      "#        email = %s\n"),
 475                    ident_default_name(),
 476                    ident_default_email());
 477        return strbuf_detach(&buf, NULL);
 478}
 479
 480int cmd_config(int argc, const char **argv, const char *prefix)
 481{
 482        int nongit = !startup_info->have_repository;
 483        char *value;
 484
 485        given_config_source.file = getenv(CONFIG_ENVIRONMENT);
 486
 487        argc = parse_options(argc, argv, prefix, builtin_config_options,
 488                             builtin_config_usage,
 489                             PARSE_OPT_STOP_AT_NON_OPTION);
 490
 491        if (use_global_config + use_system_config + use_local_config +
 492            !!given_config_source.file + !!given_config_source.blob > 1) {
 493                error("only one config file at a time.");
 494                usage_with_options(builtin_config_usage, builtin_config_options);
 495        }
 496
 497        if (use_local_config && nongit)
 498                die(_("--local can only be used inside a git repository"));
 499
 500        if (given_config_source.file &&
 501                        !strcmp(given_config_source.file, "-")) {
 502                given_config_source.file = NULL;
 503                given_config_source.use_stdin = 1;
 504        }
 505
 506        if (use_global_config) {
 507                char *user_config = expand_user_path("~/.gitconfig", 0);
 508                char *xdg_config = xdg_config_home("config");
 509
 510                if (!user_config)
 511                        /*
 512                         * It is unknown if HOME/.gitconfig exists, so
 513                         * we do not know if we should write to XDG
 514                         * location; error out even if XDG_CONFIG_HOME
 515                         * is set and points at a sane location.
 516                         */
 517                        die("$HOME not set");
 518
 519                if (access_or_warn(user_config, R_OK, 0) &&
 520                    xdg_config && !access_or_warn(xdg_config, R_OK, 0))
 521                        given_config_source.file = xdg_config;
 522                else
 523                        given_config_source.file = user_config;
 524        }
 525        else if (use_system_config)
 526                given_config_source.file = git_etc_gitconfig();
 527        else if (use_local_config)
 528                given_config_source.file = git_pathdup("config");
 529        else if (given_config_source.file) {
 530                if (!is_absolute_path(given_config_source.file) && prefix)
 531                        given_config_source.file =
 532                                prefix_filename(prefix, given_config_source.file);
 533        }
 534
 535        if (respect_includes_opt == -1)
 536                config_options.respect_includes = !given_config_source.file;
 537        else
 538                config_options.respect_includes = respect_includes_opt;
 539
 540        if (end_null) {
 541                term = '\0';
 542                delim = '\n';
 543                key_delim = '\n';
 544        }
 545
 546        if (HAS_MULTI_BITS(types)) {
 547                error("only one type at a time.");
 548                usage_with_options(builtin_config_usage, builtin_config_options);
 549        }
 550
 551        if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
 552                error("--get-color and variable type are incoherent");
 553                usage_with_options(builtin_config_usage, builtin_config_options);
 554        }
 555
 556        if (HAS_MULTI_BITS(actions)) {
 557                error("only one action at a time.");
 558                usage_with_options(builtin_config_usage, builtin_config_options);
 559        }
 560        if (actions == 0)
 561                switch (argc) {
 562                case 1: actions = ACTION_GET; break;
 563                case 2: actions = ACTION_SET; break;
 564                case 3: actions = ACTION_SET_ALL; break;
 565                default:
 566                        usage_with_options(builtin_config_usage, builtin_config_options);
 567                }
 568        if (omit_values &&
 569            !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
 570                error("--name-only is only applicable to --list or --get-regexp");
 571                usage_with_options(builtin_config_usage, builtin_config_options);
 572        }
 573
 574        if (show_origin && !(actions &
 575                (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
 576                error("--show-origin is only applicable to --get, --get-all, "
 577                          "--get-regexp, and --list.");
 578                usage_with_options(builtin_config_usage, builtin_config_options);
 579        }
 580
 581        if (actions == ACTION_LIST) {
 582                check_argc(argc, 0, 0);
 583                if (git_config_with_options(show_all_config, NULL,
 584                                            &given_config_source,
 585                                            &config_options) < 0) {
 586                        if (given_config_source.file)
 587                                die_errno("unable to read config file '%s'",
 588                                          given_config_source.file);
 589                        else
 590                                die("error processing config file(s)");
 591                }
 592        }
 593        else if (actions == ACTION_EDIT) {
 594                char *config_file;
 595
 596                check_argc(argc, 0, 0);
 597                if (!given_config_source.file && nongit)
 598                        die("not in a git directory");
 599                if (given_config_source.use_stdin)
 600                        die("editing stdin is not supported");
 601                if (given_config_source.blob)
 602                        die("editing blobs is not supported");
 603                git_config(git_default_config, NULL);
 604                config_file = given_config_source.file ?
 605                                xstrdup(given_config_source.file) :
 606                                git_pathdup("config");
 607                if (use_global_config) {
 608                        int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
 609                        if (fd >= 0) {
 610                                char *content = default_user_config();
 611                                write_str_in_full(fd, content);
 612                                free(content);
 613                                close(fd);
 614                        }
 615                        else if (errno != EEXIST)
 616                                die_errno(_("cannot create configuration file %s"), config_file);
 617                }
 618                launch_editor(config_file, NULL, NULL);
 619                free(config_file);
 620        }
 621        else if (actions == ACTION_SET) {
 622                int ret;
 623                check_write();
 624                check_argc(argc, 2, 2);
 625                value = normalize_value(argv[0], argv[1]);
 626                ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
 627                if (ret == CONFIG_NOTHING_SET)
 628                        error(_("cannot overwrite multiple values with a single value\n"
 629                        "       Use a regexp, --add or --replace-all to change %s."), argv[0]);
 630                return ret;
 631        }
 632        else if (actions == ACTION_SET_ALL) {
 633                check_write();
 634                check_argc(argc, 2, 3);
 635                value = normalize_value(argv[0], argv[1]);
 636                return git_config_set_multivar_in_file_gently(given_config_source.file,
 637                                                              argv[0], value, argv[2], 0);
 638        }
 639        else if (actions == ACTION_ADD) {
 640                check_write();
 641                check_argc(argc, 2, 2);
 642                value = normalize_value(argv[0], argv[1]);
 643                return git_config_set_multivar_in_file_gently(given_config_source.file,
 644                                                              argv[0], value,
 645                                                              CONFIG_REGEX_NONE, 0);
 646        }
 647        else if (actions == ACTION_REPLACE_ALL) {
 648                check_write();
 649                check_argc(argc, 2, 3);
 650                value = normalize_value(argv[0], argv[1]);
 651                return git_config_set_multivar_in_file_gently(given_config_source.file,
 652                                                              argv[0], value, argv[2], 1);
 653        }
 654        else if (actions == ACTION_GET) {
 655                check_argc(argc, 1, 2);
 656                return get_value(argv[0], argv[1]);
 657        }
 658        else if (actions == ACTION_GET_ALL) {
 659                do_all = 1;
 660                check_argc(argc, 1, 2);
 661                return get_value(argv[0], argv[1]);
 662        }
 663        else if (actions == ACTION_GET_REGEXP) {
 664                show_keys = 1;
 665                use_key_regexp = 1;
 666                do_all = 1;
 667                check_argc(argc, 1, 2);
 668                return get_value(argv[0], argv[1]);
 669        }
 670        else if (actions == ACTION_GET_URLMATCH) {
 671                check_argc(argc, 2, 2);
 672                return get_urlmatch(argv[0], argv[1]);
 673        }
 674        else if (actions == ACTION_UNSET) {
 675                check_write();
 676                check_argc(argc, 1, 2);
 677                if (argc == 2)
 678                        return git_config_set_multivar_in_file_gently(given_config_source.file,
 679                                                                      argv[0], NULL, argv[1], 0);
 680                else
 681                        return git_config_set_in_file_gently(given_config_source.file,
 682                                                             argv[0], NULL);
 683        }
 684        else if (actions == ACTION_UNSET_ALL) {
 685                check_write();
 686                check_argc(argc, 1, 2);
 687                return git_config_set_multivar_in_file_gently(given_config_source.file,
 688                                                              argv[0], NULL, argv[1], 1);
 689        }
 690        else if (actions == ACTION_RENAME_SECTION) {
 691                int ret;
 692                check_write();
 693                check_argc(argc, 2, 2);
 694                ret = git_config_rename_section_in_file(given_config_source.file,
 695                                                        argv[0], argv[1]);
 696                if (ret < 0)
 697                        return ret;
 698                if (ret == 0)
 699                        die("No such section!");
 700        }
 701        else if (actions == ACTION_REMOVE_SECTION) {
 702                int ret;
 703                check_write();
 704                check_argc(argc, 1, 1);
 705                ret = git_config_rename_section_in_file(given_config_source.file,
 706                                                        argv[0], NULL);
 707                if (ret < 0)
 708                        return ret;
 709                if (ret == 0)
 710                        die("No such section!");
 711        }
 712        else if (actions == ACTION_GET_COLOR) {
 713                check_argc(argc, 1, 2);
 714                get_color(argv[0], argv[1]);
 715        }
 716        else if (actions == ACTION_GET_COLORBOOL) {
 717                check_argc(argc, 1, 2);
 718                if (argc == 2)
 719                        color_stdout_is_tty = git_config_bool("command line", argv[1]);
 720                return get_colorbool(argv[0], argc == 2);
 721        }
 722
 723        return 0;
 724}