git config syntax updates
[git/haiku.git] / repo-config.c
blob58b6c5284fa806ddda70e84d3871415ba26c893f
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]]";
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_config(const char* key_, const char* value_)
17 if (value_ == NULL)
18 value_ = "";
20 if (!strcmp(key_, key) &&
21 (regexp == NULL ||
22 (do_not_match ^
23 !regexec(regexp, value_, 0, NULL, 0)))) {
24 if (do_all) {
25 printf("%s\n", value_);
26 return 0;
28 if (seen > 0) {
29 fprintf(stderr, "More than one value: %s\n", value);
30 free(value);
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 {
41 value = strdup(value_);
43 seen++;
45 return 0;
48 static int get_value(const char* key_, const char* regex_)
50 int i;
51 char *tl;
53 key = strdup(key_);
54 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
55 *tl = tolower(*tl);
56 for (tl=key; *tl && *tl != '.'; ++tl)
57 *tl = tolower(*tl);
59 if (regex_) {
60 if (regex_[0] == '!') {
61 do_not_match = 1;
62 regex_++;
65 regexp = (regex_t*)malloc(sizeof(regex_t));
66 if (regcomp(regexp, regex_, REG_EXTENDED)) {
67 fprintf(stderr, "Invalid pattern: %s\n", regex_);
68 return -1;
72 i = git_config(show_config);
73 if (value) {
74 printf("%s\n", value);
75 free(value);
77 free(key);
78 if (regexp) {
79 regfree(regexp);
80 free(regexp);
83 if (do_all)
84 return 0;
86 return seen == 1 ? 0 : 1;
89 int main(int argc, const char **argv)
91 setup_git_directory();
93 while (1 < argc) {
94 if (!strcmp(argv[1], "--int"))
95 type = T_INT;
96 else if (!strcmp(argv[1], "--bool"))
97 type = T_BOOL;
98 else
99 break;
100 argc--;
101 argv++;
104 switch (argc) {
105 case 2:
106 return get_value(argv[1], NULL);
107 case 3:
108 if (!strcmp(argv[1], "--unset"))
109 return git_config_set(argv[2], NULL);
110 else if (!strcmp(argv[1], "--unset-all"))
111 return git_config_set_multivar(argv[2], NULL, NULL, 1);
112 else if (!strcmp(argv[1], "--get"))
113 return get_value(argv[2], NULL);
114 else if (!strcmp(argv[1], "--get-all")) {
115 do_all = 1;
116 return get_value(argv[2], NULL);
117 } else
119 return git_config_set(argv[1], argv[2]);
120 case 4:
121 if (!strcmp(argv[1], "--unset"))
122 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
123 else if (!strcmp(argv[1], "--unset-all"))
124 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
125 else if (!strcmp(argv[1], "--get"))
126 return get_value(argv[2], argv[3]);
127 else if (!strcmp(argv[1], "--get-all")) {
128 do_all = 1;
129 return get_value(argv[2], argv[3]);
130 } else if (!strcmp(argv[1], "--replace-all"))
132 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
133 else
135 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
136 case 5:
137 if (!strcmp(argv[1], "--replace-all"))
138 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
139 case 1:
140 default:
141 usage(git_config_set_usage);
143 return 0;