90a28b381ff8bb25e148f3e81f9a57a46ab0c962
[git.git] / config-set.c
1 #include "cache.h"
2 #include <regex.h>
3
4 static const char git_config_set_usage[] =
5 "git-config-set [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
6
7 static char* key = NULL;
8 static char* value = NULL;
9 static regex_t* regex = NULL;
10 static int do_all = 0;
11 static int seen = 0;
12
13 static int show_config(const char* key_, const char* value_)
14 {
15         if (!strcmp(key_, key) &&
16                         (regex == NULL ||
17                          !regexec(regex, value_, 0, NULL, 0))) {
18                 if (do_all) {
19                         printf("%s\n", value_);
20                         return 0;
21                 }
22                 if (seen > 0) {
23                         fprintf(stderr, "More than one value: %s\n", value);
24                         free(value);
25                 }
26                 value = strdup(value_);
27                 seen++;
28         }
29         return 0;
30 }
31
32 static int get_value(const char* key_, const char* regex_)
33 {
34         int i;
35
36         key = malloc(strlen(key_)+1);
37         for (i = 0; key_[i]; i++)
38                 key[i] = tolower(key_[i]);
39
40         if (regex_) {
41                 regex = (regex_t*)malloc(sizeof(regex_t));
42                 if (regcomp(regex, regex_, REG_EXTENDED)) {
43                         fprintf(stderr, "Invalid pattern: %s\n", regex_);
44                         return -1;
45                 }
46         }
47
48         i = git_config(show_config);
49         if (value) {
50                 printf("%s\n", value);
51                 free(value);
52         }
53         free(key);
54         if (regex) {
55                 regfree(regex);
56                 free(regex);
57         }
58
59         if (do_all)
60                 return 0;
61
62         return seen == 1 ? 0 : 1;
63 }
64
65 int main(int argc, const char **argv)
66 {
67         setup_git_directory();
68         switch (argc) {
69         case 2:
70                 return get_value(argv[1], NULL);
71         case 3:
72                 if (!strcmp(argv[1], "--unset"))
73                         return git_config_set(argv[2], NULL);
74                 else if (!strcmp(argv[1], "--unset-all"))
75                         return git_config_set_multivar(argv[2], NULL, NULL, 1);
76                 else if (!strcmp(argv[1], "--get"))
77                         return get_value(argv[2], NULL);
78                 else if (!strcmp(argv[1], "--get-all")) {
79                         do_all = 1;
80                         return get_value(argv[2], NULL);
81                 } else
82
83                         return git_config_set(argv[1], argv[2]);
84         case 4:
85                 if (!strcmp(argv[1], "--unset"))
86                         return git_config_set_multivar(argv[2], NULL, argv[3], 0);
87                 else if (!strcmp(argv[1], "--unset-all"))
88                         return git_config_set_multivar(argv[2], NULL, argv[3], 1);
89                 else if (!strcmp(argv[1], "--get"))
90                         return get_value(argv[2], argv[3]);
91                 else if (!strcmp(argv[1], "--get-all")) {
92                         do_all = 1;
93                         return get_value(argv[2], argv[3]);
94                 } else if (!strcmp(argv[1], "--replace-all"))
95
96                         return git_config_set_multivar(argv[2], argv[3], NULL, 1);
97                 else
98
99                         return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
100         case 5:
101                 if (!strcmp(argv[1], "--replace-all"))
102                         return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
103         case 1:
104         default:
105                 usage(git_config_set_usage);
106         }
107         return 0;
108 }