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" |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 3 | #include "color.h" |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 4 | #include "parse-options.h" |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 5 | #include "urlmatch.h" |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 6 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 7 | static const char *const builtin_config_usage[] = { |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 8 | N_("git config [options]"), |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 9 | NULL |
| 10 | }; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 11 | |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 12 | static char *key; |
| 13 | static regex_t *key_regexp; |
| 14 | static regex_t *regexp; |
| 15 | static int show_keys; |
| 16 | static int use_key_regexp; |
| 17 | static int do_all; |
| 18 | static int do_not_match; |
Frank Lichtenheld | 2275d50 | 2007-06-25 16:03:55 +0200 | [diff] [blame] | 19 | static char delim = '='; |
| 20 | static char key_delim = ' '; |
| 21 | static char term = '\n'; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 22 | |
Sverre Rabbelier | 57210a6 | 2010-08-03 20:59:23 -0500 | [diff] [blame] | 23 | static int use_global_config, use_system_config, use_local_config; |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 24 | static struct git_config_source given_config_source; |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 25 | static int actions, types; |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 26 | static const char *get_color_slot, *get_colorbool_slot; |
| 27 | static int end_null; |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 28 | static int respect_includes = -1; |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 29 | |
| 30 | #define ACTION_GET (1<<0) |
| 31 | #define ACTION_GET_ALL (1<<1) |
| 32 | #define ACTION_GET_REGEXP (1<<2) |
| 33 | #define ACTION_REPLACE_ALL (1<<3) |
| 34 | #define ACTION_ADD (1<<4) |
| 35 | #define ACTION_UNSET (1<<5) |
| 36 | #define ACTION_UNSET_ALL (1<<6) |
| 37 | #define ACTION_RENAME_SECTION (1<<7) |
| 38 | #define ACTION_REMOVE_SECTION (1<<8) |
| 39 | #define ACTION_LIST (1<<9) |
| 40 | #define ACTION_EDIT (1<<10) |
| 41 | #define ACTION_SET (1<<11) |
| 42 | #define ACTION_SET_ALL (1<<12) |
| 43 | #define ACTION_GET_COLOR (1<<13) |
| 44 | #define ACTION_GET_COLORBOOL (1<<14) |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 45 | #define ACTION_GET_URLMATCH (1<<15) |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 46 | |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 47 | #define TYPE_BOOL (1<<0) |
| 48 | #define TYPE_INT (1<<1) |
| 49 | #define TYPE_BOOL_OR_INT (1<<2) |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 50 | #define TYPE_PATH (1<<3) |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 51 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 52 | static struct option builtin_config_options[] = { |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 53 | OPT_GROUP(N_("Config file location")), |
Stefan Beller | 21e047d | 2013-08-03 13:51:24 +0200 | [diff] [blame] | 54 | OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), |
| 55 | OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), |
| 56 | OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 57 | OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), |
| 58 | 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] | 59 | OPT_GROUP(N_("Action")), |
| 60 | OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET), |
| 61 | OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL), |
| 62 | OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP), |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 63 | OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 64 | OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL), |
Nguyễn Thái Ngọc Duy | f63cf8c | 2012-08-20 19:32:55 +0700 | [diff] [blame] | 65 | OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), |
| 66 | OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET), |
| 67 | OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 68 | OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), |
| 69 | OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), |
| 70 | OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), |
Nguyễn Thái Ngọc Duy | f63cf8c | 2012-08-20 19:32:55 +0700 | [diff] [blame] | 71 | OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 72 | OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR), |
| 73 | 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] | 74 | OPT_GROUP(N_("Type")), |
| 75 | OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL), |
| 76 | OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT), |
| 77 | OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), |
| 78 | OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH), |
| 79 | OPT_GROUP(N_("Other")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 80 | OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")), |
Nguyễn Thái Ngọc Duy | 1bd31ce | 2012-08-20 19:32:05 +0700 | [diff] [blame] | 81 | OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")), |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 82 | OPT_END(), |
| 83 | }; |
| 84 | |
| 85 | static void check_argc(int argc, int min, int max) { |
| 86 | if (argc >= min && argc <= max) |
| 87 | return; |
| 88 | error("wrong number of arguments"); |
| 89 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 90 | } |
| 91 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 92 | 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] | 93 | { |
| 94 | if (value_) |
Frank Lichtenheld | 2275d50 | 2007-06-25 16:03:55 +0200 | [diff] [blame] | 95 | printf("%s%c%s%c", key_, delim, value_, term); |
Petr Baudis | de791f1 | 2006-04-25 00:59:25 +0200 | [diff] [blame] | 96 | else |
Frank Lichtenheld | 2275d50 | 2007-06-25 16:03:55 +0200 | [diff] [blame] | 97 | printf("%s%c", key_, term); |
Petr Baudis | de791f1 | 2006-04-25 00:59:25 +0200 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 101 | struct strbuf_list { |
| 102 | struct strbuf *items; |
| 103 | int nr; |
| 104 | int alloc; |
| 105 | }; |
| 106 | |
Junio C Hamano | d9b9169 | 2013-07-29 14:23:16 -0700 | [diff] [blame] | 107 | 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] | 108 | { |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 109 | int must_free_vptr = 0; |
Matthieu Moy | 008e3cc | 2011-10-10 14:54:51 +0200 | [diff] [blame] | 110 | int must_print_delim = 0; |
Junio C Hamano | d9b9169 | 2013-07-29 14:23:16 -0700 | [diff] [blame] | 111 | char value[256]; |
| 112 | const char *vptr = value; |
Junio C Hamano | e098c6f | 2006-05-02 21:06:56 -0700 | [diff] [blame] | 113 | |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 114 | strbuf_init(buf, 0); |
| 115 | |
Frank Lichtenheld | b69ba46 | 2007-06-25 16:03:54 +0200 | [diff] [blame] | 116 | if (show_keys) { |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 117 | strbuf_addstr(buf, key_); |
Matthieu Moy | 008e3cc | 2011-10-10 14:54:51 +0200 | [diff] [blame] | 118 | must_print_delim = 1; |
Frank Lichtenheld | b69ba46 | 2007-06-25 16:03:54 +0200 | [diff] [blame] | 119 | } |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 120 | if (types == TYPE_INT) |
Jeff King | 0016024 | 2013-09-08 04:40:02 -0400 | [diff] [blame] | 121 | sprintf(value, "%"PRId64, |
| 122 | git_config_int64(key_, value_ ? value_ : "")); |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 123 | else if (types == TYPE_BOOL) |
Johannes Schindelin | 8f5ff31 | 2006-05-03 14:41:03 +0200 | [diff] [blame] | 124 | vptr = git_config_bool(key_, value_) ? "true" : "false"; |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 125 | else if (types == TYPE_BOOL_OR_INT) { |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 126 | int is_bool, v; |
| 127 | v = git_config_bool_or_int(key_, value_, &is_bool); |
| 128 | if (is_bool) |
| 129 | vptr = v ? "true" : "false"; |
| 130 | else |
| 131 | sprintf(value, "%d", v); |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 132 | } else if (types == TYPE_PATH) { |
Carlos Martín Nieto | 962c38e | 2012-11-15 10:10:01 -0800 | [diff] [blame] | 133 | if (git_config_pathname(&vptr, key_, value_) < 0) |
| 134 | return -1; |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 135 | must_free_vptr = 1; |
Matthieu Moy | 008e3cc | 2011-10-10 14:54:51 +0200 | [diff] [blame] | 136 | } else if (value_) { |
| 137 | vptr = value_; |
| 138 | } else { |
| 139 | /* Just show the key name */ |
| 140 | vptr = ""; |
| 141 | must_print_delim = 0; |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 142 | } |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 143 | |
| 144 | if (must_print_delim) |
| 145 | strbuf_addch(buf, key_delim); |
| 146 | strbuf_addstr(buf, vptr); |
| 147 | strbuf_addch(buf, term); |
| 148 | |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 149 | if (must_free_vptr) |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 150 | free((char *)vptr); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Junio C Hamano | d9b9169 | 2013-07-29 14:23:16 -0700 | [diff] [blame] | 154 | static int collect_config(const char *key_, const char *value_, void *cb) |
| 155 | { |
| 156 | struct strbuf_list *values = cb; |
| 157 | |
| 158 | if (!use_key_regexp && strcmp(key_, key)) |
| 159 | return 0; |
| 160 | if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) |
| 161 | return 0; |
| 162 | if (regexp != NULL && |
| 163 | (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) |
| 164 | return 0; |
| 165 | |
| 166 | ALLOC_GROW(values->items, values->nr + 1, values->alloc); |
| 167 | |
| 168 | return format_config(&values->items[values->nr++], key_, value_); |
| 169 | } |
| 170 | |
Felipe Contreras | 4b951b7 | 2009-02-21 02:48:53 +0200 | [diff] [blame] | 171 | static int get_value(const char *key_, const char *regex_) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 172 | { |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 173 | int ret = CONFIG_GENERIC_ERROR; |
Ramsay Jones | 5ba1a8a | 2012-10-28 21:05:25 +0000 | [diff] [blame] | 174 | struct strbuf_list values = {NULL}; |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 175 | int i; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 176 | |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 177 | if (use_key_regexp) { |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 178 | char *tl; |
| 179 | |
| 180 | /* |
| 181 | * NEEDSWORK: this naive pattern lowercasing obviously does not |
| 182 | * work for more complex patterns like "^[^.]*Foo.*bar". |
| 183 | * Perhaps we should deprecate this altogether someday. |
| 184 | */ |
| 185 | |
| 186 | key = xstrdup(key_); |
| 187 | for (tl = key + strlen(key) - 1; |
| 188 | tl >= key && *tl != '.'; |
| 189 | tl--) |
| 190 | *tl = tolower(*tl); |
| 191 | for (tl = key; *tl && *tl != '.'; tl++) |
| 192 | *tl = tolower(*tl); |
| 193 | |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 194 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 195 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
Junio C Hamano | e098c6f | 2006-05-02 21:06:56 -0700 | [diff] [blame] | 196 | fprintf(stderr, "Invalid key pattern: %s\n", key_); |
Jeff King | 97ed50f | 2012-10-23 15:40:06 -0400 | [diff] [blame] | 197 | free(key_regexp); |
| 198 | key_regexp = NULL; |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 199 | ret = CONFIG_INVALID_PATTERN; |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 200 | goto free_strings; |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 201 | } |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 202 | } else { |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 203 | if (git_config_parse_key(key_, &key, NULL)) { |
| 204 | ret = CONFIG_INVALID_KEY; |
Libor Pechacek | b09c53a | 2011-01-30 20:40:41 +0100 | [diff] [blame] | 205 | goto free_strings; |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 206 | } |
Johannes Schindelin | 2fa9a0f | 2006-05-02 14:22:48 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 209 | if (regex_) { |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 210 | if (regex_[0] == '!') { |
| 211 | do_not_match = 1; |
| 212 | regex_++; |
| 213 | } |
| 214 | |
Jonas Fonseca | 2d7320d | 2006-09-01 00:32:39 +0200 | [diff] [blame] | 215 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 216 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 217 | fprintf(stderr, "Invalid pattern: %s\n", regex_); |
Jeff King | 97ed50f | 2012-10-23 15:40:06 -0400 | [diff] [blame] | 218 | free(regexp); |
| 219 | regexp = NULL; |
Junio C Hamano | 9409c7a | 2012-07-29 13:43:21 -0700 | [diff] [blame] | 220 | ret = CONFIG_INVALID_PATTERN; |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 221 | goto free_strings; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Jeff King | e895589 | 2012-10-23 18:00:41 -0400 | [diff] [blame] | 225 | git_config_with_options(collect_config, &values, |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 226 | &given_config_source, respect_includes); |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 227 | |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 228 | ret = !values.nr; |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 229 | |
| 230 | for (i = 0; i < values.nr; i++) { |
| 231 | struct strbuf *buf = values.items + i; |
Jeff King | 00b347d | 2012-10-23 16:52:44 -0400 | [diff] [blame] | 232 | if (do_all || i == values.nr - 1) |
| 233 | fwrite(buf->buf, 1, buf->len, stdout); |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 234 | strbuf_release(buf); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 235 | } |
Jeff King | 7acdd6f | 2012-10-23 15:51:50 -0400 | [diff] [blame] | 236 | free(values.items); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 237 | |
Jeff King | 97ed50f | 2012-10-23 15:40:06 -0400 | [diff] [blame] | 238 | free_strings: |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 239 | free(key); |
Jeff King | 35998c8 | 2012-10-23 15:36:12 -0400 | [diff] [blame] | 240 | if (key_regexp) { |
| 241 | regfree(key_regexp); |
| 242 | free(key_regexp); |
| 243 | } |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 244 | if (regexp) { |
| 245 | regfree(regexp); |
| 246 | free(regexp); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 247 | } |
| 248 | |
Johannes Schindelin | 5f1a63e | 2006-06-20 01:48:03 +0200 | [diff] [blame] | 249 | return ret; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 250 | } |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 251 | |
Stephan Beyer | 186458b | 2008-07-24 01:09:35 +0200 | [diff] [blame] | 252 | static char *normalize_value(const char *key, const char *value) |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 253 | { |
| 254 | char *normalized; |
| 255 | |
| 256 | if (!value) |
| 257 | return NULL; |
| 258 | |
Matthieu Moy | 1349484 | 2009-12-30 17:51:53 +0100 | [diff] [blame] | 259 | if (types == 0 || types == TYPE_PATH) |
| 260 | /* |
| 261 | * We don't do normalization for TYPE_PATH here: If |
| 262 | * the path is like ~/foobar/, we prefer to store |
| 263 | * "~/foobar/" in the config file, and to expand the ~ |
| 264 | * when retrieving the value. |
| 265 | */ |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 266 | normalized = xstrdup(value); |
| 267 | else { |
| 268 | normalized = xmalloc(64); |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 269 | if (types == TYPE_INT) { |
Jeff King | 0016024 | 2013-09-08 04:40:02 -0400 | [diff] [blame] | 270 | int64_t v = git_config_int64(key, value); |
| 271 | sprintf(normalized, "%"PRId64, v); |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 272 | } |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 273 | else if (types == TYPE_BOOL) |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 274 | sprintf(normalized, "%s", |
| 275 | git_config_bool(key, value) ? "true" : "false"); |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 276 | else if (types == TYPE_BOOL_OR_INT) { |
Junio C Hamano | c35b0b5 | 2008-04-13 12:11:11 -0700 | [diff] [blame] | 277 | int is_bool, v; |
| 278 | v = git_config_bool_or_int(key, value, &is_bool); |
| 279 | if (!is_bool) |
| 280 | sprintf(normalized, "%d", v); |
| 281 | else |
| 282 | sprintf(normalized, "%s", v ? "true" : "false"); |
| 283 | } |
Frank Lichtenheld | db1696b | 2007-06-25 16:00:24 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | return normalized; |
| 287 | } |
| 288 | |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 289 | static int get_color_found; |
| 290 | static const char *get_color_slot; |
Felipe Contreras | b408457 | 2009-02-21 02:48:56 +0200 | [diff] [blame] | 291 | static const char *get_colorbool_slot; |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 292 | static char parsed_color[COLOR_MAXLEN]; |
| 293 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 294 | 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] | 295 | { |
| 296 | if (!strcmp(var, get_color_slot)) { |
Junio C Hamano | f769982 | 2008-02-11 10:48:12 -0800 | [diff] [blame] | 297 | if (!value) |
| 298 | config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 299 | if (color_parse(value, parsed_color) < 0) |
| 300 | return -1; |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 301 | get_color_found = 1; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 306 | static void get_color(const char *var, const char *def_color) |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 307 | { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 308 | get_color_slot = var; |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 309 | get_color_found = 0; |
| 310 | parsed_color[0] = '\0'; |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 311 | git_config_with_options(git_get_color_config, NULL, |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 312 | &given_config_source, respect_includes); |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 313 | |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 314 | if (!get_color_found && def_color) { |
| 315 | if (color_parse(def_color, parsed_color) < 0) |
| 316 | die(_("unable to parse default color value")); |
| 317 | } |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 318 | |
| 319 | fputs(parsed_color, stdout); |
Junio C Hamano | 9ce0352 | 2007-11-27 22:41:05 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 322 | static int get_colorbool_found; |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 323 | static int get_diff_color_found; |
Jeff King | c659f55 | 2011-08-17 22:04:56 -0700 | [diff] [blame] | 324 | static int get_color_ui_found; |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 325 | static int git_get_colorbool_config(const char *var, const char *value, |
| 326 | void *cb) |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 327 | { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 328 | if (!strcmp(var, get_colorbool_slot)) |
| 329 | get_colorbool_found = git_config_colorbool(var, value); |
| 330 | else if (!strcmp(var, "diff.color")) |
| 331 | get_diff_color_found = git_config_colorbool(var, value); |
| 332 | else if (!strcmp(var, "color.ui")) |
Jeff King | c659f55 | 2011-08-17 22:04:56 -0700 | [diff] [blame] | 333 | get_color_ui_found = git_config_colorbool(var, value); |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 337 | static int get_colorbool(const char *var, int print) |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 338 | { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 339 | get_colorbool_slot = var; |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 340 | get_colorbool_found = -1; |
| 341 | get_diff_color_found = -1; |
Matthieu Moy | b8612b4 | 2013-05-15 19:00:55 +0200 | [diff] [blame] | 342 | get_color_ui_found = -1; |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 343 | git_config_with_options(git_get_colorbool_config, NULL, |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 344 | &given_config_source, respect_includes); |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 345 | |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 346 | if (get_colorbool_found < 0) { |
Felipe Contreras | b408457 | 2009-02-21 02:48:56 +0200 | [diff] [blame] | 347 | if (!strcmp(get_colorbool_slot, "color.diff")) |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 348 | get_colorbool_found = get_diff_color_found; |
| 349 | if (get_colorbool_found < 0) |
Jeff King | c659f55 | 2011-08-17 22:04:56 -0700 | [diff] [blame] | 350 | get_colorbool_found = get_color_ui_found; |
Junio C Hamano | 69243c2 | 2007-12-05 22:12:07 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Matthieu Moy | b8612b4 | 2013-05-15 19:00:55 +0200 | [diff] [blame] | 353 | if (get_colorbool_found < 0) |
| 354 | /* default value if none found in config */ |
Matthieu Moy | 4c7f181 | 2013-06-10 16:26:09 +0200 | [diff] [blame] | 355 | get_colorbool_found = GIT_COLOR_AUTO; |
Matthieu Moy | b8612b4 | 2013-05-15 19:00:55 +0200 | [diff] [blame] | 356 | |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 357 | get_colorbool_found = want_color(get_colorbool_found); |
| 358 | |
Felipe Contreras | 0e854a2 | 2009-02-21 02:48:57 +0200 | [diff] [blame] | 359 | if (print) { |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 360 | printf("%s\n", get_colorbool_found ? "true" : "false"); |
| 361 | return 0; |
Felipe Contreras | 0e854a2 | 2009-02-21 02:48:57 +0200 | [diff] [blame] | 362 | } else |
| 363 | return get_colorbool_found ? 0 : 1; |
Junio C Hamano | 0f6f5a4 | 2007-12-05 17:26:11 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 366 | static void check_write(void) |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 367 | { |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 368 | if (given_config_source.use_stdin) |
| 369 | die("writing to stdin is not supported"); |
| 370 | |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 371 | if (given_config_source.blob) |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 372 | die("writing config blobs is not supported"); |
| 373 | } |
| 374 | |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 375 | struct urlmatch_current_candidate_value { |
| 376 | char value_is_null; |
| 377 | struct strbuf value; |
| 378 | }; |
| 379 | |
| 380 | static int urlmatch_collect_fn(const char *var, const char *value, void *cb) |
| 381 | { |
| 382 | struct string_list *values = cb; |
| 383 | struct string_list_item *item = string_list_insert(values, var); |
| 384 | struct urlmatch_current_candidate_value *matched = item->util; |
| 385 | |
| 386 | if (!matched) { |
| 387 | matched = xmalloc(sizeof(*matched)); |
| 388 | strbuf_init(&matched->value, 0); |
| 389 | item->util = matched; |
| 390 | } else { |
| 391 | strbuf_reset(&matched->value); |
| 392 | } |
| 393 | |
| 394 | if (value) { |
| 395 | strbuf_addstr(&matched->value, value); |
| 396 | matched->value_is_null = 0; |
| 397 | } else { |
| 398 | matched->value_is_null = 1; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 403 | static int get_urlmatch(const char *var, const char *url) |
| 404 | { |
| 405 | char *section_tail; |
| 406 | struct string_list_item *item; |
| 407 | struct urlmatch_config config = { STRING_LIST_INIT_DUP }; |
| 408 | struct string_list values = STRING_LIST_INIT_DUP; |
| 409 | |
| 410 | config.collect_fn = urlmatch_collect_fn; |
| 411 | config.cascade_fn = NULL; |
| 412 | config.cb = &values; |
| 413 | |
| 414 | if (!url_normalize(url, &config.url)) |
Junio C Hamano | 6667a6a | 2013-08-08 21:41:44 -0700 | [diff] [blame] | 415 | die("%s", config.url.err); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 416 | |
Jeff King | 88d5a6f | 2014-05-22 05:44:09 -0400 | [diff] [blame] | 417 | config.section = xstrdup_tolower(var); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 418 | section_tail = strchr(config.section, '.'); |
| 419 | if (section_tail) { |
| 420 | *section_tail = '\0'; |
| 421 | config.key = section_tail + 1; |
| 422 | show_keys = 0; |
| 423 | } else { |
| 424 | config.key = NULL; |
| 425 | show_keys = 1; |
| 426 | } |
| 427 | |
| 428 | git_config_with_options(urlmatch_config_entry, &config, |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 429 | &given_config_source, respect_includes); |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 430 | |
| 431 | for_each_string_list_item(item, &values) { |
| 432 | struct urlmatch_current_candidate_value *matched = item->util; |
| 433 | struct strbuf key = STRBUF_INIT; |
| 434 | struct strbuf buf = STRBUF_INIT; |
| 435 | |
| 436 | strbuf_addstr(&key, item->string); |
| 437 | format_config(&buf, key.buf, |
| 438 | matched->value_is_null ? NULL : matched->value.buf); |
| 439 | fwrite(buf.buf, 1, buf.len, stdout); |
| 440 | strbuf_release(&key); |
| 441 | strbuf_release(&buf); |
| 442 | |
| 443 | strbuf_release(&matched->value); |
| 444 | } |
| 445 | string_list_clear(&config.vars, 1); |
| 446 | string_list_clear(&values, 1); |
| 447 | free(config.url.url); |
| 448 | |
| 449 | free((void *)config.section); |
| 450 | return 0; |
| 451 | } |
| 452 | |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 453 | static char *default_user_config(void) |
| 454 | { |
| 455 | struct strbuf buf = STRBUF_INIT; |
| 456 | strbuf_addf(&buf, |
| 457 | _("# This is Git's per-user configuration file.\n" |
| 458 | "[core]\n" |
| 459 | "# Please adapt and uncomment the following lines:\n" |
| 460 | "# user = %s\n" |
| 461 | "# email = %s\n"), |
| 462 | ident_default_name(), |
| 463 | ident_default_email()); |
| 464 | return strbuf_detach(&buf, NULL); |
| 465 | } |
| 466 | |
Nguyễn Thái Ngọc Duy | 3ba7e6e | 2010-08-05 22:15:09 -0500 | [diff] [blame] | 467 | int cmd_config(int argc, const char **argv, const char *prefix) |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 468 | { |
Nguyễn Thái Ngọc Duy | 3ba7e6e | 2010-08-05 22:15:09 -0500 | [diff] [blame] | 469 | int nongit = !startup_info->have_repository; |
Felipe Contreras | 4b951b7 | 2009-02-21 02:48:53 +0200 | [diff] [blame] | 470 | char *value; |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 471 | |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 472 | given_config_source.file = getenv(CONFIG_ENVIRONMENT); |
Daniel Barkalow | dc87183 | 2008-06-30 03:37:47 -0400 | [diff] [blame] | 473 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 474 | argc = parse_options(argc, argv, prefix, builtin_config_options, |
| 475 | builtin_config_usage, |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 476 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 477 | |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 478 | if (use_global_config + use_system_config + use_local_config + |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 479 | !!given_config_source.file + !!given_config_source.blob > 1) { |
Felipe Contreras | 67052c9 | 2009-02-21 02:49:26 +0200 | [diff] [blame] | 480 | error("only one config file at a time."); |
| 481 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 482 | } |
| 483 | |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 484 | if (given_config_source.file && |
| 485 | !strcmp(given_config_source.file, "-")) { |
| 486 | given_config_source.file = NULL; |
| 487 | given_config_source.use_stdin = 1; |
| 488 | } |
| 489 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 490 | if (use_global_config) { |
Huynh Khoi Nguyen Nguyen | 21cf322 | 2012-06-22 11:03:23 +0200 | [diff] [blame] | 491 | char *user_config = NULL; |
| 492 | char *xdg_config = NULL; |
| 493 | |
| 494 | home_config_paths(&user_config, &xdg_config, "config"); |
| 495 | |
Matthieu Moy | e3ebc35 | 2012-07-12 14:04:20 +0200 | [diff] [blame] | 496 | if (!user_config) |
| 497 | /* |
| 498 | * It is unknown if HOME/.gitconfig exists, so |
| 499 | * we do not know if we should write to XDG |
| 500 | * location; error out even if XDG_CONFIG_HOME |
| 501 | * is set and points at a sane location. |
| 502 | */ |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 503 | die("$HOME not set"); |
Matthieu Moy | e3ebc35 | 2012-07-12 14:04:20 +0200 | [diff] [blame] | 504 | |
Jonathan Nieder | 4698c8f | 2013-04-12 14:03:18 -0700 | [diff] [blame] | 505 | if (access_or_warn(user_config, R_OK, 0) && |
| 506 | xdg_config && !access_or_warn(xdg_config, R_OK, 0)) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 507 | given_config_source.file = xdg_config; |
Matthieu Moy | e3ebc35 | 2012-07-12 14:04:20 +0200 | [diff] [blame] | 508 | else |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 509 | given_config_source.file = user_config; |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 510 | } |
| 511 | else if (use_system_config) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 512 | given_config_source.file = git_etc_gitconfig(); |
Sverre Rabbelier | 57210a6 | 2010-08-03 20:59:23 -0500 | [diff] [blame] | 513 | else if (use_local_config) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 514 | given_config_source.file = git_pathdup("config"); |
| 515 | else if (given_config_source.file) { |
| 516 | if (!is_absolute_path(given_config_source.file) && prefix) |
| 517 | given_config_source.file = |
Jeff King | 839de25 | 2012-02-16 03:03:52 -0500 | [diff] [blame] | 518 | xstrdup(prefix_filename(prefix, |
| 519 | strlen(prefix), |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 520 | given_config_source.file)); |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 523 | if (respect_includes == -1) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 524 | respect_includes = !given_config_source.file; |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 525 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 526 | if (end_null) { |
| 527 | term = '\0'; |
| 528 | delim = '\n'; |
| 529 | key_delim = '\n'; |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 530 | } |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 531 | |
Felipe Contreras | 16c1e93 | 2009-02-21 02:49:27 +0200 | [diff] [blame] | 532 | if (HAS_MULTI_BITS(types)) { |
| 533 | error("only one type at a time."); |
| 534 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 535 | } |
| 536 | |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 537 | if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) { |
Felipe Contreras | c238735 | 2009-02-21 02:49:29 +0200 | [diff] [blame] | 538 | error("--get-color and variable type are incoherent"); |
| 539 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 540 | } |
| 541 | |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 542 | if (HAS_MULTI_BITS(actions)) { |
| 543 | error("only one action at a time."); |
| 544 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 545 | } |
| 546 | if (actions == 0) |
| 547 | switch (argc) { |
| 548 | case 1: actions = ACTION_GET; break; |
| 549 | case 2: actions = ACTION_SET; break; |
| 550 | case 3: actions = ACTION_SET_ALL; break; |
| 551 | default: |
| 552 | usage_with_options(builtin_config_usage, builtin_config_options); |
| 553 | } |
| 554 | |
| 555 | if (actions == ACTION_LIST) { |
Felipe Contreras | 225a9ca | 2009-02-21 02:49:28 +0200 | [diff] [blame] | 556 | check_argc(argc, 0, 0); |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 557 | if (git_config_with_options(show_all_config, NULL, |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 558 | &given_config_source, |
Jeff King | 9b25a0b | 2012-02-06 04:54:04 -0500 | [diff] [blame] | 559 | respect_includes) < 0) { |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 560 | if (given_config_source.file) |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 561 | die_errno("unable to read config file '%s'", |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 562 | given_config_source.file); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 563 | else |
| 564 | die("error processing config file(s)"); |
| 565 | } |
| 566 | } |
| 567 | else if (actions == ACTION_EDIT) { |
Michael Haggerty | 3696a7c | 2014-11-16 08:37:44 +0100 | [diff] [blame] | 568 | char *config_file; |
| 569 | |
Felipe Contreras | 225a9ca | 2009-02-21 02:49:28 +0200 | [diff] [blame] | 570 | check_argc(argc, 0, 0); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 571 | if (!given_config_source.file && nongit) |
Felipe Contreras | d212ca1 | 2009-04-30 01:49:47 +0300 | [diff] [blame] | 572 | die("not in a git directory"); |
Kirill A. Shutemov | 3caec73 | 2014-02-19 00:58:55 +0200 | [diff] [blame] | 573 | if (given_config_source.use_stdin) |
| 574 | die("editing stdin is not supported"); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 575 | if (given_config_source.blob) |
Heiko Voigt | 1bc8881 | 2013-07-12 00:46:47 +0200 | [diff] [blame] | 576 | die("editing blobs is not supported"); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 577 | git_config(git_default_config, NULL); |
Michael Haggerty | 3696a7c | 2014-11-16 08:37:44 +0100 | [diff] [blame] | 578 | config_file = xstrdup(given_config_source.file ? |
| 579 | given_config_source.file : git_path("config")); |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 580 | if (use_global_config) { |
| 581 | int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); |
| 582 | if (fd) { |
| 583 | char *content = default_user_config(); |
| 584 | write_str_in_full(fd, content); |
| 585 | free(content); |
| 586 | close(fd); |
| 587 | } |
| 588 | else if (errno != EEXIST) |
| 589 | die_errno(_("cannot create configuration file %s"), config_file); |
| 590 | } |
| 591 | launch_editor(config_file, NULL, NULL); |
Michael Haggerty | 3696a7c | 2014-11-16 08:37:44 +0100 | [diff] [blame] | 592 | free(config_file); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 593 | } |
| 594 | else if (actions == ACTION_SET) { |
Michael J Gruber | 5a2df36 | 2011-05-17 17:38:53 +0200 | [diff] [blame] | 595 | int ret; |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 596 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 597 | check_argc(argc, 2, 2); |
| 598 | value = normalize_value(argv[0], argv[1]); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 599 | ret = git_config_set_in_file(given_config_source.file, argv[0], value); |
Michael J Gruber | 5a2df36 | 2011-05-17 17:38:53 +0200 | [diff] [blame] | 600 | if (ret == CONFIG_NOTHING_SET) |
| 601 | error("cannot overwrite multiple values with a single value\n" |
Jelmer Vernooij | 67e223e | 2011-12-27 03:03:45 +0100 | [diff] [blame] | 602 | " 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] | 603 | return ret; |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 604 | } |
| 605 | else if (actions == ACTION_SET_ALL) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 606 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 607 | check_argc(argc, 2, 3); |
| 608 | value = normalize_value(argv[0], argv[1]); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 609 | return git_config_set_multivar_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 610 | argv[0], value, argv[2], 0); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 611 | } |
| 612 | else if (actions == ACTION_ADD) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 613 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 614 | check_argc(argc, 2, 2); |
| 615 | value = normalize_value(argv[0], argv[1]); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 616 | return git_config_set_multivar_in_file(given_config_source.file, |
Jeff King | c1063be | 2014-08-19 02:20:00 -0400 | [diff] [blame] | 617 | argv[0], value, |
| 618 | CONFIG_REGEX_NONE, 0); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 619 | } |
| 620 | else if (actions == ACTION_REPLACE_ALL) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 621 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 622 | check_argc(argc, 2, 3); |
| 623 | value = normalize_value(argv[0], argv[1]); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 624 | return git_config_set_multivar_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 625 | argv[0], value, argv[2], 1); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 626 | } |
| 627 | else if (actions == ACTION_GET) { |
| 628 | check_argc(argc, 1, 2); |
| 629 | return get_value(argv[0], argv[1]); |
| 630 | } |
| 631 | else if (actions == ACTION_GET_ALL) { |
| 632 | do_all = 1; |
| 633 | check_argc(argc, 1, 2); |
| 634 | return get_value(argv[0], argv[1]); |
| 635 | } |
| 636 | else if (actions == ACTION_GET_REGEXP) { |
| 637 | show_keys = 1; |
| 638 | use_key_regexp = 1; |
| 639 | do_all = 1; |
| 640 | check_argc(argc, 1, 2); |
| 641 | return get_value(argv[0], argv[1]); |
| 642 | } |
Junio C Hamano | d477096 | 2013-07-31 11:14:59 -0700 | [diff] [blame] | 643 | else if (actions == ACTION_GET_URLMATCH) { |
| 644 | check_argc(argc, 2, 2); |
| 645 | return get_urlmatch(argv[0], argv[1]); |
| 646 | } |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 647 | else if (actions == ACTION_UNSET) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 648 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 649 | check_argc(argc, 1, 2); |
| 650 | if (argc == 2) |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 651 | return git_config_set_multivar_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 652 | argv[0], NULL, argv[1], 0); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 653 | else |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 654 | return git_config_set_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 655 | argv[0], NULL); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 656 | } |
| 657 | else if (actions == ACTION_UNSET_ALL) { |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 658 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 659 | check_argc(argc, 1, 2); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 660 | return git_config_set_multivar_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 661 | argv[0], NULL, argv[1], 1); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 662 | } |
| 663 | else if (actions == ACTION_RENAME_SECTION) { |
| 664 | int ret; |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 665 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 666 | check_argc(argc, 2, 2); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 667 | ret = git_config_rename_section_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 668 | argv[0], argv[1]); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 669 | if (ret < 0) |
| 670 | return ret; |
| 671 | if (ret == 0) |
| 672 | die("No such section!"); |
| 673 | } |
| 674 | else if (actions == ACTION_REMOVE_SECTION) { |
| 675 | int ret; |
Kirill A. Shutemov | 6aea9f0 | 2014-02-19 00:58:53 +0200 | [diff] [blame] | 676 | check_write(); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 677 | check_argc(argc, 1, 1); |
Kirill A. Shutemov | c8985ce | 2014-02-19 00:58:54 +0200 | [diff] [blame] | 678 | ret = git_config_rename_section_in_file(given_config_source.file, |
Jeff King | 270a344 | 2012-02-16 03:07:32 -0500 | [diff] [blame] | 679 | argv[0], NULL); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 680 | if (ret < 0) |
| 681 | return ret; |
| 682 | if (ret == 0) |
| 683 | die("No such section!"); |
| 684 | } |
| 685 | else if (actions == ACTION_GET_COLOR) { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 686 | check_argc(argc, 1, 2); |
| 687 | get_color(argv[0], argv[1]); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 688 | } |
| 689 | else if (actions == ACTION_GET_COLORBOOL) { |
Jeff King | d0e08d6 | 2014-11-20 10:15:51 -0500 | [diff] [blame] | 690 | check_argc(argc, 1, 2); |
| 691 | if (argc == 2) |
| 692 | color_stdout_is_tty = git_config_bool("command line", argv[1]); |
| 693 | return get_colorbool(argv[0], argc == 2); |
Felipe Contreras | d64ec16 | 2009-02-21 02:49:25 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 696 | return 0; |
| 697 | } |