Merge branch 'rs/diff-parseopts-cleanup'
[git/gitster.git] / builtin / var.c
blobcf5567208a277a62b872a1846919060fb6e276c3
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"
16 static const char var_usage[] = "git var (-l | <variable>)";
18 static char *committer(int ident_flag)
20 return xstrdup_or_null(git_committer_info(ident_flag));
23 static char *author(int ident_flag)
25 return xstrdup_or_null(git_author_info(ident_flag));
28 static char *editor(int ident_flag UNUSED)
30 return xstrdup_or_null(git_editor());
33 static char *sequence_editor(int ident_flag UNUSED)
35 return xstrdup_or_null(git_sequence_editor());
38 static char *pager(int ident_flag UNUSED)
40 const char *pgm = git_pager(1);
42 if (!pgm)
43 pgm = "cat";
44 return xstrdup(pgm);
47 static char *default_branch(int ident_flag UNUSED)
49 return xstrdup_or_null(git_default_branch_name(1));
52 static char *shell_path(int ident_flag UNUSED)
54 return xstrdup(SHELL_PATH);
57 static char *git_attr_val_system(int ident_flag UNUSED)
59 if (git_attr_system_is_enabled()) {
60 char *file = xstrdup(git_attr_system_file());
61 normalize_path_copy(file, file);
62 return file;
64 return NULL;
67 static char *git_attr_val_global(int ident_flag UNUSED)
69 char *file = xstrdup_or_null(git_attr_global_file());
70 if (file) {
71 normalize_path_copy(file, file);
72 return file;
74 return NULL;
77 static char *git_config_val_system(int ident_flag UNUSED)
79 if (git_config_system()) {
80 char *file = git_system_config();
81 normalize_path_copy(file, file);
82 return file;
84 return NULL;
87 static char *git_config_val_global(int ident_flag UNUSED)
89 struct strbuf buf = STRBUF_INIT;
90 char *user, *xdg;
91 size_t unused;
93 git_global_config_paths(&user, &xdg);
94 if (xdg && *xdg) {
95 normalize_path_copy(xdg, xdg);
96 strbuf_addf(&buf, "%s\n", xdg);
98 if (user && *user) {
99 normalize_path_copy(user, user);
100 strbuf_addf(&buf, "%s\n", user);
102 free(xdg);
103 free(user);
104 strbuf_trim_trailing_newline(&buf);
105 if (buf.len == 0) {
106 strbuf_release(&buf);
107 return NULL;
109 return strbuf_detach(&buf, &unused);
112 struct git_var {
113 const char *name;
114 char *(*read)(int);
115 int multivalued;
117 static struct git_var git_vars[] = {
119 .name = "GIT_COMMITTER_IDENT",
120 .read = committer,
123 .name = "GIT_AUTHOR_IDENT",
124 .read = author,
127 .name = "GIT_EDITOR",
128 .read = editor,
131 .name = "GIT_SEQUENCE_EDITOR",
132 .read = sequence_editor,
135 .name = "GIT_PAGER",
136 .read = pager,
139 .name = "GIT_DEFAULT_BRANCH",
140 .read = default_branch,
143 .name = "GIT_SHELL_PATH",
144 .read = shell_path,
147 .name = "GIT_ATTR_SYSTEM",
148 .read = git_attr_val_system,
151 .name = "GIT_ATTR_GLOBAL",
152 .read = git_attr_val_global,
155 .name = "GIT_CONFIG_SYSTEM",
156 .read = git_config_val_system,
159 .name = "GIT_CONFIG_GLOBAL",
160 .read = git_config_val_global,
161 .multivalued = 1,
164 .name = "",
165 .read = NULL,
169 static void list_vars(void)
171 struct git_var *ptr;
172 char *val;
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;
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);
187 free(val);
191 static const struct git_var *get_git_var(const char *var)
193 struct git_var *ptr;
194 for (ptr = git_vars; ptr->read; ptr++) {
195 if (strcmp(var, ptr->name) == 0) {
196 return ptr;
199 return NULL;
202 static int show_config(const char *var, const char *value,
203 const struct config_context *ctx, void *cb)
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);
212 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
214 const struct git_var *git_var;
215 char *val;
217 if (argc != 2)
218 usage(var_usage);
220 if (strcmp(argv[1], "-l") == 0) {
221 git_config(show_config, NULL);
222 list_vars();
223 return 0;
225 git_config(git_default_config, NULL);
227 git_var = get_git_var(argv[1]);
228 if (!git_var)
229 usage(var_usage);
231 val = git_var->read(IDENT_STRICT);
232 if (!val)
233 return 1;
235 printf("%s\n", val);
236 free(val);
238 return 0;