blob: dfa403b94baea97c655ab707d75e80f58f318c51 [file] [log] [blame]
Matthias Kestenholze12c0952006-08-02 23:51:59 +02001#include "builtin.h"
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +01002#include "cache.h"
3
4static const char git_config_set_usage[] =
Paolo Bonzini118f8b22007-03-02 21:53:33 +01005"git-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list";
Johannes Schindelin4ddba792005-11-20 06:52:22 +01006
David Rientjes96f1e582006-08-15 10:23:48 -07007static char *key;
8static regex_t *key_regexp;
9static regex_t *regexp;
10static int show_keys;
11static int use_key_regexp;
12static int do_all;
13static int do_not_match;
14static int seen;
Petr Baudis7162dff2006-02-12 04:14:48 +010015static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
Johannes Schindelin4ddba792005-11-20 06:52:22 +010016
Petr Baudisde791f12006-04-25 00:59:25 +020017static int show_all_config(const char *key_, const char *value_)
18{
19 if (value_)
20 printf("%s=%s\n", key_, value_);
21 else
22 printf("%s\n", key_);
23 return 0;
24}
25
Johannes Schindelin4ddba792005-11-20 06:52:22 +010026static int show_config(const char* key_, const char* value_)
27{
Junio C Hamanoe098c6f2006-05-02 21:06:56 -070028 char value[256];
29 const char *vptr = value;
Johannes Schindelin8f5ff312006-05-03 14:41:03 +020030 int dup_error = 0;
Junio C Hamanoe098c6f2006-05-02 21:06:56 -070031
Johannes Schindelin8f5ff312006-05-03 14:41:03 +020032 if (!use_key_regexp && strcmp(key_, key))
33 return 0;
34 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
35 return 0;
36 if (regexp != NULL &&
Johannes Schindelinf98d8632005-11-20 13:24:18 +010037 (do_not_match ^
Junio C Hamanoacb70142006-06-24 05:19:30 -070038 regexec(regexp, (value_?value_:""), 0, NULL, 0)))
Johannes Schindelin8f5ff312006-05-03 14:41:03 +020039 return 0;
40
41 if (show_keys)
42 printf("%s ", key_);
43 if (seen && !do_all)
44 dup_error = 1;
45 if (type == T_INT)
Junio C Hamanoacb70142006-06-24 05:19:30 -070046 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
Johannes Schindelin8f5ff312006-05-03 14:41:03 +020047 else if (type == T_BOOL)
48 vptr = git_config_bool(key_, value_) ? "true" : "false";
49 else
Junio C Hamanoacb70142006-06-24 05:19:30 -070050 vptr = value_?value_:"";
Johannes Schindelin8f5ff312006-05-03 14:41:03 +020051 seen++;
52 if (dup_error) {
53 error("More than one value for the key %s: %s",
54 key_, vptr);
Johannes Schindelin4ddba792005-11-20 06:52:22 +010055 }
Johannes Schindelin8f5ff312006-05-03 14:41:03 +020056 else
57 printf("%s\n", vptr);
58
Johannes Schindelin4ddba792005-11-20 06:52:22 +010059 return 0;
60}
61
62static int get_value(const char* key_, const char* regex_)
63{
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020064 int ret = -1;
Linus Torvaldsd14f7762006-05-09 12:24:02 -070065 char *tl;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020066 char *global = NULL, *repo_config = NULL;
Johannes Schindelin32043c92007-02-14 12:48:14 +010067 const char *system_wide = NULL, *local;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020068
Junio C Hamanod4ebc362006-12-19 01:28:15 -080069 local = getenv(CONFIG_ENVIRONMENT);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020070 if (!local) {
71 const char *home = getenv("HOME");
Junio C Hamanod4ebc362006-12-19 01:28:15 -080072 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020073 if (!local)
Shawn Pearce9befac42006-09-02 00:16:31 -040074 local = repo_config = xstrdup(git_path("config"));
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020075 if (home)
Shawn Pearce9befac42006-09-02 00:16:31 -040076 global = xstrdup(mkpath("%s/.gitconfig", home));
Johannes Schindelin32043c92007-02-14 12:48:14 +010077 system_wide = ETC_GITCONFIG;
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020078 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +010079
Shawn Pearce9befac42006-09-02 00:16:31 -040080 key = xstrdup(key_);
Linus Torvaldsd14f7762006-05-09 12:24:02 -070081 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
82 *tl = tolower(*tl);
83 for (tl=key; *tl && *tl != '.'; ++tl)
84 *tl = tolower(*tl);
Johannes Schindelin4ddba792005-11-20 06:52:22 +010085
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +020086 if (use_key_regexp) {
Jonas Fonseca2d7320d2006-09-01 00:32:39 +020087 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +020088 if (regcomp(key_regexp, key, REG_EXTENDED)) {
Junio C Hamanoe098c6f2006-05-02 21:06:56 -070089 fprintf(stderr, "Invalid key pattern: %s\n", key_);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020090 goto free_strings;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +020091 }
92 }
93
Johannes Schindelin4ddba792005-11-20 06:52:22 +010094 if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +010095 if (regex_[0] == '!') {
96 do_not_match = 1;
97 regex_++;
98 }
99
Jonas Fonseca2d7320d2006-09-01 00:32:39 +0200100 regexp = (regex_t*)xmalloc(sizeof(regex_t));
Amos Waterland0a152172006-01-04 19:31:02 -0500101 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100102 fprintf(stderr, "Invalid pattern: %s\n", regex_);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200103 goto free_strings;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100104 }
105 }
106
Johannes Schindelin32043c92007-02-14 12:48:14 +0100107 if (do_all && system_wide)
108 git_config_from_file(show_config, system_wide);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200109 if (do_all && global)
110 git_config_from_file(show_config, global);
111 git_config_from_file(show_config, local);
112 if (!do_all && !seen && global)
113 git_config_from_file(show_config, global);
Johannes Schindelin32043c92007-02-14 12:48:14 +0100114 if (!do_all && !seen && system_wide)
115 git_config_from_file(show_config, system_wide);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200116
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100117 free(key);
Amos Waterland0a152172006-01-04 19:31:02 -0500118 if (regexp) {
119 regfree(regexp);
120 free(regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100121 }
122
123 if (do_all)
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200124 ret = !seen;
125 else
Petr Baudisdc2613d2006-07-03 22:47:55 +0200126 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100127
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200128free_strings:
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700129 free(repo_config);
130 free(global);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200131 return ret;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100132}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100133
Tom Princee0d10e12007-01-28 16:16:53 -0800134int cmd_config(int argc, const char **argv, const char *prefix)
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100135{
Linus Torvalds4d599e62006-05-25 08:22:42 -0700136 int nongit = 0;
137 setup_git_directory_gently(&nongit);
Petr Baudis7162dff2006-02-12 04:14:48 +0100138
139 while (1 < argc) {
140 if (!strcmp(argv[1], "--int"))
141 type = T_INT;
142 else if (!strcmp(argv[1], "--bool"))
143 type = T_BOOL;
Johannes Schindelincfa24e12006-05-02 12:54:12 -0700144 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
145 return git_config(show_all_config);
Sean34eb3342006-11-02 10:44:20 -0500146 else if (!strcmp(argv[1], "--global")) {
147 char *home = getenv("HOME");
148 if (home) {
149 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
150 setenv("GIT_CONFIG", user_config, 1);
151 free(user_config);
152 } else {
153 die("$HOME not set");
154 }
Johannes Schindelin32043c92007-02-14 12:48:14 +0100155 }
156 else if (!strcmp(argv[1], "--system"))
157 setenv("GIT_CONFIG", ETC_GITCONFIG, 1);
158 else if (!strcmp(argv[1], "--rename-section")) {
Johannes Schindelin0667fcf2006-12-16 15:14:14 +0100159 int ret;
160 if (argc != 4)
161 usage(git_config_set_usage);
162 ret = git_config_rename_section(argv[2], argv[3]);
163 if (ret < 0)
164 return ret;
165 if (ret == 0) {
166 fprintf(stderr, "No such section!\n");
167 return 1;
168 }
169 return 0;
Johannes Schindelin32043c92007-02-14 12:48:14 +0100170 }
Paolo Bonzini118f8b22007-03-02 21:53:33 +0100171 else if (!strcmp(argv[1], "--remove-section")) {
172 int ret;
173 if (argc != 3)
174 usage(git_config_set_usage);
175 ret = git_config_rename_section(argv[2], NULL);
176 if (ret < 0)
177 return ret;
178 if (ret == 0) {
179 fprintf(stderr, "No such section!\n");
180 return 1;
181 }
182 return 0;
183 }
Johannes Schindelin32043c92007-02-14 12:48:14 +0100184 else
Petr Baudis7162dff2006-02-12 04:14:48 +0100185 break;
186 argc--;
187 argv++;
188 }
189
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100190 switch (argc) {
191 case 2:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100192 return get_value(argv[1], NULL);
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100193 case 3:
194 if (!strcmp(argv[1], "--unset"))
195 return git_config_set(argv[2], NULL);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100196 else if (!strcmp(argv[1], "--unset-all"))
197 return git_config_set_multivar(argv[2], NULL, NULL, 1);
198 else if (!strcmp(argv[1], "--get"))
199 return get_value(argv[2], NULL);
200 else if (!strcmp(argv[1], "--get-all")) {
201 do_all = 1;
202 return get_value(argv[2], NULL);
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200203 } else if (!strcmp(argv[1], "--get-regexp")) {
204 show_keys = 1;
205 use_key_regexp = 1;
206 do_all = 1;
207 return get_value(argv[2], NULL);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100208 } else
209
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100210 return git_config_set(argv[1], argv[2]);
211 case 4:
212 if (!strcmp(argv[1], "--unset"))
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100213 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
214 else if (!strcmp(argv[1], "--unset-all"))
215 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
216 else if (!strcmp(argv[1], "--get"))
217 return get_value(argv[2], argv[3]);
218 else if (!strcmp(argv[1], "--get-all")) {
219 do_all = 1;
220 return get_value(argv[2], argv[3]);
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200221 } else if (!strcmp(argv[1], "--get-regexp")) {
222 show_keys = 1;
223 use_key_regexp = 1;
224 do_all = 1;
225 return get_value(argv[2], argv[3]);
Brian Gernhardt89c4afe2006-12-15 07:39:04 -0500226 } else if (!strcmp(argv[1], "--add"))
227 return git_config_set_multivar(argv[2], argv[3], "^$", 0);
228 else if (!strcmp(argv[1], "--replace-all"))
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100229
230 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100231 else
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100232
233 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
234 case 5:
235 if (!strcmp(argv[1], "--replace-all"))
236 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
237 case 1:
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100238 default:
239 usage(git_config_set_usage);
240 }
241 return 0;
242}