Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 1 | #include "cache.h" |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 2 | #include <regex.h> |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 3 | |
| 4 | static const char git_config_set_usage[] = |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 5 | "git-repo-config [ --bool | --int ] [--get | --get-all | --replace-all | --unset | --unset-all] name [value [value_regex]]"; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 6 | |
| 7 | static char* key = NULL; |
| 8 | static char* value = NULL; |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 9 | static regex_t* regexp = NULL; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 10 | static int do_all = 0; |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 11 | static int do_not_match = 0; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 12 | static int seen = 0; |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 13 | static enum { T_RAW, T_INT, T_BOOL } type = T_RAW; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 14 | |
| 15 | static int show_config(const char* key_, const char* value_) |
| 16 | { |
Jonas Fonseca | f067a13 | 2006-03-06 06:23:30 +0100 | [diff] [blame] | 17 | if (value_ == NULL) |
| 18 | value_ = ""; |
| 19 | |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 20 | if (!strcmp(key_, key) && |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 21 | (regexp == NULL || |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 22 | (do_not_match ^ |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 23 | !regexec(regexp, value_, 0, NULL, 0)))) { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 24 | 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 Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 32 | |
| 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 Fonseca | f067a13 | 2006-03-06 06:23:30 +0100 | [diff] [blame] | 41 | value = strdup(value_); |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 42 | } |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 43 | seen++; |
| 44 | } |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static 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 Schindelin | 3dd94e3 | 2005-11-21 11:18:20 +0100 | [diff] [blame] | 55 | key[i] = 0; |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 56 | |
| 57 | if (regex_) { |
Johannes Schindelin | f98d863 | 2005-11-20 13:24:18 +0100 | [diff] [blame] | 58 | if (regex_[0] == '!') { |
| 59 | do_not_match = 1; |
| 60 | regex_++; |
| 61 | } |
| 62 | |
Amos Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 63 | regexp = (regex_t*)malloc(sizeof(regex_t)); |
| 64 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 65 | 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 Waterland | 0a15217 | 2006-01-04 19:31:02 -0500 | [diff] [blame] | 76 | if (regexp) { |
| 77 | regfree(regexp); |
| 78 | free(regexp); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (do_all) |
| 82 | return 0; |
| 83 | |
| 84 | return seen == 1 ? 0 : 1; |
| 85 | } |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 86 | |
| 87 | int main(int argc, const char **argv) |
| 88 | { |
| 89 | setup_git_directory(); |
Petr Baudis | 7162dff | 2006-02-12 04:14:48 +0100 | [diff] [blame] | 90 | |
| 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 Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 102 | switch (argc) { |
| 103 | case 2: |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 104 | return get_value(argv[1], NULL); |
Johannes Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 105 | case 3: |
| 106 | if (!strcmp(argv[1], "--unset")) |
| 107 | return git_config_set(argv[2], NULL); |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 108 | 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 Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 117 | return git_config_set(argv[1], argv[2]); |
| 118 | case 4: |
| 119 | if (!strcmp(argv[1], "--unset")) |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 120 | 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 Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 131 | else |
Johannes Schindelin | 4ddba79 | 2005-11-20 06:52:22 +0100 | [diff] [blame] | 132 | |
| 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 Schindelin | 1b1e59c | 2005-11-17 22:44:55 +0100 | [diff] [blame] | 138 | default: |
| 139 | usage(git_config_set_usage); |
| 140 | } |
| 141 | return 0; |
| 142 | } |