git/Documentation: fix SYNOPSIS style bugs
[git/jnareb-git.git] / repo-config.c
blob9cf65193f95022e50e51d380ca050bef87653555
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 | --replace-all | --unset | --unset-all] name [value [value_regex]]";
7 static char* key = NULL;
8 static char* value = NULL;
9 static regex_t* regexp = NULL;
10 static int do_all = 0;
11 static int do_not_match = 0;
12 static int seen = 0;
13 static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
15 static int show_config(const char* key_, const char* value_)
17 if (!strcmp(key_, key) &&
18 (regexp == NULL ||
19 (do_not_match ^
20 !regexec(regexp, value_, 0, NULL, 0)))) {
21 if (do_all) {
22 printf("%s\n", value_);
23 return 0;
25 if (seen > 0) {
26 fprintf(stderr, "More than one value: %s\n", value);
27 free(value);
30 if (type == T_INT) {
31 value = malloc(256);
32 sprintf(value, "%d", git_config_int(key_, value_));
33 } else if (type == T_BOOL) {
34 value = malloc(256);
35 sprintf(value, "%s", git_config_bool(key_, value_)
36 ? "true" : "false");
37 } else {
38 value = strdup(value_ ? value_ : "");
40 seen++;
42 return 0;
45 static int get_value(const char* key_, const char* regex_)
47 int i;
49 key = malloc(strlen(key_)+1);
50 for (i = 0; key_[i]; i++)
51 key[i] = tolower(key_[i]);
52 key[i] = 0;
54 if (regex_) {
55 if (regex_[0] == '!') {
56 do_not_match = 1;
57 regex_++;
60 regexp = (regex_t*)malloc(sizeof(regex_t));
61 if (regcomp(regexp, regex_, REG_EXTENDED)) {
62 fprintf(stderr, "Invalid pattern: %s\n", regex_);
63 return -1;
67 i = git_config(show_config);
68 if (value) {
69 printf("%s\n", value);
70 free(value);
72 free(key);
73 if (regexp) {
74 regfree(regexp);
75 free(regexp);
78 if (do_all)
79 return 0;
81 return seen == 1 ? 0 : 1;
84 int main(int argc, const char **argv)
86 setup_git_directory();
88 while (1 < argc) {
89 if (!strcmp(argv[1], "--int"))
90 type = T_INT;
91 else if (!strcmp(argv[1], "--bool"))
92 type = T_BOOL;
93 else
94 break;
95 argc--;
96 argv++;
99 switch (argc) {
100 case 2:
101 return get_value(argv[1], NULL);
102 case 3:
103 if (!strcmp(argv[1], "--unset"))
104 return git_config_set(argv[2], NULL);
105 else if (!strcmp(argv[1], "--unset-all"))
106 return git_config_set_multivar(argv[2], NULL, NULL, 1);
107 else if (!strcmp(argv[1], "--get"))
108 return get_value(argv[2], NULL);
109 else if (!strcmp(argv[1], "--get-all")) {
110 do_all = 1;
111 return get_value(argv[2], NULL);
112 } else
114 return git_config_set(argv[1], argv[2]);
115 case 4:
116 if (!strcmp(argv[1], "--unset"))
117 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
118 else if (!strcmp(argv[1], "--unset-all"))
119 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
120 else if (!strcmp(argv[1], "--get"))
121 return get_value(argv[2], argv[3]);
122 else if (!strcmp(argv[1], "--get-all")) {
123 do_all = 1;
124 return get_value(argv[2], argv[3]);
125 } else if (!strcmp(argv[1], "--replace-all"))
127 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
128 else
130 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
131 case 5:
132 if (!strcmp(argv[1], "--replace-all"))
133 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
134 case 1:
135 default:
136 usage(git_config_set_usage);
138 return 0;