]> git.ipfire.org Git - thirdparty/git.git/blame - repo-config.c
Make release tarballs friendlier to older tar versions
[thirdparty/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
f067a137
JF
32 if (value_ == NULL)
33 value_ = "";
34
8f5ff31f
JS
35 if (!use_key_regexp && strcmp(key_, key))
36 return 0;
37 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
38 return 0;
39 if (regexp != NULL &&
f98d863d 40 (do_not_match ^
8f5ff31f
JS
41 regexec(regexp, value_, 0, NULL, 0)))
42 return 0;
43
44 if (show_keys)
45 printf("%s ", key_);
46 if (seen && !do_all)
47 dup_error = 1;
48 if (type == T_INT)
49 sprintf(value, "%d", git_config_int(key_, value_));
50 else if (type == T_BOOL)
51 vptr = git_config_bool(key_, value_) ? "true" : "false";
52 else
53 vptr = value_;
54 seen++;
55 if (dup_error) {
56 error("More than one value for the key %s: %s",
57 key_, vptr);
4ddba79d 58 }
8f5ff31f
JS
59 else
60 printf("%s\n", vptr);
61
4ddba79d
JS
62 return 0;
63}
64
65static int get_value(const char* key_, const char* regex_)
66{
d14f7764 67 char *tl;
4ddba79d 68
d14f7764
LT
69 key = strdup(key_);
70 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
71 *tl = tolower(*tl);
72 for (tl=key; *tl && *tl != '.'; ++tl)
73 *tl = tolower(*tl);
4ddba79d 74
2fa9a0fb
JS
75 if (use_key_regexp) {
76 key_regexp = (regex_t*)malloc(sizeof(regex_t));
77 if (regcomp(key_regexp, key, REG_EXTENDED)) {
e098c6f8 78 fprintf(stderr, "Invalid key pattern: %s\n", key_);
2fa9a0fb
JS
79 return -1;
80 }
81 }
82
4ddba79d 83 if (regex_) {
f98d863d
JS
84 if (regex_[0] == '!') {
85 do_not_match = 1;
86 regex_++;
87 }
88
0a152171
AW
89 regexp = (regex_t*)malloc(sizeof(regex_t));
90 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d
JS
91 fprintf(stderr, "Invalid pattern: %s\n", regex_);
92 return -1;
93 }
94 }
95
de791f15 96 git_config(show_config);
4ddba79d 97 free(key);
0a152171
AW
98 if (regexp) {
99 regfree(regexp);
100 free(regexp);
4ddba79d
JS
101 }
102
103 if (do_all)
e098c6f8 104 return !seen;
4ddba79d 105
e098c6f8 106 return (seen == 1) ? 0 : 1;
4ddba79d 107}
1b1e59c5
JS
108
109int main(int argc, const char **argv)
110{
4d599e6b
LT
111 int nongit = 0;
112 setup_git_directory_gently(&nongit);
7162dff3
PB
113
114 while (1 < argc) {
115 if (!strcmp(argv[1], "--int"))
116 type = T_INT;
117 else if (!strcmp(argv[1], "--bool"))
118 type = T_BOOL;
cfa24e18
JS
119 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
120 return git_config(show_all_config);
7162dff3
PB
121 else
122 break;
123 argc--;
124 argv++;
125 }
126
1b1e59c5
JS
127 switch (argc) {
128 case 2:
4ddba79d 129 return get_value(argv[1], NULL);
1b1e59c5
JS
130 case 3:
131 if (!strcmp(argv[1], "--unset"))
132 return git_config_set(argv[2], NULL);
4ddba79d
JS
133 else if (!strcmp(argv[1], "--unset-all"))
134 return git_config_set_multivar(argv[2], NULL, NULL, 1);
135 else if (!strcmp(argv[1], "--get"))
136 return get_value(argv[2], NULL);
137 else if (!strcmp(argv[1], "--get-all")) {
138 do_all = 1;
139 return get_value(argv[2], NULL);
2fa9a0fb
JS
140 } else if (!strcmp(argv[1], "--get-regexp")) {
141 show_keys = 1;
142 use_key_regexp = 1;
143 do_all = 1;
144 return get_value(argv[2], NULL);
4ddba79d
JS
145 } else
146
1b1e59c5
JS
147 return git_config_set(argv[1], argv[2]);
148 case 4:
149 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
150 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
151 else if (!strcmp(argv[1], "--unset-all"))
152 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
153 else if (!strcmp(argv[1], "--get"))
154 return get_value(argv[2], argv[3]);
155 else if (!strcmp(argv[1], "--get-all")) {
156 do_all = 1;
157 return get_value(argv[2], argv[3]);
2fa9a0fb
JS
158 } else if (!strcmp(argv[1], "--get-regexp")) {
159 show_keys = 1;
160 use_key_regexp = 1;
161 do_all = 1;
162 return get_value(argv[2], argv[3]);
4ddba79d
JS
163 } else if (!strcmp(argv[1], "--replace-all"))
164
165 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
1b1e59c5 166 else
4ddba79d
JS
167
168 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
169 case 5:
170 if (!strcmp(argv[1], "--replace-all"))
171 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
172 case 1:
1b1e59c5
JS
173 default:
174 usage(git_config_set_usage);
175 }
176 return 0;
177}