2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
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);
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
);
67 static char *git_attr_val_global(int ident_flag UNUSED
)
69 char *file
= xstrdup_or_null(git_attr_global_file());
71 normalize_path_copy(file
, file
);
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
);
87 static char *git_config_val_global(int ident_flag UNUSED
)
89 struct strbuf buf
= STRBUF_INIT
;
93 git_global_config(&user
, &xdg
);
95 normalize_path_copy(xdg
, xdg
);
96 strbuf_addf(&buf
, "%s\n", xdg
);
99 normalize_path_copy(user
, user
);
100 strbuf_addf(&buf
, "%s\n", user
);
104 strbuf_trim_trailing_newline(&buf
);
106 strbuf_release(&buf
);
109 return strbuf_detach(&buf
, &unused
);
117 static struct git_var git_vars
[] = {
119 .name
= "GIT_COMMITTER_IDENT",
123 .name
= "GIT_AUTHOR_IDENT",
127 .name
= "GIT_EDITOR",
131 .name
= "GIT_SEQUENCE_EDITOR",
132 .read
= sequence_editor
,
139 .name
= "GIT_DEFAULT_BRANCH",
140 .read
= default_branch
,
143 .name
= "GIT_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
,
169 static void list_vars(void)
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
;
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);
185 printf("%s=%s\n", ptr
->name
, val
);
191 static const struct git_var
*get_git_var(const char *var
)
194 for (ptr
= git_vars
; ptr
->read
; ptr
++) {
195 if (strcmp(var
, ptr
->name
) == 0) {
202 static int show_config(const char *var
, const char *value
,
203 const struct config_context
*ctx
, void *cb
)
206 printf("%s=%s\n", var
, value
);
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
;
220 if (strcmp(argv
[1], "-l") == 0) {
221 git_config(show_config
, NULL
);
225 git_config(git_default_config
, NULL
);
227 git_var
= get_git_var(argv
[1]);
231 val
= git_var
->read(IDENT_STRICT
);