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