1#include "cache.h"
2#include <regex.h>
3
4static const char git_config_set_usage[] =
5"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
6
7static char* key = NULL;
8static char* value = NULL;
9static regex_t* regexp = NULL;
10static int do_all = 0;
11static int do_not_match = 0;
12static int seen = 0;
13static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
14
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
24static int show_config(const char* key_, const char* value_)
25{
26 if (value_ == NULL)
27 value_ = "";
28
29 if (!strcmp(key_, key) &&
30 (regexp == NULL ||
31 (do_not_match ^
32 !regexec(regexp, value_, 0, NULL, 0)))) {
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 }
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 {
50 value = strdup(value_);
51 }
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]);
64 key[i] = 0;
65
66 if (regex_) {
67 if (regex_[0] == '!') {
68 do_not_match = 1;
69 regex_++;
70 }
71
72 regexp = (regex_t*)malloc(sizeof(regex_t));
73 if (regcomp(regexp, regex_, REG_EXTENDED)) {
74 fprintf(stderr, "Invalid pattern: %s\n", regex_);
75 return -1;
76 }
77 }
78
79 git_config(show_config);
80 if (value) {
81 printf("%s\n", value);
82 free(value);
83 }
84 free(key);
85 if (regexp) {
86 regfree(regexp);
87 free(regexp);
88 }
89
90 if (do_all)
91 return 0;
92
93 return seen == 1 ? 0 : 1;
94}
95
96int main(int argc, const char **argv)
97{
98 setup_git_directory();
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;
105 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
106 return git_config(show_all_config);
107 else
108 break;
109 argc--;
110 argv++;
111 }
112
113 switch (argc) {
114 case 2:
115 return get_value(argv[1], NULL);
116 case 3:
117 if (!strcmp(argv[1], "--unset"))
118 return git_config_set(argv[2], NULL);
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
128 return git_config_set(argv[1], argv[2]);
129 case 4:
130 if (!strcmp(argv[1], "--unset"))
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);
142 else
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:
149 default:
150 usage(git_config_set_usage);
151 }
152 return 0;
153}