Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
21 | 0 | return xstrdup_or_null(git_committer_info(ident_flag)); |
22 | 0 | } |
23 | | |
24 | | static char *author(int ident_flag) |
25 | 0 | { |
26 | 0 | return xstrdup_or_null(git_author_info(ident_flag)); |
27 | 0 | } |
28 | | |
29 | | static char *editor(int ident_flag UNUSED) |
30 | 0 | { |
31 | 0 | return xstrdup_or_null(git_editor()); |
32 | 0 | } |
33 | | |
34 | | static char *sequence_editor(int ident_flag UNUSED) |
35 | 0 | { |
36 | 0 | return xstrdup_or_null(git_sequence_editor()); |
37 | 0 | } |
38 | | |
39 | | static char *pager(int ident_flag UNUSED) |
40 | 0 | { |
41 | 0 | const char *pgm = git_pager(1); |
42 | |
|
43 | 0 | if (!pgm) |
44 | 0 | pgm = "cat"; |
45 | 0 | return xstrdup(pgm); |
46 | 0 | } |
47 | | |
48 | | static char *default_branch(int ident_flag UNUSED) |
49 | 0 | { |
50 | 0 | return repo_default_branch_name(the_repository, 1); |
51 | 0 | } |
52 | | |
53 | | static char *shell_path(int ident_flag UNUSED) |
54 | 0 | { |
55 | 0 | return git_shell_path(); |
56 | 0 | } |
57 | | |
58 | | static char *git_attr_val_system(int ident_flag UNUSED) |
59 | 0 | { |
60 | 0 | if (git_attr_system_is_enabled()) { |
61 | 0 | char *file = xstrdup(git_attr_system_file()); |
62 | 0 | normalize_path_copy(file, file); |
63 | 0 | return file; |
64 | 0 | } |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | | static char *git_attr_val_global(int ident_flag UNUSED) |
69 | 0 | { |
70 | 0 | char *file = xstrdup_or_null(git_attr_global_file()); |
71 | 0 | if (file) { |
72 | 0 | normalize_path_copy(file, file); |
73 | 0 | return file; |
74 | 0 | } |
75 | 0 | return NULL; |
76 | 0 | } |
77 | | |
78 | | static char *git_config_val_system(int ident_flag UNUSED) |
79 | 0 | { |
80 | 0 | if (git_config_system()) { |
81 | 0 | char *file = git_system_config(); |
82 | 0 | normalize_path_copy(file, file); |
83 | 0 | return file; |
84 | 0 | } |
85 | 0 | return NULL; |
86 | 0 | } |
87 | | |
88 | | static char *git_config_val_global(int ident_flag UNUSED) |
89 | 0 | { |
90 | 0 | struct strbuf buf = STRBUF_INIT; |
91 | 0 | char *user, *xdg; |
92 | 0 | size_t unused; |
93 | |
|
94 | 0 | git_global_config_paths(&user, &xdg); |
95 | 0 | if (xdg && *xdg) { |
96 | 0 | normalize_path_copy(xdg, xdg); |
97 | 0 | strbuf_addf(&buf, "%s\n", xdg); |
98 | 0 | } |
99 | 0 | if (user && *user) { |
100 | 0 | normalize_path_copy(user, user); |
101 | 0 | strbuf_addf(&buf, "%s\n", user); |
102 | 0 | } |
103 | 0 | free(xdg); |
104 | 0 | free(user); |
105 | 0 | strbuf_trim_trailing_newline(&buf); |
106 | 0 | if (buf.len == 0) { |
107 | 0 | strbuf_release(&buf); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | 0 | return strbuf_detach(&buf, &unused); |
111 | 0 | } |
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 | 0 | { |
172 | 0 | struct git_var *ptr; |
173 | 0 | char *val; |
174 | |
|
175 | 0 | for (ptr = git_vars; ptr->read; ptr++) |
176 | 0 | if ((val = ptr->read(0))) { |
177 | 0 | if (ptr->multivalued && *val) { |
178 | 0 | struct string_list list = STRING_LIST_INIT_DUP; |
179 | 0 | int i; |
180 | |
|
181 | 0 | string_list_split(&list, val, '\n', -1); |
182 | 0 | for (i = 0; i < list.nr; i++) |
183 | 0 | printf("%s=%s\n", ptr->name, list.items[i].string); |
184 | 0 | string_list_clear(&list, 0); |
185 | 0 | } else { |
186 | 0 | printf("%s=%s\n", ptr->name, val); |
187 | 0 | } |
188 | 0 | free(val); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | static const struct git_var *get_git_var(const char *var) |
193 | 0 | { |
194 | 0 | struct git_var *ptr; |
195 | 0 | for (ptr = git_vars; ptr->read; ptr++) { |
196 | 0 | if (strcmp(var, ptr->name) == 0) { |
197 | 0 | return ptr; |
198 | 0 | } |
199 | 0 | } |
200 | 0 | return NULL; |
201 | 0 | } |
202 | | |
203 | | static int show_config(const char *var, const char *value, |
204 | | const struct config_context *ctx, void *cb) |
205 | 0 | { |
206 | 0 | if (value) |
207 | 0 | printf("%s=%s\n", var, value); |
208 | 0 | else |
209 | 0 | printf("%s\n", var); |
210 | 0 | return git_default_config(var, value, ctx, cb); |
211 | 0 | } |
212 | | |
213 | | int cmd_var(int argc, const char **argv, const char *prefix UNUSED) |
214 | 0 | { |
215 | 0 | const struct git_var *git_var; |
216 | 0 | char *val; |
217 | |
|
218 | 0 | if (argc != 2) |
219 | 0 | usage(var_usage); |
220 | | |
221 | 0 | if (strcmp(argv[1], "-l") == 0) { |
222 | 0 | git_config(show_config, NULL); |
223 | 0 | list_vars(); |
224 | 0 | return 0; |
225 | 0 | } |
226 | 0 | git_config(git_default_config, NULL); |
227 | |
|
228 | 0 | git_var = get_git_var(argv[1]); |
229 | 0 | if (!git_var) |
230 | 0 | usage(var_usage); |
231 | | |
232 | 0 | val = git_var->read(IDENT_STRICT); |
233 | 0 | if (!val) |
234 | 0 | return 1; |
235 | | |
236 | 0 | printf("%s\n", val); |
237 | 0 | free(val); |
238 | |
|
239 | 0 | return 0; |
240 | 0 | } |