Use preprocessor constants for environment variable names.
[git/git-svn.git] / builtin-repo-config.c
blob4885930ca460ba1143089c9a050ff55f4682a912
1 #include "builtin.h"
2 #include "cache.h"
3 #include <regex.h>
5 static const char git_config_set_usage[] =
6 "git-repo-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";
8 static char *key;
9 static regex_t *key_regexp;
10 static regex_t *regexp;
11 static int show_keys;
12 static int use_key_regexp;
13 static int do_all;
14 static int do_not_match;
15 static int seen;
16 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
18 static int show_all_config(const char *key_, const char *value_)
20 if (value_)
21 printf("%s=%s\n", key_, value_);
22 else
23 printf("%s\n", key_);
24 return 0;
27 static int show_config(const char* key_, const char* value_)
29 char value[256];
30 const char *vptr = value;
31 int dup_error = 0;
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 &&
38 (do_not_match ^
39 regexec(regexp, (value_?value_:""), 0, NULL, 0)))
40 return 0;
42 if (show_keys)
43 printf("%s ", key_);
44 if (seen && !do_all)
45 dup_error = 1;
46 if (type == T_INT)
47 sprintf(value, "%d", git_config_int(key_, value_?value_:""));
48 else if (type == T_BOOL)
49 vptr = git_config_bool(key_, value_) ? "true" : "false";
50 else
51 vptr = value_?value_:"";
52 seen++;
53 if (dup_error) {
54 error("More than one value for the key %s: %s",
55 key_, vptr);
57 else
58 printf("%s\n", vptr);
60 return 0;
63 static int get_value(const char* key_, const char* regex_)
65 int ret = -1;
66 char *tl;
67 char *global = NULL, *repo_config = NULL;
68 const char *local;
70 local = getenv(CONFIG_ENVIRONMENT);
71 if (!local) {
72 const char *home = getenv("HOME");
73 local = getenv(CONFIG_LOCAL_ENVIRONMENT);
74 if (!local)
75 local = repo_config = xstrdup(git_path("config"));
76 if (home)
77 global = xstrdup(mkpath("%s/.gitconfig", home));
80 key = xstrdup(key_);
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);
86 if (use_key_regexp) {
87 key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
88 if (regcomp(key_regexp, key, REG_EXTENDED)) {
89 fprintf(stderr, "Invalid key pattern: %s\n", key_);
90 goto free_strings;
94 if (regex_) {
95 if (regex_[0] == '!') {
96 do_not_match = 1;
97 regex_++;
100 regexp = (regex_t*)xmalloc(sizeof(regex_t));
101 if (regcomp(regexp, regex_, REG_EXTENDED)) {
102 fprintf(stderr, "Invalid pattern: %s\n", regex_);
103 goto free_strings;
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);
113 free(key);
114 if (regexp) {
115 regfree(regexp);
116 free(regexp);
119 if (do_all)
120 ret = !seen;
121 else
122 ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
124 free_strings:
125 free(repo_config);
126 free(global);
127 return ret;
130 int cmd_repo_config(int argc, const char **argv, const char *prefix)
132 int nongit = 0;
133 setup_git_directory_gently(&nongit);
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;
140 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
141 return git_config(show_all_config);
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");
151 } else if (!strcmp(argv[1], "--rename-section")) {
152 int ret;
153 if (argc != 4)
154 usage(git_config_set_usage);
155 ret = git_config_rename_section(argv[2], argv[3]);
156 if (ret < 0)
157 return ret;
158 if (ret == 0) {
159 fprintf(stderr, "No such section!\n");
160 return 1;
162 return 0;
163 } else
164 break;
165 argc--;
166 argv++;
169 switch (argc) {
170 case 2:
171 return get_value(argv[1], NULL);
172 case 3:
173 if (!strcmp(argv[1], "--unset"))
174 return git_config_set(argv[2], NULL);
175 else if (!strcmp(argv[1], "--unset-all"))
176 return git_config_set_multivar(argv[2], NULL, NULL, 1);
177 else if (!strcmp(argv[1], "--get"))
178 return get_value(argv[2], NULL);
179 else if (!strcmp(argv[1], "--get-all")) {
180 do_all = 1;
181 return get_value(argv[2], NULL);
182 } else if (!strcmp(argv[1], "--get-regexp")) {
183 show_keys = 1;
184 use_key_regexp = 1;
185 do_all = 1;
186 return get_value(argv[2], NULL);
187 } else
189 return git_config_set(argv[1], argv[2]);
190 case 4:
191 if (!strcmp(argv[1], "--unset"))
192 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
193 else if (!strcmp(argv[1], "--unset-all"))
194 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
195 else if (!strcmp(argv[1], "--get"))
196 return get_value(argv[2], argv[3]);
197 else if (!strcmp(argv[1], "--get-all")) {
198 do_all = 1;
199 return get_value(argv[2], argv[3]);
200 } else if (!strcmp(argv[1], "--get-regexp")) {
201 show_keys = 1;
202 use_key_regexp = 1;
203 do_all = 1;
204 return get_value(argv[2], argv[3]);
205 } else if (!strcmp(argv[1], "--add"))
206 return git_config_set_multivar(argv[2], argv[3], "^$", 0);
207 else if (!strcmp(argv[1], "--replace-all"))
209 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
210 else
212 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
213 case 5:
214 if (!strcmp(argv[1], "--replace-all"))
215 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
216 case 1:
217 default:
218 usage(git_config_set_usage);
220 return 0;