git

Форк
0
/
var.c 
240 строк · 4.3 Кб
1
/*
2
 * GIT - The information manager from hell
3
 *
4
 * Copyright (C) Eric Biederman, 2005
5
 */
6
#include "builtin.h"
7
#include "attr.h"
8
#include "config.h"
9
#include "editor.h"
10
#include "ident.h"
11
#include "pager.h"
12
#include "refs.h"
13
#include "path.h"
14
#include "strbuf.h"
15
#include "run-command.h"
16

17
static const char var_usage[] = "git var (-l | <variable>)";
18

19
static char *committer(int ident_flag)
20
{
21
	return xstrdup_or_null(git_committer_info(ident_flag));
22
}
23

24
static char *author(int ident_flag)
25
{
26
	return xstrdup_or_null(git_author_info(ident_flag));
27
}
28

29
static char *editor(int ident_flag UNUSED)
30
{
31
	return xstrdup_or_null(git_editor());
32
}
33

34
static char *sequence_editor(int ident_flag UNUSED)
35
{
36
	return xstrdup_or_null(git_sequence_editor());
37
}
38

39
static char *pager(int ident_flag UNUSED)
40
{
41
	const char *pgm = git_pager(1);
42

43
	if (!pgm)
44
		pgm = "cat";
45
	return xstrdup(pgm);
46
}
47

48
static char *default_branch(int ident_flag UNUSED)
49
{
50
	return repo_default_branch_name(the_repository, 1);
51
}
52

53
static char *shell_path(int ident_flag UNUSED)
54
{
55
	return git_shell_path();
56
}
57

58
static char *git_attr_val_system(int ident_flag UNUSED)
59
{
60
	if (git_attr_system_is_enabled()) {
61
		char *file = xstrdup(git_attr_system_file());
62
		normalize_path_copy(file, file);
63
		return file;
64
	}
65
	return NULL;
66
}
67

68
static char *git_attr_val_global(int ident_flag UNUSED)
69
{
70
	char *file = xstrdup_or_null(git_attr_global_file());
71
	if (file) {
72
		normalize_path_copy(file, file);
73
		return file;
74
	}
75
	return NULL;
76
}
77

78
static char *git_config_val_system(int ident_flag UNUSED)
79
{
80
	if (git_config_system()) {
81
		char *file = git_system_config();
82
		normalize_path_copy(file, file);
83
		return file;
84
	}
85
	return NULL;
86
}
87

88
static char *git_config_val_global(int ident_flag UNUSED)
89
{
90
	struct strbuf buf = STRBUF_INIT;
91
	char *user, *xdg;
92
	size_t unused;
93

94
	git_global_config_paths(&user, &xdg);
95
	if (xdg && *xdg) {
96
		normalize_path_copy(xdg, xdg);
97
		strbuf_addf(&buf, "%s\n", xdg);
98
	}
99
	if (user && *user) {
100
		normalize_path_copy(user, user);
101
		strbuf_addf(&buf, "%s\n", user);
102
	}
103
	free(xdg);
104
	free(user);
105
	strbuf_trim_trailing_newline(&buf);
106
	if (buf.len == 0) {
107
		strbuf_release(&buf);
108
		return NULL;
109
	}
110
	return strbuf_detach(&buf, &unused);
111
}
112

113
struct git_var {
114
	const char *name;
115
	char *(*read)(int);
116
	int multivalued;
117
};
118
static struct git_var git_vars[] = {
119
	{
120
		.name = "GIT_COMMITTER_IDENT",
121
		.read = committer,
122
	},
123
	{
124
		.name = "GIT_AUTHOR_IDENT",
125
		.read = author,
126
	},
127
	{
128
		.name = "GIT_EDITOR",
129
		.read = editor,
130
	},
131
	{
132
		.name = "GIT_SEQUENCE_EDITOR",
133
		.read = sequence_editor,
134
	},
135
	{
136
		.name = "GIT_PAGER",
137
		.read = pager,
138
	},
139
	{
140
		.name = "GIT_DEFAULT_BRANCH",
141
		.read = default_branch,
142
	},
143
	{
144
		.name = "GIT_SHELL_PATH",
145
		.read = shell_path,
146
	},
147
	{
148
		.name = "GIT_ATTR_SYSTEM",
149
		.read = git_attr_val_system,
150
	},
151
	{
152
		.name = "GIT_ATTR_GLOBAL",
153
		.read = git_attr_val_global,
154
	},
155
	{
156
		.name = "GIT_CONFIG_SYSTEM",
157
		.read = git_config_val_system,
158
	},
159
	{
160
		.name = "GIT_CONFIG_GLOBAL",
161
		.read = git_config_val_global,
162
		.multivalued = 1,
163
	},
164
	{
165
		.name = "",
166
		.read = NULL,
167
	},
168
};
169

170
static void list_vars(void)
171
{
172
	struct git_var *ptr;
173
	char *val;
174

175
	for (ptr = git_vars; ptr->read; ptr++)
176
		if ((val = ptr->read(0))) {
177
			if (ptr->multivalued && *val) {
178
				struct string_list list = STRING_LIST_INIT_DUP;
179
				int i;
180

181
				string_list_split(&list, val, '\n', -1);
182
				for (i = 0; i < list.nr; i++)
183
					printf("%s=%s\n", ptr->name, list.items[i].string);
184
				string_list_clear(&list, 0);
185
			} else {
186
				printf("%s=%s\n", ptr->name, val);
187
			}
188
			free(val);
189
		}
190
}
191

192
static const struct git_var *get_git_var(const char *var)
193
{
194
	struct git_var *ptr;
195
	for (ptr = git_vars; ptr->read; ptr++) {
196
		if (strcmp(var, ptr->name) == 0) {
197
			return ptr;
198
		}
199
	}
200
	return NULL;
201
}
202

203
static int show_config(const char *var, const char *value,
204
		       const struct config_context *ctx, void *cb)
205
{
206
	if (value)
207
		printf("%s=%s\n", var, value);
208
	else
209
		printf("%s\n", var);
210
	return git_default_config(var, value, ctx, cb);
211
}
212

213
int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
214
{
215
	const struct git_var *git_var;
216
	char *val;
217

218
	if (argc != 2)
219
		usage(var_usage);
220

221
	if (strcmp(argv[1], "-l") == 0) {
222
		git_config(show_config, NULL);
223
		list_vars();
224
		return 0;
225
	}
226
	git_config(git_default_config, NULL);
227

228
	git_var = get_git_var(argv[1]);
229
	if (!git_var)
230
		usage(var_usage);
231

232
	val = git_var->read(IDENT_STRICT);
233
	if (!val)
234
		return 1;
235

236
	printf("%s\n", val);
237
	free(val);
238

239
	return 0;
240
}
241

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.