config.con commit git-send-email: allow an email alias for --from (ae740a5)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 * Copyright (C) Johannes Schindelin, 2005
   6 *
   7 */
   8#include "cache.h"
   9
  10#define MAXNAME (256)
  11
  12static FILE *config_file;
  13static const char *config_file_name;
  14static int config_linenr;
  15static int zlib_compression_seen;
  16
  17static int get_next_char(void)
  18{
  19        int c;
  20        FILE *f;
  21
  22        c = '\n';
  23        if ((f = config_file) != NULL) {
  24                c = fgetc(f);
  25                if (c == '\r') {
  26                        /* DOS like systems */
  27                        c = fgetc(f);
  28                        if (c != '\n') {
  29                                ungetc(c, f);
  30                                c = '\r';
  31                        }
  32                }
  33                if (c == '\n')
  34                        config_linenr++;
  35                if (c == EOF) {
  36                        config_file = NULL;
  37                        c = '\n';
  38                }
  39        }
  40        return c;
  41}
  42
  43static char *parse_value(void)
  44{
  45        static char value[1024];
  46        int quote = 0, comment = 0, len = 0, space = 0;
  47
  48        for (;;) {
  49                int c = get_next_char();
  50                if (len >= sizeof(value))
  51                        return NULL;
  52                if (c == '\n') {
  53                        if (quote)
  54                                return NULL;
  55                        value[len] = 0;
  56                        return value;
  57                }
  58                if (comment)
  59                        continue;
  60                if (isspace(c) && !quote) {
  61                        space = 1;
  62                        continue;
  63                }
  64                if (!quote) {
  65                        if (c == ';' || c == '#') {
  66                                comment = 1;
  67                                continue;
  68                        }
  69                }
  70                if (space) {
  71                        if (len)
  72                                value[len++] = ' ';
  73                        space = 0;
  74                }
  75                if (c == '\\') {
  76                        c = get_next_char();
  77                        switch (c) {
  78                        case '\n':
  79                                continue;
  80                        case 't':
  81                                c = '\t';
  82                                break;
  83                        case 'b':
  84                                c = '\b';
  85                                break;
  86                        case 'n':
  87                                c = '\n';
  88                                break;
  89                        /* Some characters escape as themselves */
  90                        case '\\': case '"':
  91                                break;
  92                        /* Reject unknown escape sequences */
  93                        default:
  94                                return NULL;
  95                        }
  96                        value[len++] = c;
  97                        continue;
  98                }
  99                if (c == '"') {
 100                        quote = 1-quote;
 101                        continue;
 102                }
 103                value[len++] = c;
 104        }
 105}
 106
 107static inline int iskeychar(int c)
 108{
 109        return isalnum(c) || c == '-';
 110}
 111
 112static int get_value(config_fn_t fn, char *name, unsigned int len)
 113{
 114        int c;
 115        char *value;
 116
 117        /* Get the full name */
 118        for (;;) {
 119                c = get_next_char();
 120                if (c == EOF)
 121                        break;
 122                if (!iskeychar(c))
 123                        break;
 124                name[len++] = tolower(c);
 125                if (len >= MAXNAME)
 126                        return -1;
 127        }
 128        name[len] = 0;
 129        while (c == ' ' || c == '\t')
 130                c = get_next_char();
 131
 132        value = NULL;
 133        if (c != '\n') {
 134                if (c != '=')
 135                        return -1;
 136                value = parse_value();
 137                if (!value)
 138                        return -1;
 139        }
 140        return fn(name, value);
 141}
 142
 143static int get_extended_base_var(char *name, int baselen, int c)
 144{
 145        do {
 146                if (c == '\n')
 147                        return -1;
 148                c = get_next_char();
 149        } while (isspace(c));
 150
 151        /* We require the format to be '[base "extension"]' */
 152        if (c != '"')
 153                return -1;
 154        name[baselen++] = '.';
 155
 156        for (;;) {
 157                int c = get_next_char();
 158                if (c == '\n')
 159                        return -1;
 160                if (c == '"')
 161                        break;
 162                if (c == '\\') {
 163                        c = get_next_char();
 164                        if (c == '\n')
 165                                return -1;
 166                }
 167                name[baselen++] = c;
 168                if (baselen > MAXNAME / 2)
 169                        return -1;
 170        }
 171
 172        /* Final ']' */
 173        if (get_next_char() != ']')
 174                return -1;
 175        return baselen;
 176}
 177
 178static int get_base_var(char *name)
 179{
 180        int baselen = 0;
 181
 182        for (;;) {
 183                int c = get_next_char();
 184                if (c == EOF)
 185                        return -1;
 186                if (c == ']')
 187                        return baselen;
 188                if (isspace(c))
 189                        return get_extended_base_var(name, baselen, c);
 190                if (!iskeychar(c) && c != '.')
 191                        return -1;
 192                if (baselen > MAXNAME / 2)
 193                        return -1;
 194                name[baselen++] = tolower(c);
 195        }
 196}
 197
 198static int git_parse_file(config_fn_t fn)
 199{
 200        int comment = 0;
 201        int baselen = 0;
 202        static char var[MAXNAME];
 203
 204        for (;;) {
 205                int c = get_next_char();
 206                if (c == '\n') {
 207                        /* EOF? */
 208                        if (!config_file)
 209                                return 0;
 210                        comment = 0;
 211                        continue;
 212                }
 213                if (comment || isspace(c))
 214                        continue;
 215                if (c == '#' || c == ';') {
 216                        comment = 1;
 217                        continue;
 218                }
 219                if (c == '[') {
 220                        baselen = get_base_var(var);
 221                        if (baselen <= 0)
 222                                break;
 223                        var[baselen++] = '.';
 224                        var[baselen] = 0;
 225                        continue;
 226                }
 227                if (!isalpha(c))
 228                        break;
 229                var[baselen] = tolower(c);
 230                if (get_value(fn, var, baselen+1) < 0)
 231                        break;
 232        }
 233        die("bad config file line %d in %s", config_linenr, config_file_name);
 234}
 235
 236int git_config_int(const char *name, const char *value)
 237{
 238        if (value && *value) {
 239                char *end;
 240                int val = strtol(value, &end, 0);
 241                if (!*end)
 242                        return val;
 243                if (!strcasecmp(end, "k"))
 244                        return val * 1024;
 245                if (!strcasecmp(end, "m"))
 246                        return val * 1024 * 1024;
 247                if (!strcasecmp(end, "g"))
 248                        return val * 1024 * 1024 * 1024;
 249        }
 250        die("bad config value for '%s' in %s", name, config_file_name);
 251}
 252
 253int git_config_bool(const char *name, const char *value)
 254{
 255        if (!value)
 256                return 1;
 257        if (!*value)
 258                return 0;
 259        if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
 260                return 1;
 261        if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
 262                return 0;
 263        return git_config_int(name, value) != 0;
 264}
 265
 266int git_default_config(const char *var, const char *value)
 267{
 268        /* This needs a better name */
 269        if (!strcmp(var, "core.filemode")) {
 270                trust_executable_bit = git_config_bool(var, value);
 271                return 0;
 272        }
 273
 274        if (!strcmp(var, "core.quotepath")) {
 275                quote_path_fully = git_config_bool(var, value);
 276                return 0;
 277        }
 278
 279        if (!strcmp(var, "core.symlinks")) {
 280                has_symlinks = git_config_bool(var, value);
 281                return 0;
 282        }
 283
 284        if (!strcmp(var, "core.bare")) {
 285                is_bare_repository_cfg = git_config_bool(var, value);
 286                return 0;
 287        }
 288
 289        if (!strcmp(var, "core.ignorestat")) {
 290                assume_unchanged = git_config_bool(var, value);
 291                return 0;
 292        }
 293
 294        if (!strcmp(var, "core.prefersymlinkrefs")) {
 295                prefer_symlink_refs = git_config_bool(var, value);
 296                return 0;
 297        }
 298
 299        if (!strcmp(var, "core.logallrefupdates")) {
 300                log_all_ref_updates = git_config_bool(var, value);
 301                return 0;
 302        }
 303
 304        if (!strcmp(var, "core.warnambiguousrefs")) {
 305                warn_ambiguous_refs = git_config_bool(var, value);
 306                return 0;
 307        }
 308
 309        if (!strcmp(var, "core.loosecompression")) {
 310                int level = git_config_int(var, value);
 311                if (level == -1)
 312                        level = Z_DEFAULT_COMPRESSION;
 313                else if (level < 0 || level > Z_BEST_COMPRESSION)
 314                        die("bad zlib compression level %d", level);
 315                zlib_compression_level = level;
 316                zlib_compression_seen = 1;
 317                return 0;
 318        }
 319
 320        if (!strcmp(var, "core.compression")) {
 321                int level = git_config_int(var, value);
 322                if (level == -1)
 323                        level = Z_DEFAULT_COMPRESSION;
 324                else if (level < 0 || level > Z_BEST_COMPRESSION)
 325                        die("bad zlib compression level %d", level);
 326                core_compression_level = level;
 327                core_compression_seen = 1;
 328                if (!zlib_compression_seen)
 329                        zlib_compression_level = level;
 330                return 0;
 331        }
 332
 333        if (!strcmp(var, "core.packedgitwindowsize")) {
 334                int pgsz_x2 = getpagesize() * 2;
 335                packed_git_window_size = git_config_int(var, value);
 336
 337                /* This value must be multiple of (pagesize * 2) */
 338                packed_git_window_size /= pgsz_x2;
 339                if (packed_git_window_size < 1)
 340                        packed_git_window_size = 1;
 341                packed_git_window_size *= pgsz_x2;
 342                return 0;
 343        }
 344
 345        if (!strcmp(var, "core.packedgitlimit")) {
 346                packed_git_limit = git_config_int(var, value);
 347                return 0;
 348        }
 349
 350        if (!strcmp(var, "core.deltabasecachelimit")) {
 351                delta_base_cache_limit = git_config_int(var, value);
 352                return 0;
 353        }
 354
 355        if (!strcmp(var, "core.autocrlf")) {
 356                if (value && !strcasecmp(value, "input")) {
 357                        auto_crlf = -1;
 358                        return 0;
 359                }
 360                auto_crlf = git_config_bool(var, value);
 361                return 0;
 362        }
 363
 364        if (!strcmp(var, "user.name")) {
 365                strlcpy(git_default_name, value, sizeof(git_default_name));
 366                return 0;
 367        }
 368
 369        if (!strcmp(var, "user.email")) {
 370                strlcpy(git_default_email, value, sizeof(git_default_email));
 371                return 0;
 372        }
 373
 374        if (!strcmp(var, "i18n.commitencoding")) {
 375                git_commit_encoding = xstrdup(value);
 376                return 0;
 377        }
 378
 379        if (!strcmp(var, "i18n.logoutputencoding")) {
 380                git_log_output_encoding = xstrdup(value);
 381                return 0;
 382        }
 383
 384
 385        if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
 386                pager_use_color = git_config_bool(var,value);
 387                return 0;
 388        }
 389
 390        if (!strcmp(var, "core.pager")) {
 391                pager_program = xstrdup(value);
 392                return 0;
 393        }
 394
 395        /* Add other config variables here and to Documentation/config.txt. */
 396        return 0;
 397}
 398
 399int git_config_from_file(config_fn_t fn, const char *filename)
 400{
 401        int ret;
 402        FILE *f = fopen(filename, "r");
 403
 404        ret = -1;
 405        if (f) {
 406                config_file = f;
 407                config_file_name = filename;
 408                config_linenr = 1;
 409                ret = git_parse_file(fn);
 410                fclose(f);
 411                config_file_name = NULL;
 412        }
 413        return ret;
 414}
 415
 416int git_config(config_fn_t fn)
 417{
 418        int ret = 0;
 419        char *repo_config = NULL;
 420        const char *home = NULL, *filename;
 421
 422        /* $GIT_CONFIG makes git read _only_ the given config file,
 423         * $GIT_CONFIG_LOCAL will make it process it in addition to the
 424         * global config file, the same way it would the per-repository
 425         * config file otherwise. */
 426        filename = getenv(CONFIG_ENVIRONMENT);
 427        if (!filename) {
 428                if (!access(ETC_GITCONFIG, R_OK))
 429                        ret += git_config_from_file(fn, ETC_GITCONFIG);
 430                home = getenv("HOME");
 431                filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
 432                if (!filename)
 433                        filename = repo_config = xstrdup(git_path("config"));
 434        }
 435
 436        if (home) {
 437                char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
 438                if (!access(user_config, R_OK))
 439                        ret = git_config_from_file(fn, user_config);
 440                free(user_config);
 441        }
 442
 443        ret += git_config_from_file(fn, filename);
 444        free(repo_config);
 445        return ret;
 446}
 447
 448/*
 449 * Find all the stuff for git_config_set() below.
 450 */
 451
 452#define MAX_MATCHES 512
 453
 454static struct {
 455        int baselen;
 456        char* key;
 457        int do_not_match;
 458        regex_t* value_regex;
 459        int multi_replace;
 460        size_t offset[MAX_MATCHES];
 461        enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
 462        int seen;
 463} store;
 464
 465static int matches(const char* key, const char* value)
 466{
 467        return !strcmp(key, store.key) &&
 468                (store.value_regex == NULL ||
 469                 (store.do_not_match ^
 470                  !regexec(store.value_regex, value, 0, NULL, 0)));
 471}
 472
 473static int store_aux(const char* key, const char* value)
 474{
 475        const char *ep;
 476        size_t section_len;
 477
 478        switch (store.state) {
 479        case KEY_SEEN:
 480                if (matches(key, value)) {
 481                        if (store.seen == 1 && store.multi_replace == 0) {
 482                                fprintf(stderr,
 483                                        "Warning: %s has multiple values\n",
 484                                        key);
 485                        } else if (store.seen >= MAX_MATCHES) {
 486                                fprintf(stderr, "Too many matches\n");
 487                                return 1;
 488                        }
 489
 490                        store.offset[store.seen] = ftell(config_file);
 491                        store.seen++;
 492                }
 493                break;
 494        case SECTION_SEEN:
 495                /*
 496                 * What we are looking for is in store.key (both
 497                 * section and var), and its section part is baselen
 498                 * long.  We found key (again, both section and var).
 499                 * We would want to know if this key is in the same
 500                 * section as what we are looking for.  We already
 501                 * know we are in the same section as what should
 502                 * hold store.key.
 503                 */
 504                ep = strrchr(key, '.');
 505                section_len = ep - key;
 506
 507                if ((section_len != store.baselen) ||
 508                    memcmp(key, store.key, section_len+1)) {
 509                        store.state = SECTION_END_SEEN;
 510                        break;
 511                }
 512
 513                /*
 514                 * Do not increment matches: this is no match, but we
 515                 * just made sure we are in the desired section.
 516                 */
 517                store.offset[store.seen] = ftell(config_file);
 518                /* fallthru */
 519        case SECTION_END_SEEN:
 520        case START:
 521                if (matches(key, value)) {
 522                        store.offset[store.seen] = ftell(config_file);
 523                        store.state = KEY_SEEN;
 524                        store.seen++;
 525                } else {
 526                        if (strrchr(key, '.') - key == store.baselen &&
 527                              !strncmp(key, store.key, store.baselen)) {
 528                                        store.state = SECTION_SEEN;
 529                                        store.offset[store.seen] = ftell(config_file);
 530                        }
 531                }
 532        }
 533        return 0;
 534}
 535
 536static int write_error(void)
 537{
 538        fprintf(stderr, "Failed to write new configuration file\n");
 539
 540        /* Same error code as "failed to rename". */
 541        return 4;
 542}
 543
 544static int store_write_section(int fd, const char* key)
 545{
 546        const char *dot = strchr(key, '.');
 547        int len1 = store.baselen, len2 = -1;
 548
 549        dot = strchr(key, '.');
 550        if (dot) {
 551                int dotlen = dot - key;
 552                if (dotlen < len1) {
 553                        len2 = len1 - dotlen - 1;
 554                        len1 = dotlen;
 555                }
 556        }
 557
 558        if (write_in_full(fd, "[", 1) != 1 ||
 559            write_in_full(fd, key, len1) != len1)
 560                return 0;
 561        if (len2 >= 0) {
 562                if (write_in_full(fd, " \"", 2) != 2)
 563                        return 0;
 564                while (--len2 >= 0) {
 565                        unsigned char c = *++dot;
 566                        if (c == '"')
 567                                if (write_in_full(fd, "\\", 1) != 1)
 568                                        return 0;
 569                        if (write_in_full(fd, &c, 1) != 1)
 570                                return 0;
 571                }
 572                if (write_in_full(fd, "\"", 1) != 1)
 573                        return 0;
 574        }
 575        if (write_in_full(fd, "]\n", 2) != 2)
 576                return 0;
 577
 578        return 1;
 579}
 580
 581static int store_write_pair(int fd, const char* key, const char* value)
 582{
 583        int i;
 584        int length = strlen(key+store.baselen+1);
 585        int quote = 0;
 586
 587        /* Check to see if the value needs to be quoted. */
 588        if (value[0] == ' ')
 589                quote = 1;
 590        for (i = 0; value[i]; i++)
 591                if (value[i] == ';' || value[i] == '#')
 592                        quote = 1;
 593        if (value[i-1] == ' ')
 594                quote = 1;
 595
 596        if (write_in_full(fd, "\t", 1) != 1 ||
 597            write_in_full(fd, key+store.baselen+1, length) != length ||
 598            write_in_full(fd, " = ", 3) != 3)
 599                return 0;
 600        if (quote && write_in_full(fd, "\"", 1) != 1)
 601                return 0;
 602        for (i = 0; value[i]; i++)
 603                switch (value[i]) {
 604                case '\n':
 605                        if (write_in_full(fd, "\\n", 2) != 2)
 606                                return 0;
 607                        break;
 608                case '\t':
 609                        if (write_in_full(fd, "\\t", 2) != 2)
 610                                return 0;
 611                        break;
 612                case '"':
 613                case '\\':
 614                        if (write_in_full(fd, "\\", 1) != 1)
 615                                return 0;
 616                default:
 617                        if (write_in_full(fd, value+i, 1) != 1)
 618                                return 0;
 619                        break;
 620                }
 621        if (quote && write_in_full(fd, "\"", 1) != 1)
 622                return 0;
 623        if (write_in_full(fd, "\n", 1) != 1)
 624                return 0;
 625        return 1;
 626}
 627
 628static ssize_t find_beginning_of_line(const char* contents, size_t size,
 629        size_t offset_, int* found_bracket)
 630{
 631        size_t equal_offset = size, bracket_offset = size;
 632        ssize_t offset;
 633
 634        for (offset = offset_-2; offset > 0
 635                        && contents[offset] != '\n'; offset--)
 636                switch (contents[offset]) {
 637                        case '=': equal_offset = offset; break;
 638                        case ']': bracket_offset = offset; break;
 639                }
 640        if (bracket_offset < equal_offset) {
 641                *found_bracket = 1;
 642                offset = bracket_offset+1;
 643        } else
 644                offset++;
 645
 646        return offset;
 647}
 648
 649int git_config_set(const char* key, const char* value)
 650{
 651        return git_config_set_multivar(key, value, NULL, 0);
 652}
 653
 654/*
 655 * If value==NULL, unset in (remove from) config,
 656 * if value_regex!=NULL, disregard key/value pairs where value does not match.
 657 * if multi_replace==0, nothing, or only one matching key/value is replaced,
 658 *     else all matching key/values (regardless how many) are removed,
 659 *     before the new pair is written.
 660 *
 661 * Returns 0 on success.
 662 *
 663 * This function does this:
 664 *
 665 * - it locks the config file by creating ".git/config.lock"
 666 *
 667 * - it then parses the config using store_aux() as validator to find
 668 *   the position on the key/value pair to replace. If it is to be unset,
 669 *   it must be found exactly once.
 670 *
 671 * - the config file is mmap()ed and the part before the match (if any) is
 672 *   written to the lock file, then the changed part and the rest.
 673 *
 674 * - the config file is removed and the lock file rename()d to it.
 675 *
 676 */
 677int git_config_set_multivar(const char* key, const char* value,
 678        const char* value_regex, int multi_replace)
 679{
 680        int i, dot;
 681        int fd = -1, in_fd;
 682        int ret;
 683        char* config_filename;
 684        char* lock_file;
 685        const char* last_dot = strrchr(key, '.');
 686
 687        config_filename = getenv(CONFIG_ENVIRONMENT);
 688        if (!config_filename) {
 689                config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
 690                if (!config_filename)
 691                        config_filename  = git_path("config");
 692        }
 693        config_filename = xstrdup(config_filename);
 694        lock_file = xstrdup(mkpath("%s.lock", config_filename));
 695
 696        /*
 697         * Since "key" actually contains the section name and the real
 698         * key name separated by a dot, we have to know where the dot is.
 699         */
 700
 701        if (last_dot == NULL) {
 702                fprintf(stderr, "key does not contain a section: %s\n", key);
 703                ret = 2;
 704                goto out_free;
 705        }
 706        store.baselen = last_dot - key;
 707
 708        store.multi_replace = multi_replace;
 709
 710        /*
 711         * Validate the key and while at it, lower case it for matching.
 712         */
 713        store.key = xmalloc(strlen(key) + 1);
 714        dot = 0;
 715        for (i = 0; key[i]; i++) {
 716                unsigned char c = key[i];
 717                if (c == '.')
 718                        dot = 1;
 719                /* Leave the extended basename untouched.. */
 720                if (!dot || i > store.baselen) {
 721                        if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
 722                                fprintf(stderr, "invalid key: %s\n", key);
 723                                free(store.key);
 724                                ret = 1;
 725                                goto out_free;
 726                        }
 727                        c = tolower(c);
 728                } else if (c == '\n') {
 729                        fprintf(stderr, "invalid key (newline): %s\n", key);
 730                        free(store.key);
 731                        ret = 1;
 732                        goto out_free;
 733                }
 734                store.key[i] = c;
 735        }
 736        store.key[i] = 0;
 737
 738        /*
 739         * The lock_file serves a purpose in addition to locking: the new
 740         * contents of .git/config will be written into it.
 741         */
 742        fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
 743        if (fd < 0 || adjust_shared_perm(lock_file)) {
 744                fprintf(stderr, "could not lock config file\n");
 745                free(store.key);
 746                ret = -1;
 747                goto out_free;
 748        }
 749
 750        /*
 751         * If .git/config does not exist yet, write a minimal version.
 752         */
 753        in_fd = open(config_filename, O_RDONLY);
 754        if ( in_fd < 0 ) {
 755                free(store.key);
 756
 757                if ( ENOENT != errno ) {
 758                        error("opening %s: %s", config_filename,
 759                              strerror(errno));
 760                        ret = 3; /* same as "invalid config file" */
 761                        goto out_free;
 762                }
 763                /* if nothing to unset, error out */
 764                if (value == NULL) {
 765                        ret = 5;
 766                        goto out_free;
 767                }
 768
 769                store.key = (char*)key;
 770                if (!store_write_section(fd, key) ||
 771                    !store_write_pair(fd, key, value))
 772                        goto write_err_out;
 773        } else {
 774                struct stat st;
 775                char* contents;
 776                size_t contents_sz, copy_begin, copy_end;
 777                int i, new_line = 0;
 778
 779                if (value_regex == NULL)
 780                        store.value_regex = NULL;
 781                else {
 782                        if (value_regex[0] == '!') {
 783                                store.do_not_match = 1;
 784                                value_regex++;
 785                        } else
 786                                store.do_not_match = 0;
 787
 788                        store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
 789                        if (regcomp(store.value_regex, value_regex,
 790                                        REG_EXTENDED)) {
 791                                fprintf(stderr, "Invalid pattern: %s\n",
 792                                        value_regex);
 793                                free(store.value_regex);
 794                                ret = 6;
 795                                goto out_free;
 796                        }
 797                }
 798
 799                store.offset[0] = 0;
 800                store.state = START;
 801                store.seen = 0;
 802
 803                /*
 804                 * After this, store.offset will contain the *end* offset
 805                 * of the last match, or remain at 0 if no match was found.
 806                 * As a side effect, we make sure to transform only a valid
 807                 * existing config file.
 808                 */
 809                if (git_config_from_file(store_aux, config_filename)) {
 810                        fprintf(stderr, "invalid config file\n");
 811                        free(store.key);
 812                        if (store.value_regex != NULL) {
 813                                regfree(store.value_regex);
 814                                free(store.value_regex);
 815                        }
 816                        ret = 3;
 817                        goto out_free;
 818                }
 819
 820                free(store.key);
 821                if (store.value_regex != NULL) {
 822                        regfree(store.value_regex);
 823                        free(store.value_regex);
 824                }
 825
 826                /* if nothing to unset, or too many matches, error out */
 827                if ((store.seen == 0 && value == NULL) ||
 828                                (store.seen > 1 && multi_replace == 0)) {
 829                        ret = 5;
 830                        goto out_free;
 831                }
 832
 833                fstat(in_fd, &st);
 834                contents_sz = xsize_t(st.st_size);
 835                contents = xmmap(NULL, contents_sz, PROT_READ,
 836                        MAP_PRIVATE, in_fd, 0);
 837                close(in_fd);
 838
 839                if (store.seen == 0)
 840                        store.seen = 1;
 841
 842                for (i = 0, copy_begin = 0; i < store.seen; i++) {
 843                        if (store.offset[i] == 0) {
 844                                store.offset[i] = copy_end = contents_sz;
 845                        } else if (store.state != KEY_SEEN) {
 846                                copy_end = store.offset[i];
 847                        } else
 848                                copy_end = find_beginning_of_line(
 849                                        contents, contents_sz,
 850                                        store.offset[i]-2, &new_line);
 851
 852                        /* write the first part of the config */
 853                        if (copy_end > copy_begin) {
 854                                if (write_in_full(fd, contents + copy_begin,
 855                                                  copy_end - copy_begin) <
 856                                    copy_end - copy_begin)
 857                                        goto write_err_out;
 858                                if (new_line &&
 859                                    write_in_full(fd, "\n", 1) != 1)
 860                                        goto write_err_out;
 861                        }
 862                        copy_begin = store.offset[i];
 863                }
 864
 865                /* write the pair (value == NULL means unset) */
 866                if (value != NULL) {
 867                        if (store.state == START) {
 868                                if (!store_write_section(fd, key))
 869                                        goto write_err_out;
 870                        }
 871                        if (!store_write_pair(fd, key, value))
 872                                goto write_err_out;
 873                }
 874
 875                /* write the rest of the config */
 876                if (copy_begin < contents_sz)
 877                        if (write_in_full(fd, contents + copy_begin,
 878                                          contents_sz - copy_begin) <
 879                            contents_sz - copy_begin)
 880                                goto write_err_out;
 881
 882                munmap(contents, contents_sz);
 883                unlink(config_filename);
 884        }
 885
 886        if (rename(lock_file, config_filename) < 0) {
 887                fprintf(stderr, "Could not rename the lock file?\n");
 888                ret = 4;
 889                goto out_free;
 890        }
 891
 892        ret = 0;
 893
 894out_free:
 895        if (0 <= fd)
 896                close(fd);
 897        free(config_filename);
 898        if (lock_file) {
 899                unlink(lock_file);
 900                free(lock_file);
 901        }
 902        return ret;
 903
 904write_err_out:
 905        ret = write_error();
 906        goto out_free;
 907
 908}
 909
 910static int section_name_match (const char *buf, const char *name)
 911{
 912        int i = 0, j = 0, dot = 0;
 913        for (; buf[i] && buf[i] != ']'; i++) {
 914                if (!dot && isspace(buf[i])) {
 915                        dot = 1;
 916                        if (name[j++] != '.')
 917                                break;
 918                        for (i++; isspace(buf[i]); i++)
 919                                ; /* do nothing */
 920                        if (buf[i] != '"')
 921                                break;
 922                        continue;
 923                }
 924                if (buf[i] == '\\' && dot)
 925                        i++;
 926                else if (buf[i] == '"' && dot) {
 927                        for (i++; isspace(buf[i]); i++)
 928                                ; /* do_nothing */
 929                        break;
 930                }
 931                if (buf[i] != name[j++])
 932                        break;
 933        }
 934        return (buf[i] == ']' && name[j] == 0);
 935}
 936
 937/* if new_name == NULL, the section is removed instead */
 938int git_config_rename_section(const char *old_name, const char *new_name)
 939{
 940        int ret = 0, remove = 0;
 941        char *config_filename;
 942        struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
 943        int out_fd;
 944        char buf[1024];
 945
 946        config_filename = getenv(CONFIG_ENVIRONMENT);
 947        if (!config_filename) {
 948                config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
 949                if (!config_filename)
 950                        config_filename  = git_path("config");
 951        }
 952        config_filename = xstrdup(config_filename);
 953        out_fd = hold_lock_file_for_update(lock, config_filename, 0);
 954        if (out_fd < 0) {
 955                ret = error("Could not lock config file!");
 956                goto out;
 957        }
 958
 959        if (!(config_file = fopen(config_filename, "rb"))) {
 960                /* no config file means nothing to rename, no error */
 961                goto unlock_and_out;
 962        }
 963
 964        while (fgets(buf, sizeof(buf), config_file)) {
 965                int i;
 966                int length;
 967                for (i = 0; buf[i] && isspace(buf[i]); i++)
 968                        ; /* do nothing */
 969                if (buf[i] == '[') {
 970                        /* it's a section */
 971                        if (section_name_match (&buf[i+1], old_name)) {
 972                                ret++;
 973                                if (new_name == NULL) {
 974                                        remove = 1;
 975                                        continue;
 976                                }
 977                                store.baselen = strlen(new_name);
 978                                if (!store_write_section(out_fd, new_name)) {
 979                                        ret = write_error();
 980                                        goto out;
 981                                }
 982                                continue;
 983                        }
 984                        remove = 0;
 985                }
 986                if (remove)
 987                        continue;
 988                length = strlen(buf);
 989                if (write_in_full(out_fd, buf, length) != length) {
 990                        ret = write_error();
 991                        goto out;
 992                }
 993        }
 994        fclose(config_file);
 995 unlock_and_out:
 996        if (close(out_fd) || commit_lock_file(lock) < 0)
 997                        ret = error("Cannot commit config file!");
 998 out:
 999        free(config_filename);
1000        return ret;
1001}