Git 2.46-rc1
[git.git] / builtin / var.c
blobe30ff45be1ce730633d84cfc9c5caf0dab187dd1
1 /*
2 * GIT - The information manager from hell
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"
17 static const char var_usage[] = "git var (-l | <variable>)";
19 static char *committer(int ident_flag)
21 return xstrdup_or_null(git_committer_info(ident_flag));
24 static char *author(int ident_flag)
26 return xstrdup_or_null(git_author_info(ident_flag));
29 static char *editor(int ident_flag UNUSED)
31 return xstrdup_or_null(git_editor());
34 static char *sequence_editor(int ident_flag UNUSED)
36 return xstrdup_or_null(git_sequence_editor());
39 static char *pager(int ident_flag UNUSED)
41 const char *pgm = git_pager(1);
43 if (!pgm)
44 pgm = "cat";
45 return xstrdup(pgm);
48 static char *default_branch(int ident_flag UNUSED)
50 return repo_default_branch_name(the_repository, 1);
53 static char *shell_path(int ident_flag UNUSED)
55 return git_shell_path();
58 static char *git_attr_val_system(int ident_flag UNUSED)
60 if (git_attr_system_is_enabled()) {
61 char *file = xstrdup(git_attr_system_file());
62 normalize_path_copy(file, file);
63 return file;
65 return NULL;
68 static char *git_attr_val_global(int ident_flag UNUSED)
70 char *file = xstrdup_or_null(git_attr_global_file());
71 if (file) {
72 normalize_path_copy(file, file);
73 return file;
75 return NULL;
78 static char *git_config_val_system(int ident_flag UNUSED)
80 if (git_config_system()) {
81 char *file = git_system_config();
82 normalize_path_copy(file, file);
83 return file;
85 return NULL;
88 static char *git_config_val_global(int ident_flag UNUSED)
90 struct strbuf buf = STRBUF_INIT;
91 char *user, *xdg;
92 size_t unused;
94 git_global_config_paths(&user, &xdg);
95 if (xdg && *xdg) {
96 normalize_path_copy(xdg, xdg);
97 strbuf_addf(&buf, "%s\n", xdg);
99 if (user && *user) {
100 normalize_path_copy(user, user);
101 strbuf_addf(&buf, "%s\n", user);
103 free(xdg);
104 free(user);
105 strbuf_trim_trailing_newline(&buf);
106 if (buf.len == 0) {
107 strbuf_release(&buf);
108 return NULL;
110 return strbuf_detach(&buf, &unused);
113 struct git_var {
114 const char *name;
115 char *(*read)(int);
116 int multivalued;
118 static struct git_var git_vars[] = {
120 .name = "GIT_COMMITTER_IDENT",
121 .read = committer,
124 .name = "GIT_AUTHOR_IDENT",
125 .read = author,
128 .name = "GIT_EDITOR",
129 .read = editor,
132 .name = "GIT_SEQUENCE_EDITOR",
133 .read = sequence_editor,
136 .name = "GIT_PAGER",
137 .read = pager,
140 .name = "GIT_DEFAULT_BRANCH",
141 .read = default_branch,
144 .name = "GIT_SHELL_PATH",
145 .read = shell_path,
148 .name = "GIT_ATTR_SYSTEM",
149 .read = git_attr_val_system,
152 .name = "GIT_ATTR_GLOBAL",
153 .read = git_attr_val_global,
156 .name = "GIT_CONFIG_SYSTEM",
157 .read = git_config_val_system,
160 .name = "GIT_CONFIG_GLOBAL",
161 .read = git_config_val_global,
162 .multivalued = 1,
165 .name = "",
166 .read = NULL,
170 static void list_vars(void)
172 struct git_var *ptr;
173 char *val;
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;
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);
188 free(val);
192 static const struct git_var *get_git_var(const char *var)
194 struct git_var *ptr;
195 for (ptr = git_vars; ptr->read; ptr++) {
196 if (strcmp(var, ptr->name) == 0) {
197 return ptr;
200 return NULL;
203 static int show_config(const char *var, const char *value,
204 const struct config_context *ctx, void *cb)
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);
213 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
215 const struct git_var *git_var;
216 char *val;
218 if (argc != 2)
219 usage(var_usage);
221 if (strcmp(argv[1], "-l") == 0) {
222 git_config(show_config, NULL);
223 list_vars();
224 return 0;
226 git_config(git_default_config, NULL);
228 git_var = get_git_var(argv[1]);
229 if (!git_var)
230 usage(var_usage);
232 val = git_var->read(IDENT_STRICT);
233 if (!val)
234 return 1;
236 printf("%s\n", val);
237 free(val);
239 return 0;