🌐 AI搜索 & 代理 主页
blob: 9cf65193f95022e50e51d380ca050bef87653555 [file] [log] [blame]
Johannes Schindelin1b1e59c2005-11-17 21:44:551#include "cache.h"
Johannes Schindelin4ddba792005-11-20 05:52:222#include <regex.h>
Johannes Schindelin1b1e59c2005-11-17 21:44:553
4static const char git_config_set_usage[] =
Petr Baudis7162dff2006-02-12 03:14:485"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
Johannes Schindelin4ddba792005-11-20 05:52:226
7static char* key = NULL;
8static char* value = NULL;
Amos Waterland0a152172006-01-05 00:31:029static regex_t* regexp = NULL;
Johannes Schindelin4ddba792005-11-20 05:52:2210static int do_all = 0;
Johannes Schindelinf98d8632005-11-20 12:24:1811static int do_not_match = 0;
Johannes Schindelin4ddba792005-11-20 05:52:2212static int seen = 0;
Petr Baudis7162dff2006-02-12 03:14:4813static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
Johannes Schindelin4ddba792005-11-20 05:52:2214
15static int show_config(const char* key_, const char* value_)
16{
17 if (!strcmp(key_, key) &&
Amos Waterland0a152172006-01-05 00:31:0218 (regexp == NULL ||
Johannes Schindelinf98d8632005-11-20 12:24:1819 (do_not_match ^
Amos Waterland0a152172006-01-05 00:31:0220 !regexec(regexp, value_, 0, NULL, 0)))) {
Johannes Schindelin4ddba792005-11-20 05:52:2221 if (do_all) {
22 printf("%s\n", value_);
23 return 0;
24 }
25 if (seen > 0) {
26 fprintf(stderr, "More than one value: %s\n", value);
27 free(value);
28 }
Petr Baudis7162dff2006-02-12 03:14:4829
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_ : "");
39 }
Johannes Schindelin4ddba792005-11-20 05:52:2240 seen++;
41 }
42 return 0;
43}
44
45static int get_value(const char* key_, const char* regex_)
46{
47 int i;
48
49 key = malloc(strlen(key_)+1);
50 for (i = 0; key_[i]; i++)
51 key[i] = tolower(key_[i]);
Johannes Schindelin3dd94e32005-11-21 10:18:2052 key[i] = 0;
Johannes Schindelin4ddba792005-11-20 05:52:2253
54 if (regex_) {
Johannes Schindelinf98d8632005-11-20 12:24:1855 if (regex_[0] == '!') {
56 do_not_match = 1;
57 regex_++;
58 }
59
Amos Waterland0a152172006-01-05 00:31:0260 regexp = (regex_t*)malloc(sizeof(regex_t));
61 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Johannes Schindelin4ddba792005-11-20 05:52:2262 fprintf(stderr, "Invalid pattern: %s\n", regex_);
63 return -1;
64 }
65 }
66
67 i = git_config(show_config);
68 if (value) {
69 printf("%s\n", value);
70 free(value);
71 }
72 free(key);
Amos Waterland0a152172006-01-05 00:31:0273 if (regexp) {
74 regfree(regexp);
75 free(regexp);
Johannes Schindelin4ddba792005-11-20 05:52:2276 }
77
78 if (do_all)
79 return 0;
80
81 return seen == 1 ? 0 : 1;
82}
Johannes Schindelin1b1e59c2005-11-17 21:44:5583
84int main(int argc, const char **argv)
85{
86 setup_git_directory();
Petr Baudis7162dff2006-02-12 03:14:4887
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++;
97 }
98
Johannes Schindelin1b1e59c2005-11-17 21:44:5599 switch (argc) {
100 case 2:
Johannes Schindelin4ddba792005-11-20 05:52:22101 return get_value(argv[1], NULL);
Johannes Schindelin1b1e59c2005-11-17 21:44:55102 case 3:
103 if (!strcmp(argv[1], "--unset"))
104 return git_config_set(argv[2], NULL);
Johannes Schindelin4ddba792005-11-20 05:52:22105 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
113
Johannes Schindelin1b1e59c2005-11-17 21:44:55114 return git_config_set(argv[1], argv[2]);
115 case 4:
116 if (!strcmp(argv[1], "--unset"))
Johannes Schindelin4ddba792005-11-20 05:52:22117 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"))
126
127 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
Johannes Schindelin1b1e59c2005-11-17 21:44:55128 else
Johannes Schindelin4ddba792005-11-20 05:52:22129
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:
Johannes Schindelin1b1e59c2005-11-17 21:44:55135 default:
136 usage(git_config_set_usage);
137 }
138 return 0;
139}