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" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 9 | #include "lockfile.h" |
Johannes Sixt | 7f0e39f | 2007-11-13 21:05:06 +0100 | [diff] [blame] | 10 | #include "exec_cmd.h" |
Alex Riesen | 572e4f6 | 2010-03-26 23:56:01 +0100 | [diff] [blame] | 11 | #include "strbuf.h" |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 12 | #include "quote.h" |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 13 | #include "hashmap.h" |
| 14 | #include "string-list.h" |
Junio C Hamano | 599446d | 2015-04-16 10:47:45 -0700 | [diff] [blame] | 15 | #include "utf8.h" |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 16 | |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 17 | struct config_source { |
| 18 | struct config_source *prev; |
| 19 | union { |
| 20 | FILE *file; |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 21 | struct config_buf { |
| 22 | const char *buf; |
| 23 | size_t len; |
| 24 | size_t pos; |
| 25 | } buf; |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 26 | } u; |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 27 | const char *origin_type; |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 28 | const char *name; |
Jeff King | d14d424 | 2014-02-19 00:58:52 +0200 | [diff] [blame] | 29 | const char *path; |
Heiko Voigt | b2dc094 | 2013-07-12 00:48:30 +0200 | [diff] [blame] | 30 | int die_on_error; |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 31 | int linenr; |
| 32 | int eof; |
| 33 | struct strbuf value; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 34 | struct strbuf var; |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 35 | |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 36 | int (*do_fgetc)(struct config_source *c); |
| 37 | int (*do_ungetc)(int c, struct config_source *conf); |
| 38 | long (*do_ftell)(struct config_source *c); |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 41 | /* |
| 42 | * These variables record the "current" config source, which |
| 43 | * can be accessed by parsing callbacks. |
| 44 | * |
| 45 | * The "cf" variable will be non-NULL only when we are actually parsing a real |
| 46 | * config source (file, blob, cmdline, etc). |
| 47 | * |
| 48 | * The "current_config_kvi" variable will be non-NULL only when we are feeding |
| 49 | * cached config from a configset into a callback. |
| 50 | * |
| 51 | * They should generally never be non-NULL at the same time. If they are both |
| 52 | * NULL, then we aren't parsing anything (and depending on the function looking |
| 53 | * at the variables, it's either a bug for it to be called in the first place, |
| 54 | * or it's a function which can be reused for non-config purposes, and should |
| 55 | * fall back to some sane behavior). |
| 56 | */ |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 57 | static struct config_source *cf; |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 58 | static struct key_value_info *current_config_kvi; |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 59 | |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 60 | /* |
| 61 | * Similar to the variables above, this gives access to the "scope" of the |
| 62 | * current value (repo, global, etc). For cached values, it can be found via |
| 63 | * the current_config_kvi as above. During parsing, the current value can be |
| 64 | * found in this variable. It's not part of "cf" because it transcends a single |
| 65 | * file (i.e., a file included from .git/config is still in "repo" scope). |
| 66 | */ |
| 67 | static enum config_scope current_parsing_scope; |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 68 | |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 69 | static int zlib_compression_seen; |
| 70 | |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 71 | /* |
| 72 | * Default config_set that contains key-value pairs from the usual set of config |
| 73 | * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG |
| 74 | * config file and the global /etc/gitconfig) |
| 75 | */ |
| 76 | static struct config_set the_config_set; |
| 77 | |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 78 | static int config_file_fgetc(struct config_source *conf) |
| 79 | { |
Jeff King | 260d408 | 2015-04-16 04:51:18 -0400 | [diff] [blame] | 80 | return getc_unlocked(conf->u.file); |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static int config_file_ungetc(int c, struct config_source *conf) |
| 84 | { |
| 85 | return ungetc(c, conf->u.file); |
| 86 | } |
| 87 | |
| 88 | static long config_file_ftell(struct config_source *conf) |
| 89 | { |
| 90 | return ftell(conf->u.file); |
| 91 | } |
| 92 | |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 93 | |
| 94 | static int config_buf_fgetc(struct config_source *conf) |
| 95 | { |
| 96 | if (conf->u.buf.pos < conf->u.buf.len) |
| 97 | return conf->u.buf.buf[conf->u.buf.pos++]; |
| 98 | |
| 99 | return EOF; |
| 100 | } |
| 101 | |
| 102 | static int config_buf_ungetc(int c, struct config_source *conf) |
| 103 | { |
Jeff King | 1d0655c | 2015-02-05 16:00:24 -0500 | [diff] [blame] | 104 | if (conf->u.buf.pos > 0) { |
| 105 | conf->u.buf.pos--; |
| 106 | if (conf->u.buf.buf[conf->u.buf.pos] != c) |
| 107 | die("BUG: config_buf can only ungetc the same character"); |
| 108 | return c; |
| 109 | } |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 110 | |
| 111 | return EOF; |
| 112 | } |
| 113 | |
| 114 | static long config_buf_ftell(struct config_source *conf) |
| 115 | { |
| 116 | return conf->u.buf.pos; |
| 117 | } |
| 118 | |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 119 | #define MAX_INCLUDE_DEPTH 10 |
| 120 | static const char include_depth_advice[] = |
| 121 | "exceeded maximum include depth (%d) while including\n" |
| 122 | " %s\n" |
| 123 | "from\n" |
| 124 | " %s\n" |
| 125 | "Do you have circular includes?"; |
| 126 | static int handle_path_include(const char *path, struct config_include_data *inc) |
| 127 | { |
| 128 | int ret = 0; |
| 129 | struct strbuf buf = STRBUF_INIT; |
Jeff King | 67beb60 | 2014-01-27 20:37:30 -0500 | [diff] [blame] | 130 | char *expanded; |
Jeff King | 4c0a89f | 2012-04-25 08:00:36 -0400 | [diff] [blame] | 131 | |
Jeff King | 67beb60 | 2014-01-27 20:37:30 -0500 | [diff] [blame] | 132 | if (!path) |
| 133 | return config_error_nonbool("include.path"); |
| 134 | |
| 135 | expanded = expand_user_path(path); |
Jeff King | 4c0a89f | 2012-04-25 08:00:36 -0400 | [diff] [blame] | 136 | if (!expanded) |
Jeff King | 8c3ca35 | 2016-04-09 13:42:31 -0400 | [diff] [blame] | 137 | return error("could not expand include path '%s'", path); |
Jeff King | 4c0a89f | 2012-04-25 08:00:36 -0400 | [diff] [blame] | 138 | path = expanded; |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * Use an absolute path as-is, but interpret relative paths |
| 142 | * based on the including config file. |
| 143 | */ |
| 144 | if (!is_absolute_path(path)) { |
| 145 | char *slash; |
| 146 | |
Jeff King | d14d424 | 2014-02-19 00:58:52 +0200 | [diff] [blame] | 147 | if (!cf || !cf->path) |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 148 | return error("relative config includes must come from files"); |
| 149 | |
Jeff King | d14d424 | 2014-02-19 00:58:52 +0200 | [diff] [blame] | 150 | slash = find_last_dir_sep(cf->path); |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 151 | if (slash) |
Jeff King | d14d424 | 2014-02-19 00:58:52 +0200 | [diff] [blame] | 152 | strbuf_add(&buf, cf->path, slash - cf->path + 1); |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 153 | strbuf_addstr(&buf, path); |
| 154 | path = buf.buf; |
| 155 | } |
| 156 | |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 157 | if (!access_or_die(path, R_OK, 0)) { |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 158 | if (++inc->depth > MAX_INCLUDE_DEPTH) |
| 159 | die(include_depth_advice, MAX_INCLUDE_DEPTH, path, |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 160 | !cf ? "<unknown>" : |
| 161 | cf->name ? cf->name : |
| 162 | "the command line"); |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 163 | ret = git_config_from_file(git_config_include, path, inc); |
| 164 | inc->depth--; |
| 165 | } |
| 166 | strbuf_release(&buf); |
Jeff King | 4c0a89f | 2012-04-25 08:00:36 -0400 | [diff] [blame] | 167 | free(expanded); |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | int git_config_include(const char *var, const char *value, void *data) |
| 172 | { |
| 173 | struct config_include_data *inc = data; |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 174 | int ret; |
| 175 | |
| 176 | /* |
| 177 | * Pass along all values, including "include" directives; this makes it |
| 178 | * possible to query information on the includes themselves. |
| 179 | */ |
| 180 | ret = inc->fn(var, value, inc->data); |
| 181 | if (ret < 0) |
| 182 | return ret; |
| 183 | |
René Scharfe | 37007c3 | 2014-08-30 18:07:05 +0200 | [diff] [blame] | 184 | if (!strcmp(var, "include.path")) |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 185 | ret = handle_path_include(value, inc); |
| 186 | return ret; |
| 187 | } |
| 188 | |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 189 | void git_config_push_parameter(const char *text) |
| 190 | { |
| 191 | struct strbuf env = STRBUF_INIT; |
| 192 | const char *old = getenv(CONFIG_DATA_ENVIRONMENT); |
Jeff King | d1f8849 | 2016-03-22 15:50:51 -0400 | [diff] [blame] | 193 | if (old && *old) { |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 194 | strbuf_addstr(&env, old); |
| 195 | strbuf_addch(&env, ' '); |
| 196 | } |
| 197 | sq_quote_buf(&env, text); |
| 198 | setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1); |
| 199 | strbuf_release(&env); |
| 200 | } |
| 201 | |
Jeff King | 2496844 | 2011-06-09 11:56:42 -0400 | [diff] [blame] | 202 | int git_config_parse_parameter(const char *text, |
| 203 | config_fn_t fn, void *data) |
Alex Riesen | 8b1fa77 | 2010-03-26 23:53:57 +0100 | [diff] [blame] | 204 | { |
Junio C Hamano | a789ca7 | 2014-08-04 15:40:19 -0700 | [diff] [blame] | 205 | const char *value; |
Alex Riesen | 572e4f6 | 2010-03-26 23:56:01 +0100 | [diff] [blame] | 206 | struct strbuf **pair; |
Junio C Hamano | a789ca7 | 2014-08-04 15:40:19 -0700 | [diff] [blame] | 207 | |
Jeff King | f77bcca | 2011-06-09 11:55:09 -0400 | [diff] [blame] | 208 | pair = strbuf_split_str(text, '=', 2); |
Jeff King | c5d6350 | 2011-06-09 11:52:43 -0400 | [diff] [blame] | 209 | if (!pair[0]) |
| 210 | return error("bogus config parameter: %s", text); |
Junio C Hamano | a789ca7 | 2014-08-04 15:40:19 -0700 | [diff] [blame] | 211 | |
| 212 | if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') { |
Alex Riesen | 572e4f6 | 2010-03-26 23:56:01 +0100 | [diff] [blame] | 213 | strbuf_setlen(pair[0], pair[0]->len - 1); |
Junio C Hamano | a789ca7 | 2014-08-04 15:40:19 -0700 | [diff] [blame] | 214 | value = pair[1] ? pair[1]->buf : ""; |
| 215 | } else { |
| 216 | value = NULL; |
| 217 | } |
| 218 | |
Alex Riesen | 572e4f6 | 2010-03-26 23:56:01 +0100 | [diff] [blame] | 219 | strbuf_trim(pair[0]); |
| 220 | if (!pair[0]->len) { |
| 221 | strbuf_list_free(pair); |
Jeff King | 06eb708 | 2011-05-24 18:49:55 -0400 | [diff] [blame] | 222 | return error("bogus config parameter: %s", text); |
| 223 | } |
Jeff King | ffb20ce | 2014-05-23 16:03:47 -0400 | [diff] [blame] | 224 | strbuf_tolower(pair[0]); |
Junio C Hamano | a789ca7 | 2014-08-04 15:40:19 -0700 | [diff] [blame] | 225 | if (fn(pair[0]->buf, value, data) < 0) { |
Jeff King | 06eb708 | 2011-05-24 18:49:55 -0400 | [diff] [blame] | 226 | strbuf_list_free(pair); |
Alex Riesen | 8b1fa77 | 2010-03-26 23:53:57 +0100 | [diff] [blame] | 227 | return -1; |
Alex Riesen | 8b1fa77 | 2010-03-26 23:53:57 +0100 | [diff] [blame] | 228 | } |
Alex Riesen | 572e4f6 | 2010-03-26 23:56:01 +0100 | [diff] [blame] | 229 | strbuf_list_free(pair); |
Alex Riesen | 8b1fa77 | 2010-03-26 23:53:57 +0100 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
Jeff King | 06eb708 | 2011-05-24 18:49:55 -0400 | [diff] [blame] | 233 | int git_config_from_parameters(config_fn_t fn, void *data) |
| 234 | { |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 235 | const char *env = getenv(CONFIG_DATA_ENVIRONMENT); |
Jeff King | a77d6db | 2016-05-18 18:39:49 -0400 | [diff] [blame] | 236 | int ret = 0; |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 237 | char *envw; |
| 238 | const char **argv = NULL; |
| 239 | int nr = 0, alloc = 0; |
| 240 | int i; |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 241 | struct config_source source; |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 242 | |
| 243 | if (!env) |
| 244 | return 0; |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 245 | |
| 246 | memset(&source, 0, sizeof(source)); |
| 247 | source.prev = cf; |
| 248 | cf = &source; |
| 249 | |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 250 | /* sq_dequote will write over it */ |
| 251 | envw = xstrdup(env); |
| 252 | |
| 253 | if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) { |
Jeff King | a77d6db | 2016-05-18 18:39:49 -0400 | [diff] [blame] | 254 | ret = error("bogus format in " CONFIG_DATA_ENVIRONMENT); |
| 255 | goto out; |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | for (i = 0; i < nr; i++) { |
Jeff King | 06eb708 | 2011-05-24 18:49:55 -0400 | [diff] [blame] | 259 | if (git_config_parse_parameter(argv[i], fn, data) < 0) { |
Jeff King | a77d6db | 2016-05-18 18:39:49 -0400 | [diff] [blame] | 260 | ret = -1; |
| 261 | goto out; |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Jeff King | a77d6db | 2016-05-18 18:39:49 -0400 | [diff] [blame] | 265 | out: |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 266 | free(argv); |
| 267 | free(envw); |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 268 | cf = source.prev; |
Jeff King | a77d6db | 2016-05-18 18:39:49 -0400 | [diff] [blame] | 269 | return ret; |
Jeff King | 2b64fc8 | 2010-08-23 15:16:00 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 272 | static int get_next_char(void) |
| 273 | { |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 274 | int c = cf->do_fgetc(cf); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 275 | |
Heiko Voigt | dbb9a81 | 2013-05-11 15:19:29 +0200 | [diff] [blame] | 276 | if (c == '\r') { |
| 277 | /* DOS like systems */ |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 278 | c = cf->do_fgetc(cf); |
Heiko Voigt | dbb9a81 | 2013-05-11 15:19:29 +0200 | [diff] [blame] | 279 | if (c != '\n') { |
Jeff King | 5e0be13 | 2015-02-05 01:53:28 -0500 | [diff] [blame] | 280 | if (c != EOF) |
| 281 | cf->do_ungetc(c, cf); |
Heiko Voigt | dbb9a81 | 2013-05-11 15:19:29 +0200 | [diff] [blame] | 282 | c = '\r'; |
Junio C Hamano | db2c075 | 2005-11-02 13:02:57 -0800 | [diff] [blame] | 283 | } |
Heiko Voigt | dbb9a81 | 2013-05-11 15:19:29 +0200 | [diff] [blame] | 284 | } |
| 285 | if (c == '\n') |
| 286 | cf->linenr++; |
| 287 | if (c == EOF) { |
| 288 | cf->eof = 1; |
Matthieu Moy | b3b3f60 | 2014-08-07 04:59:13 -0700 | [diff] [blame] | 289 | cf->linenr++; |
Heiko Voigt | dbb9a81 | 2013-05-11 15:19:29 +0200 | [diff] [blame] | 290 | c = '\n'; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 291 | } |
| 292 | return c; |
| 293 | } |
| 294 | |
| 295 | static char *parse_value(void) |
| 296 | { |
Erik Faye-Lund | e96c19c | 2011-04-10 22:54:18 +0200 | [diff] [blame] | 297 | int quote = 0, comment = 0, space = 0; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 298 | |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 299 | strbuf_reset(&cf->value); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 300 | for (;;) { |
| 301 | int c = get_next_char(); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 302 | if (c == '\n') { |
Martin Stenberg | 4b34059 | 2012-03-09 22:57:54 +0100 | [diff] [blame] | 303 | if (quote) { |
| 304 | cf->linenr--; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 305 | return NULL; |
Martin Stenberg | 4b34059 | 2012-03-09 22:57:54 +0100 | [diff] [blame] | 306 | } |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 307 | return cf->value.buf; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 308 | } |
| 309 | if (comment) |
| 310 | continue; |
| 311 | if (isspace(c) && !quote) { |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 312 | if (cf->value.len) |
Björn Steinbrink | ebdaae3 | 2009-07-30 13:41:57 +0200 | [diff] [blame] | 313 | space++; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 314 | continue; |
| 315 | } |
Johannes Schindelin | 7ebdba6 | 2006-05-02 16:58:37 +0200 | [diff] [blame] | 316 | if (!quote) { |
| 317 | if (c == ';' || c == '#') { |
| 318 | comment = 1; |
| 319 | continue; |
| 320 | } |
| 321 | } |
Björn Steinbrink | ebdaae3 | 2009-07-30 13:41:57 +0200 | [diff] [blame] | 322 | for (; space; space--) |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 323 | strbuf_addch(&cf->value, ' '); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 324 | if (c == '\\') { |
| 325 | c = get_next_char(); |
| 326 | switch (c) { |
| 327 | case '\n': |
| 328 | continue; |
| 329 | case 't': |
| 330 | c = '\t'; |
| 331 | break; |
| 332 | case 'b': |
| 333 | c = '\b'; |
| 334 | break; |
| 335 | case 'n': |
| 336 | c = '\n'; |
| 337 | break; |
Linus Torvalds | 5cbb401 | 2005-10-11 15:24:11 -0700 | [diff] [blame] | 338 | /* Some characters escape as themselves */ |
| 339 | case '\\': case '"': |
| 340 | break; |
| 341 | /* Reject unknown escape sequences */ |
| 342 | default: |
| 343 | return NULL; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 344 | } |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 345 | strbuf_addch(&cf->value, c); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 346 | continue; |
| 347 | } |
| 348 | if (c == '"') { |
| 349 | quote = 1-quote; |
| 350 | continue; |
| 351 | } |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 352 | strbuf_addch(&cf->value, c); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 356 | static inline int iskeychar(int c) |
| 357 | { |
| 358 | return isalnum(c) || c == '-'; |
| 359 | } |
| 360 | |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 361 | static int get_value(config_fn_t fn, void *data, struct strbuf *name) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 362 | { |
| 363 | int c; |
| 364 | char *value; |
Matthieu Moy | b3b3f60 | 2014-08-07 04:59:13 -0700 | [diff] [blame] | 365 | int ret; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 366 | |
| 367 | /* Get the full name */ |
| 368 | for (;;) { |
| 369 | c = get_next_char(); |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 370 | if (cf->eof) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 371 | break; |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 372 | if (!iskeychar(c)) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 373 | break; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 374 | strbuf_addch(name, tolower(c)); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 375 | } |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 376 | |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 377 | while (c == ' ' || c == '\t') |
| 378 | c = get_next_char(); |
| 379 | |
| 380 | value = NULL; |
| 381 | if (c != '\n') { |
| 382 | if (c != '=') |
| 383 | return -1; |
| 384 | value = parse_value(); |
| 385 | if (!value) |
| 386 | return -1; |
| 387 | } |
Matthieu Moy | b3b3f60 | 2014-08-07 04:59:13 -0700 | [diff] [blame] | 388 | /* |
| 389 | * We already consumed the \n, but we need linenr to point to |
| 390 | * the line we just parsed during the call to fn to get |
| 391 | * accurate line number in error messages. |
| 392 | */ |
| 393 | cf->linenr--; |
| 394 | ret = fn(name->buf, value, data); |
| 395 | cf->linenr++; |
| 396 | return ret; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 399 | static int get_extended_base_var(struct strbuf *name, int c) |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 400 | { |
| 401 | do { |
| 402 | if (c == '\n') |
Martin Stenberg | 4b34059 | 2012-03-09 22:57:54 +0100 | [diff] [blame] | 403 | goto error_incomplete_line; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 404 | c = get_next_char(); |
| 405 | } while (isspace(c)); |
| 406 | |
| 407 | /* We require the format to be '[base "extension"]' */ |
| 408 | if (c != '"') |
| 409 | return -1; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 410 | strbuf_addch(name, '.'); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 411 | |
| 412 | for (;;) { |
| 413 | int c = get_next_char(); |
| 414 | if (c == '\n') |
Martin Stenberg | 4b34059 | 2012-03-09 22:57:54 +0100 | [diff] [blame] | 415 | goto error_incomplete_line; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 416 | if (c == '"') |
| 417 | break; |
| 418 | if (c == '\\') { |
| 419 | c = get_next_char(); |
| 420 | if (c == '\n') |
Martin Stenberg | 4b34059 | 2012-03-09 22:57:54 +0100 | [diff] [blame] | 421 | goto error_incomplete_line; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 422 | } |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 423 | strbuf_addch(name, c); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Final ']' */ |
| 427 | if (get_next_char() != ']') |
| 428 | return -1; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 429 | return 0; |
Martin Stenberg | 4b34059 | 2012-03-09 22:57:54 +0100 | [diff] [blame] | 430 | error_incomplete_line: |
| 431 | cf->linenr--; |
| 432 | return -1; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 435 | static int get_base_var(struct strbuf *name) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 436 | { |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 437 | for (;;) { |
| 438 | int c = get_next_char(); |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 439 | if (cf->eof) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 440 | return -1; |
| 441 | if (c == ']') |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 442 | return 0; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 443 | if (isspace(c)) |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 444 | return get_extended_base_var(name, c); |
Linus Torvalds | 38c5afa | 2006-10-30 08:25:36 -0800 | [diff] [blame] | 445 | if (!iskeychar(c) && c != '.') |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 446 | return -1; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 447 | strbuf_addch(name, tolower(c)); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 451 | static int git_parse_source(config_fn_t fn, void *data) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 452 | { |
| 453 | int comment = 0; |
| 454 | int baselen = 0; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 455 | struct strbuf *var = &cf->var; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 456 | |
Petr Baudis | de05640 | 2008-10-01 22:13:02 +0200 | [diff] [blame] | 457 | /* U+FEFF Byte Order Mark in UTF8 */ |
Junio C Hamano | 599446d | 2015-04-16 10:47:45 -0700 | [diff] [blame] | 458 | const char *bomptr = utf8_bom; |
Petr Baudis | de05640 | 2008-10-01 22:13:02 +0200 | [diff] [blame] | 459 | |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 460 | for (;;) { |
| 461 | int c = get_next_char(); |
Petr Baudis | de05640 | 2008-10-01 22:13:02 +0200 | [diff] [blame] | 462 | if (bomptr && *bomptr) { |
| 463 | /* We are at the file beginning; skip UTF8-encoded BOM |
| 464 | * if present. Sane editors won't put this in on their |
| 465 | * own, but e.g. Windows Notepad will do it happily. */ |
Junio C Hamano | 599446d | 2015-04-16 10:47:45 -0700 | [diff] [blame] | 466 | if (c == (*bomptr & 0377)) { |
Petr Baudis | de05640 | 2008-10-01 22:13:02 +0200 | [diff] [blame] | 467 | bomptr++; |
| 468 | continue; |
| 469 | } else { |
| 470 | /* Do not tolerate partial BOM. */ |
| 471 | if (bomptr != utf8_bom) |
| 472 | break; |
| 473 | /* No BOM at file beginning. Cool. */ |
| 474 | bomptr = NULL; |
| 475 | } |
| 476 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 477 | if (c == '\n') { |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 478 | if (cf->eof) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 479 | return 0; |
| 480 | comment = 0; |
| 481 | continue; |
| 482 | } |
| 483 | if (comment || isspace(c)) |
| 484 | continue; |
| 485 | if (c == '#' || c == ';') { |
| 486 | comment = 1; |
| 487 | continue; |
| 488 | } |
| 489 | if (c == '[') { |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 490 | /* Reset prior to determining a new stem */ |
| 491 | strbuf_reset(var); |
| 492 | if (get_base_var(var) < 0 || var->len < 1) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 493 | break; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 494 | strbuf_addch(var, '.'); |
| 495 | baselen = var->len; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 496 | continue; |
| 497 | } |
| 498 | if (!isalpha(c)) |
| 499 | break; |
Ben Walton | 0971e99 | 2012-09-30 20:44:36 +0100 | [diff] [blame] | 500 | /* |
| 501 | * Truncate the var name back to the section header |
| 502 | * stem prior to grabbing the suffix part of the name |
| 503 | * and the value. |
| 504 | */ |
| 505 | strbuf_setlen(var, baselen); |
| 506 | strbuf_addch(var, tolower(c)); |
| 507 | if (get_value(fn, data, var) < 0) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 508 | break; |
| 509 | } |
Heiko Voigt | b2dc094 | 2013-07-12 00:48:30 +0200 | [diff] [blame] | 510 | if (cf->die_on_error) |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 511 | die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name); |
Heiko Voigt | b2dc094 | 2013-07-12 00:48:30 +0200 | [diff] [blame] | 512 | else |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 513 | return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 516 | static int parse_unit_factor(const char *end, uintmax_t *val) |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 517 | { |
| 518 | if (!*end) |
| 519 | return 1; |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 520 | else if (!strcasecmp(end, "k")) { |
| 521 | *val *= 1024; |
| 522 | return 1; |
| 523 | } |
| 524 | else if (!strcasecmp(end, "m")) { |
| 525 | *val *= 1024 * 1024; |
| 526 | return 1; |
| 527 | } |
| 528 | else if (!strcasecmp(end, "g")) { |
| 529 | *val *= 1024 * 1024 * 1024; |
| 530 | return 1; |
| 531 | } |
| 532 | return 0; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 533 | } |
| 534 | |
Jeff King | 7192777 | 2013-09-08 04:29:27 -0400 | [diff] [blame] | 535 | static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 536 | { |
| 537 | if (value && *value) { |
| 538 | char *end; |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 539 | intmax_t val; |
| 540 | uintmax_t uval; |
| 541 | uintmax_t factor = 1; |
| 542 | |
| 543 | errno = 0; |
| 544 | val = strtoimax(value, &end, 0); |
| 545 | if (errno == ERANGE) |
| 546 | return 0; |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 547 | if (!parse_unit_factor(end, &factor)) { |
| 548 | errno = EINVAL; |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 549 | return 0; |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 550 | } |
René Scharfe | 83915ba | 2014-11-15 14:27:21 +0100 | [diff] [blame] | 551 | uval = labs(val); |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 552 | uval *= factor; |
René Scharfe | 83915ba | 2014-11-15 14:27:21 +0100 | [diff] [blame] | 553 | if (uval > max || labs(val) > uval) { |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 554 | errno = ERANGE; |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 555 | return 0; |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 556 | } |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 557 | val *= factor; |
| 558 | *ret = val; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 559 | return 1; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 560 | } |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 561 | errno = EINVAL; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Ramsay Jones | 0b4dc66 | 2013-10-06 21:48:29 +0100 | [diff] [blame] | 565 | static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 566 | { |
| 567 | if (value && *value) { |
| 568 | char *end; |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 569 | uintmax_t val; |
| 570 | uintmax_t oldval; |
| 571 | |
| 572 | errno = 0; |
| 573 | val = strtoumax(value, &end, 0); |
| 574 | if (errno == ERANGE) |
| 575 | return 0; |
| 576 | oldval = val; |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 577 | if (!parse_unit_factor(end, &val)) { |
| 578 | errno = EINVAL; |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 579 | return 0; |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 580 | } |
| 581 | if (val > max || oldval > val) { |
| 582 | errno = ERANGE; |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 583 | return 0; |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 584 | } |
Shawn O. Pearce | c8deb5a | 2007-12-25 02:18:05 -0500 | [diff] [blame] | 585 | *ret = val; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 586 | return 1; |
| 587 | } |
Jeff King | 33fdd77 | 2013-09-08 04:36:42 -0400 | [diff] [blame] | 588 | errno = EINVAL; |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Jeff King | 42d194e | 2013-09-08 04:33:08 -0400 | [diff] [blame] | 592 | static int git_parse_int(const char *value, int *ret) |
Jeff King | c1867ce | 2008-02-20 19:00:32 -0500 | [diff] [blame] | 593 | { |
Jeff King | 7192777 | 2013-09-08 04:29:27 -0400 | [diff] [blame] | 594 | intmax_t tmp; |
Jeff King | 42d194e | 2013-09-08 04:33:08 -0400 | [diff] [blame] | 595 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int))) |
Jeff King | 7192777 | 2013-09-08 04:29:27 -0400 | [diff] [blame] | 596 | return 0; |
| 597 | *ret = tmp; |
| 598 | return 1; |
| 599 | } |
| 600 | |
Jeff King | 0016024 | 2013-09-08 04:40:02 -0400 | [diff] [blame] | 601 | static int git_parse_int64(const char *value, int64_t *ret) |
| 602 | { |
| 603 | intmax_t tmp; |
| 604 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t))) |
| 605 | return 0; |
| 606 | *ret = tmp; |
| 607 | return 1; |
| 608 | } |
| 609 | |
Jeff King | 7192777 | 2013-09-08 04:29:27 -0400 | [diff] [blame] | 610 | int git_parse_ulong(const char *value, unsigned long *ret) |
| 611 | { |
| 612 | uintmax_t tmp; |
| 613 | if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long))) |
| 614 | return 0; |
| 615 | *ret = tmp; |
| 616 | return 1; |
| 617 | } |
| 618 | |
Jeff King | 06bdc23 | 2014-04-16 12:51:47 -0400 | [diff] [blame] | 619 | NORETURN |
Jeff King | 2f66658 | 2013-09-08 04:38:22 -0400 | [diff] [blame] | 620 | static void die_bad_number(const char *name, const char *value) |
Jeff King | c1867ce | 2008-02-20 19:00:32 -0500 | [diff] [blame] | 621 | { |
Jeff King | 2f66658 | 2013-09-08 04:38:22 -0400 | [diff] [blame] | 622 | const char *reason = errno == ERANGE ? |
| 623 | "out of range" : |
| 624 | "invalid unit"; |
| 625 | if (!value) |
| 626 | value = ""; |
| 627 | |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 628 | if (cf && cf->origin_type && cf->name) |
| 629 | die(_("bad numeric config value '%s' for '%s' in %s %s: %s"), |
| 630 | value, name, cf->origin_type, cf->name, reason); |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 631 | die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason); |
Jeff King | c1867ce | 2008-02-20 19:00:32 -0500 | [diff] [blame] | 632 | } |
| 633 | |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 634 | int git_config_int(const char *name, const char *value) |
| 635 | { |
Jeff King | 42d194e | 2013-09-08 04:33:08 -0400 | [diff] [blame] | 636 | int ret; |
| 637 | if (!git_parse_int(value, &ret)) |
Jeff King | 2f66658 | 2013-09-08 04:38:22 -0400 | [diff] [blame] | 638 | die_bad_number(name, value); |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 639 | return ret; |
| 640 | } |
| 641 | |
Jeff King | 0016024 | 2013-09-08 04:40:02 -0400 | [diff] [blame] | 642 | int64_t git_config_int64(const char *name, const char *value) |
| 643 | { |
| 644 | int64_t ret; |
| 645 | if (!git_parse_int64(value, &ret)) |
| 646 | die_bad_number(name, value); |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | unsigned long git_config_ulong(const char *name, const char *value) |
| 651 | { |
| 652 | unsigned long ret; |
| 653 | if (!git_parse_ulong(value, &ret)) |
Jeff King | 2f66658 | 2013-09-08 04:38:22 -0400 | [diff] [blame] | 654 | die_bad_number(name, value); |
Brian Downing | 0b87b6e | 2007-07-12 08:32:26 -0500 | [diff] [blame] | 655 | return ret; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Dave Borowitz | 9a549d4 | 2015-08-19 11:26:44 -0400 | [diff] [blame] | 658 | int git_parse_maybe_bool(const char *value) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 659 | { |
| 660 | if (!value) |
| 661 | return 1; |
| 662 | if (!*value) |
| 663 | return 0; |
Junio C Hamano | 8420ccd | 2010-02-16 23:59:46 -0800 | [diff] [blame] | 664 | if (!strcasecmp(value, "true") |
| 665 | || !strcasecmp(value, "yes") |
| 666 | || !strcasecmp(value, "on")) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 667 | return 1; |
Junio C Hamano | 8420ccd | 2010-02-16 23:59:46 -0800 | [diff] [blame] | 668 | if (!strcasecmp(value, "false") |
| 669 | || !strcasecmp(value, "no") |
| 670 | || !strcasecmp(value, "off")) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 671 | return 0; |
Junio C Hamano | 8420ccd | 2010-02-16 23:59:46 -0800 | [diff] [blame] | 672 | return -1; |
| 673 | } |
| 674 | |
Jeff King | b2be2f6 | 2010-11-17 12:00:45 -0500 | [diff] [blame] | 675 | int git_config_maybe_bool(const char *name, const char *value) |
| 676 | { |
Dave Borowitz | 9a549d4 | 2015-08-19 11:26:44 -0400 | [diff] [blame] | 677 | int v = git_parse_maybe_bool(value); |
Jeff King | b2be2f6 | 2010-11-17 12:00:45 -0500 | [diff] [blame] | 678 | if (0 <= v) |
| 679 | return v; |
Jeff King | 42d194e | 2013-09-08 04:33:08 -0400 | [diff] [blame] | 680 | if (git_parse_int(value, &v)) |
Jeff King | db6195e | 2010-12-18 22:36:41 -0500 | [diff] [blame] | 681 | return !!v; |
Jeff King | b2be2f6 | 2010-11-17 12:00:45 -0500 | [diff] [blame] | 682 | return -1; |
| 683 | } |
| 684 | |
Junio C Hamano | 8420ccd | 2010-02-16 23:59:46 -0800 | [diff] [blame] | 685 | int git_config_bool_or_int(const char *name, const char *value, int *is_bool) |
| 686 | { |
Dave Borowitz | 9a549d4 | 2015-08-19 11:26:44 -0400 | [diff] [blame] | 687 | int v = git_parse_maybe_bool(value); |
Junio C Hamano | 8420ccd | 2010-02-16 23:59:46 -0800 | [diff] [blame] | 688 | if (0 <= v) { |
| 689 | *is_bool = 1; |
| 690 | return v; |
| 691 | } |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 692 | *is_bool = 0; |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 693 | return git_config_int(name, value); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 696 | int git_config_bool(const char *name, const char *value) |
| 697 | { |
| 698 | int discard; |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 699 | return !!git_config_bool_or_int(name, value, &discard); |
Junio C Hamano | a53f2ec | 2008-04-12 18:33:31 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Christian Couder | ea5105a | 2008-02-16 06:00:24 +0100 | [diff] [blame] | 702 | int git_config_string(const char **dest, const char *var, const char *value) |
| 703 | { |
| 704 | if (!value) |
| 705 | return config_error_nonbool(var); |
| 706 | *dest = xstrdup(value); |
| 707 | return 0; |
| 708 | } |
| 709 | |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 710 | int git_config_pathname(const char **dest, const char *var, const char *value) |
| 711 | { |
| 712 | if (!value) |
| 713 | return config_error_nonbool(var); |
| 714 | *dest = expand_user_path(value); |
| 715 | if (!*dest) |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 716 | die(_("failed to expand user dir in: '%s'"), value); |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 717 | return 0; |
| 718 | } |
| 719 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 720 | static int git_default_core_config(const char *var, const char *value) |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 721 | { |
| 722 | /* This needs a better name */ |
| 723 | if (!strcmp(var, "core.filemode")) { |
| 724 | trust_executable_bit = git_config_bool(var, value); |
| 725 | return 0; |
| 726 | } |
Alex Riesen | 1ce4790 | 2008-07-28 08:31:28 +0200 | [diff] [blame] | 727 | if (!strcmp(var, "core.trustctime")) { |
| 728 | trust_ctime = git_config_bool(var, value); |
| 729 | return 0; |
| 730 | } |
Junio C Hamano | c1b5d73 | 2013-05-06 22:32:58 -0700 | [diff] [blame] | 731 | if (!strcmp(var, "core.checkstat")) { |
Robin Rosenberg | c08e4d5 | 2013-01-22 08:49:22 +0100 | [diff] [blame] | 732 | if (!strcasecmp(value, "default")) |
| 733 | check_stat = 1; |
| 734 | else if (!strcasecmp(value, "minimal")) |
| 735 | check_stat = 0; |
| 736 | } |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 737 | |
Junio C Hamano | 9378c16 | 2007-06-24 15:11:24 -0700 | [diff] [blame] | 738 | if (!strcmp(var, "core.quotepath")) { |
| 739 | quote_path_fully = git_config_bool(var, value); |
| 740 | return 0; |
| 741 | } |
| 742 | |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 743 | if (!strcmp(var, "core.symlinks")) { |
| 744 | has_symlinks = git_config_bool(var, value); |
| 745 | return 0; |
| 746 | } |
| 747 | |
Linus Torvalds | 0a9b88b | 2008-03-21 16:52:46 -0700 | [diff] [blame] | 748 | if (!strcmp(var, "core.ignorecase")) { |
| 749 | ignore_case = git_config_bool(var, value); |
| 750 | return 0; |
| 751 | } |
| 752 | |
Junio C Hamano | 64589a0 | 2011-10-06 13:22:24 -0500 | [diff] [blame] | 753 | if (!strcmp(var, "core.attributesfile")) |
| 754 | return git_config_pathname(&git_attributes_file, var, value); |
| 755 | |
Ævar Arnfjörð Bjarmason | 867ad08 | 2016-05-04 22:58:12 +0000 | [diff] [blame] | 756 | if (!strcmp(var, "core.hookspath")) |
| 757 | return git_config_pathname(&git_hooks_path, var, value); |
| 758 | |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 759 | if (!strcmp(var, "core.bare")) { |
| 760 | is_bare_repository_cfg = git_config_bool(var, value); |
| 761 | return 0; |
| 762 | } |
| 763 | |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 764 | if (!strcmp(var, "core.ignorestat")) { |
| 765 | assume_unchanged = git_config_bool(var, value); |
| 766 | return 0; |
| 767 | } |
| 768 | |
Junio C Hamano | e388c73 | 2006-05-02 00:40:24 -0700 | [diff] [blame] | 769 | if (!strcmp(var, "core.prefersymlinkrefs")) { |
| 770 | prefer_symlink_refs = git_config_bool(var, value); |
Johannes Schindelin | f8348be | 2005-11-15 19:24:19 +0100 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
Shawn Pearce | 6de08ae | 2006-05-17 05:55:40 -0400 | [diff] [blame] | 774 | if (!strcmp(var, "core.logallrefupdates")) { |
| 775 | log_all_ref_updates = git_config_bool(var, value); |
| 776 | return 0; |
| 777 | } |
| 778 | |
Junio C Hamano | 2f8acdb | 2006-03-20 18:45:47 -0800 | [diff] [blame] | 779 | if (!strcmp(var, "core.warnambiguousrefs")) { |
| 780 | warn_ambiguous_refs = git_config_bool(var, value); |
| 781 | return 0; |
| 782 | } |
| 783 | |
Junio C Hamano | a71f09f | 2011-03-20 22:26:24 -0700 | [diff] [blame] | 784 | if (!strcmp(var, "core.abbrev")) { |
Linus Torvalds | dce9648 | 2010-10-28 11:28:04 -0700 | [diff] [blame] | 785 | int abbrev = git_config_int(var, value); |
| 786 | if (abbrev < minimum_abbrev || abbrev > 40) |
| 787 | return -1; |
| 788 | default_abbrev = abbrev; |
| 789 | return 0; |
| 790 | } |
| 791 | |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 792 | if (!strcmp(var, "core.loosecompression")) { |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 793 | int level = git_config_int(var, value); |
| 794 | if (level == -1) |
| 795 | level = Z_DEFAULT_COMPRESSION; |
| 796 | else if (level < 0 || level > Z_BEST_COMPRESSION) |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 797 | die(_("bad zlib compression level %d"), level); |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 798 | zlib_compression_level = level; |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 799 | zlib_compression_seen = 1; |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | if (!strcmp(var, "core.compression")) { |
| 804 | int level = git_config_int(var, value); |
| 805 | if (level == -1) |
| 806 | level = Z_DEFAULT_COMPRESSION; |
| 807 | else if (level < 0 || level > Z_BEST_COMPRESSION) |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 808 | die(_("bad zlib compression level %d"), level); |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 809 | core_compression_level = level; |
| 810 | core_compression_seen = 1; |
| 811 | if (!zlib_compression_seen) |
| 812 | zlib_compression_level = level; |
Joachim B Haga | 12f6c30 | 2006-07-03 22:11:47 +0200 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 816 | if (!strcmp(var, "core.packedgitwindowsize")) { |
Junio C Hamano | 5faaf24 | 2007-02-14 13:20:41 -0800 | [diff] [blame] | 817 | int pgsz_x2 = getpagesize() * 2; |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 818 | packed_git_window_size = git_config_ulong(var, value); |
Junio C Hamano | 5faaf24 | 2007-02-14 13:20:41 -0800 | [diff] [blame] | 819 | |
| 820 | /* This value must be multiple of (pagesize * 2) */ |
| 821 | packed_git_window_size /= pgsz_x2; |
| 822 | if (packed_git_window_size < 1) |
| 823 | packed_git_window_size = 1; |
| 824 | packed_git_window_size *= pgsz_x2; |
Shawn O. Pearce | 60bb8b1 | 2006-12-23 02:34:28 -0500 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
Junio C Hamano | 1536628 | 2011-04-05 10:44:11 -0700 | [diff] [blame] | 828 | if (!strcmp(var, "core.bigfilethreshold")) { |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 829 | big_file_threshold = git_config_ulong(var, value); |
Junio C Hamano | 1536628 | 2011-04-05 10:44:11 -0700 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | |
Shawn O. Pearce | 77ccc5b | 2006-12-23 02:33:35 -0500 | [diff] [blame] | 833 | if (!strcmp(var, "core.packedgitlimit")) { |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 834 | packed_git_limit = git_config_ulong(var, value); |
Shawn O. Pearce | 77ccc5b | 2006-12-23 02:33:35 -0500 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 838 | if (!strcmp(var, "core.deltabasecachelimit")) { |
Nick Alcock | ebaa1bd | 2011-11-02 15:46:23 +0000 | [diff] [blame] | 839 | delta_base_cache_limit = git_config_ulong(var, value); |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 843 | if (!strcmp(var, "core.autocrlf")) { |
Linus Torvalds | d7f4633 | 2007-02-13 18:16:12 -0800 | [diff] [blame] | 844 | if (value && !strcasecmp(value, "input")) { |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 845 | auto_crlf = AUTO_CRLF_INPUT; |
Linus Torvalds | d7f4633 | 2007-02-13 18:16:12 -0800 | [diff] [blame] | 846 | return 0; |
| 847 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 848 | auto_crlf = git_config_bool(var, value); |
| 849 | return 0; |
| 850 | } |
| 851 | |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 852 | if (!strcmp(var, "core.safecrlf")) { |
| 853 | if (value && !strcasecmp(value, "warn")) { |
| 854 | safe_crlf = SAFE_CRLF_WARN; |
| 855 | return 0; |
| 856 | } |
| 857 | safe_crlf = git_config_bool(var, value); |
| 858 | return 0; |
| 859 | } |
| 860 | |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 861 | if (!strcmp(var, "core.eol")) { |
| 862 | if (value && !strcasecmp(value, "lf")) |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 863 | core_eol = EOL_LF; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 864 | else if (value && !strcasecmp(value, "crlf")) |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 865 | core_eol = EOL_CRLF; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 866 | else if (value && !strcasecmp(value, "native")) |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 867 | core_eol = EOL_NATIVE; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 868 | else |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 869 | core_eol = EOL_UNSET; |
Eyvind Bernhardsen | 942e774 | 2010-06-04 21:29:08 +0200 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
Johannes Schindelin | a97a746 | 2009-10-09 12:21:57 +0200 | [diff] [blame] | 873 | if (!strcmp(var, "core.notesref")) { |
| 874 | notes_ref_name = xstrdup(value); |
| 875 | return 0; |
| 876 | } |
| 877 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 878 | if (!strcmp(var, "core.pager")) |
| 879 | return git_config_string(&pager_program, var, value); |
| 880 | |
| 881 | if (!strcmp(var, "core.editor")) |
| 882 | return git_config_string(&editor_program, var, value); |
| 883 | |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 884 | if (!strcmp(var, "core.commentchar")) { |
Jeff King | 649409b | 2014-07-24 00:42:39 -0400 | [diff] [blame] | 885 | if (!value) |
| 886 | return config_error_nonbool(var); |
Junio C Hamano | ad524f8 | 2014-07-28 11:30:41 -0700 | [diff] [blame] | 887 | else if (!strcasecmp(value, "auto")) |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 888 | auto_comment_line_char = 1; |
Junio C Hamano | ad524f8 | 2014-07-28 11:30:41 -0700 | [diff] [blame] | 889 | else if (value[0] && !value[1]) { |
Jeff King | 649409b | 2014-07-24 00:42:39 -0400 | [diff] [blame] | 890 | comment_line_char = value[0]; |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 891 | auto_comment_line_char = 0; |
Nguyễn Thái Ngọc Duy | 50b54fd | 2014-05-17 08:52:22 +0700 | [diff] [blame] | 892 | } else |
| 893 | return error("core.commentChar should only be one character"); |
| 894 | return 0; |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 895 | } |
| 896 | |
Anselm Kruis | d3e7da8 | 2010-08-30 15:38:38 +0200 | [diff] [blame] | 897 | if (!strcmp(var, "core.askpass")) |
| 898 | return git_config_string(&askpass_program, var, value); |
| 899 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 900 | if (!strcmp(var, "core.excludesfile")) |
Matthieu Moy | 395de25 | 2009-11-17 18:24:25 +0100 | [diff] [blame] | 901 | return git_config_pathname(&excludes_file, var, value); |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 902 | |
| 903 | if (!strcmp(var, "core.whitespace")) { |
| 904 | if (!value) |
| 905 | return config_error_nonbool(var); |
| 906 | whitespace_rule_cfg = parse_whitespace_rule(value); |
| 907 | return 0; |
| 908 | } |
| 909 | |
Linus Torvalds | aafe9fb | 2008-06-18 15:18:44 -0700 | [diff] [blame] | 910 | if (!strcmp(var, "core.fsyncobjectfiles")) { |
| 911 | fsync_object_files = git_config_bool(var, value); |
| 912 | return 0; |
| 913 | } |
| 914 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 915 | if (!strcmp(var, "core.preloadindex")) { |
| 916 | core_preload_index = git_config_bool(var, value); |
| 917 | return 0; |
| 918 | } |
| 919 | |
Johannes Schindelin | 348df16 | 2009-04-28 00:32:25 +0200 | [diff] [blame] | 920 | if (!strcmp(var, "core.createobject")) { |
| 921 | if (!strcmp(value, "rename")) |
| 922 | object_creation_mode = OBJECT_CREATION_USES_RENAMES; |
| 923 | else if (!strcmp(value, "link")) |
| 924 | object_creation_mode = OBJECT_CREATION_USES_HARDLINKS; |
| 925 | else |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 926 | die(_("invalid mode for object creation: %s"), value); |
Johannes Schindelin | be66a6c | 2009-04-25 11:57:14 +0200 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 930 | if (!strcmp(var, "core.sparsecheckout")) { |
| 931 | core_apply_sparse_checkout = git_config_bool(var, value); |
| 932 | return 0; |
| 933 | } |
| 934 | |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 935 | if (!strcmp(var, "core.precomposeunicode")) { |
| 936 | precomposed_unicode = git_config_bool(var, value); |
| 937 | return 0; |
| 938 | } |
| 939 | |
Jeff King | a42643a | 2014-12-15 18:15:20 -0500 | [diff] [blame] | 940 | if (!strcmp(var, "core.protecthfs")) { |
| 941 | protect_hfs = git_config_bool(var, value); |
| 942 | return 0; |
| 943 | } |
| 944 | |
Johannes Schindelin | 2b4c6ef | 2014-12-16 23:46:59 +0100 | [diff] [blame] | 945 | if (!strcmp(var, "core.protectntfs")) { |
| 946 | protect_ntfs = git_config_bool(var, value); |
| 947 | return 0; |
| 948 | } |
| 949 | |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 950 | if (!strcmp(var, "core.hidedotfiles")) { |
| 951 | if (value && !strcasecmp(value, "dotgitonly")) |
| 952 | hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; |
| 953 | else |
| 954 | hide_dotfiles = git_config_bool(var, value); |
| 955 | return 0; |
| 956 | } |
| 957 | |
Linus Torvalds | 806e2ad | 2008-06-18 14:37:18 -0700 | [diff] [blame] | 958 | /* Add other config variables here and to Documentation/config.txt. */ |
| 959 | return 0; |
| 960 | } |
| 961 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 962 | static int git_default_i18n_config(const char *var, const char *value) |
Linus Torvalds | d136452 | 2008-06-18 14:40:35 -0700 | [diff] [blame] | 963 | { |
Christian Couder | ea5105a | 2008-02-16 06:00:24 +0100 | [diff] [blame] | 964 | if (!strcmp(var, "i18n.commitencoding")) |
| 965 | return git_config_string(&git_commit_encoding, var, value); |
Junio C Hamano | 4e72dce | 2005-11-27 16:09:40 -0800 | [diff] [blame] | 966 | |
Christian Couder | ea5105a | 2008-02-16 06:00:24 +0100 | [diff] [blame] | 967 | if (!strcmp(var, "i18n.logoutputencoding")) |
| 968 | return git_config_string(&git_log_output_encoding, var, value); |
Junio C Hamano | d2c11a3 | 2006-12-27 16:41:33 -0800 | [diff] [blame] | 969 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 970 | /* Add other config variables here and to Documentation/config.txt. */ |
| 971 | return 0; |
| 972 | } |
Matthias Lederhofer | aa086eb | 2006-07-30 00:27:43 +0200 | [diff] [blame] | 973 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 974 | static int git_default_branch_config(const char *var, const char *value) |
| 975 | { |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 976 | if (!strcmp(var, "branch.autosetupmerge")) { |
| 977 | if (value && !strcasecmp(value, "always")) { |
| 978 | git_branch_track = BRANCH_TRACK_ALWAYS; |
| 979 | return 0; |
| 980 | } |
| 981 | git_branch_track = git_config_bool(var, value); |
| 982 | return 0; |
| 983 | } |
Dustin Sallings | c998ae9 | 2008-05-10 15:36:29 -0700 | [diff] [blame] | 984 | if (!strcmp(var, "branch.autosetuprebase")) { |
| 985 | if (!value) |
| 986 | return config_error_nonbool(var); |
| 987 | else if (!strcmp(value, "never")) |
| 988 | autorebase = AUTOREBASE_NEVER; |
| 989 | else if (!strcmp(value, "local")) |
| 990 | autorebase = AUTOREBASE_LOCAL; |
| 991 | else if (!strcmp(value, "remote")) |
| 992 | autorebase = AUTOREBASE_REMOTE; |
| 993 | else if (!strcmp(value, "always")) |
| 994 | autorebase = AUTOREBASE_ALWAYS; |
| 995 | else |
Jeff King | 8c3ca35 | 2016-04-09 13:42:31 -0400 | [diff] [blame] | 996 | return error("malformed value for %s", var); |
Dustin Sallings | c998ae9 | 2008-05-10 15:36:29 -0700 | [diff] [blame] | 997 | return 0; |
| 998 | } |
Junio C Hamano | a9cc857 | 2007-11-02 00:24:27 -0700 | [diff] [blame] | 999 | |
Petr Baudis | 1ab661d | 2006-04-25 00:59:33 +0200 | [diff] [blame] | 1000 | /* Add other config variables here and to Documentation/config.txt. */ |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 1001 | return 0; |
| 1002 | } |
| 1003 | |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 1004 | static int git_default_push_config(const char *var, const char *value) |
| 1005 | { |
| 1006 | if (!strcmp(var, "push.default")) { |
| 1007 | if (!value) |
| 1008 | return config_error_nonbool(var); |
| 1009 | else if (!strcmp(value, "nothing")) |
| 1010 | push_default = PUSH_DEFAULT_NOTHING; |
| 1011 | else if (!strcmp(value, "matching")) |
| 1012 | push_default = PUSH_DEFAULT_MATCHING; |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 1013 | else if (!strcmp(value, "simple")) |
| 1014 | push_default = PUSH_DEFAULT_SIMPLE; |
Johan Herland | 53c4031 | 2011-02-16 01:54:24 +0100 | [diff] [blame] | 1015 | else if (!strcmp(value, "upstream")) |
| 1016 | push_default = PUSH_DEFAULT_UPSTREAM; |
| 1017 | else if (!strcmp(value, "tracking")) /* deprecated */ |
| 1018 | push_default = PUSH_DEFAULT_UPSTREAM; |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 1019 | else if (!strcmp(value, "current")) |
| 1020 | push_default = PUSH_DEFAULT_CURRENT; |
| 1021 | else { |
Jeff King | 8c3ca35 | 2016-04-09 13:42:31 -0400 | [diff] [blame] | 1022 | error("malformed value for %s: %s", var, value); |
Matthieu Moy | b55e677 | 2012-04-24 09:50:03 +0200 | [diff] [blame] | 1023 | return error("Must be one of nothing, matching, simple, " |
| 1024 | "upstream or current."); |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 1025 | } |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | /* Add other config variables here and to Documentation/config.txt. */ |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 1033 | static int git_default_mailmap_config(const char *var, const char *value) |
| 1034 | { |
| 1035 | if (!strcmp(var, "mailmap.file")) |
Øystein Walle | 9352fd5 | 2014-05-27 10:45:58 +0200 | [diff] [blame] | 1036 | return git_config_pathname(&git_mailmap_file, var, value); |
Jeff King | 0861090 | 2012-12-12 06:04:04 -0500 | [diff] [blame] | 1037 | if (!strcmp(var, "mailmap.blob")) |
| 1038 | return git_config_string(&git_mailmap_blob, var, value); |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 1039 | |
| 1040 | /* Add other config variables here and to Documentation/config.txt. */ |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1044 | int git_default_config(const char *var, const char *value, void *dummy) |
| 1045 | { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1046 | if (starts_with(var, "core.")) |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1047 | return git_default_core_config(var, value); |
| 1048 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1049 | if (starts_with(var, "user.")) |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 1050 | return git_ident_config(var, value, dummy); |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1051 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1052 | if (starts_with(var, "i18n.")) |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1053 | return git_default_i18n_config(var, value); |
| 1054 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1055 | if (starts_with(var, "branch.")) |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1056 | return git_default_branch_config(var, value); |
| 1057 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1058 | if (starts_with(var, "push.")) |
Finn Arne Gangstad | 5215374 | 2009-03-16 16:42:51 +0100 | [diff] [blame] | 1059 | return git_default_push_config(var, value); |
| 1060 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1061 | if (starts_with(var, "mailmap.")) |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 1062 | return git_default_mailmap_config(var, value); |
| 1063 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1064 | if (starts_with(var, "advice.")) |
Jeff King | 7519443 | 2009-09-09 07:38:58 -0400 | [diff] [blame] | 1065 | return git_default_advice_config(var, value); |
| 1066 | |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1067 | if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) { |
| 1068 | pager_use_color = git_config_bool(var,value); |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 1072 | if (!strcmp(var, "pack.packsizelimit")) { |
| 1073 | pack_size_limit_cfg = git_config_ulong(var, value); |
| 1074 | return 0; |
| 1075 | } |
Linus Torvalds | 1141f49 | 2008-06-18 15:00:11 -0700 | [diff] [blame] | 1076 | /* Add other config variables here and to Documentation/config.txt. */ |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
Heiko Voigt | ca4b5de | 2013-05-11 15:18:52 +0200 | [diff] [blame] | 1080 | /* |
Heiko Voigt | b2dc094 | 2013-07-12 00:48:30 +0200 | [diff] [blame] | 1081 | * All source specific fields in the union, die_on_error, name and the callbacks |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 1082 | * fgetc, ungetc, ftell of top need to be initialized before calling |
Heiko Voigt | ca4b5de | 2013-05-11 15:18:52 +0200 | [diff] [blame] | 1083 | * this function. |
| 1084 | */ |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 1085 | static int do_config_from(struct config_source *top, config_fn_t fn, void *data) |
Heiko Voigt | ca4b5de | 2013-05-11 15:18:52 +0200 | [diff] [blame] | 1086 | { |
| 1087 | int ret; |
| 1088 | |
| 1089 | /* push config-file parsing state stack */ |
| 1090 | top->prev = cf; |
| 1091 | top->linenr = 1; |
| 1092 | top->eof = 0; |
| 1093 | strbuf_init(&top->value, 1024); |
| 1094 | strbuf_init(&top->var, 1024); |
| 1095 | cf = top; |
| 1096 | |
Heiko Voigt | 4d8dd14 | 2013-07-12 00:44:39 +0200 | [diff] [blame] | 1097 | ret = git_parse_source(fn, data); |
Heiko Voigt | ca4b5de | 2013-05-11 15:18:52 +0200 | [diff] [blame] | 1098 | |
| 1099 | /* pop config-file parsing state stack */ |
| 1100 | strbuf_release(&top->value); |
| 1101 | strbuf_release(&top->var); |
| 1102 | cf = top->prev; |
| 1103 | |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1107 | static int do_config_from_file(config_fn_t fn, |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1108 | const char *origin_type, const char *name, const char *path, FILE *f, |
| 1109 | void *data) |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1110 | { |
| 1111 | struct config_source top; |
| 1112 | |
| 1113 | top.u.file = f; |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1114 | top.origin_type = origin_type; |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1115 | top.name = name; |
| 1116 | top.path = path; |
| 1117 | top.die_on_error = 1; |
| 1118 | top.do_fgetc = config_file_fgetc; |
| 1119 | top.do_ungetc = config_file_ungetc; |
| 1120 | top.do_ftell = config_file_ftell; |
| 1121 | |
| 1122 | return do_config_from(&top, fn, data); |
| 1123 | } |
| 1124 | |
| 1125 | static int git_config_from_stdin(config_fn_t fn, void *data) |
| 1126 | { |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1127 | return do_config_from_file(fn, "standard input", "", NULL, stdin, data); |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 1130 | 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] | 1131 | { |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1132 | int ret = -1; |
| 1133 | FILE *f; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 1134 | |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1135 | f = fopen(filename, "r"); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 1136 | if (f) { |
Jeff King | 260d408 | 2015-04-16 04:51:18 -0400 | [diff] [blame] | 1137 | flockfile(f); |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1138 | ret = do_config_from_file(fn, "file", filename, filename, f, data); |
Jeff King | 260d408 | 2015-04-16 04:51:18 -0400 | [diff] [blame] | 1139 | funlockfile(f); |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 1140 | fclose(f); |
| 1141 | } |
| 1142 | return ret; |
| 1143 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1144 | |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1145 | int git_config_from_mem(config_fn_t fn, const char *origin_type, |
| 1146 | const char *name, const char *buf, size_t len, void *data) |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 1147 | { |
| 1148 | struct config_source top; |
| 1149 | |
| 1150 | top.u.buf.buf = buf; |
| 1151 | top.u.buf.len = len; |
| 1152 | top.u.buf.pos = 0; |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1153 | top.origin_type = origin_type; |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 1154 | top.name = name; |
Jeff King | d14d424 | 2014-02-19 00:58:52 +0200 | [diff] [blame] | 1155 | top.path = NULL; |
Heiko Voigt | b2dc094 | 2013-07-12 00:48:30 +0200 | [diff] [blame] | 1156 | top.die_on_error = 0; |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 1157 | top.do_fgetc = config_buf_fgetc; |
| 1158 | top.do_ungetc = config_buf_ungetc; |
| 1159 | top.do_ftell = config_buf_ftell; |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 1160 | |
| 1161 | return do_config_from(&top, fn, data); |
| 1162 | } |
| 1163 | |
| 1164 | static int git_config_from_blob_sha1(config_fn_t fn, |
| 1165 | const char *name, |
| 1166 | const unsigned char *sha1, |
| 1167 | void *data) |
| 1168 | { |
| 1169 | enum object_type type; |
| 1170 | char *buf; |
| 1171 | unsigned long size; |
| 1172 | int ret; |
| 1173 | |
| 1174 | buf = read_sha1_file(sha1, &type, &size); |
| 1175 | if (!buf) |
| 1176 | return error("unable to load config blob object '%s'", name); |
| 1177 | if (type != OBJ_BLOB) { |
| 1178 | free(buf); |
| 1179 | return error("reference '%s' does not point to a blob", name); |
| 1180 | } |
| 1181 | |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 1182 | ret = git_config_from_mem(fn, "blob", name, buf, size, data); |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 1183 | free(buf); |
| 1184 | |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
| 1188 | static int git_config_from_blob_ref(config_fn_t fn, |
| 1189 | const char *name, |
| 1190 | void *data) |
| 1191 | { |
| 1192 | unsigned char sha1[20]; |
| 1193 | |
| 1194 | if (get_sha1(name, sha1) < 0) |
| 1195 | return error("unable to resolve config blob '%s'", name); |
| 1196 | return git_config_from_blob_sha1(fn, name, sha1, data); |
| 1197 | } |
| 1198 | |
Johannes Sixt | 506b17b | 2007-11-13 21:05:05 +0100 | [diff] [blame] | 1199 | const char *git_etc_gitconfig(void) |
| 1200 | { |
Johannes Sixt | 7f0e39f | 2007-11-13 21:05:06 +0100 | [diff] [blame] | 1201 | static const char *system_wide; |
Steffen Prohaska | 2de9de5 | 2008-07-13 22:31:18 +0200 | [diff] [blame] | 1202 | if (!system_wide) |
| 1203 | system_wide = system_path(ETC_GITCONFIG); |
Johannes Sixt | 7f0e39f | 2007-11-13 21:05:06 +0100 | [diff] [blame] | 1204 | return system_wide; |
Johannes Sixt | 506b17b | 2007-11-13 21:05:05 +0100 | [diff] [blame] | 1205 | } |
| 1206 | |
Steffen Prohaska | 23b0c47 | 2014-08-26 17:23:21 +0200 | [diff] [blame] | 1207 | /* |
| 1208 | * Parse environment variable 'k' as a boolean (in various |
| 1209 | * possible spellings); if missing, use the default value 'def'. |
| 1210 | */ |
Lars R. Damerow | 0ef3716 | 2010-03-17 12:55:51 -0700 | [diff] [blame] | 1211 | int git_env_bool(const char *k, int def) |
Jeff King | ab88c36 | 2008-02-06 05:11:18 -0500 | [diff] [blame] | 1212 | { |
| 1213 | const char *v = getenv(k); |
| 1214 | return v ? git_config_bool(k, v) : def; |
| 1215 | } |
| 1216 | |
Steffen Prohaska | 23b0c47 | 2014-08-26 17:23:21 +0200 | [diff] [blame] | 1217 | /* |
| 1218 | * Parse environment variable 'k' as ulong with possibly a unit |
| 1219 | * suffix; if missing, use the default value 'val'. |
| 1220 | */ |
| 1221 | unsigned long git_env_ulong(const char *k, unsigned long val) |
| 1222 | { |
| 1223 | const char *v = getenv(k); |
| 1224 | if (v && !git_parse_ulong(v, &val)) |
| 1225 | die("failed to parse %s", k); |
| 1226 | return val; |
| 1227 | } |
| 1228 | |
Jeff King | ab88c36 | 2008-02-06 05:11:18 -0500 | [diff] [blame] | 1229 | int git_config_system(void) |
| 1230 | { |
| 1231 | return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0); |
| 1232 | } |
| 1233 | |
Jeff King | 8018186 | 2016-03-11 17:37:03 -0500 | [diff] [blame] | 1234 | static int do_git_config_sequence(config_fn_t fn, void *data) |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 1235 | { |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1236 | int ret = 0; |
Paul Tan | 509adc3 | 2015-05-06 16:01:03 +0800 | [diff] [blame] | 1237 | char *xdg_config = xdg_config_home("config"); |
| 1238 | char *user_config = expand_user_path("~/.gitconfig"); |
Jeff King | 8018186 | 2016-03-11 17:37:03 -0500 | [diff] [blame] | 1239 | char *repo_config = git_pathdup("config"); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 1240 | |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 1241 | current_parsing_scope = CONFIG_SCOPE_SYSTEM; |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1242 | if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 1243 | ret += git_config_from_file(fn, git_etc_gitconfig(), |
| 1244 | data); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 1245 | |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 1246 | current_parsing_scope = CONFIG_SCOPE_GLOBAL; |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1247 | if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 1248 | ret += git_config_from_file(fn, xdg_config, data); |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 1249 | |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1250 | if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 1251 | ret += git_config_from_file(fn, user_config, data); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 1252 | |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 1253 | current_parsing_scope = CONFIG_SCOPE_REPO; |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1254 | if (repo_config && !access_or_die(repo_config, R_OK, 0)) |
Felipe Contreras | aa38740 | 2009-02-21 02:48:55 +0200 | [diff] [blame] | 1255 | ret += git_config_from_file(fn, repo_config, data); |
Alex Riesen | 8b1fa77 | 2010-03-26 23:53:57 +0100 | [diff] [blame] | 1256 | |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 1257 | current_parsing_scope = CONFIG_SCOPE_CMDLINE; |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1258 | if (git_config_from_parameters(fn, data) < 0) |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 1259 | die(_("unable to parse command-line config")); |
Alex Riesen | 8b1fa77 | 2010-03-26 23:53:57 +0100 | [diff] [blame] | 1260 | |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 1261 | current_parsing_scope = CONFIG_SCOPE_UNKNOWN; |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 1262 | free(xdg_config); |
| 1263 | free(user_config); |
Jeff King | 8018186 | 2016-03-11 17:37:03 -0500 | [diff] [blame] | 1264 | free(repo_config); |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1265 | return ret; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 1266 | } |
| 1267 | |
Jeff King | c9b5e2a | 2012-02-16 03:05:56 -0500 | [diff] [blame] | 1268 | int git_config_with_options(config_fn_t fn, void *data, |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 1269 | struct git_config_source *config_source, |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 1270 | int respect_includes) |
Nguyễn Thái Ngọc Duy | dbdf585 | 2010-11-26 22:32:33 +0700 | [diff] [blame] | 1271 | { |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 1272 | struct config_include_data inc = CONFIG_INCLUDE_INIT; |
| 1273 | |
| 1274 | if (respect_includes) { |
| 1275 | inc.fn = fn; |
| 1276 | inc.data = data; |
| 1277 | fn = git_config_include; |
| 1278 | data = &inc; |
| 1279 | } |
Nguyễn Thái Ngọc Duy | dbdf585 | 2010-11-26 22:32:33 +0700 | [diff] [blame] | 1280 | |
Jeff King | c9b5e2a | 2012-02-16 03:05:56 -0500 | [diff] [blame] | 1281 | /* |
| 1282 | * If we have a specific filename, use it. Otherwise, follow the |
| 1283 | * regular lookup sequence. |
| 1284 | */ |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 1285 | if (config_source && config_source->use_stdin) |
| 1286 | return git_config_from_stdin(fn, data); |
| 1287 | else if (config_source && config_source->file) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 1288 | return git_config_from_file(fn, config_source->file, data); |
| 1289 | else if (config_source && config_source->blob) |
| 1290 | return git_config_from_blob_ref(fn, config_source->blob, data); |
Jeff King | c9b5e2a | 2012-02-16 03:05:56 -0500 | [diff] [blame] | 1291 | |
Jeff King | 8018186 | 2016-03-11 17:37:03 -0500 | [diff] [blame] | 1292 | return do_git_config_sequence(fn, data); |
Nguyễn Thái Ngọc Duy | dbdf585 | 2010-11-26 22:32:33 +0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1295 | static void git_config_raw(config_fn_t fn, void *data) |
Jeff King | c9b5e2a | 2012-02-16 03:05:56 -0500 | [diff] [blame] | 1296 | { |
Tanay Abhra | aace438 | 2014-08-07 04:59:15 -0700 | [diff] [blame] | 1297 | if (git_config_with_options(fn, data, NULL, 1) < 0) |
| 1298 | /* |
| 1299 | * git_config_with_options() normally returns only |
Jeff King | c72ee44 | 2016-05-18 18:39:02 -0400 | [diff] [blame] | 1300 | * zero, as most errors are fatal, and |
Tanay Abhra | aace438 | 2014-08-07 04:59:15 -0700 | [diff] [blame] | 1301 | * non-fatal potential errors are guarded by "if" |
| 1302 | * statements that are entered only when no error is |
| 1303 | * possible. |
| 1304 | * |
| 1305 | * If we ever encounter a non-fatal error, it means |
| 1306 | * something went really wrong and we should stop |
| 1307 | * immediately. |
| 1308 | */ |
Peter Colberg | 3a39f61 | 2016-06-10 15:05:26 -0400 | [diff] [blame] | 1309 | die(_("unknown error occurred while reading the configuration files")); |
Jeff King | c9b5e2a | 2012-02-16 03:05:56 -0500 | [diff] [blame] | 1310 | } |
| 1311 | |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1312 | static void configset_iter(struct config_set *cs, config_fn_t fn, void *data) |
| 1313 | { |
| 1314 | int i, value_index; |
| 1315 | struct string_list *values; |
| 1316 | struct config_set_element *entry; |
| 1317 | struct configset_list *list = &cs->list; |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1318 | |
| 1319 | for (i = 0; i < list->nr; i++) { |
| 1320 | entry = list->items[i].e; |
| 1321 | value_index = list->items[i].value_index; |
| 1322 | values = &entry->value_list; |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 1323 | |
| 1324 | current_config_kvi = values->items[value_index].util; |
| 1325 | |
| 1326 | if (fn(entry->key, values->items[value_index].string, data) < 0) |
| 1327 | git_die_config_linenr(entry->key, |
| 1328 | current_config_kvi->filename, |
| 1329 | current_config_kvi->linenr); |
| 1330 | |
| 1331 | current_config_kvi = NULL; |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | static void git_config_check_init(void); |
| 1336 | |
| 1337 | void git_config(config_fn_t fn, void *data) |
| 1338 | { |
| 1339 | git_config_check_init(); |
| 1340 | configset_iter(&the_config_set, fn, data); |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1341 | } |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 1342 | |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1343 | static struct config_set_element *configset_find_element(struct config_set *cs, const char *key) |
| 1344 | { |
| 1345 | struct config_set_element k; |
| 1346 | struct config_set_element *found_entry; |
| 1347 | char *normalized_key; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1348 | /* |
| 1349 | * `key` may come from the user, so normalize it before using it |
| 1350 | * for querying entries from the hashmap. |
| 1351 | */ |
Stefan Beller | 270cd9e | 2016-04-27 14:30:39 -0700 | [diff] [blame] | 1352 | if (git_config_parse_key(key, &normalized_key, NULL)) |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1353 | return NULL; |
| 1354 | |
| 1355 | hashmap_entry_init(&k, strhash(normalized_key)); |
| 1356 | k.key = normalized_key; |
| 1357 | found_entry = hashmap_get(&cs->config_hash, &k, NULL); |
| 1358 | free(normalized_key); |
| 1359 | return found_entry; |
| 1360 | } |
| 1361 | |
| 1362 | static int configset_add_value(struct config_set *cs, const char *key, const char *value) |
| 1363 | { |
| 1364 | struct config_set_element *e; |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1365 | struct string_list_item *si; |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1366 | struct configset_list_item *l_item; |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1367 | struct key_value_info *kv_info = xmalloc(sizeof(*kv_info)); |
| 1368 | |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1369 | e = configset_find_element(cs, key); |
| 1370 | /* |
| 1371 | * Since the keys are being fed by git_config*() callback mechanism, they |
| 1372 | * are already normalized. So simply add them without any further munging. |
| 1373 | */ |
| 1374 | if (!e) { |
| 1375 | e = xmalloc(sizeof(*e)); |
| 1376 | hashmap_entry_init(e, strhash(key)); |
| 1377 | e->key = xstrdup(key); |
| 1378 | string_list_init(&e->value_list, 1); |
| 1379 | hashmap_add(&cs->config_hash, e); |
| 1380 | } |
Jeff King | 8c53f07 | 2015-01-12 20:59:09 -0500 | [diff] [blame] | 1381 | si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value)); |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1382 | |
| 1383 | ALLOC_GROW(cs->list.items, cs->list.nr + 1, cs->list.alloc); |
| 1384 | l_item = &cs->list.items[cs->list.nr++]; |
| 1385 | l_item->e = e; |
| 1386 | l_item->value_index = e->value_list.nr - 1; |
| 1387 | |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 1388 | if (!cf) |
| 1389 | die("BUG: configset_add_value has no source"); |
| 1390 | if (cf->name) { |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1391 | kv_info->filename = strintern(cf->name); |
| 1392 | kv_info->linenr = cf->linenr; |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 1393 | kv_info->origin_type = strintern(cf->origin_type); |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1394 | } else { |
| 1395 | /* for values read from `git_config_from_parameters()` */ |
| 1396 | kv_info->filename = NULL; |
| 1397 | kv_info->linenr = -1; |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 1398 | kv_info->origin_type = NULL; |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1399 | } |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 1400 | kv_info->scope = current_parsing_scope; |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1401 | si->util = kv_info; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | static int config_set_element_cmp(const struct config_set_element *e1, |
| 1407 | const struct config_set_element *e2, const void *unused) |
| 1408 | { |
| 1409 | return strcmp(e1->key, e2->key); |
| 1410 | } |
| 1411 | |
| 1412 | void git_configset_init(struct config_set *cs) |
| 1413 | { |
| 1414 | hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0); |
| 1415 | cs->hash_initialized = 1; |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1416 | cs->list.nr = 0; |
| 1417 | cs->list.alloc = 0; |
| 1418 | cs->list.items = NULL; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | void git_configset_clear(struct config_set *cs) |
| 1422 | { |
| 1423 | struct config_set_element *entry; |
| 1424 | struct hashmap_iter iter; |
| 1425 | if (!cs->hash_initialized) |
| 1426 | return; |
| 1427 | |
| 1428 | hashmap_iter_init(&cs->config_hash, &iter); |
| 1429 | while ((entry = hashmap_iter_next(&iter))) { |
| 1430 | free(entry->key); |
Tanay Abhra | 3df8fd6 | 2014-08-07 04:59:14 -0700 | [diff] [blame] | 1431 | string_list_clear(&entry->value_list, 1); |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1432 | } |
| 1433 | hashmap_free(&cs->config_hash, 1); |
| 1434 | cs->hash_initialized = 0; |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1435 | free(cs->list.items); |
| 1436 | cs->list.nr = 0; |
| 1437 | cs->list.alloc = 0; |
| 1438 | cs->list.items = NULL; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static int config_set_callback(const char *key, const char *value, void *cb) |
| 1442 | { |
| 1443 | struct config_set *cs = cb; |
| 1444 | configset_add_value(cs, key, value); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | int git_configset_add_file(struct config_set *cs, const char *filename) |
| 1449 | { |
| 1450 | return git_config_from_file(config_set_callback, filename, cs); |
| 1451 | } |
| 1452 | |
| 1453 | int git_configset_get_value(struct config_set *cs, const char *key, const char **value) |
| 1454 | { |
| 1455 | const struct string_list *values = NULL; |
| 1456 | /* |
| 1457 | * Follows "last one wins" semantic, i.e., if there are multiple matches for the |
| 1458 | * queried key in the files of the configset, the value returned will be the last |
| 1459 | * value in the value list for that key. |
| 1460 | */ |
| 1461 | values = git_configset_get_value_multi(cs, key); |
| 1462 | |
| 1463 | if (!values) |
| 1464 | return 1; |
| 1465 | assert(values->nr > 0); |
| 1466 | *value = values->items[values->nr - 1].string; |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key) |
| 1471 | { |
| 1472 | struct config_set_element *e = configset_find_element(cs, key); |
| 1473 | return e ? &e->value_list : NULL; |
| 1474 | } |
| 1475 | |
| 1476 | int git_configset_get_string_const(struct config_set *cs, const char *key, const char **dest) |
| 1477 | { |
| 1478 | const char *value; |
| 1479 | if (!git_configset_get_value(cs, key, &value)) |
| 1480 | return git_config_string(dest, key, value); |
| 1481 | else |
| 1482 | return 1; |
| 1483 | } |
| 1484 | |
| 1485 | int git_configset_get_string(struct config_set *cs, const char *key, char **dest) |
| 1486 | { |
| 1487 | return git_configset_get_string_const(cs, key, (const char **)dest); |
| 1488 | } |
| 1489 | |
| 1490 | int git_configset_get_int(struct config_set *cs, const char *key, int *dest) |
| 1491 | { |
| 1492 | const char *value; |
| 1493 | if (!git_configset_get_value(cs, key, &value)) { |
| 1494 | *dest = git_config_int(key, value); |
| 1495 | return 0; |
| 1496 | } else |
| 1497 | return 1; |
| 1498 | } |
| 1499 | |
| 1500 | int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest) |
| 1501 | { |
| 1502 | const char *value; |
| 1503 | if (!git_configset_get_value(cs, key, &value)) { |
| 1504 | *dest = git_config_ulong(key, value); |
| 1505 | return 0; |
| 1506 | } else |
| 1507 | return 1; |
| 1508 | } |
| 1509 | |
| 1510 | int git_configset_get_bool(struct config_set *cs, const char *key, int *dest) |
| 1511 | { |
| 1512 | const char *value; |
| 1513 | if (!git_configset_get_value(cs, key, &value)) { |
| 1514 | *dest = git_config_bool(key, value); |
| 1515 | return 0; |
| 1516 | } else |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
| 1520 | int git_configset_get_bool_or_int(struct config_set *cs, const char *key, |
| 1521 | int *is_bool, int *dest) |
| 1522 | { |
| 1523 | const char *value; |
| 1524 | if (!git_configset_get_value(cs, key, &value)) { |
| 1525 | *dest = git_config_bool_or_int(key, value, is_bool); |
| 1526 | return 0; |
| 1527 | } else |
| 1528 | return 1; |
| 1529 | } |
| 1530 | |
| 1531 | int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest) |
| 1532 | { |
| 1533 | const char *value; |
| 1534 | if (!git_configset_get_value(cs, key, &value)) { |
| 1535 | *dest = git_config_maybe_bool(key, value); |
| 1536 | if (*dest == -1) |
| 1537 | return -1; |
| 1538 | return 0; |
| 1539 | } else |
| 1540 | return 1; |
| 1541 | } |
| 1542 | |
| 1543 | int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest) |
| 1544 | { |
| 1545 | const char *value; |
| 1546 | if (!git_configset_get_value(cs, key, &value)) |
| 1547 | return git_config_pathname(dest, key, value); |
| 1548 | else |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | static void git_config_check_init(void) |
| 1553 | { |
| 1554 | if (the_config_set.hash_initialized) |
| 1555 | return; |
| 1556 | git_configset_init(&the_config_set); |
Tanay Abhra | 155ef25 | 2014-08-07 04:59:17 -0700 | [diff] [blame] | 1557 | git_config_raw(config_set_callback, &the_config_set); |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | void git_config_clear(void) |
| 1561 | { |
| 1562 | if (!the_config_set.hash_initialized) |
| 1563 | return; |
| 1564 | git_configset_clear(&the_config_set); |
| 1565 | } |
| 1566 | |
| 1567 | int git_config_get_value(const char *key, const char **value) |
| 1568 | { |
| 1569 | git_config_check_init(); |
| 1570 | return git_configset_get_value(&the_config_set, key, value); |
| 1571 | } |
| 1572 | |
| 1573 | const struct string_list *git_config_get_value_multi(const char *key) |
| 1574 | { |
| 1575 | git_config_check_init(); |
| 1576 | return git_configset_get_value_multi(&the_config_set, key); |
| 1577 | } |
| 1578 | |
| 1579 | int git_config_get_string_const(const char *key, const char **dest) |
| 1580 | { |
Tanay Abhra | 5a80e97 | 2014-08-07 04:59:16 -0700 | [diff] [blame] | 1581 | int ret; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1582 | git_config_check_init(); |
Tanay Abhra | 5a80e97 | 2014-08-07 04:59:16 -0700 | [diff] [blame] | 1583 | ret = git_configset_get_string_const(&the_config_set, key, dest); |
| 1584 | if (ret < 0) |
| 1585 | git_die_config(key, NULL); |
| 1586 | return ret; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | int git_config_get_string(const char *key, char **dest) |
| 1590 | { |
| 1591 | git_config_check_init(); |
| 1592 | return git_config_get_string_const(key, (const char **)dest); |
| 1593 | } |
| 1594 | |
| 1595 | int git_config_get_int(const char *key, int *dest) |
| 1596 | { |
| 1597 | git_config_check_init(); |
| 1598 | return git_configset_get_int(&the_config_set, key, dest); |
| 1599 | } |
| 1600 | |
| 1601 | int git_config_get_ulong(const char *key, unsigned long *dest) |
| 1602 | { |
| 1603 | git_config_check_init(); |
| 1604 | return git_configset_get_ulong(&the_config_set, key, dest); |
| 1605 | } |
| 1606 | |
| 1607 | int git_config_get_bool(const char *key, int *dest) |
| 1608 | { |
| 1609 | git_config_check_init(); |
| 1610 | return git_configset_get_bool(&the_config_set, key, dest); |
| 1611 | } |
| 1612 | |
| 1613 | int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest) |
| 1614 | { |
| 1615 | git_config_check_init(); |
| 1616 | return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest); |
| 1617 | } |
| 1618 | |
| 1619 | int git_config_get_maybe_bool(const char *key, int *dest) |
| 1620 | { |
| 1621 | git_config_check_init(); |
| 1622 | return git_configset_get_maybe_bool(&the_config_set, key, dest); |
| 1623 | } |
| 1624 | |
| 1625 | int git_config_get_pathname(const char *key, const char **dest) |
| 1626 | { |
Tanay Abhra | 5a80e97 | 2014-08-07 04:59:16 -0700 | [diff] [blame] | 1627 | int ret; |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1628 | git_config_check_init(); |
Tanay Abhra | 5a80e97 | 2014-08-07 04:59:16 -0700 | [diff] [blame] | 1629 | ret = git_configset_get_pathname(&the_config_set, key, dest); |
| 1630 | if (ret < 0) |
| 1631 | git_die_config(key, NULL); |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1635 | int git_config_get_untracked_cache(void) |
| 1636 | { |
| 1637 | int val = -1; |
| 1638 | const char *v; |
| 1639 | |
Christian Couder | dae6c32 | 2016-01-27 07:58:06 +0100 | [diff] [blame] | 1640 | /* Hack for test programs like test-dump-untracked-cache */ |
| 1641 | if (ignore_untracked_cache_config) |
| 1642 | return -1; |
| 1643 | |
Christian Couder | 435ec09 | 2016-01-27 07:58:05 +0100 | [diff] [blame] | 1644 | if (!git_config_get_maybe_bool("core.untrackedcache", &val)) |
| 1645 | return val; |
| 1646 | |
| 1647 | if (!git_config_get_value("core.untrackedcache", &v)) { |
| 1648 | if (!strcasecmp(v, "keep")) |
| 1649 | return -1; |
| 1650 | |
| 1651 | error("unknown core.untrackedCache value '%s'; " |
| 1652 | "using 'keep' default value", v); |
| 1653 | return -1; |
| 1654 | } |
| 1655 | |
| 1656 | return -1; /* default value */ |
| 1657 | } |
| 1658 | |
Tanay Abhra | 5a80e97 | 2014-08-07 04:59:16 -0700 | [diff] [blame] | 1659 | NORETURN |
| 1660 | void git_die_config_linenr(const char *key, const char *filename, int linenr) |
| 1661 | { |
| 1662 | if (!filename) |
| 1663 | die(_("unable to parse '%s' from command-line config"), key); |
| 1664 | else |
| 1665 | die(_("bad config variable '%s' in file '%s' at line %d"), |
| 1666 | key, filename, linenr); |
| 1667 | } |
| 1668 | |
| 1669 | NORETURN __attribute__((format(printf, 2, 3))) |
| 1670 | void git_die_config(const char *key, const char *err, ...) |
| 1671 | { |
| 1672 | const struct string_list *values; |
| 1673 | struct key_value_info *kv_info; |
| 1674 | |
| 1675 | if (err) { |
| 1676 | va_list params; |
| 1677 | va_start(params, err); |
| 1678 | vreportf("error: ", err, params); |
| 1679 | va_end(params); |
| 1680 | } |
| 1681 | values = git_config_get_value_multi(key); |
| 1682 | kv_info = values->items[values->nr - 1].util; |
| 1683 | git_die_config_linenr(key, kv_info->filename, kv_info->linenr); |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1686 | /* |
| 1687 | * Find all the stuff for git_config_set() below. |
| 1688 | */ |
| 1689 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1690 | static struct { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1691 | int baselen; |
| 1692 | char *key; |
| 1693 | int do_not_match; |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1694 | regex_t *value_regex; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1695 | int multi_replace; |
Thomas Rast | 83786fa | 2013-11-13 11:19:00 +0100 | [diff] [blame] | 1696 | size_t *offset; |
| 1697 | unsigned int offset_alloc; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1698 | enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state; |
| 1699 | int seen; |
| 1700 | } store; |
| 1701 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1702 | static int matches(const char *key, const char *value) |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 1703 | { |
Jeff King | c1063be | 2014-08-19 02:20:00 -0400 | [diff] [blame] | 1704 | if (strcmp(key, store.key)) |
| 1705 | return 0; /* not ours */ |
| 1706 | if (!store.value_regex) |
| 1707 | return 1; /* always matches */ |
| 1708 | if (store.value_regex == CONFIG_REGEX_NONE) |
| 1709 | return 0; /* never matches */ |
| 1710 | |
| 1711 | return store.do_not_match ^ |
| 1712 | (value && !regexec(store.value_regex, value, 0, NULL, 0)); |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1715 | static int store_aux(const char *key, const char *value, void *cb) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1716 | { |
Junio C Hamano | ae9ee41 | 2007-05-12 21:49:33 -0700 | [diff] [blame] | 1717 | const char *ep; |
| 1718 | size_t section_len; |
| 1719 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1720 | switch (store.state) { |
| 1721 | case KEY_SEEN: |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 1722 | if (matches(key, value)) { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1723 | if (store.seen == 1 && store.multi_replace == 0) { |
Matthieu Moy | 8262aaa | 2014-08-07 04:59:12 -0700 | [diff] [blame] | 1724 | warning(_("%s has multiple values"), key); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1725 | } |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1726 | |
Thomas Rast | 83786fa | 2013-11-13 11:19:00 +0100 | [diff] [blame] | 1727 | ALLOC_GROW(store.offset, store.seen + 1, |
| 1728 | store.offset_alloc); |
| 1729 | |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 1730 | store.offset[store.seen] = cf->do_ftell(cf); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1731 | store.seen++; |
| 1732 | } |
| 1733 | break; |
| 1734 | case SECTION_SEEN: |
Junio C Hamano | ae9ee41 | 2007-05-12 21:49:33 -0700 | [diff] [blame] | 1735 | /* |
| 1736 | * What we are looking for is in store.key (both |
| 1737 | * section and var), and its section part is baselen |
| 1738 | * long. We found key (again, both section and var). |
| 1739 | * We would want to know if this key is in the same |
| 1740 | * section as what we are looking for. We already |
| 1741 | * know we are in the same section as what should |
| 1742 | * hold store.key. |
| 1743 | */ |
| 1744 | ep = strrchr(key, '.'); |
| 1745 | section_len = ep - key; |
| 1746 | |
| 1747 | if ((section_len != store.baselen) || |
| 1748 | memcmp(key, store.key, section_len+1)) { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1749 | store.state = SECTION_END_SEEN; |
| 1750 | break; |
Junio C Hamano | ae9ee41 | 2007-05-12 21:49:33 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Do not increment matches: this is no match, but we |
| 1755 | * just made sure we are in the desired section. |
| 1756 | */ |
Thomas Rast | 83786fa | 2013-11-13 11:19:00 +0100 | [diff] [blame] | 1757 | ALLOC_GROW(store.offset, store.seen + 1, |
| 1758 | store.offset_alloc); |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 1759 | store.offset[store.seen] = cf->do_ftell(cf); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1760 | /* fallthru */ |
| 1761 | case SECTION_END_SEEN: |
| 1762 | case START: |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 1763 | if (matches(key, value)) { |
Thomas Rast | 83786fa | 2013-11-13 11:19:00 +0100 | [diff] [blame] | 1764 | ALLOC_GROW(store.offset, store.seen + 1, |
| 1765 | store.offset_alloc); |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 1766 | store.offset[store.seen] = cf->do_ftell(cf); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1767 | store.state = KEY_SEEN; |
| 1768 | store.seen++; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 1769 | } else { |
| 1770 | if (strrchr(key, '.') - key == store.baselen && |
sean | bdf0ef0 | 2006-05-06 14:14:02 -0400 | [diff] [blame] | 1771 | !strncmp(key, store.key, store.baselen)) { |
sean | 93ddef3 | 2006-05-05 09:49:15 -0400 | [diff] [blame] | 1772 | store.state = SECTION_SEEN; |
Thomas Rast | 83786fa | 2013-11-13 11:19:00 +0100 | [diff] [blame] | 1773 | ALLOC_GROW(store.offset, |
| 1774 | store.seen + 1, |
| 1775 | store.offset_alloc); |
Jeff King | 49d6cfa | 2013-08-26 17:57:18 -0400 | [diff] [blame] | 1776 | store.offset[store.seen] = cf->do_ftell(cf); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 1777 | } |
sean | bdf0ef0 | 2006-05-06 14:14:02 -0400 | [diff] [blame] | 1778 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1779 | } |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1783 | static int write_error(const char *filename) |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1784 | { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 1785 | error("failed to write new configuration file %s", filename); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1786 | |
| 1787 | /* Same error code as "failed to rename". */ |
| 1788 | return 4; |
| 1789 | } |
| 1790 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1791 | static int store_write_section(int fd, const char *key) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1792 | { |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1793 | const char *dot; |
| 1794 | int i, success; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1795 | struct strbuf sb = STRBUF_INIT; |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 1796 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1797 | dot = memchr(key, '.', store.baselen); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 1798 | if (dot) { |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1799 | strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key); |
| 1800 | for (i = dot - key + 1; i < store.baselen; i++) { |
Bryan Donlan | e5c349b | 2008-05-04 01:37:52 -0400 | [diff] [blame] | 1801 | if (key[i] == '"' || key[i] == '\\') |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1802 | strbuf_addch(&sb, '\\'); |
| 1803 | strbuf_addch(&sb, key[i]); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 1804 | } |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1805 | strbuf_addstr(&sb, "\"]\n"); |
| 1806 | } else { |
| 1807 | strbuf_addf(&sb, "[%.*s]\n", store.baselen, key); |
Linus Torvalds | d14f776 | 2006-05-09 12:24:02 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1810 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; |
| 1811 | strbuf_release(&sb); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1812 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1813 | return success; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1816 | 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] | 1817 | { |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1818 | int i, success; |
| 1819 | int length = strlen(key + store.baselen + 1); |
| 1820 | const char *quote = ""; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 1821 | struct strbuf sb = STRBUF_INIT; |
Brian Gernhardt | cdd4fb1 | 2007-01-09 00:27:41 -0500 | [diff] [blame] | 1822 | |
Jim Meyering | 6281f39 | 2007-12-08 16:48:05 +0100 | [diff] [blame] | 1823 | /* |
| 1824 | * Check to see if the value needs to be surrounded with a dq pair. |
| 1825 | * Note that problematic characters are always backslash-quoted; this |
| 1826 | * check is about not losing leading or trailing SP and strings that |
| 1827 | * follow beginning-of-comment characters (i.e. ';' and '#') by the |
| 1828 | * configuration parser. |
| 1829 | */ |
Brian Gernhardt | cdd4fb1 | 2007-01-09 00:27:41 -0500 | [diff] [blame] | 1830 | if (value[0] == ' ') |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1831 | quote = "\""; |
Brian Gernhardt | cdd4fb1 | 2007-01-09 00:27:41 -0500 | [diff] [blame] | 1832 | for (i = 0; value[i]; i++) |
| 1833 | if (value[i] == ';' || value[i] == '#') |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1834 | quote = "\""; |
| 1835 | if (i && value[i - 1] == ' ') |
| 1836 | quote = "\""; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1837 | |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1838 | strbuf_addf(&sb, "\t%.*s = %s", |
| 1839 | length, key + store.baselen + 1, quote); |
| 1840 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1841 | for (i = 0; value[i]; i++) |
| 1842 | switch (value[i]) { |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1843 | case '\n': |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1844 | strbuf_addstr(&sb, "\\n"); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1845 | break; |
| 1846 | case '\t': |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1847 | strbuf_addstr(&sb, "\\t"); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1848 | break; |
| 1849 | case '"': |
| 1850 | case '\\': |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1851 | strbuf_addch(&sb, '\\'); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1852 | default: |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1853 | strbuf_addch(&sb, value[i]); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 1854 | break; |
| 1855 | } |
Kristian Høgsberg | cb891a5 | 2007-12-14 15:59:58 -0500 | [diff] [blame] | 1856 | strbuf_addf(&sb, "%s\n", quote); |
| 1857 | |
| 1858 | success = write_in_full(fd, sb.buf, sb.len) == sb.len; |
| 1859 | strbuf_release(&sb); |
| 1860 | |
| 1861 | return success; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1862 | } |
| 1863 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 1864 | static ssize_t find_beginning_of_line(const char *contents, size_t size, |
| 1865 | size_t offset_, int *found_bracket) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1866 | { |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 1867 | size_t equal_offset = size, bracket_offset = size; |
| 1868 | ssize_t offset; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1869 | |
Frank Lichtenheld | 7a31cc0 | 2008-02-11 01:23:03 +0100 | [diff] [blame] | 1870 | contline: |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 1871 | for (offset = offset_-2; offset > 0 |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1872 | && contents[offset] != '\n'; offset--) |
| 1873 | switch (contents[offset]) { |
| 1874 | case '=': equal_offset = offset; break; |
| 1875 | case ']': bracket_offset = offset; break; |
| 1876 | } |
Frank Lichtenheld | 7a31cc0 | 2008-02-11 01:23:03 +0100 | [diff] [blame] | 1877 | if (offset > 0 && contents[offset-1] == '\\') { |
| 1878 | offset_ = offset; |
| 1879 | goto contline; |
| 1880 | } |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 1881 | if (bracket_offset < equal_offset) { |
| 1882 | *found_bracket = 1; |
| 1883 | offset = bracket_offset+1; |
| 1884 | } else |
| 1885 | offset++; |
| 1886 | |
| 1887 | return offset; |
| 1888 | } |
| 1889 | |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 1890 | int git_config_set_in_file_gently(const char *config_filename, |
| 1891 | const char *key, const char *value) |
Ramkumar Ramachandra | 5ec3118 | 2011-08-04 16:09:00 +0530 | [diff] [blame] | 1892 | { |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 1893 | return git_config_set_multivar_in_file_gently(config_filename, key, value, NULL, 0); |
Ramkumar Ramachandra | 5ec3118 | 2011-08-04 16:09:00 +0530 | [diff] [blame] | 1894 | } |
| 1895 | |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 1896 | void git_config_set_in_file(const char *config_filename, |
| 1897 | const char *key, const char *value) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1898 | { |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 1899 | git_config_set_multivar_in_file(config_filename, key, value, NULL, 0); |
Patrick Steinhardt | b4c8aba | 2016-02-16 13:56:28 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 1902 | int git_config_set_gently(const char *key, const char *value) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1903 | { |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 1904 | return git_config_set_multivar_gently(key, value, NULL, 0); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1905 | } |
| 1906 | |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 1907 | void git_config_set(const char *key, const char *value) |
Patrick Steinhardt | b4c8aba | 2016-02-16 13:56:28 +0100 | [diff] [blame] | 1908 | { |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 1909 | git_config_set_multivar(key, value, NULL, 0); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | /* |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1913 | * Auxiliary function to sanity-check and split the key into the section |
| 1914 | * identifier and variable name. |
| 1915 | * |
| 1916 | * Returns 0 on success, -1 when there is an invalid character in the key and |
| 1917 | * -2 if there is no section name in the key. |
| 1918 | * |
| 1919 | * store_key - pointer to char* which will hold a copy of the key with |
| 1920 | * lowercase section and variable name |
| 1921 | * baselen - pointer to int which will hold the length of the |
| 1922 | * section + subsection part, can be NULL |
| 1923 | */ |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1924 | static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet) |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1925 | { |
| 1926 | int i, dot, baselen; |
| 1927 | const char *last_dot = strrchr(key, '.'); |
| 1928 | |
| 1929 | /* |
| 1930 | * Since "key" actually contains the section name and the real |
| 1931 | * key name separated by a dot, we have to know where the dot is. |
| 1932 | */ |
| 1933 | |
Libor Pechacek | 2169ddc | 2011-02-01 08:13:47 +0100 | [diff] [blame] | 1934 | if (last_dot == NULL || last_dot == key) { |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1935 | if (!quiet) |
| 1936 | error("key does not contain a section: %s", key); |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 1937 | return -CONFIG_NO_SECTION_OR_NAME; |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1938 | } |
| 1939 | |
Libor Pechacek | 2169ddc | 2011-02-01 08:13:47 +0100 | [diff] [blame] | 1940 | if (!last_dot[1]) { |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1941 | if (!quiet) |
| 1942 | error("key does not contain variable name: %s", key); |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 1943 | return -CONFIG_NO_SECTION_OR_NAME; |
Libor Pechacek | 2169ddc | 2011-02-01 08:13:47 +0100 | [diff] [blame] | 1944 | } |
| 1945 | |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1946 | baselen = last_dot - key; |
| 1947 | if (baselen_) |
| 1948 | *baselen_ = baselen; |
| 1949 | |
| 1950 | /* |
| 1951 | * Validate the key and while at it, lower case it for matching. |
| 1952 | */ |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1953 | if (store_key) |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 1954 | *store_key = xmallocz(strlen(key)); |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1955 | |
| 1956 | dot = 0; |
| 1957 | for (i = 0; key[i]; i++) { |
| 1958 | unsigned char c = key[i]; |
| 1959 | if (c == '.') |
| 1960 | dot = 1; |
| 1961 | /* Leave the extended basename untouched.. */ |
| 1962 | if (!dot || i > baselen) { |
| 1963 | if (!iskeychar(c) || |
| 1964 | (i == baselen + 1 && !isalpha(c))) { |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1965 | if (!quiet) |
| 1966 | error("invalid key: %s", key); |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1967 | goto out_free_ret_1; |
| 1968 | } |
| 1969 | c = tolower(c); |
| 1970 | } else if (c == '\n') { |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1971 | if (!quiet) |
| 1972 | error("invalid key (newline): %s", key); |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1973 | goto out_free_ret_1; |
| 1974 | } |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1975 | if (store_key) |
| 1976 | (*store_key)[i] = c; |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1977 | } |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1978 | |
| 1979 | return 0; |
| 1980 | |
| 1981 | out_free_ret_1: |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1982 | if (store_key) { |
| 1983 | free(*store_key); |
| 1984 | *store_key = NULL; |
| 1985 | } |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 1986 | return -CONFIG_INVALID_KEY; |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1987 | } |
| 1988 | |
Jeff King | 9e9de18 | 2015-08-24 02:11:33 -0400 | [diff] [blame] | 1989 | int git_config_parse_key(const char *key, char **store_key, int *baselen) |
| 1990 | { |
| 1991 | return git_config_parse_key_1(key, store_key, baselen, 0); |
| 1992 | } |
| 1993 | |
| 1994 | int git_config_key_is_valid(const char *key) |
| 1995 | { |
| 1996 | return !git_config_parse_key_1(key, NULL, NULL, 1); |
| 1997 | } |
| 1998 | |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 1999 | /* |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2000 | * If value==NULL, unset in (remove from) config, |
| 2001 | * if value_regex!=NULL, disregard key/value pairs where value does not match. |
Jeff King | c1063be | 2014-08-19 02:20:00 -0400 | [diff] [blame] | 2002 | * if value_regex==CONFIG_REGEX_NONE, do not match any existing values |
| 2003 | * (only add a new one) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2004 | * if multi_replace==0, nothing, or only one matching key/value is replaced, |
| 2005 | * else all matching key/values (regardless how many) are removed, |
| 2006 | * before the new pair is written. |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2007 | * |
| 2008 | * Returns 0 on success. |
| 2009 | * |
| 2010 | * This function does this: |
| 2011 | * |
| 2012 | * - it locks the config file by creating ".git/config.lock" |
| 2013 | * |
| 2014 | * - it then parses the config using store_aux() as validator to find |
| 2015 | * the position on the key/value pair to replace. If it is to be unset, |
| 2016 | * it must be found exactly once. |
| 2017 | * |
| 2018 | * - the config file is mmap()ed and the part before the match (if any) is |
| 2019 | * written to the lock file, then the changed part and the rest. |
| 2020 | * |
| 2021 | * - the config file is removed and the lock file rename()d to it. |
| 2022 | * |
| 2023 | */ |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 2024 | int git_config_set_multivar_in_file_gently(const char *config_filename, |
| 2025 | const char *key, const char *value, |
| 2026 | const char *value_regex, |
| 2027 | int multi_replace) |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2028 | { |
Sven Strickroth | 54d160e | 2015-08-14 22:21:17 +0200 | [diff] [blame] | 2029 | int fd = -1, in_fd = -1; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2030 | int ret; |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 2031 | struct lock_file *lock = NULL; |
Jeff King | 0a5f575 | 2012-02-16 03:04:05 -0500 | [diff] [blame] | 2032 | char *filename_buf = NULL; |
Jeff King | 3a1b312 | 2015-05-28 03:54:43 -0400 | [diff] [blame] | 2033 | char *contents = NULL; |
| 2034 | size_t contents_sz; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2035 | |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 2036 | /* parse-key returns negative; flip the sign to feed exit(3) */ |
| 2037 | ret = 0 - git_config_parse_key(key, &store.key, &store.baselen); |
| 2038 | if (ret) |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2039 | goto out_free; |
Johannes Schindelin | b17e659 | 2005-11-20 21:22:19 +0100 | [diff] [blame] | 2040 | |
| 2041 | store.multi_replace = multi_replace; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2042 | |
Jeff King | 0a5f575 | 2012-02-16 03:04:05 -0500 | [diff] [blame] | 2043 | if (!config_filename) |
| 2044 | config_filename = filename_buf = git_pathdup("config"); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2045 | |
| 2046 | /* |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 2047 | * The lock serves a purpose in addition to locking: the new |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2048 | * contents of .git/config will be written into it. |
| 2049 | */ |
Brian Gesiak | f1064f6 | 2014-05-27 00:33:46 +0900 | [diff] [blame] | 2050 | lock = xcalloc(1, sizeof(struct lock_file)); |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 2051 | fd = hold_lock_file_for_update(lock, config_filename, 0); |
| 2052 | if (fd < 0) { |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2053 | error_errno("could not lock config file %s", config_filename); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2054 | free(store.key); |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2055 | ret = CONFIG_NO_LOCK; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2056 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * If .git/config does not exist yet, write a minimal version. |
| 2061 | */ |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 2062 | in_fd = open(config_filename, O_RDONLY); |
| 2063 | if ( in_fd < 0 ) { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2064 | free(store.key); |
| 2065 | |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 2066 | if ( ENOENT != errno ) { |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2067 | error_errno("opening %s", config_filename); |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2068 | ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */ |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2069 | goto out_free; |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 2070 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2071 | /* if nothing to unset, error out */ |
| 2072 | if (value == NULL) { |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2073 | ret = CONFIG_NOTHING_SET; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2074 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 2077 | store.key = (char *)key; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 2078 | if (!store_write_section(fd, key) || |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2079 | !store_write_pair(fd, key, value)) |
| 2080 | goto write_err_out; |
| 2081 | } else { |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 2082 | struct stat st; |
Jeff King | 3a1b312 | 2015-05-28 03:54:43 -0400 | [diff] [blame] | 2083 | size_t copy_begin, copy_end; |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2084 | int i, new_line = 0; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2085 | |
| 2086 | if (value_regex == NULL) |
| 2087 | store.value_regex = NULL; |
Jeff King | c1063be | 2014-08-19 02:20:00 -0400 | [diff] [blame] | 2088 | else if (value_regex == CONFIG_REGEX_NONE) |
| 2089 | store.value_regex = CONFIG_REGEX_NONE; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2090 | else { |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 2091 | if (value_regex[0] == '!') { |
| 2092 | store.do_not_match = 1; |
| 2093 | value_regex++; |
| 2094 | } else |
| 2095 | store.do_not_match = 0; |
| 2096 | |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 2097 | store.value_regex = (regex_t*)xmalloc(sizeof(regex_t)); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2098 | if (regcomp(store.value_regex, value_regex, |
| 2099 | REG_EXTENDED)) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 2100 | error("invalid pattern: %s", value_regex); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2101 | free(store.value_regex); |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2102 | ret = CONFIG_INVALID_PATTERN; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2103 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
Thomas Rast | 83786fa | 2013-11-13 11:19:00 +0100 | [diff] [blame] | 2107 | ALLOC_GROW(store.offset, 1, store.offset_alloc); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2108 | store.offset[0] = 0; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2109 | store.state = START; |
| 2110 | store.seen = 0; |
| 2111 | |
| 2112 | /* |
| 2113 | * After this, store.offset will contain the *end* offset |
| 2114 | * of the last match, or remain at 0 if no match was found. |
| 2115 | * As a side effect, we make sure to transform only a valid |
| 2116 | * existing config file. |
| 2117 | */ |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 2118 | if (git_config_from_file(store_aux, config_filename, NULL)) { |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 2119 | error("invalid config file %s", config_filename); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2120 | free(store.key); |
Jeff King | c1063be | 2014-08-19 02:20:00 -0400 | [diff] [blame] | 2121 | if (store.value_regex != NULL && |
| 2122 | store.value_regex != CONFIG_REGEX_NONE) { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2123 | regfree(store.value_regex); |
| 2124 | free(store.value_regex); |
| 2125 | } |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2126 | ret = CONFIG_INVALID_FILE; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2127 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | free(store.key); |
Jeff King | c1063be | 2014-08-19 02:20:00 -0400 | [diff] [blame] | 2131 | if (store.value_regex != NULL && |
| 2132 | store.value_regex != CONFIG_REGEX_NONE) { |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2133 | regfree(store.value_regex); |
| 2134 | free(store.value_regex); |
| 2135 | } |
| 2136 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2137 | /* if nothing to unset, or too many matches, error out */ |
| 2138 | if ((store.seen == 0 && value == NULL) || |
| 2139 | (store.seen > 1 && multi_replace == 0)) { |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2140 | ret = CONFIG_NOTHING_SET; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2141 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2142 | } |
| 2143 | |
Alex Riesen | 88fb958 | 2006-01-05 12:43:34 +0100 | [diff] [blame] | 2144 | fstat(in_fd, &st); |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2145 | contents_sz = xsize_t(st.st_size); |
Jeff King | 1570856 | 2015-05-28 03:56:15 -0400 | [diff] [blame] | 2146 | contents = xmmap_gently(NULL, contents_sz, PROT_READ, |
| 2147 | MAP_PRIVATE, in_fd, 0); |
| 2148 | if (contents == MAP_FAILED) { |
Jeff King | 0e8771f | 2015-05-28 04:03:01 -0400 | [diff] [blame] | 2149 | if (errno == ENODEV && S_ISDIR(st.st_mode)) |
| 2150 | errno = EISDIR; |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2151 | error_errno("unable to mmap '%s'", config_filename); |
Jeff King | 1570856 | 2015-05-28 03:56:15 -0400 | [diff] [blame] | 2152 | ret = CONFIG_INVALID_FILE; |
| 2153 | contents = NULL; |
| 2154 | goto out_free; |
| 2155 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2156 | close(in_fd); |
Sven Strickroth | 54d160e | 2015-08-14 22:21:17 +0200 | [diff] [blame] | 2157 | in_fd = -1; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2158 | |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 2159 | if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) { |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2160 | error_errno("chmod on %s failed", get_lock_file_path(lock)); |
Eric Wong | daa22c6 | 2014-05-06 00:17:14 +0000 | [diff] [blame] | 2161 | ret = CONFIG_NO_WRITE; |
| 2162 | goto out_free; |
| 2163 | } |
| 2164 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2165 | if (store.seen == 0) |
| 2166 | store.seen = 1; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2167 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2168 | for (i = 0, copy_begin = 0; i < store.seen; i++) { |
| 2169 | if (store.offset[i] == 0) { |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2170 | store.offset[i] = copy_end = contents_sz; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2171 | } else if (store.state != KEY_SEEN) { |
| 2172 | copy_end = store.offset[i]; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2173 | } else |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2174 | copy_end = find_beginning_of_line( |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2175 | contents, contents_sz, |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2176 | store.offset[i]-2, &new_line); |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2177 | |
Jeff King | 02e5ba4 | 2008-01-01 01:17:34 -0500 | [diff] [blame] | 2178 | if (copy_end > 0 && contents[copy_end-1] != '\n') |
| 2179 | new_line = 1; |
| 2180 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2181 | /* write the first part of the config */ |
| 2182 | if (copy_end > copy_begin) { |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2183 | if (write_in_full(fd, contents + copy_begin, |
| 2184 | copy_end - copy_begin) < |
| 2185 | copy_end - copy_begin) |
| 2186 | goto write_err_out; |
| 2187 | if (new_line && |
Jim Meyering | 2b7ca83 | 2009-09-12 10:54:32 +0200 | [diff] [blame] | 2188 | write_str_in_full(fd, "\n") != 1) |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2189 | goto write_err_out; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2190 | } |
| 2191 | copy_begin = store.offset[i]; |
| 2192 | } |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2193 | |
| 2194 | /* write the pair (value == NULL means unset) */ |
| 2195 | if (value != NULL) { |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2196 | if (store.state == START) { |
| 2197 | if (!store_write_section(fd, key)) |
| 2198 | goto write_err_out; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 2199 | } |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2200 | if (!store_write_pair(fd, key, value)) |
| 2201 | goto write_err_out; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /* write the rest of the config */ |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2205 | if (copy_begin < contents_sz) |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2206 | if (write_in_full(fd, contents + copy_begin, |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 2207 | contents_sz - copy_begin) < |
| 2208 | contents_sz - copy_begin) |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2209 | goto write_err_out; |
Karsten Blees | 7a64592 | 2015-06-30 16:34:13 +0200 | [diff] [blame] | 2210 | |
| 2211 | munmap(contents, contents_sz); |
| 2212 | contents = NULL; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2213 | } |
| 2214 | |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 2215 | if (commit_lock_file(lock) < 0) { |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2216 | error_errno("could not write config file %s", config_filename); |
Michael J Gruber | 7a39741 | 2011-05-17 17:38:52 +0200 | [diff] [blame] | 2217 | ret = CONFIG_NO_WRITE; |
Michael Haggerty | e831855 | 2014-10-01 12:28:26 +0200 | [diff] [blame] | 2218 | lock = NULL; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2219 | goto out_free; |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2220 | } |
| 2221 | |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 2222 | /* |
| 2223 | * lock is committed, so don't try to roll it back below. |
| 2224 | * NOTE: Since lockfile.c keeps a linked list of all created |
| 2225 | * lock_file structures, it isn't safe to free(lock). It's |
| 2226 | * better to just leave it hanging around. |
| 2227 | */ |
| 2228 | lock = NULL; |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2229 | ret = 0; |
| 2230 | |
Tanay Abhra | 3c8687a | 2014-07-28 03:10:38 -0700 | [diff] [blame] | 2231 | /* Invalidate the config cache */ |
| 2232 | git_config_clear(); |
| 2233 | |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2234 | out_free: |
Bradford C. Smith | 6cbf973 | 2007-07-26 12:55:28 -0400 | [diff] [blame] | 2235 | if (lock) |
| 2236 | rollback_lock_file(lock); |
Jeff King | 0a5f575 | 2012-02-16 03:04:05 -0500 | [diff] [blame] | 2237 | free(filename_buf); |
Jeff King | 3a1b312 | 2015-05-28 03:54:43 -0400 | [diff] [blame] | 2238 | if (contents) |
| 2239 | munmap(contents, contents_sz); |
Sven Strickroth | 54d160e | 2015-08-14 22:21:17 +0200 | [diff] [blame] | 2240 | if (in_fd >= 0) |
| 2241 | close(in_fd); |
Serge E. Hallyn | dafc88b | 2006-04-17 10:14:48 -0500 | [diff] [blame] | 2242 | return ret; |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2243 | |
| 2244 | write_err_out: |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 2245 | ret = write_error(get_lock_file_path(lock)); |
Junio C Hamano | 93c1e07 | 2007-01-11 13:16:26 -0800 | [diff] [blame] | 2246 | goto out_free; |
| 2247 | |
Johannes Schindelin | 10bea15 | 2005-11-17 22:32:36 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 2250 | void git_config_set_multivar_in_file(const char *config_filename, |
| 2251 | const char *key, const char *value, |
| 2252 | const char *value_regex, int multi_replace) |
Ramkumar Ramachandra | 5ec3118 | 2011-08-04 16:09:00 +0530 | [diff] [blame] | 2253 | { |
Jeff King | 1cae428 | 2016-04-09 13:43:54 -0400 | [diff] [blame] | 2254 | if (!git_config_set_multivar_in_file_gently(config_filename, key, value, |
| 2255 | value_regex, multi_replace)) |
| 2256 | return; |
| 2257 | if (value) |
Jeff King | 8c3ca35 | 2016-04-09 13:42:31 -0400 | [diff] [blame] | 2258 | die(_("could not set '%s' to '%s'"), key, value); |
Jeff King | 1cae428 | 2016-04-09 13:43:54 -0400 | [diff] [blame] | 2259 | else |
| 2260 | die(_("could not unset '%s'"), key); |
Patrick Steinhardt | b4c8aba | 2016-02-16 13:56:28 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 2263 | int git_config_set_multivar_gently(const char *key, const char *value, |
| 2264 | const char *value_regex, int multi_replace) |
Ramkumar Ramachandra | 5ec3118 | 2011-08-04 16:09:00 +0530 | [diff] [blame] | 2265 | { |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 2266 | return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex, |
| 2267 | multi_replace); |
Ramkumar Ramachandra | 5ec3118 | 2011-08-04 16:09:00 +0530 | [diff] [blame] | 2268 | } |
| 2269 | |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 2270 | void git_config_set_multivar(const char *key, const char *value, |
| 2271 | const char *value_regex, int multi_replace) |
Patrick Steinhardt | b4c8aba | 2016-02-16 13:56:28 +0100 | [diff] [blame] | 2272 | { |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 2273 | git_config_set_multivar_in_file(NULL, key, value, value_regex, |
| 2274 | multi_replace); |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | static int section_name_match (const char *buf, const char *name) |
| 2278 | { |
| 2279 | int i = 0, j = 0, dot = 0; |
| 2280 | if (buf[i] != '[') |
| 2281 | return 0; |
| 2282 | for (i = 1; buf[i] && buf[i] != ']'; i++) { |
| 2283 | if (!dot && isspace(buf[i])) { |
| 2284 | dot = 1; |
Alex Vandiver | a4c0d46 | 2009-07-24 17:21:43 -0400 | [diff] [blame] | 2285 | if (name[j++] != '.') |
| 2286 | break; |
| 2287 | for (i++; isspace(buf[i]); i++) |
| 2288 | ; /* do nothing */ |
| 2289 | if (buf[i] != '"') |
| 2290 | break; |
| 2291 | continue; |
| 2292 | } |
| 2293 | if (buf[i] == '\\' && dot) |
| 2294 | i++; |
| 2295 | else if (buf[i] == '"' && dot) { |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 2296 | for (i++; isspace(buf[i]); i++) |
| 2297 | ; /* do_nothing */ |
| 2298 | break; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2299 | } |
| 2300 | if (buf[i] != name[j++]) |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 2301 | break; |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 2302 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2303 | if (buf[i] == ']' && name[j] == 0) { |
| 2304 | /* |
| 2305 | * We match, now just find the right length offset by |
Ramsay Jones | 924aaf3 | 2011-06-16 21:24:51 +0100 | [diff] [blame] | 2306 | * gobbling up any whitespace after it, as well |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2307 | */ |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 2308 | i++; |
| 2309 | for (; buf[i] && isspace(buf[i]); i++) |
| 2310 | ; /* do nothing */ |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 2311 | return i; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2312 | } |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 2313 | return 0; |
Alex Riesen | 64c0d71 | 2008-05-12 23:41:04 +0200 | [diff] [blame] | 2314 | } |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 2315 | |
Jeff King | 94a35b1 | 2012-04-25 21:47:14 -0400 | [diff] [blame] | 2316 | static int section_name_is_ok(const char *name) |
| 2317 | { |
| 2318 | /* Empty section names are bogus. */ |
| 2319 | if (!*name) |
| 2320 | return 0; |
| 2321 | |
| 2322 | /* |
| 2323 | * Before a dot, we must be alphanumeric or dash. After the first dot, |
| 2324 | * anything goes, so we can stop checking. |
| 2325 | */ |
| 2326 | for (; *name && *name != '.'; name++) |
| 2327 | if (*name != '-' && !isalnum(*name)) |
| 2328 | return 0; |
| 2329 | return 1; |
| 2330 | } |
| 2331 | |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 2332 | /* if new_name == NULL, the section is removed instead */ |
Jeff King | 42bd39b | 2012-02-16 03:04:25 -0500 | [diff] [blame] | 2333 | int git_config_rename_section_in_file(const char *config_filename, |
| 2334 | const char *old_name, const char *new_name) |
Geert Bosch | 01ebb9d | 2007-04-05 10:20:55 -0400 | [diff] [blame] | 2335 | { |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 2336 | int ret = 0, remove = 0; |
Jeff King | 42bd39b | 2012-02-16 03:04:25 -0500 | [diff] [blame] | 2337 | char *filename_buf = NULL; |
Jeff King | 94a35b1 | 2012-04-25 21:47:14 -0400 | [diff] [blame] | 2338 | struct lock_file *lock; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2339 | int out_fd; |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 2340 | char buf[1024]; |
Alex Vandiver | 9a5abfc | 2009-07-24 17:21:44 -0400 | [diff] [blame] | 2341 | FILE *config_file; |
Eric Wong | daa22c6 | 2014-05-06 00:17:14 +0000 | [diff] [blame] | 2342 | struct stat st; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 2343 | |
Jeff King | 94a35b1 | 2012-04-25 21:47:14 -0400 | [diff] [blame] | 2344 | if (new_name && !section_name_is_ok(new_name)) { |
| 2345 | ret = error("invalid section name: %s", new_name); |
| 2346 | goto out; |
| 2347 | } |
| 2348 | |
Jeff King | 42bd39b | 2012-02-16 03:04:25 -0500 | [diff] [blame] | 2349 | if (!config_filename) |
| 2350 | config_filename = filename_buf = git_pathdup("config"); |
| 2351 | |
Brian Gesiak | f1064f6 | 2014-05-27 00:33:46 +0900 | [diff] [blame] | 2352 | lock = xcalloc(1, sizeof(struct lock_file)); |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2353 | out_fd = hold_lock_file_for_update(lock, config_filename, 0); |
| 2354 | if (out_fd < 0) { |
| 2355 | ret = error("could not lock config file %s", config_filename); |
| 2356 | goto out; |
| 2357 | } |
| 2358 | |
| 2359 | if (!(config_file = fopen(config_filename, "rb"))) { |
| 2360 | /* no config file means nothing to rename, no error */ |
| 2361 | goto unlock_and_out; |
| 2362 | } |
| 2363 | |
Eric Wong | daa22c6 | 2014-05-06 00:17:14 +0000 | [diff] [blame] | 2364 | fstat(fileno(config_file), &st); |
| 2365 | |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 2366 | if (chmod(get_lock_file_path(lock), st.st_mode & 07777) < 0) { |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2367 | ret = error_errno("chmod on %s failed", |
| 2368 | get_lock_file_path(lock)); |
Eric Wong | daa22c6 | 2014-05-06 00:17:14 +0000 | [diff] [blame] | 2369 | goto out; |
| 2370 | } |
| 2371 | |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2372 | while (fgets(buf, sizeof(buf), config_file)) { |
| 2373 | int i; |
| 2374 | int length; |
| 2375 | char *output = buf; |
| 2376 | for (i = 0; buf[i] && isspace(buf[i]); i++) |
| 2377 | ; /* do nothing */ |
| 2378 | if (buf[i] == '[') { |
| 2379 | /* it's a section */ |
Alex Vandiver | a4c0d46 | 2009-07-24 17:21:43 -0400 | [diff] [blame] | 2380 | int offset = section_name_match(&buf[i], old_name); |
| 2381 | if (offset > 0) { |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 2382 | ret++; |
| 2383 | if (new_name == NULL) { |
| 2384 | remove = 1; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2385 | continue; |
| 2386 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2387 | store.baselen = strlen(new_name); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 2388 | if (!store_write_section(out_fd, new_name)) { |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 2389 | ret = write_error(get_lock_file_path(lock)); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 2390 | goto out; |
| 2391 | } |
Alex Vandiver | 9a5abfc | 2009-07-24 17:21:44 -0400 | [diff] [blame] | 2392 | /* |
| 2393 | * We wrote out the new section, with |
| 2394 | * a newline, now skip the old |
| 2395 | * section's length |
| 2396 | */ |
| 2397 | output += offset + i; |
| 2398 | if (strlen(output) > 0) { |
| 2399 | /* |
| 2400 | * More content means there's |
| 2401 | * a declaration to put on the |
| 2402 | * next line; indent with a |
| 2403 | * tab |
| 2404 | */ |
| 2405 | output -= 1; |
| 2406 | output[0] = '\t'; |
| 2407 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2408 | } |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 2409 | remove = 0; |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2410 | } |
Paolo Bonzini | 118f8b2 | 2007-03-02 21:53:33 +0100 | [diff] [blame] | 2411 | if (remove) |
| 2412 | continue; |
Alex Vandiver | 9a5abfc | 2009-07-24 17:21:44 -0400 | [diff] [blame] | 2413 | length = strlen(output); |
| 2414 | if (write_in_full(out_fd, output, length) != length) { |
Michael Haggerty | b4fb09e | 2015-08-10 11:47:39 +0200 | [diff] [blame] | 2415 | ret = write_error(get_lock_file_path(lock)); |
Andy Whitcroft | 480c9e5 | 2007-01-08 15:58:38 +0000 | [diff] [blame] | 2416 | goto out; |
| 2417 | } |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2418 | } |
Junio C Hamano | fc1905b | 2006-12-19 21:55:27 -0800 | [diff] [blame] | 2419 | fclose(config_file); |
Pavan Kumar Sunkara | 8b59007 | 2011-07-17 03:25:52 +0530 | [diff] [blame] | 2420 | unlock_and_out: |
Brandon Casey | 4ed7cd3 | 2008-01-16 13:12:46 -0600 | [diff] [blame] | 2421 | if (commit_lock_file(lock) < 0) |
Nguyễn Thái Ngọc Duy | f0658ec | 2016-05-08 16:47:38 +0700 | [diff] [blame] | 2422 | ret = error_errno("could not write config file %s", |
| 2423 | config_filename); |
Pavan Kumar Sunkara | 8b59007 | 2011-07-17 03:25:52 +0530 | [diff] [blame] | 2424 | out: |
Jeff King | 42bd39b | 2012-02-16 03:04:25 -0500 | [diff] [blame] | 2425 | free(filename_buf); |
Johannes Schindelin | 0667fcf | 2006-12-16 15:14:14 +0100 | [diff] [blame] | 2426 | return ret; |
| 2427 | } |
Junio C Hamano | 40ea4ed | 2008-02-11 10:41:18 -0800 | [diff] [blame] | 2428 | |
Jeff King | 42bd39b | 2012-02-16 03:04:25 -0500 | [diff] [blame] | 2429 | int git_config_rename_section(const char *old_name, const char *new_name) |
| 2430 | { |
Jeff King | 4a7bb5b | 2012-02-16 03:09:32 -0500 | [diff] [blame] | 2431 | return git_config_rename_section_in_file(NULL, old_name, new_name); |
Jeff King | 42bd39b | 2012-02-16 03:04:25 -0500 | [diff] [blame] | 2432 | } |
| 2433 | |
Junio C Hamano | 40ea4ed | 2008-02-11 10:41:18 -0800 | [diff] [blame] | 2434 | /* |
| 2435 | * Call this to report error for your variable that should not |
| 2436 | * get a boolean value (i.e. "[my] var" means "true"). |
| 2437 | */ |
Jeff King | a469a10 | 2012-12-15 12:42:10 -0500 | [diff] [blame] | 2438 | #undef config_error_nonbool |
Junio C Hamano | 40ea4ed | 2008-02-11 10:41:18 -0800 | [diff] [blame] | 2439 | int config_error_nonbool(const char *var) |
| 2440 | { |
Jeff King | 8c3ca35 | 2016-04-09 13:42:31 -0400 | [diff] [blame] | 2441 | return error("missing value for '%s'", var); |
Junio C Hamano | 40ea4ed | 2008-02-11 10:41:18 -0800 | [diff] [blame] | 2442 | } |
Jeff King | 1b86bbb | 2013-01-23 01:23:05 -0500 | [diff] [blame] | 2443 | |
| 2444 | int parse_config_key(const char *var, |
| 2445 | const char *section, |
| 2446 | const char **subsection, int *subsection_len, |
| 2447 | const char **key) |
| 2448 | { |
| 2449 | int section_len = strlen(section); |
| 2450 | const char *dot; |
| 2451 | |
| 2452 | /* Does it start with "section." ? */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 2453 | if (!starts_with(var, section) || var[section_len] != '.') |
Jeff King | 1b86bbb | 2013-01-23 01:23:05 -0500 | [diff] [blame] | 2454 | return -1; |
| 2455 | |
| 2456 | /* |
| 2457 | * Find the key; we don't know yet if we have a subsection, but we must |
| 2458 | * parse backwards from the end, since the subsection may have dots in |
| 2459 | * it, too. |
| 2460 | */ |
| 2461 | dot = strrchr(var, '.'); |
| 2462 | *key = dot + 1; |
| 2463 | |
| 2464 | /* Did we have a subsection at all? */ |
| 2465 | if (dot == var + section_len) { |
| 2466 | *subsection = NULL; |
| 2467 | *subsection_len = 0; |
| 2468 | } |
| 2469 | else { |
| 2470 | *subsection = var + section_len + 1; |
| 2471 | *subsection_len = dot - *subsection; |
| 2472 | } |
| 2473 | |
| 2474 | return 0; |
| 2475 | } |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 2476 | |
| 2477 | const char *current_config_origin_type(void) |
| 2478 | { |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 2479 | const char *type; |
| 2480 | if (current_config_kvi) |
| 2481 | type = current_config_kvi->origin_type; |
| 2482 | else if(cf) |
| 2483 | type = cf->origin_type; |
| 2484 | else |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 2485 | die("BUG: current_config_origin_type called outside config callback"); |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 2486 | return type ? type : "command line"; |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | const char *current_config_name(void) |
| 2490 | { |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 2491 | const char *name; |
| 2492 | if (current_config_kvi) |
| 2493 | name = current_config_kvi->filename; |
| 2494 | else if (cf) |
| 2495 | name = cf->name; |
| 2496 | else |
Jeff King | 3258258 | 2016-05-18 18:41:08 -0400 | [diff] [blame] | 2497 | die("BUG: current_config_name called outside config callback"); |
Jeff King | 0d44a2d | 2016-05-26 20:32:23 -0400 | [diff] [blame] | 2498 | return name ? name : ""; |
Lars Schneider | 473166b | 2016-02-19 10:16:01 +0100 | [diff] [blame] | 2499 | } |
Jeff King | 9acc591 | 2016-05-18 18:44:23 -0400 | [diff] [blame] | 2500 | |
| 2501 | enum config_scope current_config_scope(void) |
| 2502 | { |
| 2503 | if (current_config_kvi) |
| 2504 | return current_config_kvi->scope; |
| 2505 | else |
| 2506 | return current_parsing_scope; |
Linus Torvalds | 1771299 | 2005-10-10 16:31:08 -0700 | [diff] [blame] | 2507 | } |