]>
Commit | Line | Data |
---|---|---|
e12c095a | 1 | #include "builtin.h" |
1b1e59c5 JS |
2 | #include "cache.h" |
3 | ||
4 | static const char git_config_set_usage[] = | |
9bc20aa7 | 5 | "git-config [ --global | --system ] [ --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"; |
4ddba79d | 6 | |
96f1e58f DR |
7 | static char *key; |
8 | static regex_t *key_regexp; | |
9 | static regex_t *regexp; | |
10 | static int show_keys; | |
11 | static int use_key_regexp; | |
12 | static int do_all; | |
13 | static int do_not_match; | |
14 | static int seen; | |
7162dff3 | 15 | static enum { T_RAW, T_INT, T_BOOL } type = T_RAW; |
4ddba79d | 16 | |
de791f15 PB |
17 | static 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 | ||
4ddba79d JS |
26 | static int show_config(const char* key_, const char* value_) |
27 | { | |
e098c6f8 JH |
28 | char value[256]; |
29 | const char *vptr = value; | |
8f5ff31f | 30 | int dup_error = 0; |
e098c6f8 | 31 | |
8f5ff31f JS |
32 | 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 && | |
f98d863d | 37 | (do_not_match ^ |
acb70149 | 38 | regexec(regexp, (value_?value_:""), 0, NULL, 0))) |
8f5ff31f JS |
39 | 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) | |
acb70149 | 46 | sprintf(value, "%d", git_config_int(key_, value_?value_:"")); |
8f5ff31f JS |
47 | else if (type == T_BOOL) |
48 | vptr = git_config_bool(key_, value_) ? "true" : "false"; | |
49 | else | |
acb70149 | 50 | vptr = value_?value_:""; |
8f5ff31f JS |
51 | seen++; |
52 | if (dup_error) { | |
53 | error("More than one value for the key %s: %s", | |
54 | key_, vptr); | |
4ddba79d | 55 | } |
8f5ff31f JS |
56 | else |
57 | printf("%s\n", vptr); | |
58 | ||
4ddba79d JS |
59 | return 0; |
60 | } | |
61 | ||
62 | static int get_value(const char* key_, const char* regex_) | |
63 | { | |
5f1a63e0 | 64 | int ret = -1; |
d14f7764 | 65 | char *tl; |
5f1a63e0 | 66 | char *global = NULL, *repo_config = NULL; |
32043c9f | 67 | const char *system_wide = NULL, *local; |
5f1a63e0 | 68 | |
d4ebc36c | 69 | local = getenv(CONFIG_ENVIRONMENT); |
5f1a63e0 JS |
70 | if (!local) { |
71 | const char *home = getenv("HOME"); | |
d4ebc36c | 72 | local = getenv(CONFIG_LOCAL_ENVIRONMENT); |
5f1a63e0 | 73 | if (!local) |
9befac47 | 74 | local = repo_config = xstrdup(git_path("config")); |
5f1a63e0 | 75 | if (home) |
9befac47 | 76 | global = xstrdup(mkpath("%s/.gitconfig", home)); |
32043c9f | 77 | system_wide = ETC_GITCONFIG; |
5f1a63e0 | 78 | } |
4ddba79d | 79 | |
9befac47 | 80 | key = xstrdup(key_); |
d14f7764 LT |
81 | 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); | |
4ddba79d | 85 | |
2fa9a0fb | 86 | if (use_key_regexp) { |
2d7320d0 | 87 | key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
2fa9a0fb | 88 | if (regcomp(key_regexp, key, REG_EXTENDED)) { |
e098c6f8 | 89 | fprintf(stderr, "Invalid key pattern: %s\n", key_); |
5f1a63e0 | 90 | goto free_strings; |
2fa9a0fb JS |
91 | } |
92 | } | |
93 | ||
4ddba79d | 94 | if (regex_) { |
f98d863d JS |
95 | if (regex_[0] == '!') { |
96 | do_not_match = 1; | |
97 | regex_++; | |
98 | } | |
99 | ||
2d7320d0 | 100 | regexp = (regex_t*)xmalloc(sizeof(regex_t)); |
0a152171 | 101 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
4ddba79d | 102 | fprintf(stderr, "Invalid pattern: %s\n", regex_); |
5f1a63e0 | 103 | goto free_strings; |
4ddba79d JS |
104 | } |
105 | } | |
106 | ||
32043c9f JS |
107 | if (do_all && system_wide) |
108 | git_config_from_file(show_config, system_wide); | |
5f1a63e0 JS |
109 | 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); | |
32043c9f JS |
114 | if (!do_all && !seen && system_wide) |
115 | git_config_from_file(show_config, system_wide); | |
5f1a63e0 | 116 | |
4ddba79d | 117 | free(key); |
0a152171 AW |
118 | if (regexp) { |
119 | regfree(regexp); | |
120 | free(regexp); | |
4ddba79d JS |
121 | } |
122 | ||
123 | if (do_all) | |
5f1a63e0 JS |
124 | ret = !seen; |
125 | else | |
dc2613de | 126 | ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1; |
5f1a63e0 JS |
127 | |
128 | free_strings: | |
4cac42b1 JH |
129 | free(repo_config); |
130 | free(global); | |
5f1a63e0 | 131 | return ret; |
4ddba79d | 132 | } |
1b1e59c5 | 133 | |
e0d10e1c | 134 | int cmd_config(int argc, const char **argv, const char *prefix) |
1b1e59c5 | 135 | { |
4d599e6b LT |
136 | int nongit = 0; |
137 | setup_git_directory_gently(&nongit); | |
7162dff3 PB |
138 | |
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; | |
cfa24e18 JS |
144 | else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l")) |
145 | return git_config(show_all_config); | |
34eb3340 SE |
146 | 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 | } | |
32043c9f JS |
155 | } |
156 | else if (!strcmp(argv[1], "--system")) | |
157 | setenv("GIT_CONFIG", ETC_GITCONFIG, 1); | |
158 | else if (!strcmp(argv[1], "--rename-section")) { | |
0667fcfb JS |
159 | 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; | |
32043c9f | 170 | } |
118f8b24 PB |
171 | 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 | } | |
32043c9f | 184 | else |
7162dff3 PB |
185 | break; |
186 | argc--; | |
187 | argv++; | |
188 | } | |
189 | ||
1b1e59c5 JS |
190 | switch (argc) { |
191 | case 2: | |
4ddba79d | 192 | return get_value(argv[1], NULL); |
1b1e59c5 JS |
193 | case 3: |
194 | if (!strcmp(argv[1], "--unset")) | |
195 | return git_config_set(argv[2], NULL); | |
4ddba79d JS |
196 | 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); | |
2fa9a0fb JS |
203 | } 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); | |
4ddba79d JS |
208 | } else |
209 | ||
1b1e59c5 JS |
210 | return git_config_set(argv[1], argv[2]); |
211 | case 4: | |
212 | if (!strcmp(argv[1], "--unset")) | |
4ddba79d JS |
213 | 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]); | |
2fa9a0fb JS |
221 | } 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]); | |
89c4afe0 BG |
226 | } 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")) | |
4ddba79d JS |
229 | |
230 | return git_config_set_multivar(argv[2], argv[3], NULL, 1); | |
1b1e59c5 | 231 | else |
4ddba79d JS |
232 | |
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: | |
1b1e59c5 JS |
238 | default: |
239 | usage(git_config_set_usage); | |
240 | } | |
241 | return 0; | |
242 | } |