blob: c5ebb7668a2632ef94cc798552ddd3f6f173e4f4 [file] [log] [blame]
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +01001#include "cache.h"
Johannes Schindelin4ddba792005-11-20 06:52:22 +01002#include <regex.h>
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +01003
4static const char git_config_set_usage[] =
Petr Baudis7162dff2006-02-12 04:14:48 +01005"git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
Johannes Schindelin4ddba792005-11-20 06:52:22 +01006
7static char* key = NULL;
8static char* value = NULL;
Amos Waterland0a152172006-01-04 19:31:02 -05009static regex_t* regexp = NULL;
Johannes Schindelin4ddba792005-11-20 06:52:22 +010010static int do_all = 0;
Johannes Schindelinf98d8632005-11-20 13:24:18 +010011static int do_not_match = 0;
Johannes Schindelin4ddba792005-11-20 06:52:22 +010012static int seen = 0;
Petr Baudis7162dff2006-02-12 04:14:48 +010013static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
Johannes Schindelin4ddba792005-11-20 06:52:22 +010014
15static int show_config(const char* key_, const char* value_)
16{
Jonas Fonsecaf067a132006-03-06 06:23:30 +010017 if (value_ == NULL)
18 value_ = "";
19
Johannes Schindelin4ddba792005-11-20 06:52:22 +010020 if (!strcmp(key_, key) &&
Amos Waterland0a152172006-01-04 19:31:02 -050021 (regexp == NULL ||
Johannes Schindelinf98d8632005-11-20 13:24:18 +010022 (do_not_match ^
Amos Waterland0a152172006-01-04 19:31:02 -050023 !regexec(regexp, value_, 0, NULL, 0)))) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +010024 if (do_all) {
25 printf("%s\n", value_);
26 return 0;
27 }
28 if (seen > 0) {
29 fprintf(stderr, "More than one value: %s\n", value);
30 free(value);
31 }
Petr Baudis7162dff2006-02-12 04:14:48 +010032
33 if (type == T_INT) {
34 value = malloc(256);
35 sprintf(value, "%d", git_config_int(key_, value_));
36 } else if (type == T_BOOL) {
37 value = malloc(256);
38 sprintf(value, "%s", git_config_bool(key_, value_)
39 ? "true" : "false");
40 } else {
Jonas Fonsecaf067a132006-03-06 06:23:30 +010041 value = strdup(value_);
Petr Baudis7162dff2006-02-12 04:14:48 +010042 }
Johannes Schindelin4ddba792005-11-20 06:52:22 +010043 seen++;
44 }
45 return 0;
46}
47
48static int get_value(const char* key_, const char* regex_)
49{
50 int i;
51
52 key = malloc(strlen(key_)+1);
53 for (i = 0; key_[i]; i++)
54 key[i] = tolower(key_[i]);
Johannes Schindelin3dd94e32005-11-21 11:18:20 +010055 key[i] = 0;
Johannes Schindelin4ddba792005-11-20 06:52:22 +010056
57 if (regex_) {
Johannes Schindelinf98d8632005-11-20 13:24:18 +010058 if (regex_[0] == '!') {
59 do_not_match = 1;
60 regex_++;
61 }
62
Amos Waterland0a152172006-01-04 19:31:02 -050063 regexp = (regex_t*)malloc(sizeof(regex_t));
64 if (regcomp(regexp, regex_, REG_EXTENDED)) {
Johannes Schindelin4ddba792005-11-20 06:52:22 +010065 fprintf(stderr, "Invalid pattern: %s\n", regex_);
66 return -1;
67 }
68 }
69
70 i = git_config(show_config);
71 if (value) {
72 printf("%s\n", value);
73 free(value);
74 }
75 free(key);
Amos Waterland0a152172006-01-04 19:31:02 -050076 if (regexp) {
77 regfree(regexp);
78 free(regexp);
Johannes Schindelin4ddba792005-11-20 06:52:22 +010079 }
80
81 if (do_all)
82 return 0;
83
84 return seen == 1 ? 0 : 1;
85}
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +010086
87int main(int argc, const char **argv)
88{
89 setup_git_directory();
Petr Baudis7162dff2006-02-12 04:14:48 +010090
91 while (1 < argc) {
92 if (!strcmp(argv[1], "--int"))
93 type = T_INT;
94 else if (!strcmp(argv[1], "--bool"))
95 type = T_BOOL;
96 else
97 break;
98 argc--;
99 argv++;
100 }
101
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100102 switch (argc) {
103 case 2:
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100104 return get_value(argv[1], NULL);
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100105 case 3:
106 if (!strcmp(argv[1], "--unset"))
107 return git_config_set(argv[2], NULL);
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100108 else if (!strcmp(argv[1], "--unset-all"))
109 return git_config_set_multivar(argv[2], NULL, NULL, 1);
110 else if (!strcmp(argv[1], "--get"))
111 return get_value(argv[2], NULL);
112 else if (!strcmp(argv[1], "--get-all")) {
113 do_all = 1;
114 return get_value(argv[2], NULL);
115 } else
116
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100117 return git_config_set(argv[1], argv[2]);
118 case 4:
119 if (!strcmp(argv[1], "--unset"))
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100120 return git_config_set_multivar(argv[2], NULL, argv[3], 0);
121 else if (!strcmp(argv[1], "--unset-all"))
122 return git_config_set_multivar(argv[2], NULL, argv[3], 1);
123 else if (!strcmp(argv[1], "--get"))
124 return get_value(argv[2], argv[3]);
125 else if (!strcmp(argv[1], "--get-all")) {
126 do_all = 1;
127 return get_value(argv[2], argv[3]);
128 } else if (!strcmp(argv[1], "--replace-all"))
129
130 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100131 else
Johannes Schindelin4ddba792005-11-20 06:52:22 +0100132
133 return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
134 case 5:
135 if (!strcmp(argv[1], "--replace-all"))
136 return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
137 case 1:
Johannes Schindelin1b1e59c2005-11-17 22:44:55 +0100138 default:
139 usage(git_config_set_usage);
140 }
141 return 0;
142}