Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | * Copyright (C) Johannes Schindelin, 2005 |
| 6 | * |
| 7 | */ |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 8 | #include "cache.h" |
Johannes Sixt | 7f0e39f | 2007-11-13 21:05:06 +0100 | [diff] [blame] | 9 | #include "exec_cmd.h" |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 10 | |
| 11 | #define MAXNAME (256) |
| 12 | |
| 13 | static FILE *config_file; |
Junio C Hamano | 0dccc7d | 2005-11-28 01:46:15 -0800 | [diff] [blame] | 14 | static const char *config_file_name; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 15 | static int config_linenr; |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 16 | static int config_file_eof; |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 17 | static int zlib_compression_seen; |
| 18 | |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 19 | const char *config_exclusive_filename = NULL; |
| 20 | |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 21 | static int get_next_char(void) |
| 22 | { |
| 23 | int c; |
| 24 | FILE *f; |
| 25 | |
| 26 | c = '\n'; |
| 27 | if ((f = config_file) != NULL) { |
| 28 | c = fgetc(f); |
Junio C Hamano | db2c075 | 2005-11-02 13:02:57 -0800 | [diff] [blame] | 29 | if (c == '\r') { |
| 30 | /* DOS like systems */ |
| 31 | c = fgetc(f); |
| 32 | if (c != '\n') { |
| 33 | ungetc(c, f); |
| 34 | c = '\r'; |
| 35 | } |
| 36 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 37 | if (c == '\n') |
| 38 | config_linenr++; |
| 39 | if (c == EOF) { |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 40 | config_file_eof = 1; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 41 | c = '\n'; |
| 42 | } |
| 43 | } |
| 44 | return c; |
| 45 | } |
| 46 | |
| 47 | static char *parse_value(void) |
| 48 | { |
| 49 | static char value[1024]; |
| 50 | int quote = 0, comment = 0, len = 0, space = 0; |
| 51 | |
| 52 | for (;;) { |
| 53 | int c = get_next_char(); |
| 54 | if (len >= sizeof(value)) |
| 55 | return NULL; |
| 56 | if (c == '\n') { |
| 57 | if (quote) |
| 58 | return NULL; |
| 59 | value[len] = 0; |
| 60 | return value; |
| 61 | } |
| 62 | if (comment) |
| 63 | continue; |
| 64 | if (isspace(c) && !quote) { |
| 65 | space = 1; |
| 66 | continue; |
| 67 | } |
Johannes Schindelin | 7ebdba6 | 2006-05-02 16:58:37 +0200 | [diff] [blame] | 68 | if (!quote) { |
| 69 | if (c == ';' || c == '#') { |
| 70 | comment = 1; |
| 71 | continue; |
| 72 | } |
| 73 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 74 | if (space) { |
| 75 | if (len) |
| 76 | value[len++] = ' '; |
| 77 | space = 0; |
| 78 | } |
| 79 | if (c == '\\') { |
| 80 | c = get_next_char(); |
| 81 | switch (c) { |
| 82 | case '\n': |
| 83 | continue; |
| 84 | case 't': |
| 85 | c = '\t'; |
| 86 | break; |
| 87 | case 'b': |
| 88 | c = '\b'; |
| 89 | break; |
| 90 | case 'n': |
| 91 | c = '\n'; |
| 92 | break; |
Linus Torvalds | 5cbb401 | 2005-10-11 15:24:11 -0700 | [diff] [blame] | 93 | /* Some characters escape as themselves */ |
| 94 | case '\\': case '"': |
| 95 | break; |
| 96 | /* Reject unknown escape sequences */ |
| 97 | default: |
| 98 | return NULL; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 99 | } |
| 100 | value[len++] = c; |
| 101 | continue; |
| 102 | } |
| 103 | if (c == '"') { |
| 104 | quote = 1-quote; |
| 105 | continue; |
| 106 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 107 | value[len++] = c; |
| 108 | } |
| 109 | } |
| 110 | |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 111 | static inline int iskeychar(int c) |
| 112 | { |
| 113 | return isalnum(c) || c == '-'; |
| 114 | } |
| 115 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 116 | static int get_value(config_fn_t fn, void *data, char *name, unsigned int len) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 117 | { |
| 118 | int c; |
| 119 | char *value; |
| 120 | |
| 121 | /* Get the full name */ |
| 122 | for (;;) { |
| 123 | c = get_next_char(); |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 124 | if (config_file_eof) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 125 | break; |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 126 | if (!iskeychar(c)) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 127 | break; |
| 128 | name[len++] = tolower(c); |
| 129 | if (len >= MAXNAME) |
| 130 | return -1; |
| 131 | } |
| 132 | name[len] = 0; |
| 133 | while (c == ' ' || c == '\t') |
| 134 | c = get_next_char(); |
| 135 | |
| 136 | value = NULL; |
| 137 | if (c != '\n') { |
| 138 | if (c != '=') |
| 139 | return -1; |
| 140 | value = parse_value(); |
| 141 | if (!value) |
| 142 | return -1; |
| 143 | } |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 144 | return fn(name, value, data); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 147 | static int get_extended_base_var(char *name, int baselen, int c) |
| 148 | { |
| 149 | do { |
| 150 | if (c == '\n') |
| 151 | return -1; |
| 152 | c = get_next_char(); |
| 153 | } while (isspace(c)); |
| 154 | |
| 155 | /* We require the format to be '[base "extension"]' */ |
| 156 | if (c != '"') |
| 157 | return -1; |
| 158 | name[baselen++] = '.'; |
| 159 | |
| 160 | for (;;) { |
| 161 | int c = get_next_char(); |
| 162 | if (c == '\n') |
| 163 | return -1; |
| 164 | if (c == '"') |
| 165 | break; |
| 166 | if (c == '\\') { |
| 167 | c = get_next_char(); |
| 168 | if (c == '\n') |
| 169 | return -1; |
| 170 | } |
| 171 | name[baselen++] = c; |
| 172 | if (baselen > MAXNAME / 2) |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | /* Final ']' */ |
| 177 | if (get_next_char() != ']') |
| 178 | return -1; |
| 179 | return baselen; |
| 180 | } |
| 181 | |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 182 | static int get_base_var(char *name) |
| 183 | { |
| 184 | int baselen = 0; |
| 185 | |
| 186 | for (;;) { |
| 187 | int c = get_next_char(); |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 188 | if (config_file_eof) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 189 | return -1; |
| 190 | if (c == ']') |
| 191 | return baselen; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 192 | if (isspace(c)) |
| 193 | return get_extended_base_var(name, baselen, c); |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 194 | if (!iskeychar(c) && c != '.') |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | if (baselen > MAXNAME / 2) |
| 197 | return -1; |
| 198 | name[baselen++] = tolower(c); |
| 199 | } |
| 200 | } |
| 201 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 202 | static int git_parse_file(config_fn_t fn, void *data) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 203 | { |
| 204 | int comment = 0; |
| 205 | int baselen = 0; |
| 206 | static char var[MAXNAME]; |
| 207 | |
Petr Baudis | de05640 | 2008-10-01 22:13:02 +0200 | [diff] [blame] | 208 | /* U+FEFF Byte Order Mark in UTF8 */ |
| 209 | static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf"; |
| 210 | const unsigned char *bomptr = utf8_bom; |
| 211 | |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 212 | for (;;) { |
| 213 | int c = get_next_char(); |
Petr Baudis | de05640 | 2008-10-01 22:13:02 +0200 | [diff] [blame] | 214 | if (bomptr && *bomptr) { |
| 215 | /* We are at the file beginning; skip UTF8-encoded BOM |
| 216 | * if present. Sane editors won't put this in on their |
| 217 | * own, but e.g. Windows Notepad will do it happily. */ |
| 218 | if ((unsigned char) c == *bomptr) { |
| 219 | bomptr++; |
| 220 | continue; |
| 221 | } else { |
| 222 | /* Do not tolerate partial BOM. */ |
| 223 | if (bomptr != utf8_bom) |
| 224 | break; |
| 225 | /* No BOM at file beginning. Cool. */ |
| 226 | bomptr = NULL; |
| 227 | } |
| 228 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 229 | if (c == '\n') { |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 230 | if (config_file_eof) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 231 | return 0; |
| 232 | comment = 0; |
| 233 | continue; |
| 234 | } |
| 235 | if (comment || isspace(c)) |
| 236 | continue; |
| 237 | if (c == '#' || c == ';') { |
| 238 | comment = 1; |
| 239 | continue; |
| 240 | } |
| 241 | if (c == '[') { |
| 242 | baselen = get_base_var(var); |
| 243 | if (baselen <= 0) |
| 244 | break; |
| 245 | var[baselen++] = '.'; |
| 246 | var[baselen] = 0; |
| 247 | continue; |
| 248 | } |
| 249 | if (!isalpha(c)) |
| 250 | break; |
Linus Torvalds | 128af9d | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 251 | var[baselen] = tolower(c); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 252 | if (get_value(fn, data, var, baselen+1) < 0) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 253 | break; |
| 254 | } |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 255 | die("bad config file line %d in %s", config_linenr, config_file_name); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 258 | static int parse_unit_factor(const char *end, unsigned long *val) |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 259 | { |
| 260 | if (!*end) |
| 261 | return 1; |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 262 | else if (!strcasecmp(end, "k")) { |
| 263 | *val *= 1024; |
| 264 | return 1; |
| 265 | } |
| 266 | else if (!strcasecmp(end, "m")) { |
| 267 | *val *= 1024 * 1024; |
| 268 | return 1; |
| 269 | } |
| 270 | else if (!strcasecmp(end, "g")) { |
| 271 | *val *= 1024 * 1024 * 1024; |
| 272 | return 1; |
| 273 | } |
| 274 | return 0; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Nanako Shiraishi | 0433bcd | 2008-10-02 19:14:27 +0900 | [diff] [blame] | 277 | static int git_parse_long(const char *value, long *ret) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 278 | { |
| 279 | if (value && *value) { |
| 280 | char *end; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 281 | long val = strtol(value, &end, 0); |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 282 | unsigned long factor = 1; |
| 283 | if (!parse_unit_factor(end, &factor)) |
| 284 | return 0; |
| 285 | *ret = val * factor; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 286 | return 1; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 287 | } |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | int git_parse_ulong(const char *value, unsigned long *ret) |
| 292 | { |
| 293 | if (value && *value) { |
| 294 | char *end; |
| 295 | unsigned long val = strtoul(value, &end, 0); |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 296 | if (!parse_unit_factor(end, &val)) |
| 297 | return 0; |
| 298 | *ret = val; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
Jeff King | c1867ce | 2008-02-20 19:00:32 -0500 | [diff] [blame] | 304 | static void die_bad_config(const char *name) |
| 305 | { |
| 306 | if (config_file_name) |
| 307 | die("bad config value for '%s' in %s", name, config_file_name); |
| 308 | die("bad config value for '%s'", name); |
| 309 | } |
| 310 | |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 311 | int git_config_int(const char *name, const char *value) |
| 312 | { |
Nanako Shiraishi | 0433bcd | 2008-10-02 19:14:27 +0900 | [diff] [blame] | 313 | long ret = 0; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 314 | if (!git_parse_long(value, &ret)) |
Jeff King | c1867ce | 2008-02-20 19:00:32 -0500 | [diff] [blame] | 315 | die_bad_config(name); |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | unsigned long git_config_ulong(const char *name, const char *value) |
| 320 | { |
| 321 | unsigned long ret; |
| 322 | if (!git_parse_ulong(value, &ret)) |
Jeff King | c1867ce | 2008-02-20 19:00:32 -0500 | [diff] [blame] | 323 | die_bad_config(name); |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 324 | return ret; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 327 | int git_config_bool_or_int(const char *name, const char *value, int *is_bool) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 328 | { |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 329 | *is_bool = 1; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 330 | if (!value) |
| 331 | return 1; |
| 332 | if (!*value) |
| 333 | return 0; |
Junio C Hamano | 624314f | 2006-07-03 18:48:23 -0700 | [diff] [blame] | 334 | if (!strcasecmp(value, "true") || !strcasecmp(value, "yes")) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 335 | return 1; |
Junio C Hamano | 624314f | 2006-07-03 18:48:23 -0700 | [diff] [blame] | 336 | if (!strcasecmp(value, "false") || !strcasecmp(value, "no")) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 337 | return 0; |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 338 | *is_bool = 0; |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 339 | return git_config_int(name, value); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 342 | int git_config_bool(const char *name, const char *value) |
| 343 | { |
| 344 | int discard; |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 345 | return !!git_config_bool_or_int(name, value, &discard); |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Christian Couder | ea5105a | 2008-02-16 06:00:24 +0100 | [diff] [blame] | 348 | int git_config_string(const char **dest, const char *var, const char *value) |
| 349 | { |
| 350 | if (!value) |
| 351 | return config_error_nonbool(var); |
| 352 | *dest = xstrdup(value); |
| 353 | return 0; |
| 354 | } |
| 355 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 356 | static int git_default_core_config(const char *var, const char *value) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 357 | { |
| 358 | /* This needs a better name */ |
| 359 | if (!strcmp(var, "core.filemode")) { |
| 360 | trust_executable_bit = git_config_bool(var, value); |
| 361 | return 0; |
| 362 | } |
Alex Riesen | 1ce4790 | 2008-07-28 08:31:28 +0200 | [diff] [blame] | 363 | if (!strcmp(var, "core.trustctime")) { |
| 364 | trust_ctime = git_config_bool(var, value); |
| 365 | return 0; |
| 366 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 367 | |
Junio C Hamano | 9378c16 | 2007-06-24 15:11:24 -0700 | [diff] [blame] | 368 | if (!strcmp(var, "core.quotepath")) { |
| 369 | quote_path_fully = git_config_bool(var, value); |
| 370 | return 0; |
| 371 | } |
| 372 | |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 373 | if (!strcmp(var, "core.symlinks")) { |
| 374 | has_symlinks = git_config_bool(var, value); |
| 375 | return 0; |
| 376 | } |
| 377 | |
Linus Torvalds | 0a9b88b | 2008-03-21 16:52:46 -0700 | [diff] [blame] | 378 | if (!strcmp(var, "core.ignorecase")) { |
| 379 | ignore_case = git_config_bool(var, value); |
| 380 | return 0; |
| 381 | } |
| 382 | |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 383 | if (!strcmp(var, "core.bare")) { |
| 384 | is_bare_repository_cfg = git_config_bool(var, value); |
| 385 | return 0; |
| 386 | } |
| 387 | |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 388 | if (!strcmp(var, "core.ignorestat")) { |
| 389 | assume_unchanged = git_config_bool(var, value); |
| 390 | return 0; |
| 391 | } |
| 392 | |
Junio C Hamano | e388c73 | 2006-05-02 00:40:24 -0700 | [diff] [blame] | 393 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
| 394 | prefer_symlink_refs = git_config_bool(var, value); |
Johannes Schindelin | f8348be | 2005-11-15 19:24:19 +0100 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Shawn Pearce | 6de08ae | 2006-05-17 05:55:40 -0400 | [diff] [blame] | 398 | if (!strcmp(var, "core.logallrefupdates")) { |
| 399 | log_all_ref_updates = git_config_bool(var, value); |
| 400 | return 0; |
| 401 | } |
| 402 | |
Junio C Hamano | 2f8acdb | 2006-03-20 18:45:47 -0800 | [diff] [blame] | 403 | if (!strcmp(var, "core.warnambiguousrefs")) { |
| 404 | warn_ambiguous_refs = git_config_bool(var, value); |
| 405 | return 0; |
| 406 | } |
| 407 | |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 408 | if (!strcmp(var, "core.loosecompression")) { |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 409 | int level = git_config_int(var, value); |
| 410 | if (level == -1) |
| 411 | level = Z_DEFAULT_COMPRESSION; |
| 412 | else if (level < 0 || level > Z_BEST_COMPRESSION) |
| 413 | die("bad zlib compression level %d", level); |
| 414 | zlib_compression_level = level; |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 415 | zlib_compression_seen = 1; |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | if (!strcmp(var, "core.compression")) { |
| 420 | int level = git_config_int(var, value); |
| 421 | if (level == -1) |
| 422 | level = Z_DEFAULT_COMPRESSION; |
| 423 | else if (level < 0 || level > Z_BEST_COMPRESSION) |
| 424 | die("bad zlib compression level %d", level); |
| 425 | core_compression_level = level; |
| 426 | core_compression_seen = 1; |
| 427 | if (!zlib_compression_seen) |
| 428 | zlib_compression_level = level; |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 432 | if (!strcmp(var, "core.packedgitwindowsize")) { |
Junio C Hamano | 5faaf24 | 2007-02-14 13:20:41 -0800 | [diff] [blame] | 433 | int pgsz_x2 = getpagesize() * 2; |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 434 | packed_git_window_size = git_config_int(var, value); |
Junio C Hamano | 5faaf24 | 2007-02-14 13:20:41 -0800 | [diff] [blame] | 435 | |
| 436 | /* This value must be multiple of (pagesize * 2) */ |
| 437 | packed_git_window_size /= pgsz_x2; |
| 438 | if (packed_git_window_size < 1) |
| 439 | packed_git_window_size = 1; |
| 440 | packed_git_window_size *= pgsz_x2; |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
Shawn O. Pearce | 77ccc5b | 2006-12-23 02:33:35 -0500 | [diff] [blame] | 444 | if (!strcmp(var, "core.packedgitlimit")) { |
| 445 | packed_git_limit = git_config_int(var, value); |
| 446 | return 0; |
| 447 | } |
| 448 | |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 449 | if (!strcmp(var, "core.deltabasecachelimit")) { |
| 450 | delta_base_cache_limit = git_config_int(var, value); |
| 451 | return 0; |
| 452 | } |
| 453 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 454 | if (!strcmp(var, "core.autocrlf")) { |
Linus Torvalds | d7f4633 | 2007-02-13 18:16:12 -0800 | [diff] [blame] | 455 | if (value && !strcasecmp(value, "input")) { |
| 456 | auto_crlf = -1; |
| 457 | return 0; |
| 458 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 459 | auto_crlf = git_config_bool(var, value); |
| 460 | return 0; |
| 461 | } |
| 462 | |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 463 | if (!strcmp(var, "core.safecrlf")) { |
| 464 | if (value && !strcasecmp(value, "warn")) { |
| 465 | safe_crlf = SAFE_CRLF_WARN; |
| 466 | return 0; |
| 467 | } |
| 468 | safe_crlf = git_config_bool(var, value); |
| 469 | return 0; |
| 470 | } |
| 471 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 472 | if (!strcmp(var, "core.pager")) |
| 473 | return git_config_string(&pager_program, var, value); |
| 474 | |
| 475 | if (!strcmp(var, "core.editor")) |
| 476 | return git_config_string(&editor_program, var, value); |
| 477 | |
| 478 | if (!strcmp(var, "core.excludesfile")) |
| 479 | return git_config_string(&excludes_file, var, value); |
| 480 | |
| 481 | if (!strcmp(var, "core.whitespace")) { |
| 482 | if (!value) |
| 483 | return config_error_nonbool(var); |
| 484 | whitespace_rule_cfg = parse_whitespace_rule(value); |
| 485 | return 0; |
| 486 | } |
| 487 | |
Linus Torvalds | aafe9fb | 2008-06-18 15:18:44 -0700 | [diff] [blame] | 488 | if (!strcmp(var, "core.fsyncobjectfiles")) { |
| 489 | fsync_object_files = git_config_bool(var, value); |
| 490 | return 0; |
| 491 | } |
| 492 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 493 | if (!strcmp(var, "core.preloadindex")) { |
| 494 | core_preload_index = git_config_bool(var, value); |
| 495 | return 0; |
| 496 | } |
| 497 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 498 | /* Add other config variables here and to Documentation/config.txt. */ |
| 499 | return 0; |
| 500 | } |
| 501 | |
Linus Torvalds | d136452 | 2008-06-18 14:40:35 -0700 | [diff] [blame] | 502 | static int git_default_user_config(const char *var, const char *value) |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 503 | { |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 504 | if (!strcmp(var, "user.name")) { |
Junio C Hamano | 6c47d0e | 2008-02-11 13:10:27 -0800 | [diff] [blame] | 505 | if (!value) |
| 506 | return config_error_nonbool(var); |
Peter Eriksen | 817151e | 2006-06-24 16:01:25 +0200 | [diff] [blame] | 507 | strlcpy(git_default_name, value, sizeof(git_default_name)); |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 508 | if (git_default_email[0]) |
| 509 | user_ident_explicitly_given = 1; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | if (!strcmp(var, "user.email")) { |
Junio C Hamano | 6c47d0e | 2008-02-11 13:10:27 -0800 | [diff] [blame] | 514 | if (!value) |
| 515 | return config_error_nonbool(var); |
Peter Eriksen | 817151e | 2006-06-24 16:01:25 +0200 | [diff] [blame] | 516 | strlcpy(git_default_email, value, sizeof(git_default_email)); |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 517 | if (git_default_name[0]) |
| 518 | user_ident_explicitly_given = 1; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Linus Torvalds | d136452 | 2008-06-18 14:40:35 -0700 | [diff] [blame] | 522 | /* Add other config variables here and to Documentation/config.txt. */ |
| 523 | return 0; |
| 524 | } |
| 525 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 526 | static int git_default_i18n_config(const char *var, const char *value) |
Linus Torvalds | d136452 | 2008-06-18 14:40:35 -0700 | [diff] [blame] | 527 | { |
Christian Couder | ea5105a | 2008-02-16 06:00:24 +0100 | [diff] [blame] | 528 | if (!strcmp(var, "i18n.commitencoding")) |
| 529 | return git_config_string(&git_commit_encoding, var, value); |
Junio C Hamano | 4e72dce | 2005-11-27 16:09:40 -0800 | [diff] [blame] | 530 | |
Christian Couder | ea5105a | 2008-02-16 06:00:24 +0100 | [diff] [blame] | 531 | if (!strcmp(var, "i18n.logoutputencoding")) |
| 532 | return git_config_string(&git_log_output_encoding, var, value); |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 533 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 534 | /* Add other config variables here and to Documentation/config.txt. */ |
| 535 | return 0; |
| 536 | } |
Matthias Lederhofer | aa086eb | 2006-07-30 00:27:43 +0200 | [diff] [blame] | 537 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 538 | static int git_default_branch_config(const char *var, const char *value) |
| 539 | { |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 540 | if (!strcmp(var, "branch.autosetupmerge")) { |
| 541 | if (value && !strcasecmp(value, "always")) { |
| 542 | git_branch_track = BRANCH_TRACK_ALWAYS; |
| 543 | return 0; |
| 544 | } |
| 545 | git_branch_track = git_config_bool(var, value); |
| 546 | return 0; |
| 547 | } |
Dustin Sallings | c998ae9 | 2008-05-10 15:36:29 -0700 | [diff] [blame] | 548 | if (!strcmp(var, "branch.autosetuprebase")) { |
| 549 | if (!value) |
| 550 | return config_error_nonbool(var); |
| 551 | else if (!strcmp(value, "never")) |
| 552 | autorebase = AUTOREBASE_NEVER; |
| 553 | else if (!strcmp(value, "local")) |
| 554 | autorebase = AUTOREBASE_LOCAL; |
| 555 | else if (!strcmp(value, "remote")) |
| 556 | autorebase = AUTOREBASE_REMOTE; |
| 557 | else if (!strcmp(value, "always")) |
| 558 | autorebase = AUTOREBASE_ALWAYS; |
| 559 | else |
| 560 | return error("Malformed value for %s", var); |
| 561 | return 0; |
| 562 | } |
Junio C Hamano | a9cc857 | 2007-11-02 00:24:27 -0700 | [diff] [blame] | 563 | |
Petr Baudis | 1ab661d | 2006-04-25 00:59:33 +0200 | [diff] [blame] | 564 | /* Add other config variables here and to Documentation/config.txt. */ |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 568 | int git_default_config(const char *var, const char *value, void *dummy) |
| 569 | { |
| 570 | if (!prefixcmp(var, "core.")) |
| 571 | return git_default_core_config(var, value); |
| 572 | |
| 573 | if (!prefixcmp(var, "user.")) |
| 574 | return git_default_user_config(var, value); |
| 575 | |
| 576 | if (!prefixcmp(var, "i18n.")) |
| 577 | return git_default_i18n_config(var, value); |
| 578 | |
| 579 | if (!prefixcmp(var, "branch.")) |
| 580 | return git_default_branch_config(var, value); |
| 581 | |
| 582 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
| 583 | pager_use_color = git_config_bool(var,value); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | /* Add other config variables here and to Documentation/config.txt. */ |
| 588 | return 0; |
| 589 | } |
| 590 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 591 | int git_config_from_file(config_fn_t fn, const char *filename, void *data) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 592 | { |
| 593 | int ret; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 594 | FILE *f = fopen(filename, "r"); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 595 | |
| 596 | ret = -1; |
| 597 | if (f) { |
| 598 | config_file = f; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 599 | config_file_name = filename; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 600 | config_linenr = 1; |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 601 | config_file_eof = 0; |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 602 | ret = git_parse_file(fn, data); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 603 | fclose(f); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 604 | config_file_name = NULL; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 605 | } |
| 606 | return ret; |
| 607 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 608 | |
Johannes Sixt | 506b17b | 2007-11-13 21:05:05 +0100 | [diff] [blame] | 609 | const char *git_etc_gitconfig(void) |
| 610 | { |
Johannes Sixt | 7f0e39f | 2007-11-13 21:05:06 +0100 | [diff] [blame] | 611 | static const char *system_wide; |
Steffen Prohaska | 2de9de5 | 2008-07-13 22:31:18 +0200 | [diff] [blame] | 612 | if (!system_wide) |
| 613 | system_wide = system_path(ETC_GITCONFIG); |
Johannes Sixt | 7f0e39f | 2007-11-13 21:05:06 +0100 | [diff] [blame] | 614 | return system_wide; |
Johannes Sixt | 506b17b | 2007-11-13 21:05:05 +0100 | [diff] [blame] | 615 | } |
| 616 | |
しらいしななこ | e4bffb5 | 2008-06-19 08:21:11 +0900 | [diff] [blame] | 617 | static int git_env_bool(const char *k, int def) |
Jeff King | ab88c36 | 2008-02-06 05:11:18 -0500 | [diff] [blame] | 618 | { |
| 619 | const char *v = getenv(k); |
| 620 | return v ? git_config_bool(k, v) : def; |
| 621 | } |
| 622 | |
| 623 | int git_config_system(void) |
| 624 | { |
| 625 | return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); |
| 626 | } |
| 627 | |
| 628 | int git_config_global(void) |
| 629 | { |
| 630 | return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0); |
| 631 | } |
| 632 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 633 | int git_config(config_fn_t fn, void *data) |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 634 | { |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 635 | int ret = 0; |
| 636 | char *repo_config = NULL; |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 637 | const char *home = NULL; |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 638 | |
Johannes Schindelin | 8befc50 | 2008-12-14 23:10:52 +0100 | [diff] [blame] | 639 | /* Setting $GIT_CONFIG makes git read _only_ the given config file. */ |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 640 | if (config_exclusive_filename) |
| 641 | return git_config_from_file(fn, config_exclusive_filename, data); |
| 642 | if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) |
| 643 | ret += git_config_from_file(fn, git_etc_gitconfig(), |
| 644 | data); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 645 | |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 646 | home = getenv("HOME"); |
Jeff King | ab88c36 | 2008-02-06 05:11:18 -0500 | [diff] [blame] | 647 | if (git_config_global() && home) { |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 648 | char *user_config = xstrdup(mkpath("%s/.gitconfig", home)); |
Johannes Schindelin | e33d061 | 2006-06-20 09:51:09 +0200 | [diff] [blame] | 649 | if (!access(user_config, R_OK)) |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 650 | ret += git_config_from_file(fn, user_config, data); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 651 | free(user_config); |
| 652 | } |
| 653 | |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 654 | repo_config = git_pathdup("config"); |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 655 | ret += git_config_from_file(fn, repo_config, data); |
Junio C Hamano | 4cac42b | 2006-08-27 21:19:39 -0700 | [diff] [blame] | 656 | free(repo_config); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 657 | return ret; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 658 | } |
| 659 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 660 | /* |
| 661 | * Find all the stuff for git_config_set() below. |
| 662 | */ |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 663 | |
| 664 | #define MAX_MATCHES 512 |
| 665 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 666 | static struct { |
| 667 | int baselen; |
| 668 | char* key; |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 669 | int do_not_match; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 670 | regex_t* value_regex; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 671 | int multi_replace; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 672 | size_t offset[MAX_MATCHES]; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 673 | enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; |
| 674 | int seen; |
| 675 | } store; |
| 676 | |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 677 | static int matches(const char* key, const char* value) |
| 678 | { |
| 679 | return !strcmp(key, store.key) && |
| 680 | (store.value_regex == NULL || |
| 681 | (store.do_not_match ^ |
| 682 | !regexec(store.value_regex, value, 0, NULL, 0))); |
| 683 | } |
| 684 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 685 | static int store_aux(const char* key, const char* value, void *cb) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 686 | { |
Junio C Hamano | ae9ee41 | 2007-05-12 21:49:33 -0700 | [diff] [blame] | 687 | const char *ep; |
| 688 | size_t section_len; |
| 689 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 690 | switch (store.state) { |
| 691 | case KEY_SEEN: |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 692 | if (matches(key, value)) { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 693 | if (store.seen == 1 && store.multi_replace == 0) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 694 | warning("%s has multiple values", key); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 695 | } else if (store.seen >= MAX_MATCHES) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 696 | error("too many matches for %s", key); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 697 | return 1; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 698 | } |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 699 | |
| 700 | store.offset[store.seen] = ftell(config_file); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 701 | store.seen++; |
| 702 | } |
| 703 | break; |
| 704 | case SECTION_SEEN: |
Junio C Hamano | ae9ee41 | 2007-05-12 21:49:33 -0700 | [diff] [blame] | 705 | /* |
| 706 | * What we are looking for is in store.key (both |
| 707 | * section and var), and its section part is baselen |
| 708 | * long. We found key (again, both section and var). |
| 709 | * We would want to know if this key is in the same |
| 710 | * section as what we are looking for. We already |
| 711 | * know we are in the same section as what should |
| 712 | * hold store.key. |
| 713 | */ |
| 714 | ep = strrchr(key, '.'); |
| 715 | section_len = ep - key; |
| 716 | |
| 717 | if ((section_len != store.baselen) || |
| 718 | memcmp(key, store.key, section_len+1)) { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 719 | store.state = SECTION_END_SEEN; |
| 720 | break; |
Junio C Hamano | ae9ee41 | 2007-05-12 21:49:33 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Do not increment matches: this is no match, but we |
| 725 | * just made sure we are in the desired section. |
| 726 | */ |
| 727 | store.offset[store.seen] = ftell(config_file); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 728 | /* fallthru */ |
| 729 | case SECTION_END_SEEN: |
| 730 | case START: |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 731 | if (matches(key, value)) { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 732 | store.offset[store.seen] = ftell(config_file); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 733 | store.state = KEY_SEEN; |
| 734 | store.seen++; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 735 | } else { |
| 736 | if (strrchr(key, '.') - key == store.baselen && |
sean | bdf0ef0 | 2006-05-06 14:14:02 -0400 | [diff] [blame] | 737 | !strncmp(key, store.key, store.baselen)) { |
sean | 93ddef3 | 2006-05-05 09:49:15 -0400 | [diff] [blame] | 738 | store.state = SECTION_SEEN; |
sean | bdf0ef0 | 2006-05-06 14:14:02 -0400 | [diff] [blame] | 739 | store.offset[store.seen] = ftell(config_file); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 740 | } |
sean | bdf0ef0 | 2006-05-06 14:14:02 -0400 | [diff] [blame] | 741 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 742 | } |
| 743 | return 0; |
| 744 | } |
| 745 | |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 746 | static int write_error(const char *filename) |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 747 | { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 748 | error("failed to write new configuration file %s", filename); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 749 | |
| 750 | /* Same error code as "failed to rename". */ |
| 751 | return 4; |
| 752 | } |
| 753 | |
| 754 | static int store_write_section(int fd, const char* key) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 755 | { |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 756 | const char *dot; |
| 757 | int i, success; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 758 | struct strbuf sb = STRBUF_INIT; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 759 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 760 | dot = memchr(key, '.', store.baselen); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 761 | if (dot) { |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 762 | strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key); |
| 763 | for (i = dot - key + 1; i < store.baselen; i++) { |
Bryan Donlan | e5c349b | 2008-05-04 01:37:52 -0400 | [diff] [blame] | 764 | if (key[i] == '"' || key[i] == '\\') |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 765 | strbuf_addch(&sb, '\\'); |
| 766 | strbuf_addch(&sb, key[i]); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 767 | } |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 768 | strbuf_addstr(&sb, "\"]\n"); |
| 769 | } else { |
| 770 | strbuf_addf(&sb, "[%.*s]\n", store.baselen, key); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 773 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; |
| 774 | strbuf_release(&sb); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 775 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 776 | return success; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 779 | static int store_write_pair(int fd, const char* key, const char* value) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 780 | { |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 781 | int i, success; |
| 782 | int length = strlen(key + store.baselen + 1); |
| 783 | const char *quote = ""; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 784 | struct strbuf sb = STRBUF_INIT; |
Brian Gernhardt | cdd4fb1 | 2007-01-09 00:27:41 -0500 | [diff] [blame] | 785 | |
Jim Meyering | 6281f39 | 2007-12-08 16:48:05 +0100 | [diff] [blame] | 786 | /* |
| 787 | * Check to see if the value needs to be surrounded with a dq pair. |
| 788 | * Note that problematic characters are always backslash-quoted; this |
| 789 | * check is about not losing leading or trailing SP and strings that |
| 790 | * follow beginning-of-comment characters (i.e. ';' and '#') by the |
| 791 | * configuration parser. |
| 792 | */ |
Brian Gernhardt | cdd4fb1 | 2007-01-09 00:27:41 -0500 | [diff] [blame] | 793 | if (value[0] == ' ') |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 794 | quote = "\""; |
Brian Gernhardt | cdd4fb1 | 2007-01-09 00:27:41 -0500 | [diff] [blame] | 795 | for (i = 0; value[i]; i++) |
| 796 | if (value[i] == ';' || value[i] == '#') |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 797 | quote = "\""; |
| 798 | if (i && value[i - 1] == ' ') |
| 799 | quote = "\""; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 800 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 801 | strbuf_addf(&sb, "\t%.*s = %s", |
| 802 | length, key + store.baselen + 1, quote); |
| 803 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 804 | for (i = 0; value[i]; i++) |
| 805 | switch (value[i]) { |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 806 | case '\n': |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 807 | strbuf_addstr(&sb, "\\n"); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 808 | break; |
| 809 | case '\t': |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 810 | strbuf_addstr(&sb, "\\t"); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 811 | break; |
| 812 | case '"': |
| 813 | case '\\': |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 814 | strbuf_addch(&sb, '\\'); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 815 | default: |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 816 | strbuf_addch(&sb, value[i]); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 817 | break; |
| 818 | } |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 819 | strbuf_addf(&sb, "%s\n", quote); |
| 820 | |
| 821 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; |
| 822 | strbuf_release(&sb); |
| 823 | |
| 824 | return success; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 825 | } |
| 826 | |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 827 | static ssize_t find_beginning_of_line(const char* contents, size_t size, |
| 828 | size_t offset_, int* found_bracket) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 829 | { |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 830 | size_t equal_offset = size, bracket_offset = size; |
| 831 | ssize_t offset; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 832 | |
Frank Lichtenheld | 7a31cc0 | 2008-02-11 01:23:03 +0100 | [diff] [blame] | 833 | contline: |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 834 | for (offset = offset_-2; offset > 0 |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 835 | && contents[offset] != '\n'; offset--) |
| 836 | switch (contents[offset]) { |
| 837 | case '=': equal_offset = offset; break; |
| 838 | case ']': bracket_offset = offset; break; |
| 839 | } |
Frank Lichtenheld | 7a31cc0 | 2008-02-11 01:23:03 +0100 | [diff] [blame] | 840 | if (offset > 0 && contents[offset-1] == '\\') { |
| 841 | offset_ = offset; |
| 842 | goto contline; |
| 843 | } |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 844 | if (bracket_offset < equal_offset) { |
| 845 | *found_bracket = 1; |
| 846 | offset = bracket_offset+1; |
| 847 | } else |
| 848 | offset++; |
| 849 | |
| 850 | return offset; |
| 851 | } |
| 852 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 853 | int git_config_set(const char* key, const char* value) |
| 854 | { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 855 | return git_config_set_multivar(key, value, NULL, 0); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* |
| 859 | * If value==NULL, unset in (remove from) config, |
| 860 | * if value_regex!=NULL, disregard key/value pairs where value does not match. |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 861 | * if multi_replace==0, nothing, or only one matching key/value is replaced, |
| 862 | * else all matching key/values (regardless how many) are removed, |
| 863 | * before the new pair is written. |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 864 | * |
| 865 | * Returns 0 on success. |
| 866 | * |
| 867 | * This function does this: |
| 868 | * |
| 869 | * - it locks the config file by creating ".git/config.lock" |
| 870 | * |
| 871 | * - it then parses the config using store_aux() as validator to find |
| 872 | * the position on the key/value pair to replace. If it is to be unset, |
| 873 | * it must be found exactly once. |
| 874 | * |
| 875 | * - the config file is mmap()ed and the part before the match (if any) is |
| 876 | * written to the lock file, then the changed part and the rest. |
| 877 | * |
| 878 | * - the config file is removed and the lock file rename()d to it. |
| 879 | * |
| 880 | */ |
| 881 | int git_config_set_multivar(const char* key, const char* value, |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 882 | const char* value_regex, int multi_replace) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 883 | { |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 884 | int i, dot; |
Junio C Hamano | f8ba655 | 2006-05-07 21:27:30 -0700 | [diff] [blame] | 885 | int fd = -1, in_fd; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 886 | int ret; |
Johannes Schindelin | 9c3796f | 2006-06-20 00:51:58 +0200 | [diff] [blame] | 887 | char* config_filename; |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 888 | struct lock_file *lock = NULL; |
Johannes Schindelin | b17e659 | 2005-11-20 21:22:19 +0100 | [diff] [blame] | 889 | const char* last_dot = strrchr(key, '.'); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 890 | |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 891 | if (config_exclusive_filename) |
| 892 | config_filename = xstrdup(config_exclusive_filename); |
| 893 | else |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 894 | config_filename = git_pathdup("config"); |
Johannes Schindelin | 9c3796f | 2006-06-20 00:51:58 +0200 | [diff] [blame] | 895 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 896 | /* |
| 897 | * Since "key" actually contains the section name and the real |
| 898 | * key name separated by a dot, we have to know where the dot is. |
| 899 | */ |
Johannes Schindelin | b17e659 | 2005-11-20 21:22:19 +0100 | [diff] [blame] | 900 | |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 901 | if (last_dot == NULL) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 902 | error("key does not contain a section: %s", key); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 903 | ret = 2; |
| 904 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 905 | } |
Johannes Schindelin | b17e659 | 2005-11-20 21:22:19 +0100 | [diff] [blame] | 906 | store.baselen = last_dot - key; |
| 907 | |
| 908 | store.multi_replace = multi_replace; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 909 | |
| 910 | /* |
| 911 | * Validate the key and while at it, lower case it for matching. |
| 912 | */ |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 913 | store.key = xmalloc(strlen(key) + 1); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 914 | dot = 0; |
| 915 | for (i = 0; key[i]; i++) { |
| 916 | unsigned char c = key[i]; |
| 917 | if (c == '.') |
| 918 | dot = 1; |
| 919 | /* Leave the extended basename untouched.. */ |
| 920 | if (!dot || i > store.baselen) { |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 921 | if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 922 | error("invalid key: %s", key); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 923 | free(store.key); |
| 924 | ret = 1; |
| 925 | goto out_free; |
| 926 | } |
| 927 | c = tolower(c); |
Johannes Schindelin | 6f71686 | 2007-01-20 02:25:37 +0100 | [diff] [blame] | 928 | } else if (c == '\n') { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 929 | error("invalid key (newline): %s", key); |
Johannes Schindelin | 6f71686 | 2007-01-20 02:25:37 +0100 | [diff] [blame] | 930 | free(store.key); |
| 931 | ret = 1; |
| 932 | goto out_free; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 933 | } |
| 934 | store.key[i] = c; |
| 935 | } |
Johannes Schindelin | 3dd94e3 | 2005-11-21 11:18:20 +0100 | [diff] [blame] | 936 | store.key[i] = 0; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 937 | |
| 938 | /* |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 939 | * The lock serves a purpose in addition to locking: the new |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 940 | * contents of .git/config will be written into it. |
| 941 | */ |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 942 | lock = xcalloc(sizeof(struct lock_file), 1); |
| 943 | fd = hold_lock_file_for_update(lock, config_filename, 0); |
| 944 | if (fd < 0) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 945 | error("could not lock config file %s", config_filename); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 946 | free(store.key); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 947 | ret = -1; |
| 948 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /* |
| 952 | * If .git/config does not exist yet, write a minimal version. |
| 953 | */ |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 954 | in_fd = open(config_filename, O_RDONLY); |
| 955 | if ( in_fd < 0 ) { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 956 | free(store.key); |
| 957 | |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 958 | if ( ENOENT != errno ) { |
| 959 | error("opening %s: %s", config_filename, |
| 960 | strerror(errno)); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 961 | ret = 3; /* same as "invalid config file" */ |
| 962 | goto out_free; |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 963 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 964 | /* if nothing to unset, error out */ |
| 965 | if (value == NULL) { |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 966 | ret = 5; |
| 967 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | store.key = (char*)key; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 971 | if (!store_write_section(fd, key) || |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 972 | !store_write_pair(fd, key, value)) |
| 973 | goto write_err_out; |
| 974 | } else { |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 975 | struct stat st; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 976 | char* contents; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 977 | size_t contents_sz, copy_begin, copy_end; |
| 978 | int i, new_line = 0; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 979 | |
| 980 | if (value_regex == NULL) |
| 981 | store.value_regex = NULL; |
| 982 | else { |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 983 | if (value_regex[0] == '!') { |
| 984 | store.do_not_match = 1; |
| 985 | value_regex++; |
| 986 | } else |
| 987 | store.do_not_match = 0; |
| 988 | |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 989 | store.value_regex = (regex_t*)xmalloc(sizeof(regex_t)); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 990 | if (regcomp(store.value_regex, value_regex, |
| 991 | REG_EXTENDED)) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 992 | error("invalid pattern: %s", value_regex); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 993 | free(store.value_regex); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 994 | ret = 6; |
| 995 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 999 | store.offset[0] = 0; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1000 | store.state = START; |
| 1001 | store.seen = 0; |
| 1002 | |
| 1003 | /* |
| 1004 | * After this, store.offset will contain the *end* offset |
| 1005 | * of the last match, or remain at 0 if no match was found. |
| 1006 | * As a side effect, we make sure to transform only a valid |
| 1007 | * existing config file. |
| 1008 | */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 1009 | if (git_config_from_file(store_aux, config_filename, NULL)) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1010 | error("invalid config file %s", config_filename); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1011 | free(store.key); |
| 1012 | if (store.value_regex != NULL) { |
| 1013 | regfree(store.value_regex); |
| 1014 | free(store.value_regex); |
| 1015 | } |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 1016 | ret = 3; |
| 1017 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | free(store.key); |
| 1021 | if (store.value_regex != NULL) { |
| 1022 | regfree(store.value_regex); |
| 1023 | free(store.value_regex); |
| 1024 | } |
| 1025 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1026 | /* if nothing to unset, or too many matches, error out */ |
| 1027 | if ((store.seen == 0 && value == NULL) || |
| 1028 | (store.seen > 1 && multi_replace == 0)) { |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 1029 | ret = 5; |
| 1030 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 1033 | fstat(in_fd, &st); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1034 | contents_sz = xsize_t(st.st_size); |
| 1035 | contents = xmmap(NULL, contents_sz, PROT_READ, |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1036 | MAP_PRIVATE, in_fd, 0); |
| 1037 | close(in_fd); |
| 1038 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1039 | if (store.seen == 0) |
| 1040 | store.seen = 1; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1041 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1042 | for (i = 0, copy_begin = 0; i < store.seen; i++) { |
| 1043 | if (store.offset[i] == 0) { |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1044 | store.offset[i] = copy_end = contents_sz; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1045 | } else if (store.state != KEY_SEEN) { |
| 1046 | copy_end = store.offset[i]; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1047 | } else |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1048 | copy_end = find_beginning_of_line( |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1049 | contents, contents_sz, |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1050 | store.offset[i]-2, &new_line); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1051 | |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 1052 | if (copy_end > 0 && contents[copy_end-1] != '\n') |
| 1053 | new_line = 1; |
| 1054 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1055 | /* write the first part of the config */ |
| 1056 | if (copy_end > copy_begin) { |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1057 | if (write_in_full(fd, contents + copy_begin, |
| 1058 | copy_end - copy_begin) < |
| 1059 | copy_end - copy_begin) |
| 1060 | goto write_err_out; |
| 1061 | if (new_line && |
| 1062 | write_in_full(fd, "\n", 1) != 1) |
| 1063 | goto write_err_out; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1064 | } |
| 1065 | copy_begin = store.offset[i]; |
| 1066 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1067 | |
| 1068 | /* write the pair (value == NULL means unset) */ |
| 1069 | if (value != NULL) { |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1070 | if (store.state == START) { |
| 1071 | if (!store_write_section(fd, key)) |
| 1072 | goto write_err_out; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1073 | } |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1074 | if (!store_write_pair(fd, key, value)) |
| 1075 | goto write_err_out; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* write the rest of the config */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1079 | if (copy_begin < contents_sz) |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1080 | if (write_in_full(fd, contents + copy_begin, |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1081 | contents_sz - copy_begin) < |
| 1082 | contents_sz - copy_begin) |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1083 | goto write_err_out; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1084 | |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1085 | munmap(contents, contents_sz); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 1088 | if (commit_lock_file(lock) < 0) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1089 | error("could not commit config file %s", config_filename); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 1090 | ret = 4; |
| 1091 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 1094 | /* |
| 1095 | * lock is committed, so don't try to roll it back below. |
| 1096 | * NOTE: Since lockfile.c keeps a linked list of all created |
| 1097 | * lock_file structures, it isn't safe to free(lock). It's |
| 1098 | * better to just leave it hanging around. |
| 1099 | */ |
| 1100 | lock = NULL; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 1101 | ret = 0; |
| 1102 | |
| 1103 | out_free: |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 1104 | if (lock) |
| 1105 | rollback_lock_file(lock); |
Junio C Hamano | 4cac42b | 2006-08-27 21:19:39 -0700 | [diff] [blame] | 1106 | free(config_filename); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 1107 | return ret; |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1108 | |
| 1109 | write_err_out: |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1110 | ret = write_error(lock->filename); |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 1111 | goto out_free; |
| 1112 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 1115 | static int section_name_match (const char *buf, const char *name) |
| 1116 | { |
| 1117 | int i = 0, j = 0, dot = 0; |
| 1118 | for (; buf[i] && buf[i] != ']'; i++) { |
| 1119 | if (!dot && isspace(buf[i])) { |
| 1120 | dot = 1; |
| 1121 | if (name[j++] != '.') |
| 1122 | break; |
| 1123 | for (i++; isspace(buf[i]); i++) |
| 1124 | ; /* do nothing */ |
| 1125 | if (buf[i] != '"') |
| 1126 | break; |
| 1127 | continue; |
| 1128 | } |
| 1129 | if (buf[i] == '\\' && dot) |
| 1130 | i++; |
| 1131 | else if (buf[i] == '"' && dot) { |
| 1132 | for (i++; isspace(buf[i]); i++) |
| 1133 | ; /* do_nothing */ |
| 1134 | break; |
| 1135 | } |
| 1136 | if (buf[i] != name[j++]) |
| 1137 | break; |
| 1138 | } |
| 1139 | return (buf[i] == ']' && name[j] == 0); |
| 1140 | } |
| 1141 | |
| 1142 | /* if new_name == NULL, the section is removed instead */ |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1143 | int git_config_rename_section(const char *old_name, const char *new_name) |
| 1144 | { |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 1145 | int ret = 0, remove = 0; |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1146 | char *config_filename; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1147 | struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1); |
| 1148 | int out_fd; |
| 1149 | char buf[1024]; |
| 1150 | |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 1151 | if (config_exclusive_filename) |
| 1152 | config_filename = xstrdup(config_exclusive_filename); |
| 1153 | else |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 1154 | config_filename = git_pathdup("config"); |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1155 | out_fd = hold_lock_file_for_update(lock, config_filename, 0); |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1156 | if (out_fd < 0) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1157 | ret = error("could not lock config file %s", config_filename); |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1158 | goto out; |
| 1159 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1160 | |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1161 | if (!(config_file = fopen(config_filename, "rb"))) { |
Geert Bosch | 01ebb9d | 2007-04-05 10:20:55 -0400 | [diff] [blame] | 1162 | /* no config file means nothing to rename, no error */ |
| 1163 | goto unlock_and_out; |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1164 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1165 | |
| 1166 | while (fgets(buf, sizeof(buf), config_file)) { |
| 1167 | int i; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1168 | int length; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1169 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
| 1170 | ; /* do nothing */ |
| 1171 | if (buf[i] == '[') { |
| 1172 | /* it's a section */ |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 1173 | if (section_name_match (&buf[i+1], old_name)) { |
| 1174 | ret++; |
| 1175 | if (new_name == NULL) { |
| 1176 | remove = 1; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1177 | continue; |
| 1178 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1179 | store.baselen = strlen(new_name); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1180 | if (!store_write_section(out_fd, new_name)) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1181 | ret = write_error(lock->filename); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1182 | goto out; |
| 1183 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1184 | continue; |
| 1185 | } |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 1186 | remove = 0; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1187 | } |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 1188 | if (remove) |
| 1189 | continue; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1190 | length = strlen(buf); |
| 1191 | if (write_in_full(out_fd, buf, length) != length) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1192 | ret = write_error(lock->filename); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1193 | goto out; |
| 1194 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1195 | } |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1196 | fclose(config_file); |
Geert Bosch | 01ebb9d | 2007-04-05 10:20:55 -0400 | [diff] [blame] | 1197 | unlock_and_out: |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 1198 | if (commit_lock_file(lock) < 0) |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1199 | ret = error("could not commit config file %s", config_filename); |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 1200 | out: |
| 1201 | free(config_filename); |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 1202 | return ret; |
| 1203 | } |
Junio C Hamano | 40ea4ed | 2008-02-11 10:41:18 -0800 | [diff] [blame] | 1204 | |
| 1205 | /* |
| 1206 | * Call this to report error for your variable that should not |
| 1207 | * get a boolean value (i.e. "[my] var" means "true"). |
| 1208 | */ |
| 1209 | int config_error_nonbool(const char *var) |
| 1210 | { |
| 1211 | return error("Missing value for '%s'", var); |
| 1212 | } |