]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/var.c
Merge branch 'vd/fsck-submodule-url-test'
[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 */
c2e86add 6#include "builtin.h"
576a37fc 7#include "attr.h"
b2141fc1 8#include "config.h"
4e120823 9#include "editor.h"
b5fa6081 10#include "ident.h"
ca4eed70 11#include "pager.h"
e06c9e1d 12#include "refs.h"
89d62d5e
JH
13#include "path.h"
14#include "strbuf.h"
aed022ab 15
9fabb6d7 16static const char var_usage[] = "git var (-l | <variable>)";
aed022ab 17
cdd489ea 18static char *committer(int ident_flag)
44fcb497 19{
cdd489ea 20 return xstrdup_or_null(git_committer_info(ident_flag));
44fcb497
JN
21}
22
cdd489ea 23static char *author(int ident_flag)
4c3dd930 24{
cdd489ea 25 return xstrdup_or_null(git_author_info(ident_flag));
4c3dd930
SA
26}
27
cdd489ea 28static char *editor(int ident_flag UNUSED)
29{
30 return xstrdup_or_null(git_editor());
31}
32
33static char *sequence_editor(int ident_flag UNUSED)
34{
35 return xstrdup_or_null(git_sequence_editor());
36}
37
38static char *pager(int ident_flag UNUSED)
63618245 39{
64778d24 40 const char *pgm = git_pager(1);
63618245
JN
41
42 if (!pgm)
43 pgm = "cat";
cdd489ea 44 return xstrdup(pgm);
63618245
JN
45}
46
cdd489ea 47static char *default_branch(int ident_flag UNUSED)
e06c9e1d 48{
cdd489ea 49 return xstrdup_or_null(git_default_branch_name(1));
e06c9e1d
TW
50}
51
cdd489ea 52static char *shell_path(int ident_flag UNUSED)
1e657212 53{
cdd489ea 54 return xstrdup(SHELL_PATH);
63618245
JN
55}
56
576a37fc 57static char *git_attr_val_system(int ident_flag UNUSED)
e06c9e1d 58{
576a37fc 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
67static char *git_attr_val_global(int ident_flag UNUSED)
68{
256a94ef 69 char *file = xstrdup_or_null(git_attr_global_file());
576a37fc 70 if (file) {
71 normalize_path_copy(file, file);
72 return file;
73 }
74 return NULL;
75}
76
ed773a18 77static 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
87static 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
ecffa3ed 93 git_global_config_paths(&user, &xdg);
ed773a18 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);
e06c9e1d
TW
110}
111
aed022ab
EB
112struct git_var {
113 const char *name;
cdd489ea 114 char *(*read)(int);
ed773a18 115 int multivalued;
aed022ab
EB
116};
117static struct git_var git_vars[] = {
f74c90dc 118 {
119 .name = "GIT_COMMITTER_IDENT",
cdd489ea 120 .read = committer,
f74c90dc 121 },
122 {
123 .name = "GIT_AUTHOR_IDENT",
cdd489ea 124 .read = author,
f74c90dc 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 },
576a37fc 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 },
ed773a18 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 },
f74c90dc 163 {
164 .name = "",
165 .read = NULL,
166 },
aed022ab
EB
167};
168
169static void list_vars(void)
170{
171 struct git_var *ptr;
cdd489ea 172 char *val;
44fcb497 173
eeefa7c9 174 for (ptr = git_vars; ptr->read; ptr++)
cdd489ea 175 if ((val = ptr->read(0))) {
ed773a18 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 }
cdd489ea 187 free(val);
188 }
aed022ab
EB
189}
190
26b8abc7 191static const struct git_var *get_git_var(const char *var)
aed022ab
EB
192{
193 struct git_var *ptr;
eeefa7c9 194 for (ptr = git_vars; ptr->read; ptr++) {
aed022ab 195 if (strcmp(var, ptr->name) == 0) {
26b8abc7 196 return ptr;
aed022ab
EB
197 }
198 }
26b8abc7 199 return NULL;
aed022ab
EB
200}
201
a4e7e317
GC
202static int show_config(const char *var, const char *value,
203 const struct config_context *ctx, void *cb)
e1b10391
LT
204{
205 if (value)
206 printf("%s=%s\n", var, value);
207 else
208 printf("%s\n", var);
a4e7e317 209 return git_default_config(var, value, ctx, cb);
e1b10391
LT
210}
211
5247b762 212int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
aed022ab 213{
26b8abc7 214 const struct git_var *git_var;
cdd489ea 215 char *val;
26b8abc7 216
2bc8c1a8 217 if (argc != 2)
aed022ab 218 usage(var_usage);
aed022ab
EB
219
220 if (strcmp(argv[1], "-l") == 0) {
ef90d6d4 221 git_config(show_config, NULL);
aed022ab
EB
222 list_vars();
223 return 0;
224 }
ef90d6d4 225 git_config(git_default_config, NULL);
26b8abc7
SA
226
227 git_var = get_git_var(argv[1]);
228 if (!git_var)
aed022ab 229 usage(var_usage);
a6080a0a 230
26b8abc7
SA
231 val = git_var->read(IDENT_STRICT);
232 if (!val)
233 return 1;
234
aed022ab 235 printf("%s\n", val);
cdd489ea 236 free(val);
a6080a0a 237
aed022ab
EB
238 return 0;
239}