]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/var.c
The eighth batch
[thirdparty/git.git] / builtin / var.c
CommitLineData
aed022ab
EB
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Eric Biederman, 2005
5 */
41f43b82 6
03eae9af 7#define USE_THE_REPOSITORY_VARIABLE
41f43b82 8
c2e86add 9#include "builtin.h"
03eae9af 10
576a37fc 11#include "attr.h"
b2141fc1 12#include "config.h"
4e120823 13#include "editor.h"
b5fa6081 14#include "ident.h"
ca4eed70 15#include "pager.h"
e06c9e1d 16#include "refs.h"
89d62d5e
JH
17#include "path.h"
18#include "strbuf.h"
9ed143ee 19#include "run-command.h"
aed022ab 20
9fabb6d7 21static const char var_usage[] = "git var (-l | <variable>)";
aed022ab 22
cdd489ea 23static char *committer(int ident_flag)
44fcb497 24{
cdd489ea 25 return xstrdup_or_null(git_committer_info(ident_flag));
44fcb497
JN
26}
27
cdd489ea 28static char *author(int ident_flag)
4c3dd930 29{
cdd489ea 30 return xstrdup_or_null(git_author_info(ident_flag));
4c3dd930
SA
31}
32
cdd489ea 33static char *editor(int ident_flag UNUSED)
34{
35 return xstrdup_or_null(git_editor());
36}
37
38static char *sequence_editor(int ident_flag UNUSED)
39{
40 return xstrdup_or_null(git_sequence_editor());
41}
42
43static char *pager(int ident_flag UNUSED)
63618245 44{
59b6131a 45 const char *pgm = git_pager(the_repository, 1);
63618245
JN
46
47 if (!pgm)
48 pgm = "cat";
cdd489ea 49 return xstrdup(pgm);
63618245
JN
50}
51
cdd489ea 52static char *default_branch(int ident_flag UNUSED)
e06c9e1d 53{
97abaab5 54 return repo_default_branch_name(the_repository, 1);
e06c9e1d
TW
55}
56
cdd489ea 57static char *shell_path(int ident_flag UNUSED)
1e657212 58{
9ed143ee 59 return git_shell_path();
63618245
JN
60}
61
576a37fc 62static char *git_attr_val_system(int ident_flag UNUSED)
e06c9e1d 63{
576a37fc 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
72static char *git_attr_val_global(int ident_flag UNUSED)
73{
256a94ef 74 char *file = xstrdup_or_null(git_attr_global_file());
576a37fc 75 if (file) {
76 normalize_path_copy(file, file);
77 return file;
78 }
79 return NULL;
80}
81
ed773a18 82static 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
92static 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
ecffa3ed 98 git_global_config_paths(&user, &xdg);
ed773a18 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);
e06c9e1d
TW
115}
116
aed022ab
EB
117struct git_var {
118 const char *name;
cdd489ea 119 char *(*read)(int);
ed773a18 120 int multivalued;
aed022ab
EB
121};
122static struct git_var git_vars[] = {
f74c90dc 123 {
124 .name = "GIT_COMMITTER_IDENT",
cdd489ea 125 .read = committer,
f74c90dc 126 },
127 {
128 .name = "GIT_AUTHOR_IDENT",
cdd489ea 129 .read = author,
f74c90dc 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 },
576a37fc 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 },
ed773a18 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 },
f74c90dc 168 {
169 .name = "",
170 .read = NULL,
171 },
aed022ab
EB
172};
173
174static void list_vars(void)
175{
176 struct git_var *ptr;
cdd489ea 177 char *val;
44fcb497 178
eeefa7c9 179 for (ptr = git_vars; ptr->read; ptr++)
cdd489ea 180 if ((val = ptr->read(0))) {
ed773a18 181 if (ptr->multivalued && *val) {
182 struct string_list list = STRING_LIST_INIT_DUP;
ed773a18 183
184 string_list_split(&list, val, '\n', -1);
80c9e70e 185 for (size_t i = 0; i < list.nr; i++)
ed773a18 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 }
cdd489ea 191 free(val);
192 }
aed022ab
EB
193}
194
26b8abc7 195static const struct git_var *get_git_var(const char *var)
aed022ab
EB
196{
197 struct git_var *ptr;
eeefa7c9 198 for (ptr = git_vars; ptr->read; ptr++) {
aed022ab 199 if (strcmp(var, ptr->name) == 0) {
26b8abc7 200 return ptr;
aed022ab
EB
201 }
202 }
26b8abc7 203 return NULL;
aed022ab
EB
204}
205
a4e7e317
GC
206static int show_config(const char *var, const char *value,
207 const struct config_context *ctx, void *cb)
e1b10391
LT
208{
209 if (value)
210 printf("%s=%s\n", var, value);
211 else
212 printf("%s\n", var);
a4e7e317 213 return git_default_config(var, value, ctx, cb);
e1b10391
LT
214}
215
9b1cb507
JC
216int cmd_var(int argc,
217 const char **argv,
218 const char *prefix UNUSED,
219 struct repository *repo UNUSED)
aed022ab 220{
26b8abc7 221 const struct git_var *git_var;
cdd489ea 222 char *val;
26b8abc7 223
a36a822d 224 show_usage_if_asked(argc, argv, var_usage);
2bc8c1a8 225 if (argc != 2)
aed022ab 226 usage(var_usage);
aed022ab
EB
227
228 if (strcmp(argv[1], "-l") == 0) {
ef90d6d4 229 git_config(show_config, NULL);
aed022ab
EB
230 list_vars();
231 return 0;
232 }
ef90d6d4 233 git_config(git_default_config, NULL);
26b8abc7
SA
234
235 git_var = get_git_var(argv[1]);
236 if (!git_var)
aed022ab 237 usage(var_usage);
a6080a0a 238
26b8abc7
SA
239 val = git_var->read(IDENT_STRICT);
240 if (!val)
241 return 1;
242
aed022ab 243 printf("%s\n", val);
cdd489ea 244 free(val);
a6080a0a 245
aed022ab
EB
246 return 0;
247}