blob: 0f9051da17c6e399709bd476ab867f4505ecba63 [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[] =
Tom Princee0d10e12007-01-28 16:16:53 -08005"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 | --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;
67 const char *local;
68
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 Schindelin5f1a63e2006-06-20 01:48:03 +020077 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +010078
Shawn Pearce9befac42006-09-02 00:16:31 -040079 key = xstrdup(key_);
Linus Torvaldsd14f7762006-05-09 12:24:02 -070080 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
81 *tl = tolower(*tl);
82 for (tl=key; *tl && *tl != '.'; ++tl)
83 *tl = tolower(*tl);
Johannes Schindelin4ddba792005-11-20 06:52:22 +010084
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +020085 if (use_key_regexp) {
Jonas Fonseca2d7320d2006-09-01 00:32:39 +020086 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +020087 if (regcomp(key_regexp, key, REG_EXTENDED)) {
Junio C Hamanoe098c6f2006-05-02 21:06:56 -070088 fprintf(stderr, "Invalid key pattern: %s\n", key_);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +020089 goto free_strings;
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +020090 }
91 }
92
Johannes Schindelin4ddba792005-11-20 06:52:22 +010093 if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +010094 if (regex_[0] == '!') {
95 do_not_match = 1;
96 regex_++;
97 }
98
Jonas Fonseca2d7320d2006-09-01 00:32:39 +020099 regexp = (regex_t*)xmalloc(sizeof(regex_t));
Amos Waterland0a152172006-01-04 19:31:02 -0500100 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100101 fprintf(stderr, "Invalid pattern: %s\n", regex_);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200102 goto free_strings;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100103 }
104 }
105
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200106 if (do_all && global)
107 git_config_from_file(show_config, global);
108 git_config_from_file(show_config, local);
109 if (!do_all && !seen && global)
110 git_config_from_file(show_config, global);
111
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100112 free(key);
Amos Waterland0a152172006-01-04 19:31:02 -0500113 if (regexp) {
114 regfree(regexp);
115 free(regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100116 }
117
118 if (do_all)
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200119 ret = !seen;
120 else
Petr Baudisdc2613d2006-07-03 22:47:55 +0200121 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100122
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200123free_strings:
Junio C Hamano4cac42b2006-08-27 21:19:39 -0700124 free(repo_config);
125 free(global);
Johannes Schindelin5f1a63e2006-06-20 01:48:03 +0200126 return ret;
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100127}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100128
Tom Princee0d10e12007-01-28 16:16:53 -0800129int cmd_config(int argc, const char **argv, const char *prefix)
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100130{
Linus Torvalds4d599e62006-05-25 08:22:42 -0700131 int nongit = 0;
132 setup_git_directory_gently(&nongit);
Petr Baudis7162dff2006-02-12 04:14:48 +0100133
134 while (1 < argc) {
135 if (!strcmp(argv[1], "--int"))
136 type = T_INT;
137 else if (!strcmp(argv[1], "--bool"))
138 type = T_BOOL;
Johannes Schindelincfa24e12006-05-02 12:54:12 -0700139 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
140 return git_config(show_all_config);
Sean34eb3342006-11-02 10:44:20 -0500141 else if (!strcmp(argv[1], "--global")) {
142 char *home = getenv("HOME");
143 if (home) {
144 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
145 setenv("GIT_CONFIG", user_config, 1);
146 free(user_config);
147 } else {
148 die("$HOME not set");
149 }
Johannes Schindelin0667fcf2006-12-16 15:14:14 +0100150 } else if (!strcmp(argv[1], "--rename-section")) {
151 int ret;
152 if (argc != 4)
153 usage(git_config_set_usage);
154 ret = git_config_rename_section(argv[2], argv[3]);
155 if (ret < 0)
156 return ret;
157 if (ret == 0) {
158 fprintf(stderr, "No such section!\n");
159 return 1;
160 }
161 return 0;
Sean34eb3342006-11-02 10:44:20 -0500162 } else
Petr Baudis7162dff2006-02-12 04:14:48 +0100163 break;
164 argc--;
165 argv++;
166 }
167
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100168 switch (argc) {
169 case 2:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100170 return get_value(argv[1], NULL);
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100171 case 3:
172 if (!strcmp(argv[1], "--unset"))
173 return git_config_set(argv[2], NULL);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100174 else if (!strcmp(argv[1], "--unset-all"))
175 return git_config_set_multivar(argv[2], NULL, NULL, 1);
176 else if (!strcmp(argv[1], "--get"))
177 return get_value(argv[2], NULL);
178 else if (!strcmp(argv[1], "--get-all")) {
179 do_all = 1;
180 return get_value(argv[2], NULL);
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200181 } else if (!strcmp(argv[1], "--get-regexp")) {
182 show_keys = 1;
183 use_key_regexp = 1;
184 do_all = 1;
185 return get_value(argv[2], NULL);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100186 } else
187
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100188 return git_config_set(argv[1], argv[2]);
189 case 4:
190 if (!strcmp(argv[1], "--unset"))
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100191 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
192 else if (!strcmp(argv[1], "--unset-all"))
193 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
194 else if (!strcmp(argv[1], "--get"))
195 return get_value(argv[2], argv[3]);
196 else if (!strcmp(argv[1], "--get-all")) {
197 do_all = 1;
198 return get_value(argv[2], argv[3]);
Johannes Schindelin2fa9a0f2006-05-02 14:22:48 +0200199 } else if (!strcmp(argv[1], "--get-regexp")) {
200 show_keys = 1;
201 use_key_regexp = 1;
202 do_all = 1;
203 return get_value(argv[2], argv[3]);
Brian Gernhardt89c4afe2006-12-15 07:39:04 -0500204 } else if (!strcmp(argv[1], "--add"))
205 return git_config_set_multivar(argv[2], argv[3], "^$", 0);
206 else if (!strcmp(argv[1], "--replace-all"))
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100207
208 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100209 else
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100210
211 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
212 case 5:
213 if (!strcmp(argv[1], "--replace-all"))
214 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
215 case 1:
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100216 default:
217 usage(git_config_set_usage);
218 }
219 return 0;
220}