repo-config.c 4.0 KB
Newer Older
1
#include "cache.h"
2
#include <regex.h>
3 4

static const char git_config_set_usage[] =
5
"git-repo-config [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list";
6 7

static char* key = NULL;
8
static regex_t* key_regexp = NULL;
9
static regex_t* regexp = NULL;
10 11
static int show_keys = 0;
static int use_key_regexp = 0;
12
static int do_all = 0;
13
static int do_not_match = 0;
14
static int seen = 0;
15
static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
16

P
Petr Baudis 已提交
17 18 19 20 21 22 23 24 25
static int show_all_config(const char *key_, const char *value_)
{
	if (value_)
		printf("%s=%s\n", key_, value_);
	else
		printf("%s\n", key_);
	return 0;
}

26 27
static int show_config(const char* key_, const char* value_)
{
28 29
	char value[256];
	const char *vptr = value;
30
	int dup_error = 0;
31

32 33 34
	if (value_ == NULL)
		value_ = "";

35 36 37 38 39
	if (!use_key_regexp && strcmp(key_, key))
		return 0;
	if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
		return 0;
	if (regexp != NULL &&
40
			 (do_not_match ^
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
			  regexec(regexp, value_, 0, NULL, 0)))
		return 0;

	if (show_keys)
		printf("%s ", key_);
	if (seen && !do_all)
		dup_error = 1;
	if (type == T_INT)
		sprintf(value, "%d", git_config_int(key_, value_));
	else if (type == T_BOOL)
		vptr = git_config_bool(key_, value_) ? "true" : "false";
	else
		vptr = value_;
	seen++;
	if (dup_error) {
		error("More than one value for the key %s: %s",
				key_, vptr);
58
	}
59 60 61
	else
		printf("%s\n", vptr);

62 63 64 65 66 67 68 69 70 71
	return 0;
}

static int get_value(const char* key_, const char* regex_)
{
	int i;

	key = malloc(strlen(key_)+1);
	for (i = 0; key_[i]; i++)
		key[i] = tolower(key_[i]);
72
	key[i] = 0;
73

74 75 76
	if (use_key_regexp) {
		key_regexp = (regex_t*)malloc(sizeof(regex_t));
		if (regcomp(key_regexp, key, REG_EXTENDED)) {
77
			fprintf(stderr, "Invalid key pattern: %s\n", key_);
78 79 80 81
			return -1;
		}
	}

82
	if (regex_) {
83 84 85 86 87
		if (regex_[0] == '!') {
			do_not_match = 1;
			regex_++;
		}

88 89
		regexp = (regex_t*)malloc(sizeof(regex_t));
		if (regcomp(regexp, regex_, REG_EXTENDED)) {
90 91 92 93 94
			fprintf(stderr, "Invalid pattern: %s\n", regex_);
			return -1;
		}
	}

P
Petr Baudis 已提交
95
	git_config(show_config);
96
	free(key);
97 98 99
	if (regexp) {
		regfree(regexp);
		free(regexp);
100 101 102
	}

	if (do_all)
103
		return !seen;
104

105
	return (seen == 1) ? 0 : 1;
106
}
107 108 109 110

int main(int argc, const char **argv)
{
	setup_git_directory();
111 112 113 114 115 116

	while (1 < argc) {
		if (!strcmp(argv[1], "--int"))
			type = T_INT;
		else if (!strcmp(argv[1], "--bool"))
			type = T_BOOL;
117 118
		else if (!strcmp(argv[1], "--list") || !strcmp(argv[1], "-l"))
			return git_config(show_all_config);
119 120 121 122 123 124
		else
			break;
		argc--;
		argv++;
	}

125 126
	switch (argc) {
	case 2:
127
		return get_value(argv[1], NULL);
128 129 130
	case 3:
		if (!strcmp(argv[1], "--unset"))
			return git_config_set(argv[2], NULL);
131 132 133 134 135 136 137
		else if (!strcmp(argv[1], "--unset-all"))
			return git_config_set_multivar(argv[2], NULL, NULL, 1);
		else if (!strcmp(argv[1], "--get"))
			return get_value(argv[2], NULL);
		else if (!strcmp(argv[1], "--get-all")) {
			do_all = 1;
			return get_value(argv[2], NULL);
138 139 140 141 142
		} else if (!strcmp(argv[1], "--get-regexp")) {
			show_keys = 1;
			use_key_regexp = 1;
			do_all = 1;
			return get_value(argv[2], NULL);
143 144
		} else

145 146 147
			return git_config_set(argv[1], argv[2]);
	case 4:
		if (!strcmp(argv[1], "--unset"))
148 149 150 151 152 153 154 155
			return git_config_set_multivar(argv[2], NULL, argv[3], 0);
		else if (!strcmp(argv[1], "--unset-all"))
			return git_config_set_multivar(argv[2], NULL, argv[3], 1);
		else if (!strcmp(argv[1], "--get"))
			return get_value(argv[2], argv[3]);
		else if (!strcmp(argv[1], "--get-all")) {
			do_all = 1;
			return get_value(argv[2], argv[3]);
156 157 158 159 160
		} else if (!strcmp(argv[1], "--get-regexp")) {
			show_keys = 1;
			use_key_regexp = 1;
			do_all = 1;
			return get_value(argv[2], argv[3]);
161 162 163
		} else if (!strcmp(argv[1], "--replace-all"))

			return git_config_set_multivar(argv[2], argv[3], NULL, 1);
164
		else
165 166 167 168 169 170

			return git_config_set_multivar(argv[1], argv[2], argv[3], 0);
	case 5:
		if (!strcmp(argv[1], "--replace-all"))
			return git_config_set_multivar(argv[2], argv[3], argv[4], 1);
	case 1:
171 172 173 174 175
	default:
		usage(git_config_set_usage);
	}
	return 0;
}