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