merge-recursive: Replace 'except:'
[git/git.git] / config-set.c
CommitLineData
1b1e59c5 1#include "cache.h"
4ddba79d 2#include <regex.h>
1b1e59c5
JS
3
4static const char git_config_set_usage[] =
4ddba79d
JS
5"git-config-set [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
6
7static char* key = NULL;
8static char* value = NULL;
9static regex_t* regex = NULL;
10static int do_all = 0;
11static int seen = 0;
12
13static 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
32static 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}
1b1e59c5
JS
64
65int main(int argc, const char **argv)
66{
67 setup_git_directory();
68 switch (argc) {
69 case 2:
4ddba79d 70 return get_value(argv[1], NULL);
1b1e59c5
JS
71 case 3:
72 if (!strcmp(argv[1], "--unset"))
73 return git_config_set(argv[2], NULL);
4ddba79d
JS
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
1b1e59c5
JS
83 return git_config_set(argv[1], argv[2]);
84 case 4:
85 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
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);
1b1e59c5 97 else
4ddba79d
JS
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:
1b1e59c5
JS
104 default:
105 usage(git_config_set_usage);
106 }
107 return 0;
108}