Make git-prune-packed a builtin
[git/git.git] / repo-config.c
CommitLineData
1b1e59c5 1#include "cache.h"
4ddba79d 2#include <regex.h>
1b1e59c5
JS
3
4static const char git_config_set_usage[] =
e098c6f8 5"git-repo-config [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
4ddba79d
JS
6
7static char* key = NULL;
2fa9a0fb 8static regex_t* key_regexp = NULL;
0a152171 9static regex_t* regexp = NULL;
2fa9a0fb
JS
10static int show_keys = 0;
11static int use_key_regexp = 0;
4ddba79d 12static int do_all = 0;
f98d863d 13static int do_not_match = 0;
4ddba79d 14static int seen = 0;
7162dff3 15static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
4ddba79d 16
de791f15
PB
17static 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
26static 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
62static int get_value(const char* key_, const char* regex_)
63{
5f1a63e0 64 int ret = -1;
d14f7764 65 char *tl;
5f1a63e0
JS
66 char *global = NULL, *repo_config = NULL;
67 const char *local;
68
69 local = getenv("GIT_CONFIG");
70 if (!local) {
71 const char *home = getenv("HOME");
72 local = getenv("GIT_CONFIG_LOCAL");
73 if (!local)
5f1a63e0
JS
74 local = repo_config = strdup(git_path("config"));
75 if (home)
76 global = strdup(mkpath("%s/.gitconfig", home));
77 }
4ddba79d 78
d14f7764
LT
79 key = strdup(key_);
80 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);
4ddba79d 84
2fa9a0fb
JS
85 if (use_key_regexp) {
86 key_regexp = (regex_t*)malloc(sizeof(regex_t));
87 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 88 fprintf(stderr, "Invalid key pattern: %s\n", key_);
5f1a63e0 89 goto free_strings;
2fa9a0fb
JS
90 }
91 }
92
4ddba79d 93 if (regex_) {
f98d863d
JS
94 if (regex_[0] == '!') {
95 do_not_match = 1;
96 regex_++;
97 }
98
0a152171
AW
99 regexp = (regex_t*)malloc(sizeof(regex_t));
100 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d 101 fprintf(stderr, "Invalid pattern: %s\n", regex_);
5f1a63e0 102 goto free_strings;
4ddba79d
JS
103 }
104 }
105
5f1a63e0
JS
106 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
4ddba79d 112 free(key);
0a152171
AW
113 if (regexp) {
114 regfree(regexp);
115 free(regexp);
4ddba79d
JS
116 }
117
118 if (do_all)
5f1a63e0
JS
119 ret = !seen;
120 else
121 ret = (seen == 1) ? 0 : 1;
122
123free_strings:
124 if (repo_config)
125 free(repo_config);
126 if (global)
127 free(global);
128 return ret;
4ddba79d 129}
1b1e59c5
JS
130
131int main(int argc, const char **argv)
132{
4d599e6b
LT
133 int nongit = 0;
134 setup_git_directory_gently(&nongit);
7162dff3
PB
135
136 while (1 < argc) {
137 if (!strcmp(argv[1], "--int"))
138 type = T_INT;
139 else if (!strcmp(argv[1], "--bool"))
140 type = T_BOOL;
cfa24e18
JS
141 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
142 return git_config(show_all_config);
7162dff3
PB
143 else
144 break;
145 argc--;
146 argv++;
147 }
148
1b1e59c5
JS
149 switch (argc) {
150 case 2:
4ddba79d 151 return get_value(argv[1], NULL);
1b1e59c5
JS
152 case 3:
153 if (!strcmp(argv[1], "--unset"))
154 return git_config_set(argv[2], NULL);
4ddba79d
JS
155 else if (!strcmp(argv[1], "--unset-all"))
156 return git_config_set_multivar(argv[2], NULL, NULL, 1);
157 else if (!strcmp(argv[1], "--get"))
158 return get_value(argv[2], NULL);
159 else if (!strcmp(argv[1], "--get-all")) {
160 do_all = 1;
161 return get_value(argv[2], NULL);
2fa9a0fb
JS
162 } else if (!strcmp(argv[1], "--get-regexp")) {
163 show_keys = 1;
164 use_key_regexp = 1;
165 do_all = 1;
166 return get_value(argv[2], NULL);
4ddba79d
JS
167 } else
168
1b1e59c5
JS
169 return git_config_set(argv[1], argv[2]);
170 case 4:
171 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
172 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
173 else if (!strcmp(argv[1], "--unset-all"))
174 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
175 else if (!strcmp(argv[1], "--get"))
176 return get_value(argv[2], argv[3]);
177 else if (!strcmp(argv[1], "--get-all")) {
178 do_all = 1;
179 return get_value(argv[2], argv[3]);
2fa9a0fb
JS
180 } else if (!strcmp(argv[1], "--get-regexp")) {
181 show_keys = 1;
182 use_key_regexp = 1;
183 do_all = 1;
184 return get_value(argv[2], argv[3]);
4ddba79d
JS
185 } else if (!strcmp(argv[1], "--replace-all"))
186
187 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
1b1e59c5 188 else
4ddba79d
JS
189
190 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
191 case 5:
192 if (!strcmp(argv[1], "--replace-all"))
193 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
194 case 1:
1b1e59c5
JS
195 default:
196 usage(git_config_set_usage);
197 }
198 return 0;
199}