Add a test-case for git-apply trying to add an ending line
[git/spearce.git] / repo-config.c
blob127afd784c1fcdb531c1d3435f724d0041151e90
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 | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
7 static char* key = NULL;
8 static regex_t* key_regexp = NULL;
9 static regex_t* regexp = NULL;
10 static int show_keys = 0;
11 static int use_key_regexp = 0;
12 static int do_all = 0;
13 static int do_not_match = 0;
14 static int seen = 0;
15 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
17 static int show_all_config(const char *key_, const char *value_)
19 if (value_)
20 printf("%s=%s\n", key_, value_);
21 else
22 printf("%s\n", key_);
23 return 0;
26 static int show_config(const char* key_, const char* value_)
28 char value[256];
29 const char *vptr = value;
30 int dup_error = 0;
32 if (value_ == NULL)
33 value_ = "";
35 if (!use_key_regexp && strcmp(key_, key))
36 return 0;
37 if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
38 return 0;
39 if (regexp != NULL &&
40 (do_not_match ^
41 regexec(regexp, value_, 0, NULL, 0)))
42 return 0;
44 if (show_keys)
45 printf("%s ", key_);
46 if (seen && !do_all)
47 dup_error = 1;
48 if (type == T_INT)
49 sprintf(value, "%d", git_config_int(key_, value_));
50 else if (type == T_BOOL)
51 vptr = git_config_bool(key_, value_) ? "true" : "false";
52 else
53 vptr = value_;
54 seen++;
55 if (dup_error) {
56 error("More than one value for the key %s: %s",
57 key_, vptr);
59 else
60 printf("%s\n", vptr);
62 return 0;
65 static int get_value(const char* key_, const char* regex_)
67 char *tl;
69 key = strdup(key_);
70 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
71 *tl = tolower(*tl);
72 for (tl=key; *tl && *tl != '.'; ++tl)
73 *tl = tolower(*tl);
75 if (use_key_regexp) {
76 key_regexp = (regex_t*)malloc(sizeof(regex_t));
77 if (regcomp(key_regexp, key, REG_EXTENDED)) {
78 fprintf(stderr, "Invalid key pattern: %s\n", key_);
79 return -1;
83 if (regex_) {
84 if (regex_[0] == '!') {
85 do_not_match = 1;
86 regex_++;
89 regexp = (regex_t*)malloc(sizeof(regex_t));
90 if (regcomp(regexp, regex_, REG_EXTENDED)) {
91 fprintf(stderr, "Invalid pattern: %s\n", regex_);
92 return -1;
96 git_config(show_config);
97 free(key);
98 if (regexp) {
99 regfree(regexp);
100 free(regexp);
103 if (do_all)
104 return !seen;
106 return (seen == 1) ? 0 : 1;
109 int main(int argc, const char **argv)
111 setup_git_directory();
113 while (1 < argc) {
114 if (!strcmp(argv[1], "--int"))
115 type = T_INT;
116 else if (!strcmp(argv[1], "--bool"))
117 type = T_BOOL;
118 else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
119 return git_config(show_all_config);
120 else
121 break;
122 argc--;
123 argv++;
126 switch (argc) {
127 case 2:
128 return get_value(argv[1], NULL);
129 case 3:
130 if (!strcmp(argv[1], "--unset"))
131 return git_config_set(argv[2], NULL);
132 else if (!strcmp(argv[1], "--unset-all"))
133 return git_config_set_multivar(argv[2], NULL, NULL, 1);
134 else if (!strcmp(argv[1], "--get"))
135 return get_value(argv[2], NULL);
136 else if (!strcmp(argv[1], "--get-all")) {
137 do_all = 1;
138 return get_value(argv[2], NULL);
139 } else if (!strcmp(argv[1], "--get-regexp")) {
140 show_keys = 1;
141 use_key_regexp = 1;
142 do_all = 1;
143 return get_value(argv[2], NULL);
144 } else
146 return git_config_set(argv[1], argv[2]);
147 case 4:
148 if (!strcmp(argv[1], "--unset"))
149 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
150 else if (!strcmp(argv[1], "--unset-all"))
151 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
152 else if (!strcmp(argv[1], "--get"))
153 return get_value(argv[2], argv[3]);
154 else if (!strcmp(argv[1], "--get-all")) {
155 do_all = 1;
156 return get_value(argv[2], argv[3]);
157 } else if (!strcmp(argv[1], "--get-regexp")) {
158 show_keys = 1;
159 use_key_regexp = 1;
160 do_all = 1;
161 return get_value(argv[2], argv[3]);
162 } else if (!strcmp(argv[1], "--replace-all"))
164 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
165 else
167 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
168 case 5:
169 if (!strcmp(argv[1], "--replace-all"))
170 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
171 case 1:
172 default:
173 usage(git_config_set_usage);
175 return 0;