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