]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Eric Biederman, 2005 | |
5 | */ | |
6 | ||
7 | #define USE_THE_REPOSITORY_VARIABLE | |
8 | ||
9 | #include "builtin.h" | |
10 | ||
11 | #include "attr.h" | |
12 | #include "config.h" | |
13 | #include "editor.h" | |
14 | #include "ident.h" | |
15 | #include "pager.h" | |
16 | #include "refs.h" | |
17 | #include "path.h" | |
18 | #include "strbuf.h" | |
19 | #include "run-command.h" | |
20 | ||
21 | static const char var_usage[] = "git var (-l | <variable>)"; | |
22 | ||
23 | static char *committer(int ident_flag) | |
24 | { | |
25 | return xstrdup_or_null(git_committer_info(ident_flag)); | |
26 | } | |
27 | ||
28 | static char *author(int ident_flag) | |
29 | { | |
30 | return xstrdup_or_null(git_author_info(ident_flag)); | |
31 | } | |
32 | ||
33 | static char *editor(int ident_flag UNUSED) | |
34 | { | |
35 | return xstrdup_or_null(git_editor()); | |
36 | } | |
37 | ||
38 | static char *sequence_editor(int ident_flag UNUSED) | |
39 | { | |
40 | return xstrdup_or_null(git_sequence_editor()); | |
41 | } | |
42 | ||
43 | static char *pager(int ident_flag UNUSED) | |
44 | { | |
45 | const char *pgm = git_pager(the_repository, 1); | |
46 | ||
47 | if (!pgm) | |
48 | pgm = "cat"; | |
49 | return xstrdup(pgm); | |
50 | } | |
51 | ||
52 | static char *default_branch(int ident_flag UNUSED) | |
53 | { | |
54 | return repo_default_branch_name(the_repository, 1); | |
55 | } | |
56 | ||
57 | static char *shell_path(int ident_flag UNUSED) | |
58 | { | |
59 | return git_shell_path(); | |
60 | } | |
61 | ||
62 | static char *git_attr_val_system(int ident_flag UNUSED) | |
63 | { | |
64 | if (git_attr_system_is_enabled()) { | |
65 | char *file = xstrdup(git_attr_system_file()); | |
66 | normalize_path_copy(file, file); | |
67 | return file; | |
68 | } | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | static char *git_attr_val_global(int ident_flag UNUSED) | |
73 | { | |
74 | char *file = xstrdup_or_null(git_attr_global_file()); | |
75 | if (file) { | |
76 | normalize_path_copy(file, file); | |
77 | return file; | |
78 | } | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | static char *git_config_val_system(int ident_flag UNUSED) | |
83 | { | |
84 | if (git_config_system()) { | |
85 | char *file = git_system_config(); | |
86 | normalize_path_copy(file, file); | |
87 | return file; | |
88 | } | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | static char *git_config_val_global(int ident_flag UNUSED) | |
93 | { | |
94 | struct strbuf buf = STRBUF_INIT; | |
95 | char *user, *xdg; | |
96 | size_t unused; | |
97 | ||
98 | git_global_config_paths(&user, &xdg); | |
99 | if (xdg && *xdg) { | |
100 | normalize_path_copy(xdg, xdg); | |
101 | strbuf_addf(&buf, "%s\n", xdg); | |
102 | } | |
103 | if (user && *user) { | |
104 | normalize_path_copy(user, user); | |
105 | strbuf_addf(&buf, "%s\n", user); | |
106 | } | |
107 | free(xdg); | |
108 | free(user); | |
109 | strbuf_trim_trailing_newline(&buf); | |
110 | if (buf.len == 0) { | |
111 | strbuf_release(&buf); | |
112 | return NULL; | |
113 | } | |
114 | return strbuf_detach(&buf, &unused); | |
115 | } | |
116 | ||
117 | struct git_var { | |
118 | const char *name; | |
119 | char *(*read)(int); | |
120 | int multivalued; | |
121 | }; | |
122 | static struct git_var git_vars[] = { | |
123 | { | |
124 | .name = "GIT_COMMITTER_IDENT", | |
125 | .read = committer, | |
126 | }, | |
127 | { | |
128 | .name = "GIT_AUTHOR_IDENT", | |
129 | .read = author, | |
130 | }, | |
131 | { | |
132 | .name = "GIT_EDITOR", | |
133 | .read = editor, | |
134 | }, | |
135 | { | |
136 | .name = "GIT_SEQUENCE_EDITOR", | |
137 | .read = sequence_editor, | |
138 | }, | |
139 | { | |
140 | .name = "GIT_PAGER", | |
141 | .read = pager, | |
142 | }, | |
143 | { | |
144 | .name = "GIT_DEFAULT_BRANCH", | |
145 | .read = default_branch, | |
146 | }, | |
147 | { | |
148 | .name = "GIT_SHELL_PATH", | |
149 | .read = shell_path, | |
150 | }, | |
151 | { | |
152 | .name = "GIT_ATTR_SYSTEM", | |
153 | .read = git_attr_val_system, | |
154 | }, | |
155 | { | |
156 | .name = "GIT_ATTR_GLOBAL", | |
157 | .read = git_attr_val_global, | |
158 | }, | |
159 | { | |
160 | .name = "GIT_CONFIG_SYSTEM", | |
161 | .read = git_config_val_system, | |
162 | }, | |
163 | { | |
164 | .name = "GIT_CONFIG_GLOBAL", | |
165 | .read = git_config_val_global, | |
166 | .multivalued = 1, | |
167 | }, | |
168 | { | |
169 | .name = "", | |
170 | .read = NULL, | |
171 | }, | |
172 | }; | |
173 | ||
174 | static void list_vars(void) | |
175 | { | |
176 | struct git_var *ptr; | |
177 | char *val; | |
178 | ||
179 | for (ptr = git_vars; ptr->read; ptr++) | |
180 | if ((val = ptr->read(0))) { | |
181 | if (ptr->multivalued && *val) { | |
182 | struct string_list list = STRING_LIST_INIT_DUP; | |
183 | ||
184 | string_list_split(&list, val, '\n', -1); | |
185 | for (size_t i = 0; i < list.nr; i++) | |
186 | printf("%s=%s\n", ptr->name, list.items[i].string); | |
187 | string_list_clear(&list, 0); | |
188 | } else { | |
189 | printf("%s=%s\n", ptr->name, val); | |
190 | } | |
191 | free(val); | |
192 | } | |
193 | } | |
194 | ||
195 | static const struct git_var *get_git_var(const char *var) | |
196 | { | |
197 | struct git_var *ptr; | |
198 | for (ptr = git_vars; ptr->read; ptr++) { | |
199 | if (strcmp(var, ptr->name) == 0) { | |
200 | return ptr; | |
201 | } | |
202 | } | |
203 | return NULL; | |
204 | } | |
205 | ||
206 | static int show_config(const char *var, const char *value, | |
207 | const struct config_context *ctx, void *cb) | |
208 | { | |
209 | if (value) | |
210 | printf("%s=%s\n", var, value); | |
211 | else | |
212 | printf("%s\n", var); | |
213 | return git_default_config(var, value, ctx, cb); | |
214 | } | |
215 | ||
216 | int cmd_var(int argc, | |
217 | const char **argv, | |
218 | const char *prefix UNUSED, | |
219 | struct repository *repo UNUSED) | |
220 | { | |
221 | const struct git_var *git_var; | |
222 | char *val; | |
223 | ||
224 | show_usage_if_asked(argc, argv, var_usage); | |
225 | if (argc != 2) | |
226 | usage(var_usage); | |
227 | ||
228 | if (strcmp(argv[1], "-l") == 0) { | |
229 | git_config(show_config, NULL); | |
230 | list_vars(); | |
231 | return 0; | |
232 | } | |
233 | git_config(git_default_config, NULL); | |
234 | ||
235 | git_var = get_git_var(argv[1]); | |
236 | if (!git_var) | |
237 | usage(var_usage); | |
238 | ||
239 | val = git_var->read(IDENT_STRICT); | |
240 | if (!val) | |
241 | return 1; | |
242 | ||
243 | printf("%s\n", val); | |
244 | free(val); | |
245 | ||
246 | return 0; | |
247 | } |