]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/var.c
ada642a9fe5257ed00454ecae769d023661cae74
2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
7 #define USE_THE_REPOSITORY_VARIABLE
19 #include "run-command.h"
21 static const char var_usage
[] = "git var (-l | <variable>)";
23 static char *committer(int ident_flag
)
25 return xstrdup_or_null(git_committer_info(ident_flag
));
28 static char *author(int ident_flag
)
30 return xstrdup_or_null(git_author_info(ident_flag
));
33 static char *editor(int ident_flag UNUSED
)
35 return xstrdup_or_null(git_editor());
38 static char *sequence_editor(int ident_flag UNUSED
)
40 return xstrdup_or_null(git_sequence_editor());
43 static char *pager(int ident_flag UNUSED
)
45 const char *pgm
= git_pager(the_repository
, 1);
52 static char *default_branch(int ident_flag UNUSED
)
54 return repo_default_branch_name(the_repository
, 1);
57 static char *shell_path(int ident_flag UNUSED
)
59 return git_shell_path();
62 static char *git_attr_val_system(int ident_flag UNUSED
)
64 if (git_attr_system_is_enabled()) {
65 char *file
= xstrdup(git_attr_system_file());
66 normalize_path_copy(file
, file
);
72 static char *git_attr_val_global(int ident_flag UNUSED
)
74 char *file
= xstrdup_or_null(git_attr_global_file());
76 normalize_path_copy(file
, file
);
82 static char *git_config_val_system(int ident_flag UNUSED
)
84 if (git_config_system()) {
85 char *file
= git_system_config();
86 normalize_path_copy(file
, file
);
92 static char *git_config_val_global(int ident_flag UNUSED
)
94 struct strbuf buf
= STRBUF_INIT
;
98 git_global_config_paths(&user
, &xdg
);
100 normalize_path_copy(xdg
, xdg
);
101 strbuf_addf(&buf
, "%s\n", xdg
);
104 normalize_path_copy(user
, user
);
105 strbuf_addf(&buf
, "%s\n", user
);
109 strbuf_trim_trailing_newline(&buf
);
111 strbuf_release(&buf
);
114 return strbuf_detach(&buf
, &unused
);
122 static struct git_var git_vars
[] = {
124 .name
= "GIT_COMMITTER_IDENT",
128 .name
= "GIT_AUTHOR_IDENT",
132 .name
= "GIT_EDITOR",
136 .name
= "GIT_SEQUENCE_EDITOR",
137 .read
= sequence_editor
,
144 .name
= "GIT_DEFAULT_BRANCH",
145 .read
= default_branch
,
148 .name
= "GIT_SHELL_PATH",
152 .name
= "GIT_ATTR_SYSTEM",
153 .read
= git_attr_val_system
,
156 .name
= "GIT_ATTR_GLOBAL",
157 .read
= git_attr_val_global
,
160 .name
= "GIT_CONFIG_SYSTEM",
161 .read
= git_config_val_system
,
164 .name
= "GIT_CONFIG_GLOBAL",
165 .read
= git_config_val_global
,
174 static void list_vars(void)
179 for (ptr
= git_vars
; ptr
->read
; ptr
++)
180 if ((val
= ptr
->read(0))) {
181 if (ptr
->multivalued
&& *val
) {
182 struct string_list list
= STRING_LIST_INIT_DUP
;
184 string_list_split(&list
, val
, '\n', -1);
185 for (size_t i
= 0; i
< list
.nr
; i
++)
186 printf("%s=%s\n", ptr
->name
, list
.items
[i
].string
);
187 string_list_clear(&list
, 0);
189 printf("%s=%s\n", ptr
->name
, val
);
195 static const struct git_var
*get_git_var(const char *var
)
198 for (ptr
= git_vars
; ptr
->read
; ptr
++) {
199 if (strcmp(var
, ptr
->name
) == 0) {
206 static int show_config(const char *var
, const char *value
,
207 const struct config_context
*ctx
, void *cb
)
210 printf("%s=%s\n", var
, value
);
213 return git_default_config(var
, value
, ctx
, cb
);
216 int cmd_var(int argc
,
218 const char *prefix UNUSED
,
219 struct repository
*repo UNUSED
)
221 const struct git_var
*git_var
;
224 show_usage_if_asked(argc
, argv
, var_usage
);
228 if (strcmp(argv
[1], "-l") == 0) {
229 git_config(show_config
, NULL
);
233 git_config(git_default_config
, NULL
);
235 git_var
= get_git_var(argv
[1]);
239 val
= git_var
->read(IDENT_STRICT
);