Teach bash the new features of 'git show'.
[git/git.git] / builtin-repo-config.c
CommitLineData
e12c095a 1#include "builtin.h"
1b1e59c5 2#include "cache.h"
4ddba79d 3#include <regex.h>
1b1e59c5
JS
4
5static const char git_config_set_usage[] =
34eb3340 6"git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
4ddba79d 7
96f1e58f
DR
8static char *key;
9static regex_t *key_regexp;
10static regex_t *regexp;
11static int show_keys;
12static int use_key_regexp;
13static int do_all;
14static int do_not_match;
15static int seen;
7162dff3 16static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
4ddba79d 17
de791f15
PB
18static int show_all_config(const char *key_, const char *value_)
19{
20 if (value_)
21 printf("%s=%s\n", key_, value_);
22 else
23 printf("%s\n", key_);
24 return 0;
25}
26
4ddba79d
JS
27static int show_config(const char* key_, const char* value_)
28{
e098c6f8
JH
29 char value[256];
30 const char *vptr = value;
8f5ff31f 31 int dup_error = 0;
e098c6f8 32
8f5ff31f
JS
33 if (!use_key_regexp && strcmp(key_, key))
34 return 0;
35 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
36 return 0;
37 if (regexp != NULL &&
f98d863d 38 (do_not_match ^
acb70149 39 regexec(regexp, (value_?value_:""), 0, NULL, 0)))
8f5ff31f
JS
40 return 0;
41
42 if (show_keys)
43 printf("%s ", key_);
44 if (seen && !do_all)
45 dup_error = 1;
46 if (type == T_INT)
acb70149 47 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
8f5ff31f
JS
48 else if (type == T_BOOL)
49 vptr = git_config_bool(key_, value_) ? "true" : "false";
50 else
acb70149 51 vptr = value_?value_:"";
8f5ff31f
JS
52 seen++;
53 if (dup_error) {
54 error("More than one value for the key %s: %s",
55 key_, vptr);
4ddba79d 56 }
8f5ff31f
JS
57 else
58 printf("%s\n", vptr);
59
4ddba79d
JS
60 return 0;
61}
62
63static int get_value(const char* key_, const char* regex_)
64{
5f1a63e0 65 int ret = -1;
d14f7764 66 char *tl;
5f1a63e0
JS
67 char *global = NULL, *repo_config = NULL;
68 const char *local;
69
70 local = getenv("GIT_CONFIG");
71 if (!local) {
72 const char *home = getenv("HOME");
73 local = getenv("GIT_CONFIG_LOCAL");
74 if (!local)
9befac47 75 local = repo_config = xstrdup(git_path("config"));
5f1a63e0 76 if (home)
9befac47 77 global = xstrdup(mkpath("%s/.gitconfig", home));
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
5f1a63e0
JS
107 if (do_all && global)
108 git_config_from_file(show_config, global);
109 git_config_from_file(show_config, local);
110 if (!do_all && !seen && global)
111 git_config_from_file(show_config, global);
112
4ddba79d 113 free(key);
0a152171
AW
114 if (regexp) {
115 regfree(regexp);
116 free(regexp);
4ddba79d
JS
117 }
118
119 if (do_all)
5f1a63e0
JS
120 ret = !seen;
121 else
dc2613de 122 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
5f1a63e0
JS
123
124free_strings:
4cac42b1
JH
125 free(repo_config);
126 free(global);
5f1a63e0 127 return ret;
4ddba79d 128}
1b1e59c5 129
e12c095a 130int cmd_repo_config(int argc, const char **argv, const char *prefix)
1b1e59c5 131{
4d599e6b
LT
132 int nongit = 0;
133 setup_git_directory_gently(&nongit);
7162dff3
PB
134
135 while (1 < argc) {
136 if (!strcmp(argv[1], "--int"))
137 type = T_INT;
138 else if (!strcmp(argv[1], "--bool"))
139 type = T_BOOL;
cfa24e18
JS
140 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
141 return git_config(show_all_config);
34eb3340
SE
142 else if (!strcmp(argv[1], "--global")) {
143 char *home = getenv("HOME");
144 if (home) {
145 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
146 setenv("GIT_CONFIG", user_config, 1);
147 free(user_config);
148 } else {
149 die("$HOME not set");
150 }
151 } else
7162dff3
PB
152 break;
153 argc--;
154 argv++;
155 }
156
1b1e59c5
JS
157 switch (argc) {
158 case 2:
4ddba79d 159 return get_value(argv[1], NULL);
1b1e59c5
JS
160 case 3:
161 if (!strcmp(argv[1], "--unset"))
162 return git_config_set(argv[2], NULL);
4ddba79d
JS
163 else if (!strcmp(argv[1], "--unset-all"))
164 return git_config_set_multivar(argv[2], NULL, NULL, 1);
165 else if (!strcmp(argv[1], "--get"))
166 return get_value(argv[2], NULL);
167 else if (!strcmp(argv[1], "--get-all")) {
168 do_all = 1;
169 return get_value(argv[2], NULL);
2fa9a0fb
JS
170 } else if (!strcmp(argv[1], "--get-regexp")) {
171 show_keys = 1;
172 use_key_regexp = 1;
173 do_all = 1;
174 return get_value(argv[2], NULL);
4ddba79d
JS
175 } else
176
1b1e59c5
JS
177 return git_config_set(argv[1], argv[2]);
178 case 4:
179 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
180 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
181 else if (!strcmp(argv[1], "--unset-all"))
182 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
183 else if (!strcmp(argv[1], "--get"))
184 return get_value(argv[2], argv[3]);
185 else if (!strcmp(argv[1], "--get-all")) {
186 do_all = 1;
187 return get_value(argv[2], argv[3]);
2fa9a0fb
JS
188 } else if (!strcmp(argv[1], "--get-regexp")) {
189 show_keys = 1;
190 use_key_regexp = 1;
191 do_all = 1;
192 return get_value(argv[2], argv[3]);
4ddba79d
JS
193 } else if (!strcmp(argv[1], "--replace-all"))
194
195 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
1b1e59c5 196 else
4ddba79d
JS
197
198 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
199 case 5:
200 if (!strcmp(argv[1], "--replace-all"))
201 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
202 case 1:
1b1e59c5
JS
203 default:
204 usage(git_config_set_usage);
205 }
206 return 0;
207}