rename detection with -M100 means "exact renames only".
[git/jrn.git] / config-set.c
blob5f654f7aff1cfa4cdbe5400d328363d4a91d490c
1 #include "cache.h"
2 #include <regex.h>
4 static const char git_config_set_usage[] =
5 "git-config-set [--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]);
42 if (regex_) {
43 if (regex_[0] == '!') {
44 do_not_match = 1;
45 regex_++;
48 regex = (regex_t*)malloc(sizeof(regex_t));
49 if (regcomp(regex, regex_, REG_EXTENDED)) {
50 fprintf(stderr, "Invalid pattern: %s\n", regex_);
51 return -1;
55 i = git_config(show_config);
56 if (value) {
57 printf("%s\n", value);
58 free(value);
60 free(key);
61 if (regex) {
62 regfree(regex);
63 free(regex);
66 if (do_all)
67 return 0;
69 return seen == 1 ? 0 : 1;
72 int main(int argc, const char **argv)
74 setup_git_directory();
75 switch (argc) {
76 case 2:
77 return get_value(argv[1], NULL);
78 case 3:
79 if (!strcmp(argv[1], "--unset"))
80 return git_config_set(argv[2], NULL);
81 else if (!strcmp(argv[1], "--unset-all"))
82 return git_config_set_multivar(argv[2], NULL, NULL, 1);
83 else if (!strcmp(argv[1], "--get"))
84 return get_value(argv[2], NULL);
85 else if (!strcmp(argv[1], "--get-all")) {
86 do_all = 1;
87 return get_value(argv[2], NULL);
88 } else
90 return git_config_set(argv[1], argv[2]);
91 case 4:
92 if (!strcmp(argv[1], "--unset"))
93 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
94 else if (!strcmp(argv[1], "--unset-all"))
95 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
96 else if (!strcmp(argv[1], "--get"))
97 return get_value(argv[2], argv[3]);
98 else if (!strcmp(argv[1], "--get-all")) {
99 do_all = 1;
100 return get_value(argv[2], argv[3]);
101 } else if (!strcmp(argv[1], "--replace-all"))
103 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
104 else
106 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
107 case 5:
108 if (!strcmp(argv[1], "--replace-all"))
109 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
110 case 1:
111 default:
112 usage(git_config_set_usage);
114 return 0;