builtin-grep: allow more than one patterns.
[git/dscho.git] / repo-config.c
blobfa8aba7a1b8760f0316ef0d47c40b0078316ff57
1 #include "cache.h"
2 #include <regex.h>
4 static 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";
7 static char* key = NULL;
8 static char* value = NULL;
9 static regex_t* regexp = NULL;
10 static int do_all = 0;
11 static int do_not_match = 0;
12 static int seen = 0;
13 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
15 static int show_all_config(const char *key_, const char *value_)
17 if (value_)
18 printf("%s=%s\n", key_, value_);
19 else
20 printf("%s\n", key_);
21 return 0;
24 static int show_config(const char* key_, const char* value_)
26 if (value_ == NULL)
27 value_ = "";
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;
37 if (seen > 0) {
38 fprintf(stderr, "More than one value: %s\n", value);
39 free(value);
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_);
52 seen++;
54 return 0;
57 static int get_value(const char* key_, const char* regex_)
59 int i;
61 key = malloc(strlen(key_)+1);
62 for (i = 0; key_[i]; i++)
63 key[i] = tolower(key_[i]);
64 key[i] = 0;
66 if (regex_) {
67 if (regex_[0] == '!') {
68 do_not_match = 1;
69 regex_++;
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;
79 git_config(show_config);
80 if (value) {
81 printf("%s\n", value);
82 free(value);
84 free(key);
85 if (regexp) {
86 regfree(regexp);
87 free(regexp);
90 if (do_all)
91 return 0;
93 return seen == 1 ? 0 : 1;
96 int main(int argc, const char **argv)
98 setup_git_directory();
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
106 break;
107 argc--;
108 argv++;
111 if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
112 return git_config(show_all_config);
114 switch (argc) {
115 case 2:
116 return get_value(argv[1], NULL);
117 case 3:
118 if (!strcmp(argv[1], "--unset"))
119 return git_config_set(argv[2], NULL);
120 else if (!strcmp(argv[1], "--unset-all"))
121 return git_config_set_multivar(argv[2], NULL, NULL, 1);
122 else if (!strcmp(argv[1], "--get"))
123 return get_value(argv[2], NULL);
124 else if (!strcmp(argv[1], "--get-all")) {
125 do_all = 1;
126 return get_value(argv[2], NULL);
127 } else
129 return git_config_set(argv[1], argv[2]);
130 case 4:
131 if (!strcmp(argv[1], "--unset"))
132 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
133 else if (!strcmp(argv[1], "--unset-all"))
134 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
135 else if (!strcmp(argv[1], "--get"))
136 return get_value(argv[2], argv[3]);
137 else if (!strcmp(argv[1], "--get-all")) {
138 do_all = 1;
139 return get_value(argv[2], argv[3]);
140 } else if (!strcmp(argv[1], "--replace-all"))
142 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
143 else
145 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
146 case 5:
147 if (!strcmp(argv[1], "--replace-all"))
148 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
149 case 1:
150 default:
151 usage(git_config_set_usage);
153 return 0;