Another config file parsing fix.
[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[] =
7162dff3 5"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
4ddba79d
JS
6
7static char* key = NULL;
8static char* value = NULL;
0a152171 9static regex_t* regexp = NULL;
4ddba79d 10static int do_all = 0;
f98d863d 11static int do_not_match = 0;
4ddba79d 12static int seen = 0;
7162dff3 13static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
4ddba79d
JS
14
15static int show_config(const char* key_, const char* value_)
16{
f067a137
JF
17 if (value_ == NULL)
18 value_ = "";
19
4ddba79d 20 if (!strcmp(key_, key) &&
0a152171 21 (regexp == NULL ||
f98d863d 22 (do_not_match ^
0a152171 23 !regexec(regexp, value_, 0, NULL, 0)))) {
4ddba79d
JS
24 if (do_all) {
25 printf("%s\n", value_);
26 return 0;
27 }
28 if (seen > 0) {
29 fprintf(stderr, "More than one value: %s\n", value);
30 free(value);
31 }
7162dff3
PB
32
33 if (type == T_INT) {
34 value = malloc(256);
35 sprintf(value, "%d", git_config_int(key_, value_));
36 } else if (type == T_BOOL) {
37 value = malloc(256);
38 sprintf(value, "%s", git_config_bool(key_, value_)
39 ? "true" : "false");
40 } else {
f067a137 41 value = strdup(value_);
7162dff3 42 }
4ddba79d
JS
43 seen++;
44 }
45 return 0;
46}
47
48static int get_value(const char* key_, const char* regex_)
49{
50 int i;
51
52 key = malloc(strlen(key_)+1);
53 for (i = 0; key_[i]; i++)
54 key[i] = tolower(key_[i]);
3dd94e3b 55 key[i] = 0;
4ddba79d
JS
56
57 if (regex_) {
f98d863d
JS
58 if (regex_[0] == '!') {
59 do_not_match = 1;
60 regex_++;
61 }
62
0a152171
AW
63 regexp = (regex_t*)malloc(sizeof(regex_t));
64 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d
JS
65 fprintf(stderr, "Invalid pattern: %s\n", regex_);
66 return -1;
67 }
68 }
69
70 i = git_config(show_config);
71 if (value) {
72 printf("%s\n", value);
73 free(value);
74 }
75 free(key);
0a152171
AW
76 if (regexp) {
77 regfree(regexp);
78 free(regexp);
4ddba79d
JS
79 }
80
81 if (do_all)
82 return 0;
83
84 return seen == 1 ? 0 : 1;
85}
1b1e59c5
JS
86
87int main(int argc, const char **argv)
88{
89 setup_git_directory();
7162dff3
PB
90
91 while (1 < argc) {
92 if (!strcmp(argv[1], "--int"))
93 type = T_INT;
94 else if (!strcmp(argv[1], "--bool"))
95 type = T_BOOL;
96 else
97 break;
98 argc--;
99 argv++;
100 }
101
1b1e59c5
JS
102 switch (argc) {
103 case 2:
4ddba79d 104 return get_value(argv[1], NULL);
1b1e59c5
JS
105 case 3:
106 if (!strcmp(argv[1], "--unset"))
107 return git_config_set(argv[2], NULL);
4ddba79d
JS
108 else if (!strcmp(argv[1], "--unset-all"))
109 return git_config_set_multivar(argv[2], NULL, NULL, 1);
110 else if (!strcmp(argv[1], "--get"))
111 return get_value(argv[2], NULL);
112 else if (!strcmp(argv[1], "--get-all")) {
113 do_all = 1;
114 return get_value(argv[2], NULL);
115 } else
116
1b1e59c5
JS
117 return git_config_set(argv[1], argv[2]);
118 case 4:
119 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
120 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
121 else if (!strcmp(argv[1], "--unset-all"))
122 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
123 else if (!strcmp(argv[1], "--get"))
124 return get_value(argv[2], argv[3]);
125 else if (!strcmp(argv[1], "--get-all")) {
126 do_all = 1;
127 return get_value(argv[2], argv[3]);
128 } else if (!strcmp(argv[1], "--replace-all"))
129
130 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
1b1e59c5 131 else
4ddba79d
JS
132
133 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
134 case 5:
135 if (!strcmp(argv[1], "--replace-all"))
136 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
137 case 1:
1b1e59c5
JS
138 default:
139 usage(git_config_set_usage);
140 }
141 return 0;
142}