Documentation: stdout of update-hook is connected to /dev/null
[git/jrn.git] / repo-config.c
blobb2569b7901b8250fd964707569264b9ecb2acf30
1 #include "cache.h"
2 #include <regex.h>
4 static const char git_config_set_usage[] =
5 "git-repo-config [--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* regex = NULL;
10 static int do_all = 0;
11 static int do_not_match = 0;
12 static int seen = 0;
14 static int show_config(const char* key_, const char* value_)
16 if (!strcmp(key_, key) &&
17 (regex == NULL ||
18 (do_not_match ^
19 !regexec(regex, value_, 0, NULL, 0)))) {
20 if (do_all) {
21 printf("%s\n", value_);
22 return 0;
24 if (seen > 0) {
25 fprintf(stderr, "More than one value: %s\n", value);
26 free(value);
28 value = strdup(value_);
29 seen++;
31 return 0;
34 static int get_value(const char* key_, const char* regex_)
36 int i;
38 key = malloc(strlen(key_)+1);
39 for (i = 0; key_[i]; i++)
40 key[i] = tolower(key_[i]);
41 key[i] = 0;
43 if (regex_) {
44 if (regex_[0] == '!') {
45 do_not_match = 1;
46 regex_++;
49 regex = (regex_t*)malloc(sizeof(regex_t));
50 if (regcomp(regex, regex_, REG_EXTENDED)) {
51 fprintf(stderr, "Invalid pattern: %s\n", regex_);
52 return -1;
56 i = git_config(show_config);
57 if (value) {
58 printf("%s\n", value);
59 free(value);
61 free(key);
62 if (regex) {
63 regfree(regex);
64 free(regex);
67 if (do_all)
68 return 0;
70 return seen == 1 ? 0 : 1;
73 int main(int argc, const char **argv)
75 setup_git_directory();
76 switch (argc) {
77 case 2:
78 return get_value(argv[1], NULL);
79 case 3:
80 if (!strcmp(argv[1], "--unset"))
81 return git_config_set(argv[2], NULL);
82 else if (!strcmp(argv[1], "--unset-all"))
83 return git_config_set_multivar(argv[2], NULL, NULL, 1);
84 else if (!strcmp(argv[1], "--get"))
85 return get_value(argv[2], NULL);
86 else if (!strcmp(argv[1], "--get-all")) {
87 do_all = 1;
88 return get_value(argv[2], NULL);
89 } else
91 return git_config_set(argv[1], argv[2]);
92 case 4:
93 if (!strcmp(argv[1], "--unset"))
94 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
95 else if (!strcmp(argv[1], "--unset-all"))
96 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
97 else if (!strcmp(argv[1], "--get"))
98 return get_value(argv[2], argv[3]);
99 else if (!strcmp(argv[1], "--get-all")) {
100 do_all = 1;
101 return get_value(argv[2], argv[3]);
102 } else if (!strcmp(argv[1], "--replace-all"))
104 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
105 else
107 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
108 case 5:
109 if (!strcmp(argv[1], "--replace-all"))
110 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
111 case 1:
112 default:
113 usage(git_config_set_usage);
115 return 0;