shallow: fix leak when unregistering last shallow root
[alt-git.git] / builtin / var.c
blob2ecaed51b441963ddbfb6983403bd364d4fd72fd
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
5 */
6 #define USE_THE_REPOSITORY_VARIABLE
7 #include "builtin.h"
9 #include "attr.h"
10 #include "config.h"
11 #include "editor.h"
12 #include "ident.h"
13 #include "pager.h"
14 #include "refs.h"
15 #include "path.h"
16 #include "strbuf.h"
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);
45 if (!pgm)
46 pgm = "cat";
47 return xstrdup(pgm);
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);
65 return file;
67 return NULL;
70 static char *git_attr_val_global(int ident_flag UNUSED)
72 char *file = xstrdup_or_null(git_attr_global_file());
73 if (file) {
74 normalize_path_copy(file, file);
75 return file;
77 return NULL;
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);
85 return file;
87 return NULL;
90 static char *git_config_val_global(int ident_flag UNUSED)
92 struct strbuf buf = STRBUF_INIT;
93 char *user, *xdg;
94 size_t unused;
96 git_global_config_paths(&user, &xdg);
97 if (xdg && *xdg) {
98 normalize_path_copy(xdg, xdg);
99 strbuf_addf(&buf, "%s\n", xdg);
101 if (user && *user) {
102 normalize_path_copy(user, user);
103 strbuf_addf(&buf, "%s\n", user);
105 free(xdg);
106 free(user);
107 strbuf_trim_trailing_newline(&buf);
108 if (buf.len == 0) {
109 strbuf_release(&buf);
110 return NULL;
112 return strbuf_detach(&buf, &unused);
115 struct git_var {
116 const char *name;
117 char *(*read)(int);
118 int multivalued;
120 static struct git_var git_vars[] = {
122 .name = "GIT_COMMITTER_IDENT",
123 .read = committer,
126 .name = "GIT_AUTHOR_IDENT",
127 .read = author,
130 .name = "GIT_EDITOR",
131 .read = editor,
134 .name = "GIT_SEQUENCE_EDITOR",
135 .read = sequence_editor,
138 .name = "GIT_PAGER",
139 .read = pager,
142 .name = "GIT_DEFAULT_BRANCH",
143 .read = default_branch,
146 .name = "GIT_SHELL_PATH",
147 .read = 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,
164 .multivalued = 1,
167 .name = "",
168 .read = NULL,
172 static void list_vars(void)
174 struct git_var *ptr;
175 char *val;
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;
181 int i;
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);
190 free(val);
194 static const struct git_var *get_git_var(const char *var)
196 struct git_var *ptr;
197 for (ptr = git_vars; ptr->read; ptr++) {
198 if (strcmp(var, ptr->name) == 0) {
199 return ptr;
202 return NULL;
205 static int show_config(const char *var, const char *value,
206 const struct config_context *ctx, void *cb)
208 if (value)
209 printf("%s=%s\n", var, value);
210 else
211 printf("%s\n", var);
212 return git_default_config(var, value, ctx, cb);
215 int cmd_var(int argc,
216 const char **argv,
217 const char *prefix UNUSED,
218 struct repository *repo UNUSED)
220 const struct git_var *git_var;
221 char *val;
223 if (argc != 2)
224 usage(var_usage);
226 if (strcmp(argv[1], "-l") == 0) {
227 git_config(show_config, NULL);
228 list_vars();
229 return 0;
231 git_config(git_default_config, NULL);
233 git_var = get_git_var(argv[1]);
234 if (!git_var)
235 usage(var_usage);
237 val = git_var->read(IDENT_STRICT);
238 if (!val)
239 return 1;
241 printf("%s\n", val);
242 free(val);
244 return 0;