2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
6 #define USE_THE_REPOSITORY_VARIABLE
17 #include "run-command.h"
19 static const char var_usage
[] = "git var (-l | <variable>)";
21 static char *committer(int ident_flag
)
23 return xstrdup_or_null(git_committer_info(ident_flag
));
26 static char *author(int ident_flag
)
28 return xstrdup_or_null(git_author_info(ident_flag
));
31 static char *editor(int ident_flag UNUSED
)
33 return xstrdup_or_null(git_editor());
36 static char *sequence_editor(int ident_flag UNUSED
)
38 return xstrdup_or_null(git_sequence_editor());
41 static char *pager(int ident_flag UNUSED
)
43 const char *pgm
= git_pager(1);
50 static char *default_branch(int ident_flag UNUSED
)
52 return repo_default_branch_name(the_repository
, 1);
55 static char *shell_path(int ident_flag UNUSED
)
57 return git_shell_path();
60 static char *git_attr_val_system(int ident_flag UNUSED
)
62 if (git_attr_system_is_enabled()) {
63 char *file
= xstrdup(git_attr_system_file());
64 normalize_path_copy(file
, file
);
70 static char *git_attr_val_global(int ident_flag UNUSED
)
72 char *file
= xstrdup_or_null(git_attr_global_file());
74 normalize_path_copy(file
, file
);
80 static char *git_config_val_system(int ident_flag UNUSED
)
82 if (git_config_system()) {
83 char *file
= git_system_config();
84 normalize_path_copy(file
, file
);
90 static char *git_config_val_global(int ident_flag UNUSED
)
92 struct strbuf buf
= STRBUF_INIT
;
96 git_global_config_paths(&user
, &xdg
);
98 normalize_path_copy(xdg
, xdg
);
99 strbuf_addf(&buf
, "%s\n", xdg
);
102 normalize_path_copy(user
, user
);
103 strbuf_addf(&buf
, "%s\n", user
);
107 strbuf_trim_trailing_newline(&buf
);
109 strbuf_release(&buf
);
112 return strbuf_detach(&buf
, &unused
);
120 static struct git_var git_vars
[] = {
122 .name
= "GIT_COMMITTER_IDENT",
126 .name
= "GIT_AUTHOR_IDENT",
130 .name
= "GIT_EDITOR",
134 .name
= "GIT_SEQUENCE_EDITOR",
135 .read
= sequence_editor
,
142 .name
= "GIT_DEFAULT_BRANCH",
143 .read
= default_branch
,
146 .name
= "GIT_SHELL_PATH",
150 .name
= "GIT_ATTR_SYSTEM",
151 .read
= git_attr_val_system
,
154 .name
= "GIT_ATTR_GLOBAL",
155 .read
= git_attr_val_global
,
158 .name
= "GIT_CONFIG_SYSTEM",
159 .read
= git_config_val_system
,
162 .name
= "GIT_CONFIG_GLOBAL",
163 .read
= git_config_val_global
,
172 static void list_vars(void)
177 for (ptr
= git_vars
; ptr
->read
; ptr
++)
178 if ((val
= ptr
->read(0))) {
179 if (ptr
->multivalued
&& *val
) {
180 struct string_list list
= STRING_LIST_INIT_DUP
;
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);
188 printf("%s=%s\n", ptr
->name
, val
);
194 static const struct git_var
*get_git_var(const char *var
)
197 for (ptr
= git_vars
; ptr
->read
; ptr
++) {
198 if (strcmp(var
, ptr
->name
) == 0) {
205 static int show_config(const char *var
, const char *value
,
206 const struct config_context
*ctx
, void *cb
)
209 printf("%s=%s\n", var
, value
);
212 return git_default_config(var
, value
, ctx
, cb
);
215 int cmd_var(int argc
,
217 const char *prefix UNUSED
,
218 struct repository
*repo UNUSED
)
220 const struct git_var
*git_var
;
226 if (strcmp(argv
[1], "-l") == 0) {
227 git_config(show_config
, NULL
);
231 git_config(git_default_config
, NULL
);
233 git_var
= get_git_var(argv
[1]);
237 val
= git_var
->read(IDENT_STRICT
);