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