Matthias Kestenholz | e12c095 | 2006-08-02 23:51:59 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 2 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 4 | #include "color.h" |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 5 | #include "parse-options.h" |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 6 | #include "urlmatch.h" |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 7 | #include "quote.h" |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 8 | #include "worktree.h" |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 9 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 10 | static const char *const builtin_config_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 11 | N_("git config [<options>]"), |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 12 | NULL |
| 13 | }; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 14 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 15 | static char *key; |
| 16 | static regex_t *key_regexp; |
| 17 | static regex_t *regexp; |
| 18 | static int show_keys; |
SZEDER Gábor | 578625f | 2015-08-10 11:46:06 +0200 | [diff] [blame] | 19 | static int omit_values; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 20 | static int use_key_regexp; |
| 21 | static int do_all; |
| 22 | static int do_not_match; |
Frank Lichtenheld | 2275d50 | 2007-06-25 16:03:55 +0200 | [diff] [blame] | 23 | static char delim = '='; |
| 24 | static char key_delim = ' '; |
| 25 | static char term = '\n'; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 26 | |
Sverre Rabbelier | 57210a6 | 2010-08-03 20:59:23 -0500 | [diff] [blame] | 27 | static int use_global_config, use_system_config, use_local_config; |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 28 | static int use_worktree_config; |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 29 | static struct git_config_source given_config_source; |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 30 | static int actions, type; |
Taylor Blau | eeaa24b | 2018-04-09 17:18:26 -0700 | [diff] [blame] | 31 | static char *default_value; |
Matthew Rogers | 329e6ec | 2020-01-24 00:21:01 +0000 | [diff] [blame] | 32 | static int end_nul; |
Nguyễn Thái Ngọc Duy | c48f4b3 | 2017-04-17 17:10:00 +0700 | [diff] [blame] | 33 | static int respect_includes_opt = -1; |
| 34 | static struct config_options config_options; |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 35 | static int show_origin; |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 36 | |
| 37 | #define ACTION_GET (1<<0) |
| 38 | #define ACTION_GET_ALL (1<<1) |
| 39 | #define ACTION_GET_REGEXP (1<<2) |
| 40 | #define ACTION_REPLACE_ALL (1<<3) |
| 41 | #define ACTION_ADD (1<<4) |
| 42 | #define ACTION_UNSET (1<<5) |
| 43 | #define ACTION_UNSET_ALL (1<<6) |
| 44 | #define ACTION_RENAME_SECTION (1<<7) |
| 45 | #define ACTION_REMOVE_SECTION (1<<8) |
| 46 | #define ACTION_LIST (1<<9) |
| 47 | #define ACTION_EDIT (1<<10) |
| 48 | #define ACTION_SET (1<<11) |
| 49 | #define ACTION_SET_ALL (1<<12) |
| 50 | #define ACTION_GET_COLOR (1<<13) |
| 51 | #define ACTION_GET_COLORBOOL (1<<14) |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 52 | #define ACTION_GET_URLMATCH (1<<15) |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 53 | |
Martin Ågren | 32888b8 | 2018-02-21 19:51:43 +0100 | [diff] [blame] | 54 | /* |
| 55 | * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than |
| 56 | * one line of output and which should therefore be paged. |
| 57 | */ |
| 58 | #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \ |
| 59 | ACTION_GET_REGEXP | ACTION_GET_URLMATCH) |
| 60 | |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 61 | #define TYPE_BOOL 1 |
| 62 | #define TYPE_INT 2 |
| 63 | #define TYPE_BOOL_OR_INT 3 |
| 64 | #define TYPE_PATH 4 |
| 65 | #define TYPE_EXPIRY_DATE 5 |
Taylor Blau | 63e2a0f | 2018-04-09 17:18:31 -0700 | [diff] [blame] | 66 | #define TYPE_COLOR 6 |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 67 | |
Taylor Blau | fb0dc3b | 2018-04-18 14:43:35 -0700 | [diff] [blame] | 68 | #define OPT_CALLBACK_VALUE(s, l, v, h, i) \ |
| 69 | { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \ |
| 70 | PARSE_OPT_NONEG, option_parse_type, (i) } |
| 71 | |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 72 | static NORETURN void usage_builtin_config(void); |
Taylor Blau | fb0dc3b | 2018-04-18 14:43:35 -0700 | [diff] [blame] | 73 | |
| 74 | static int option_parse_type(const struct option *opt, const char *arg, |
| 75 | int unset) |
| 76 | { |
| 77 | int new_type, *to_type; |
| 78 | |
| 79 | if (unset) { |
| 80 | *((int *) opt->value) = 0; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * To support '--<type>' style flags, begin with new_type equal to |
| 86 | * opt->defval. |
| 87 | */ |
| 88 | new_type = opt->defval; |
| 89 | if (!new_type) { |
| 90 | if (!strcmp(arg, "bool")) |
| 91 | new_type = TYPE_BOOL; |
| 92 | else if (!strcmp(arg, "int")) |
| 93 | new_type = TYPE_INT; |
| 94 | else if (!strcmp(arg, "bool-or-int")) |
| 95 | new_type = TYPE_BOOL_OR_INT; |
| 96 | else if (!strcmp(arg, "path")) |
| 97 | new_type = TYPE_PATH; |
| 98 | else if (!strcmp(arg, "expiry-date")) |
| 99 | new_type = TYPE_EXPIRY_DATE; |
Taylor Blau | 63e2a0f | 2018-04-09 17:18:31 -0700 | [diff] [blame] | 100 | else if (!strcmp(arg, "color")) |
| 101 | new_type = TYPE_COLOR; |
Taylor Blau | fb0dc3b | 2018-04-18 14:43:35 -0700 | [diff] [blame] | 102 | else |
| 103 | die(_("unrecognized --type argument, %s"), arg); |
| 104 | } |
| 105 | |
| 106 | to_type = opt->value; |
| 107 | if (*to_type && *to_type != new_type) { |
| 108 | /* |
| 109 | * Complain when there is a new type not equal to the old type. |
| 110 | * This allows for combinations like '--int --type=int' and |
| 111 | * '--type=int --type=int', but disallows ones like '--type=bool |
| 112 | * --int' and '--type=bool |
| 113 | * --type=int'. |
| 114 | */ |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 115 | error(_("only one type at a time")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 116 | usage_builtin_config(); |
Taylor Blau | fb0dc3b | 2018-04-18 14:43:35 -0700 | [diff] [blame] | 117 | } |
| 118 | *to_type = new_type; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 123 | static struct option builtin_config_options[] = { |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 124 | OPT_GROUP(N_("Config file location")), |
Stefan Beller | 21e047d | 2013-08-03 13:51:24 +0200 | [diff] [blame] | 125 | OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), |
| 126 | OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), |
| 127 | OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 128 | OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")), |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 129 | OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), |
| 130 | OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 131 | OPT_GROUP(N_("Action")), |
| 132 | OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), |
| 133 | OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL), |
| 134 | OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP), |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 135 | OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 136 | OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL), |
Nguyễn Thái Ngọc Duy | f63cf8c | 2012-08-20 19:32:55 +0700 | [diff] [blame] | 137 | OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), |
| 138 | OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET), |
| 139 | OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 140 | OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), |
| 141 | OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), |
| 142 | OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), |
Nguyễn Thái Ngọc Duy | f63cf8c | 2012-08-20 19:32:55 +0700 | [diff] [blame] | 143 | OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 144 | OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR), |
| 145 | OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 146 | OPT_GROUP(N_("Type")), |
Taylor Blau | fb0dc3b | 2018-04-18 14:43:35 -0700 | [diff] [blame] | 147 | OPT_CALLBACK('t', "type", &type, "", N_("value is given this type"), option_parse_type), |
| 148 | OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), |
| 149 | OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), |
| 150 | OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), |
| 151 | OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), |
| 152 | OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 153 | OPT_GROUP(N_("Other")), |
Matthew Rogers | 329e6ec | 2020-01-24 00:21:01 +0000 | [diff] [blame] | 154 | OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")), |
SZEDER Gábor | 578625f | 2015-08-10 11:46:06 +0200 | [diff] [blame] | 155 | OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")), |
Nguyễn Thái Ngọc Duy | c48f4b3 | 2017-04-17 17:10:00 +0700 | [diff] [blame] | 156 | OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")), |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 157 | OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")), |
Taylor Blau | eeaa24b | 2018-04-09 17:18:26 -0700 | [diff] [blame] | 158 | OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")), |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 159 | OPT_END(), |
| 160 | }; |
| 161 | |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 162 | static NORETURN void usage_builtin_config(void) |
| 163 | { |
| 164 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 165 | } |
| 166 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 167 | static void check_argc(int argc, int min, int max) |
| 168 | { |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 169 | if (argc >= min && argc <= max) |
| 170 | return; |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 171 | if (min == max) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 172 | error(_("wrong number of arguments, should be %d"), min); |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 173 | else |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 174 | error(_("wrong number of arguments, should be from %d to %d"), |
Nguyễn Thái Ngọc Duy | 1a07e59 | 2018-07-21 09:49:19 +0200 | [diff] [blame] | 175 | min, max); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 176 | usage_builtin_config(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 179 | static void show_config_origin(struct strbuf *buf) |
| 180 | { |
Matthew Rogers | 329e6ec | 2020-01-24 00:21:01 +0000 | [diff] [blame] | 181 | const char term = end_nul ? '\0' : '\t'; |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 182 | |
| 183 | strbuf_addstr(buf, current_config_origin_type()); |
| 184 | strbuf_addch(buf, ':'); |
Matthew Rogers | 329e6ec | 2020-01-24 00:21:01 +0000 | [diff] [blame] | 185 | if (end_nul) |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 186 | strbuf_addstr(buf, current_config_name()); |
| 187 | else |
| 188 | quote_c_style(current_config_name(), buf, NULL, 0); |
| 189 | strbuf_addch(buf, term); |
| 190 | } |
| 191 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 192 | static int show_all_config(const char *key_, const char *value_, void *cb) |
Petr Baudis | de791f1 | 2006-04-25 00:59:25 +0200 | [diff] [blame] | 193 | { |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 194 | if (show_origin) { |
| 195 | struct strbuf buf = STRBUF_INIT; |
| 196 | show_config_origin(&buf); |
| 197 | /* Use fwrite as "buf" can contain \0's if "end_null" is set. */ |
| 198 | fwrite(buf.buf, 1, buf.len, stdout); |
| 199 | strbuf_release(&buf); |
| 200 | } |
SZEDER Gábor | 578625f | 2015-08-10 11:46:06 +0200 | [diff] [blame] | 201 | if (!omit_values && value_) |
Frank Lichtenheld | 2275d50 | 2007-06-25 16:03:55 +0200 | [diff] [blame] | 202 | printf("%s%c%s%c", key_, delim, value_, term); |
Petr Baudis | de791f1 | 2006-04-25 00:59:25 +0200 | [diff] [blame] | 203 | else |
Frank Lichtenheld | 2275d50 | 2007-06-25 16:03:55 +0200 | [diff] [blame] | 204 | printf("%s%c", key_, term); |
Petr Baudis | de791f1 | 2006-04-25 00:59:25 +0200 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 208 | struct strbuf_list { |
| 209 | struct strbuf *items; |
| 210 | int nr; |
| 211 | int alloc; |
| 212 | }; |
| 213 | |
Junio C Hamano | d9b9169 | 2013-07-29 14:23:16 -0700 | [diff] [blame] | 214 | static int format_config(struct strbuf *buf, const char *key_, const char *value_) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 215 | { |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 216 | if (show_origin) |
| 217 | show_config_origin(buf); |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 218 | if (show_keys) |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 219 | strbuf_addstr(buf, key_); |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 220 | if (!omit_values) { |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 221 | if (show_keys) |
| 222 | strbuf_addch(buf, key_delim); |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 223 | |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 224 | if (type == TYPE_INT) |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 225 | strbuf_addf(buf, "%"PRId64, |
| 226 | git_config_int64(key_, value_ ? value_ : "")); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 227 | else if (type == TYPE_BOOL) |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 228 | strbuf_addstr(buf, git_config_bool(key_, value_) ? |
| 229 | "true" : "false"); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 230 | else if (type == TYPE_BOOL_OR_INT) { |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 231 | int is_bool, v; |
| 232 | v = git_config_bool_or_int(key_, value_, &is_bool); |
| 233 | if (is_bool) |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 234 | strbuf_addstr(buf, v ? "true" : "false"); |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 235 | else |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 236 | strbuf_addf(buf, "%d", v); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 237 | } else if (type == TYPE_PATH) { |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 238 | const char *v; |
| 239 | if (git_config_pathname(&v, key_, value_) < 0) |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 240 | return -1; |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 241 | strbuf_addstr(buf, v); |
| 242 | free((char *)v); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 243 | } else if (type == TYPE_EXPIRY_DATE) { |
Haaris Mehmood | 5f96742 | 2017-11-18 02:27:27 +0000 | [diff] [blame] | 244 | timestamp_t t; |
| 245 | if (git_config_expiry_date(&t, key_, value_) < 0) |
| 246 | return -1; |
| 247 | strbuf_addf(buf, "%"PRItime, t); |
Taylor Blau | 63e2a0f | 2018-04-09 17:18:31 -0700 | [diff] [blame] | 248 | } else if (type == TYPE_COLOR) { |
| 249 | char v[COLOR_MAXLEN]; |
| 250 | if (git_config_color(v, key_, value_) < 0) |
| 251 | return -1; |
| 252 | strbuf_addstr(buf, v); |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 253 | } else if (value_) { |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 254 | strbuf_addstr(buf, value_); |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 255 | } else { |
Jeff King | f225987 | 2015-08-20 10:47:34 -0400 | [diff] [blame] | 256 | /* Just show the key name; back out delimiter */ |
| 257 | if (show_keys) |
| 258 | strbuf_setlen(buf, buf->len - 1); |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 259 | } |
SZEDER Gábor | ebca2d4 | 2015-08-20 16:14:22 +0200 | [diff] [blame] | 260 | } |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 261 | strbuf_addch(buf, term); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Junio C Hamano | d9b9169 | 2013-07-29 14:23:16 -0700 | [diff] [blame] | 265 | static int collect_config(const char *key_, const char *value_, void *cb) |
| 266 | { |
| 267 | struct strbuf_list *values = cb; |
| 268 | |
| 269 | if (!use_key_regexp && strcmp(key_, key)) |
| 270 | return 0; |
| 271 | if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) |
| 272 | return 0; |
| 273 | if (regexp != NULL && |
| 274 | (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) |
| 275 | return 0; |
| 276 | |
| 277 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); |
Jeff King | 9f1429d | 2015-08-20 10:46:04 -0400 | [diff] [blame] | 278 | strbuf_init(&values->items[values->nr], 0); |
Junio C Hamano | d9b9169 | 2013-07-29 14:23:16 -0700 | [diff] [blame] | 279 | |
| 280 | return format_config(&values->items[values->nr++], key_, value_); |
| 281 | } |
| 282 | |
Felipe Contreras | 4b951b7 | 2009-02-21 02:48:53 +0200 | [diff] [blame] | 283 | static int get_value(const char *key_, const char *regex_) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 284 | { |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 285 | int ret = CONFIG_GENERIC_ERROR; |
Ramsay Jones | 5ba1a8a | 2012-10-28 21:05:25 +0000 | [diff] [blame] | 286 | struct strbuf_list values = {NULL}; |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 287 | int i; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 288 | |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 289 | if (use_key_regexp) { |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 290 | char *tl; |
| 291 | |
| 292 | /* |
| 293 | * NEEDSWORK: this naive pattern lowercasing obviously does not |
| 294 | * work for more complex patterns like "^[^.]*Foo.*bar". |
| 295 | * Perhaps we should deprecate this altogether someday. |
| 296 | */ |
| 297 | |
| 298 | key = xstrdup(key_); |
| 299 | for (tl = key + strlen(key) - 1; |
| 300 | tl >= key && *tl != '.'; |
| 301 | tl--) |
| 302 | *tl = tolower(*tl); |
| 303 | for (tl = key; *tl && *tl != '.'; tl++) |
| 304 | *tl = tolower(*tl); |
| 305 | |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 306 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 307 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 308 | error(_("invalid key pattern: %s"), key_); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 309 | FREE_AND_NULL(key_regexp); |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 310 | ret = CONFIG_INVALID_PATTERN; |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 311 | goto free_strings; |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 312 | } |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 313 | } else { |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 314 | if (git_config_parse_key(key_, &key, NULL)) { |
| 315 | ret = CONFIG_INVALID_KEY; |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 316 | goto free_strings; |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 317 | } |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 320 | if (regex_) { |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 321 | if (regex_[0] == '!') { |
| 322 | do_not_match = 1; |
| 323 | regex_++; |
| 324 | } |
| 325 | |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 326 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 327 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 328 | error(_("invalid pattern: %s"), regex_); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 329 | FREE_AND_NULL(regexp); |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 330 | ret = CONFIG_INVALID_PATTERN; |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 331 | goto free_strings; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Brandon Williams | dc8441f | 2017-06-14 11:07:39 -0700 | [diff] [blame] | 335 | config_with_options(collect_config, &values, |
| 336 | &given_config_source, &config_options); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 337 | |
Taylor Blau | eeaa24b | 2018-04-09 17:18:26 -0700 | [diff] [blame] | 338 | if (!values.nr && default_value) { |
| 339 | struct strbuf *item; |
| 340 | ALLOC_GROW(values.items, values.nr + 1, values.alloc); |
| 341 | item = &values.items[values.nr++]; |
| 342 | strbuf_init(item, 0); |
| 343 | if (format_config(item, key_, default_value) < 0) |
| 344 | die(_("failed to format default config value: %s"), |
| 345 | default_value); |
| 346 | } |
| 347 | |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 348 | ret = !values.nr; |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 349 | |
| 350 | for (i = 0; i < values.nr; i++) { |
| 351 | struct strbuf *buf = values.items + i; |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 352 | if (do_all || i == values.nr - 1) |
| 353 | fwrite(buf->buf, 1, buf->len, stdout); |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 354 | strbuf_release(buf); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 355 | } |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 356 | free(values.items); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 357 | |
Jeff King | 97ed50f | 2012-10-23 15:40:06 -0400 | [diff] [blame] | 358 | free_strings: |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 359 | free(key); |
Jeff King | 35998c8 | 2012-10-23 15:36:12 -0400 | [diff] [blame] | 360 | if (key_regexp) { |
| 361 | regfree(key_regexp); |
| 362 | free(key_regexp); |
| 363 | } |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 364 | if (regexp) { |
| 365 | regfree(regexp); |
| 366 | free(regexp); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 369 | return ret; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 370 | } |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 371 | |
Stephan Beyer | 186458b | 2008-07-24 01:09:35 +0200 | [diff] [blame] | 372 | static char *normalize_value(const char *key, const char *value) |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 373 | { |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 374 | if (!value) |
| 375 | return NULL; |
| 376 | |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 377 | if (type == 0 || type == TYPE_PATH || type == TYPE_EXPIRY_DATE) |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 378 | /* |
| 379 | * We don't do normalization for TYPE_PATH here: If |
| 380 | * the path is like ~/foobar/, we prefer to store |
| 381 | * "~/foobar/" in the config file, and to expand the ~ |
| 382 | * when retrieving the value. |
Haaris Mehmood | 5f96742 | 2017-11-18 02:27:27 +0000 | [diff] [blame] | 383 | * Also don't do normalization for expiry dates. |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 384 | */ |
Jeff King | 3ec832c | 2015-09-24 17:07:05 -0400 | [diff] [blame] | 385 | return xstrdup(value); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 386 | if (type == TYPE_INT) |
Jeff King | 3ec832c | 2015-09-24 17:07:05 -0400 | [diff] [blame] | 387 | return xstrfmt("%"PRId64, git_config_int64(key, value)); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 388 | if (type == TYPE_BOOL) |
Jeff King | 3ec832c | 2015-09-24 17:07:05 -0400 | [diff] [blame] | 389 | return xstrdup(git_config_bool(key, value) ? "true" : "false"); |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 390 | if (type == TYPE_BOOL_OR_INT) { |
Jeff King | 3ec832c | 2015-09-24 17:07:05 -0400 | [diff] [blame] | 391 | int is_bool, v; |
| 392 | v = git_config_bool_or_int(key, value, &is_bool); |
| 393 | if (!is_bool) |
| 394 | return xstrfmt("%d", v); |
| 395 | else |
| 396 | return xstrdup(v ? "true" : "false"); |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 397 | } |
Taylor Blau | 63e2a0f | 2018-04-09 17:18:31 -0700 | [diff] [blame] | 398 | if (type == TYPE_COLOR) { |
| 399 | char v[COLOR_MAXLEN]; |
| 400 | if (git_config_color(v, key, value)) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 401 | die(_("cannot parse color '%s'"), value); |
Taylor Blau | 63e2a0f | 2018-04-09 17:18:31 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * The contents of `v` now contain an ANSI escape |
| 405 | * sequence, not suitable for including within a |
| 406 | * configuration file. Treat the above as a |
| 407 | * "sanity-check", and return the given value, which we |
| 408 | * know is representable as valid color code. |
| 409 | */ |
| 410 | return xstrdup(value); |
| 411 | } |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 412 | |
Junio C Hamano | 50f08db | 2018-05-30 14:04:07 +0900 | [diff] [blame] | 413 | BUG("cannot normalize type %d", type); |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 416 | static int get_color_found; |
| 417 | static const char *get_color_slot; |
Felipe Contreras | b408457 | 2009-02-21 02:48:56 +0200 | [diff] [blame] | 418 | static const char *get_colorbool_slot; |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 419 | static char parsed_color[COLOR_MAXLEN]; |
| 420 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 421 | static int git_get_color_config(const char *var, const char *value, void *cb) |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 422 | { |
| 423 | if (!strcmp(var, get_color_slot)) { |
Junio C Hamano | f769982 | 2008-02-11 10:48:12 -0800 | [diff] [blame] | 424 | if (!value) |
| 425 | config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 426 | if (color_parse(value, parsed_color) < 0) |
| 427 | return -1; |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 428 | get_color_found = 1; |
| 429 | } |
| 430 | return 0; |
| 431 | } |
| 432 | |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 433 | static void get_color(const char *var, const char *def_color) |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 434 | { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 435 | get_color_slot = var; |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 436 | get_color_found = 0; |
| 437 | parsed_color[0] = '\0'; |
Brandon Williams | dc8441f | 2017-06-14 11:07:39 -0700 | [diff] [blame] | 438 | config_with_options(git_get_color_config, NULL, |
| 439 | &given_config_source, &config_options); |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 440 | |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 441 | if (!get_color_found && def_color) { |
| 442 | if (color_parse(def_color, parsed_color) < 0) |
| 443 | die(_("unable to parse default color value")); |
| 444 | } |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 445 | |
| 446 | fputs(parsed_color, stdout); |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 449 | static int get_colorbool_found; |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 450 | static int get_diff_color_found; |
Jeff King | c659f55 | 2011-08-17 22:04:56 -0700 | [diff] [blame] | 451 | static int get_color_ui_found; |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 452 | static int git_get_colorbool_config(const char *var, const char *value, |
| 453 | void *cb) |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 454 | { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 455 | if (!strcmp(var, get_colorbool_slot)) |
| 456 | get_colorbool_found = git_config_colorbool(var, value); |
| 457 | else if (!strcmp(var, "diff.color")) |
| 458 | get_diff_color_found = git_config_colorbool(var, value); |
| 459 | else if (!strcmp(var, "color.ui")) |
Jeff King | c659f55 | 2011-08-17 22:04:56 -0700 | [diff] [blame] | 460 | get_color_ui_found = git_config_colorbool(var, value); |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 464 | static int get_colorbool(const char *var, int print) |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 465 | { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 466 | get_colorbool_slot = var; |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 467 | get_colorbool_found = -1; |
| 468 | get_diff_color_found = -1; |
Matthieu Moy | b8612b4 | 2013-05-15 19:00:55 +0200 | [diff] [blame] | 469 | get_color_ui_found = -1; |
Brandon Williams | dc8441f | 2017-06-14 11:07:39 -0700 | [diff] [blame] | 470 | config_with_options(git_get_colorbool_config, NULL, |
| 471 | &given_config_source, &config_options); |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 472 | |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 473 | if (get_colorbool_found < 0) { |
Felipe Contreras | b408457 | 2009-02-21 02:48:56 +0200 | [diff] [blame] | 474 | if (!strcmp(get_colorbool_slot, "color.diff")) |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 475 | get_colorbool_found = get_diff_color_found; |
| 476 | if (get_colorbool_found < 0) |
Jeff King | c659f55 | 2011-08-17 22:04:56 -0700 | [diff] [blame] | 477 | get_colorbool_found = get_color_ui_found; |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Matthieu Moy | b8612b4 | 2013-05-15 19:00:55 +0200 | [diff] [blame] | 480 | if (get_colorbool_found < 0) |
| 481 | /* default value if none found in config */ |
Matthieu Moy | 4c7f181 | 2013-06-10 16:26:09 +0200 | [diff] [blame] | 482 | get_colorbool_found = GIT_COLOR_AUTO; |
Matthieu Moy | b8612b4 | 2013-05-15 19:00:55 +0200 | [diff] [blame] | 483 | |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 484 | get_colorbool_found = want_color(get_colorbool_found); |
| 485 | |
Felipe Contreras | 0e854a2 | 2009-02-21 02:48:57 +0200 | [diff] [blame] | 486 | if (print) { |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 487 | printf("%s\n", get_colorbool_found ? "true" : "false"); |
| 488 | return 0; |
Felipe Contreras | 0e854a2 | 2009-02-21 02:48:57 +0200 | [diff] [blame] | 489 | } else |
| 490 | return get_colorbool_found ? 0 : 1; |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 493 | static void check_write(void) |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 494 | { |
Johannes Schindelin | 638fa62 | 2016-02-24 13:48:11 +0100 | [diff] [blame] | 495 | if (!given_config_source.file && !startup_info->have_repository) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 496 | die(_("not in a git directory")); |
Johannes Schindelin | 638fa62 | 2016-02-24 13:48:11 +0100 | [diff] [blame] | 497 | |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 498 | if (given_config_source.use_stdin) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 499 | die(_("writing to stdin is not supported")); |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 500 | |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 501 | if (given_config_source.blob) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 502 | die(_("writing config blobs is not supported")); |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 505 | struct urlmatch_current_candidate_value { |
| 506 | char value_is_null; |
| 507 | struct strbuf value; |
| 508 | }; |
| 509 | |
| 510 | static int urlmatch_collect_fn(const char *var, const char *value, void *cb) |
| 511 | { |
| 512 | struct string_list *values = cb; |
| 513 | struct string_list_item *item = string_list_insert(values, var); |
| 514 | struct urlmatch_current_candidate_value *matched = item->util; |
| 515 | |
| 516 | if (!matched) { |
| 517 | matched = xmalloc(sizeof(*matched)); |
| 518 | strbuf_init(&matched->value, 0); |
| 519 | item->util = matched; |
| 520 | } else { |
| 521 | strbuf_reset(&matched->value); |
| 522 | } |
| 523 | |
| 524 | if (value) { |
| 525 | strbuf_addstr(&matched->value, value); |
| 526 | matched->value_is_null = 0; |
| 527 | } else { |
| 528 | matched->value_is_null = 1; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 533 | static int get_urlmatch(const char *var, const char *url) |
| 534 | { |
John Keeping | 27b30be | 2016-02-28 11:54:35 +0000 | [diff] [blame] | 535 | int ret; |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 536 | char *section_tail; |
| 537 | struct string_list_item *item; |
| 538 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; |
| 539 | struct string_list values = STRING_LIST_INIT_DUP; |
| 540 | |
| 541 | config.collect_fn = urlmatch_collect_fn; |
| 542 | config.cascade_fn = NULL; |
| 543 | config.cb = &values; |
| 544 | |
| 545 | if (!url_normalize(url, &config.url)) |
Junio C Hamano | 6667a6a | 2013-08-08 21:41:44 -0700 | [diff] [blame] | 546 | die("%s", config.url.err); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 547 | |
Jeff King | 88d5a6f | 2014-05-22 05:44:09 -0400 | [diff] [blame] | 548 | config.section = xstrdup_tolower(var); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 549 | section_tail = strchr(config.section, '.'); |
| 550 | if (section_tail) { |
| 551 | *section_tail = '\0'; |
| 552 | config.key = section_tail + 1; |
| 553 | show_keys = 0; |
| 554 | } else { |
| 555 | config.key = NULL; |
| 556 | show_keys = 1; |
| 557 | } |
| 558 | |
Brandon Williams | dc8441f | 2017-06-14 11:07:39 -0700 | [diff] [blame] | 559 | config_with_options(urlmatch_config_entry, &config, |
| 560 | &given_config_source, &config_options); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 561 | |
John Keeping | 27b30be | 2016-02-28 11:54:35 +0000 | [diff] [blame] | 562 | ret = !values.nr; |
| 563 | |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 564 | for_each_string_list_item(item, &values) { |
| 565 | struct urlmatch_current_candidate_value *matched = item->util; |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 566 | struct strbuf buf = STRBUF_INIT; |
| 567 | |
Jeff King | a92330d | 2015-08-20 10:49:45 -0400 | [diff] [blame] | 568 | format_config(&buf, item->string, |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 569 | matched->value_is_null ? NULL : matched->value.buf); |
| 570 | fwrite(buf.buf, 1, buf.len, stdout); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 571 | strbuf_release(&buf); |
| 572 | |
| 573 | strbuf_release(&matched->value); |
| 574 | } |
| 575 | string_list_clear(&config.vars, 1); |
| 576 | string_list_clear(&values, 1); |
| 577 | free(config.url.url); |
| 578 | |
| 579 | free((void *)config.section); |
John Keeping | 27b30be | 2016-02-28 11:54:35 +0000 | [diff] [blame] | 580 | return ret; |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 583 | static char *default_user_config(void) |
| 584 | { |
| 585 | struct strbuf buf = STRBUF_INIT; |
| 586 | strbuf_addf(&buf, |
| 587 | _("# This is Git's per-user configuration file.\n" |
Ossi Herrala | 7e11052 | 2015-04-17 17:50:10 +0300 | [diff] [blame] | 588 | "[user]\n" |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 589 | "# Please adapt and uncomment the following lines:\n" |
Ossi Herrala | 7e11052 | 2015-04-17 17:50:10 +0300 | [diff] [blame] | 590 | "# name = %s\n" |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 591 | "# email = %s\n"), |
| 592 | ident_default_name(), |
| 593 | ident_default_email()); |
| 594 | return strbuf_detach(&buf, NULL); |
| 595 | } |
| 596 | |
Nguyễn Thái Ngọc Duy | 3ba7e6e | 2010-08-05 22:15:09 -0500 | [diff] [blame] | 597 | int cmd_config(int argc, const char **argv, const char *prefix) |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 598 | { |
Nguyễn Thái Ngọc Duy | 3ba7e6e | 2010-08-05 22:15:09 -0500 | [diff] [blame] | 599 | int nongit = !startup_info->have_repository; |
Felipe Contreras | 4b951b7 | 2009-02-21 02:48:53 +0200 | [diff] [blame] | 600 | char *value; |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 601 | |
Jeff King | 423ff9b | 2019-01-11 17:15:54 -0500 | [diff] [blame] | 602 | given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 603 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 604 | argc = parse_options(argc, argv, prefix, builtin_config_options, |
| 605 | builtin_config_usage, |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 606 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 607 | |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 608 | if (use_global_config + use_system_config + use_local_config + |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 609 | use_worktree_config + |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 610 | !!given_config_source.file + !!given_config_source.blob > 1) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 611 | error(_("only one config file at a time")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 612 | usage_builtin_config(); |
Felipe Contreras | 67052c9 | 2009-02-21 02:49:26 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Jeff King | 25cd291 | 2017-05-12 23:29:31 -0400 | [diff] [blame] | 615 | if (use_local_config && nongit) |
| 616 | die(_("--local can only be used inside a git repository")); |
| 617 | |
Jeff King | 17b8a2d | 2018-05-18 15:27:04 -0700 | [diff] [blame] | 618 | if (given_config_source.blob && nongit) |
| 619 | die(_("--blob can only be used inside a git repository")); |
| 620 | |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 621 | if (given_config_source.file && |
| 622 | !strcmp(given_config_source.file, "-")) { |
| 623 | given_config_source.file = NULL; |
| 624 | given_config_source.use_stdin = 1; |
| 625 | } |
| 626 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 627 | if (use_global_config) { |
Nguyễn Thái Ngọc Duy | 4aad2f1 | 2017-04-05 17:24:38 +0700 | [diff] [blame] | 628 | char *user_config = expand_user_path("~/.gitconfig", 0); |
Paul Tan | 509adc3 | 2015-05-06 16:01:03 +0800 | [diff] [blame] | 629 | char *xdg_config = xdg_config_home("config"); |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 630 | |
Matthieu Moy | e3ebc35 | 2012-07-12 14:04:20 +0200 | [diff] [blame] | 631 | if (!user_config) |
| 632 | /* |
| 633 | * It is unknown if HOME/.gitconfig exists, so |
| 634 | * we do not know if we should write to XDG |
| 635 | * location; error out even if XDG_CONFIG_HOME |
| 636 | * is set and points at a sane location. |
| 637 | */ |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 638 | die(_("$HOME not set")); |
Matthieu Moy | e3ebc35 | 2012-07-12 14:04:20 +0200 | [diff] [blame] | 639 | |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 640 | if (access_or_warn(user_config, R_OK, 0) && |
Jeff King | 6c6b08d | 2017-09-05 09:04:20 -0400 | [diff] [blame] | 641 | xdg_config && !access_or_warn(xdg_config, R_OK, 0)) { |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 642 | given_config_source.file = xdg_config; |
Jeff King | 6c6b08d | 2017-09-05 09:04:20 -0400 | [diff] [blame] | 643 | free(user_config); |
| 644 | } else { |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 645 | given_config_source.file = user_config; |
Jeff King | 6c6b08d | 2017-09-05 09:04:20 -0400 | [diff] [blame] | 646 | free(xdg_config); |
| 647 | } |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 648 | } |
| 649 | else if (use_system_config) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 650 | given_config_source.file = git_etc_gitconfig(); |
Sverre Rabbelier | 57210a6 | 2010-08-03 20:59:23 -0500 | [diff] [blame] | 651 | else if (use_local_config) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 652 | given_config_source.file = git_pathdup("config"); |
Nguyễn Thái Ngọc Duy | 58b284a | 2018-10-21 16:02:28 +0200 | [diff] [blame] | 653 | else if (use_worktree_config) { |
| 654 | struct worktree **worktrees = get_worktrees(0); |
| 655 | if (repository_format_worktree_config) |
| 656 | given_config_source.file = git_pathdup("config.worktree"); |
| 657 | else if (worktrees[0] && worktrees[1]) |
| 658 | die(_("--worktree cannot be used with multiple " |
| 659 | "working trees unless the config\n" |
| 660 | "extension worktreeConfig is enabled. " |
| 661 | "Please read \"CONFIGURATION FILE\"\n" |
| 662 | "section in \"git help worktree\" for details")); |
| 663 | else |
| 664 | given_config_source.file = git_pathdup("config"); |
| 665 | free_worktrees(worktrees); |
| 666 | } else if (given_config_source.file) { |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 667 | if (!is_absolute_path(given_config_source.file) && prefix) |
| 668 | given_config_source.file = |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 669 | prefix_filename(prefix, given_config_source.file); |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 670 | } |
| 671 | |
Nguyễn Thái Ngọc Duy | c48f4b3 | 2017-04-17 17:10:00 +0700 | [diff] [blame] | 672 | if (respect_includes_opt == -1) |
| 673 | config_options.respect_includes = !given_config_source.file; |
| 674 | else |
| 675 | config_options.respect_includes = respect_includes_opt; |
Brandon Williams | dc8441f | 2017-06-14 11:07:39 -0700 | [diff] [blame] | 676 | if (!nongit) { |
| 677 | config_options.commondir = get_git_common_dir(); |
| 678 | config_options.git_dir = get_git_dir(); |
| 679 | } |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 680 | |
Matthew Rogers | 329e6ec | 2020-01-24 00:21:01 +0000 | [diff] [blame] | 681 | if (end_nul) { |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 682 | term = '\0'; |
| 683 | delim = '\n'; |
| 684 | key_delim = '\n'; |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 685 | } |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 686 | |
Taylor Blau | 0a8950b | 2018-04-09 15:46:54 -0700 | [diff] [blame] | 687 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 688 | error(_("--get-color and variable type are incoherent")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 689 | usage_builtin_config(); |
Felipe Contreras | c238735 | 2009-02-21 02:49:29 +0200 | [diff] [blame] | 690 | } |
| 691 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 692 | if (HAS_MULTI_BITS(actions)) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 693 | error(_("only one action at a time")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 694 | usage_builtin_config(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 695 | } |
| 696 | if (actions == 0) |
| 697 | switch (argc) { |
| 698 | case 1: actions = ACTION_GET; break; |
| 699 | case 2: actions = ACTION_SET; break; |
| 700 | case 3: actions = ACTION_SET_ALL; break; |
| 701 | default: |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 702 | usage_builtin_config(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 703 | } |
SZEDER Gábor | 578625f | 2015-08-10 11:46:06 +0200 | [diff] [blame] | 704 | if (omit_values && |
| 705 | !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 706 | error(_("--name-only is only applicable to --list or --get-regexp")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 707 | usage_builtin_config(); |
SZEDER Gábor | 578625f | 2015-08-10 11:46:06 +0200 | [diff] [blame] | 708 | } |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 709 | |
| 710 | if (show_origin && !(actions & |
| 711 | (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 712 | error(_("--show-origin is only applicable to --get, --get-all, " |
| 713 | "--get-regexp, and --list")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 714 | usage_builtin_config(); |
Lars Schneider | 70bd879 | 2016-02-19 10:16:02 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Taylor Blau | eeaa24b | 2018-04-09 17:18:26 -0700 | [diff] [blame] | 717 | if (default_value && !(actions & ACTION_GET)) { |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 718 | error(_("--default is only applicable to --get")); |
Beat Bolli | 6aaded5 | 2018-07-05 20:34:45 +0200 | [diff] [blame] | 719 | usage_builtin_config(); |
Taylor Blau | eeaa24b | 2018-04-09 17:18:26 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Martin Ågren | 32888b8 | 2018-02-21 19:51:43 +0100 | [diff] [blame] | 722 | if (actions & PAGING_ACTIONS) |
Martin Ågren | c0e9f5b | 2018-02-21 19:51:44 +0100 | [diff] [blame] | 723 | setup_auto_pager("config", 1); |
Martin Ågren | 32888b8 | 2018-02-21 19:51:43 +0100 | [diff] [blame] | 724 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 725 | if (actions == ACTION_LIST) { |
Felipe Contreras | 225a9ca | 2009-02-21 02:49:28 +0200 | [diff] [blame] | 726 | check_argc(argc, 0, 0); |
Brandon Williams | dc8441f | 2017-06-14 11:07:39 -0700 | [diff] [blame] | 727 | if (config_with_options(show_all_config, NULL, |
| 728 | &given_config_source, |
| 729 | &config_options) < 0) { |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 730 | if (given_config_source.file) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 731 | die_errno(_("unable to read config file '%s'"), |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 732 | given_config_source.file); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 733 | else |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 734 | die(_("error processing config file(s)")); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | else if (actions == ACTION_EDIT) { |
Michael Haggerty | 3696a7c | 2014-11-16 08:37:44 +0100 | [diff] [blame] | 738 | char *config_file; |
| 739 | |
Felipe Contreras | 225a9ca | 2009-02-21 02:49:28 +0200 | [diff] [blame] | 740 | check_argc(argc, 0, 0); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 741 | if (!given_config_source.file && nongit) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 742 | die(_("not in a git directory")); |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 743 | if (given_config_source.use_stdin) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 744 | die(_("editing stdin is not supported")); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 745 | if (given_config_source.blob) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 746 | die(_("editing blobs is not supported")); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 747 | git_config(git_default_config, NULL); |
Jeff King | d9c6964 | 2017-04-20 17:09:09 -0400 | [diff] [blame] | 748 | config_file = given_config_source.file ? |
| 749 | xstrdup(given_config_source.file) : |
| 750 | git_pathdup("config"); |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 751 | if (use_global_config) { |
| 752 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); |
Jeff King | aabbd3f | 2016-07-08 05:06:50 -0400 | [diff] [blame] | 753 | if (fd >= 0) { |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 754 | char *content = default_user_config(); |
| 755 | write_str_in_full(fd, content); |
| 756 | free(content); |
| 757 | close(fd); |
| 758 | } |
| 759 | else if (errno != EEXIST) |
| 760 | die_errno(_("cannot create configuration file %s"), config_file); |
| 761 | } |
| 762 | launch_editor(config_file, NULL, NULL); |
Michael Haggerty | 3696a7c | 2014-11-16 08:37:44 +0100 | [diff] [blame] | 763 | free(config_file); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 764 | } |
| 765 | else if (actions == ACTION_SET) { |
Michael J Gruber | 5a2df36 | 2011-05-17 17:38:53 +0200 | [diff] [blame] | 766 | int ret; |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 767 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 768 | check_argc(argc, 2, 2); |
| 769 | value = normalize_value(argv[0], argv[1]); |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 770 | UNLEAK(value); |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 771 | ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value); |
Michael J Gruber | 5a2df36 | 2011-05-17 17:38:53 +0200 | [diff] [blame] | 772 | if (ret == CONFIG_NOTHING_SET) |
Vasco Almeida | ccf6380 | 2016-09-15 14:59:00 +0000 | [diff] [blame] | 773 | error(_("cannot overwrite multiple values with a single value\n" |
| 774 | " Use a regexp, --add or --replace-all to change %s."), argv[0]); |
Michael J Gruber | 5a2df36 | 2011-05-17 17:38:53 +0200 | [diff] [blame] | 775 | return ret; |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 776 | } |
| 777 | else if (actions == ACTION_SET_ALL) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 778 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 779 | check_argc(argc, 2, 3); |
| 780 | value = normalize_value(argv[0], argv[1]); |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 781 | UNLEAK(value); |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 782 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
| 783 | argv[0], value, argv[2], 0); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 784 | } |
| 785 | else if (actions == ACTION_ADD) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 786 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 787 | check_argc(argc, 2, 2); |
| 788 | value = normalize_value(argv[0], argv[1]); |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 789 | UNLEAK(value); |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 790 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
| 791 | argv[0], value, |
| 792 | CONFIG_REGEX_NONE, 0); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 793 | } |
| 794 | else if (actions == ACTION_REPLACE_ALL) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 795 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 796 | check_argc(argc, 2, 3); |
| 797 | value = normalize_value(argv[0], argv[1]); |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 798 | UNLEAK(value); |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 799 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
| 800 | argv[0], value, argv[2], 1); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 801 | } |
| 802 | else if (actions == ACTION_GET) { |
| 803 | check_argc(argc, 1, 2); |
| 804 | return get_value(argv[0], argv[1]); |
| 805 | } |
| 806 | else if (actions == ACTION_GET_ALL) { |
| 807 | do_all = 1; |
| 808 | check_argc(argc, 1, 2); |
| 809 | return get_value(argv[0], argv[1]); |
| 810 | } |
| 811 | else if (actions == ACTION_GET_REGEXP) { |
| 812 | show_keys = 1; |
| 813 | use_key_regexp = 1; |
| 814 | do_all = 1; |
| 815 | check_argc(argc, 1, 2); |
| 816 | return get_value(argv[0], argv[1]); |
| 817 | } |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 818 | else if (actions == ACTION_GET_URLMATCH) { |
| 819 | check_argc(argc, 2, 2); |
| 820 | return get_urlmatch(argv[0], argv[1]); |
| 821 | } |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 822 | else if (actions == ACTION_UNSET) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 823 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 824 | check_argc(argc, 1, 2); |
| 825 | if (argc == 2) |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 826 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
| 827 | argv[0], NULL, argv[1], 0); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 828 | else |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 829 | return git_config_set_in_file_gently(given_config_source.file, |
| 830 | argv[0], NULL); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 831 | } |
| 832 | else if (actions == ACTION_UNSET_ALL) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 833 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 834 | check_argc(argc, 1, 2); |
Patrick Steinhardt | 30598ad | 2016-02-22 12:23:35 +0100 | [diff] [blame] | 835 | return git_config_set_multivar_in_file_gently(given_config_source.file, |
| 836 | argv[0], NULL, argv[1], 1); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 837 | } |
| 838 | else if (actions == ACTION_RENAME_SECTION) { |
| 839 | int ret; |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 840 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 841 | check_argc(argc, 2, 2); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 842 | ret = git_config_rename_section_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 843 | argv[0], argv[1]); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 844 | if (ret < 0) |
| 845 | return ret; |
| 846 | if (ret == 0) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 847 | die(_("no such section: %s"), argv[0]); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 848 | } |
| 849 | else if (actions == ACTION_REMOVE_SECTION) { |
| 850 | int ret; |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 851 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 852 | check_argc(argc, 1, 1); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 853 | ret = git_config_rename_section_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 854 | argv[0], NULL); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 855 | if (ret < 0) |
| 856 | return ret; |
| 857 | if (ret == 0) |
Nguyễn Thái Ngọc Duy | 1d28ff4 | 2018-07-21 09:49:22 +0200 | [diff] [blame] | 858 | die(_("no such section: %s"), argv[0]); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 859 | } |
| 860 | else if (actions == ACTION_GET_COLOR) { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 861 | check_argc(argc, 1, 2); |
| 862 | get_color(argv[0], argv[1]); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 863 | } |
| 864 | else if (actions == ACTION_GET_COLORBOOL) { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 865 | check_argc(argc, 1, 2); |
| 866 | if (argc == 2) |
| 867 | color_stdout_is_tty = git_config_bool("command line", argv[1]); |
| 868 | return get_colorbool(argv[0], argc == 2); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 869 | } |
| 870 | |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 871 | return 0; |
| 872 | } |