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