t5500: adjust to change in pack-object reporting behaviour.
[git/git.git] / repo-config.c
CommitLineData
1b1e59c5 1#include "cache.h"
4ddba79d 2#include <regex.h>
1b1e59c5
JS
3
4static const char git_config_set_usage[] =
ee72aeaf 5"git-repo-config [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]";
4ddba79d
JS
6
7static char* key = NULL;
8static char* value = NULL;
0a152171 9static regex_t* regexp = NULL;
4ddba79d 10static int do_all = 0;
f98d863d 11static int do_not_match = 0;
4ddba79d
JS
12static int seen = 0;
13
14static int show_config(const char* key_, const char* value_)
15{
16 if (!strcmp(key_, key) &&
0a152171 17 (regexp == NULL ||
f98d863d 18 (do_not_match ^
0a152171 19 !regexec(regexp, value_, 0, NULL, 0)))) {
4ddba79d
JS
20 if (do_all) {
21 printf("%s\n", value_);
22 return 0;
23 }
24 if (seen > 0) {
25 fprintf(stderr, "More than one value: %s\n", value);
26 free(value);
27 }
28 value = strdup(value_);
29 seen++;
30 }
31 return 0;
32}
33
34static int get_value(const char* key_, const char* regex_)
35{
36 int i;
37
38 key = malloc(strlen(key_)+1);
39 for (i = 0; key_[i]; i++)
40 key[i] = tolower(key_[i]);
3dd94e3b 41 key[i] = 0;
4ddba79d
JS
42
43 if (regex_) {
f98d863d
JS
44 if (regex_[0] == '!') {
45 do_not_match = 1;
46 regex_++;
47 }
48
0a152171
AW
49 regexp = (regex_t*)malloc(sizeof(regex_t));
50 if (regcomp(regexp, regex_, REG_EXTENDED)) {
4ddba79d
JS
51 fprintf(stderr, "Invalid pattern: %s\n", regex_);
52 return -1;
53 }
54 }
55
56 i = git_config(show_config);
57 if (value) {
58 printf("%s\n", value);
59 free(value);
60 }
61 free(key);
0a152171
AW
62 if (regexp) {
63 regfree(regexp);
64 free(regexp);
4ddba79d
JS
65 }
66
67 if (do_all)
68 return 0;
69
70 return seen == 1 ? 0 : 1;
71}
1b1e59c5
JS
72
73int main(int argc, const char **argv)
74{
75 setup_git_directory();
76 switch (argc) {
77 case 2:
4ddba79d 78 return get_value(argv[1], NULL);
1b1e59c5
JS
79 case 3:
80 if (!strcmp(argv[1], "--unset"))
81 return git_config_set(argv[2], NULL);
4ddba79d
JS
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
90
1b1e59c5
JS
91 return git_config_set(argv[1], argv[2]);
92 case 4:
93 if (!strcmp(argv[1], "--unset"))
4ddba79d
JS
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"))
103
104 return git_config_set_multivar(argv[2], argv[3], NULL, 1);
1b1e59c5 105 else
4ddba79d
JS
106
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:
1b1e59c5
JS
112 default:
113 usage(git_config_set_usage);
114 }
115 return 0;
116}