fix imap-send for OSX
[git/jnareb-git.git] / repo-config.c
blobc5ebb7668a2632ef94cc798552ddd3f6f173e4f4
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;
52 key = malloc(strlen(key_)+1);
53 for (i = 0; key_[i]; i++)
54 key[i] = tolower(key_[i]);
55 key[i] = 0;
57 if (regex_) {
58 if (regex_[0] == '!') {
59 do_not_match = 1;
60 regex_++;
63 regexp = (regex_t*)malloc(sizeof(regex_t));
64 if (regcomp(regexp, regex_, REG_EXTENDED)) {
65 fprintf(stderr, "Invalid pattern: %s\n", regex_);
66 return -1;
70 i = git_config(show_config);
71 if (value) {
72 printf("%s\n", value);
73 free(value);
75 free(key);
76 if (regexp) {
77 regfree(regexp);
78 free(regexp);
81 if (do_all)
82 return 0;
84 return seen == 1 ? 0 : 1;
87 int main(int argc, const char **argv)
89 setup_git_directory();
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++;
102 switch (argc) {
103 case 2:
104 return get_value(argv[1], NULL);
105 case 3:
106 if (!strcmp(argv[1], "--unset"))
107 return git_config_set(argv[2], NULL);
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
117 return git_config_set(argv[1], argv[2]);
118 case 4:
119 if (!strcmp(argv[1], "--unset"))
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"))
130 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
131 else
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:
138 default:
139 usage(git_config_set_usage);
141 return 0;