Merge branch 'ab/makefile-msgfmt-wo-stats'
[git/debian.git] / builtin / var.c
blob491db2742926dbd6fd08f681817821addcfafab7
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
5 */
6 #include "builtin.h"
7 #include "config.h"
8 #include "refs.h"
10 static const char var_usage[] = "git var (-l | <variable>)";
12 static const char *editor(int flag)
14 const char *pgm = git_editor();
16 if (!pgm && flag & IDENT_STRICT)
17 die("Terminal is dumb, but EDITOR unset");
19 return pgm;
22 static const char *pager(int flag)
24 const char *pgm = git_pager(1);
26 if (!pgm)
27 pgm = "cat";
28 return pgm;
31 static const char *default_branch(int flag)
33 return git_default_branch_name(1);
36 struct git_var {
37 const char *name;
38 const char *(*read)(int);
40 static struct git_var git_vars[] = {
41 { "GIT_COMMITTER_IDENT", git_committer_info },
42 { "GIT_AUTHOR_IDENT", git_author_info },
43 { "GIT_EDITOR", editor },
44 { "GIT_PAGER", pager },
45 { "GIT_DEFAULT_BRANCH", default_branch },
46 { "", NULL },
49 static void list_vars(void)
51 struct git_var *ptr;
52 const char *val;
54 for (ptr = git_vars; ptr->read; ptr++)
55 if ((val = ptr->read(0)))
56 printf("%s=%s\n", ptr->name, val);
59 static const char *read_var(const char *var)
61 struct git_var *ptr;
62 const char *val;
63 val = NULL;
64 for (ptr = git_vars; ptr->read; ptr++) {
65 if (strcmp(var, ptr->name) == 0) {
66 val = ptr->read(IDENT_STRICT);
67 break;
70 return val;
73 static int show_config(const char *var, const char *value, void *cb)
75 if (value)
76 printf("%s=%s\n", var, value);
77 else
78 printf("%s\n", var);
79 return git_default_config(var, value, cb);
82 int cmd_var(int argc, const char **argv, const char *prefix)
84 const char *val = NULL;
85 if (argc != 2)
86 usage(var_usage);
88 if (strcmp(argv[1], "-l") == 0) {
89 git_config(show_config, NULL);
90 list_vars();
91 return 0;
93 git_config(git_default_config, NULL);
94 val = read_var(argv[1]);
95 if (!val)
96 usage(var_usage);
98 printf("%s\n", val);
100 return 0;