| Johannes Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 1 | #include "cache.h" |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 2 | #include <regex.h> |
| Johannes Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 3 | |
| 4 | static const char git_config_set_usage[] = |
| Petr Baudis | 7162dff | 2006-02-12 03:14:48 | [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 05:52:22 | [diff] [blame] | 6 | |
| 7 | static char* key = NULL; |
| 8 | static char* value = NULL; |
| Amos Waterland | 0a15217 | 2006-01-05 00:31:02 | [diff] [blame] | 9 | static regex_t* regexp = NULL; |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 10 | static int do_all = 0; |
| Johannes Schindelin | f98d863 | 2005-11-20 12:24:18 | [diff] [blame] | 11 | static int do_not_match = 0; |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 12 | static int seen = 0; |
| Petr Baudis | 7162dff | 2006-02-12 03:14:48 | [diff] [blame] | 13 | static enum { T_RAW, T_INT, T_BOOL } type = T_RAW; |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 14 | |
| 15 | static int show_config(const char* key_, const char* value_) |
| 16 | { |
| 17 | if (!strcmp(key_, key) && |
| Amos Waterland | 0a15217 | 2006-01-05 00:31:02 | [diff] [blame] | 18 | (regexp == NULL || |
| Johannes Schindelin | f98d863 | 2005-11-20 12:24:18 | [diff] [blame] | 19 | (do_not_match ^ |
| Amos Waterland | 0a15217 | 2006-01-05 00:31:02 | [diff] [blame] | 20 | !regexec(regexp, value_, 0, NULL, 0)))) { |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 21 | 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 Baudis | 7162dff | 2006-02-12 03:14:48 | [diff] [blame] | 29 | |
| 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 Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 40 | seen++; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static 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 Schindelin | 3dd94e3 | 2005-11-21 10:18:20 | [diff] [blame] | 52 | key[i] = 0; |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 53 | |
| 54 | if (regex_) { |
| Johannes Schindelin | f98d863 | 2005-11-20 12:24:18 | [diff] [blame] | 55 | if (regex_[0] == '!') { |
| 56 | do_not_match = 1; |
| 57 | regex_++; |
| 58 | } |
| 59 | |
| Amos Waterland | 0a15217 | 2006-01-05 00:31:02 | [diff] [blame] | 60 | regexp = (regex_t*)malloc(sizeof(regex_t)); |
| 61 | if (regcomp(regexp, regex_, REG_EXTENDED)) { |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 62 | 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 Waterland | 0a15217 | 2006-01-05 00:31:02 | [diff] [blame] | 73 | if (regexp) { |
| 74 | regfree(regexp); |
| 75 | free(regexp); |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (do_all) |
| 79 | return 0; |
| 80 | |
| 81 | return seen == 1 ? 0 : 1; |
| 82 | } |
| Johannes Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 83 | |
| 84 | int main(int argc, const char **argv) |
| 85 | { |
| 86 | setup_git_directory(); |
| Petr Baudis | 7162dff | 2006-02-12 03:14:48 | [diff] [blame] | 87 | |
| 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 Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 99 | switch (argc) { |
| 100 | case 2: |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 101 | return get_value(argv[1], NULL); |
| Johannes Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 102 | case 3: |
| 103 | if (!strcmp(argv[1], "--unset")) |
| 104 | return git_config_set(argv[2], NULL); |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 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 |
| 113 | |
| Johannes Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 114 | return git_config_set(argv[1], argv[2]); |
| 115 | case 4: |
| 116 | if (!strcmp(argv[1], "--unset")) |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 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")) |
| 126 | |
| 127 | return git_config_set_multivar(argv[2], argv[3], NULL, 1); |
| Johannes Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 128 | else |
| Johannes Schindelin | 4ddba79 | 2005-11-20 05:52:22 | [diff] [blame] | 129 | |
| 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 Schindelin | 1b1e59c | 2005-11-17 21:44:55 | [diff] [blame] | 135 | default: |
| 136 | usage(git_config_set_usage); |
| 137 | } |
| 138 | return 0; |
| 139 | } |