1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Eric Biederman, 2005
5 */
6#include "builtin.h"
7
8static const char var_usage[] = "git var (-l | <variable>)";
9
10static const char *editor(int flag)
11{
12 const char *pgm = git_editor();
13
14 if (!pgm && flag & IDENT_ERROR_ON_NO_NAME)
15 die("Terminal is dumb, but EDITOR unset");
16
17 return pgm;
18}
19
20static const char *pager(int flag)
21{
22 const char *pgm = git_pager(1);
23
24 if (!pgm)
25 pgm = "cat";
26 return pgm;
27}
28
29struct git_var {
30 const char *name;
31 const char *(*read)(int);
32};
33static struct git_var git_vars[] = {
34 { "GIT_COMMITTER_IDENT", git_committer_info },
35 { "GIT_AUTHOR_IDENT", git_author_info },
36 { "GIT_EDITOR", editor },
37 { "GIT_PAGER", pager },
38 { "", NULL },
39};
40
41static void list_vars(void)
42{
43 struct git_var *ptr;
44 const char *val;
45
46 for (ptr = git_vars; ptr->read; ptr++)
47 if ((val = ptr->read(0)))
48 printf("%s=%s\n", ptr->name, val);
49}
50
51static const char *read_var(const char *var)
52{
53 struct git_var *ptr;
54 const char *val;
55 val = NULL;
56 for (ptr = git_vars; ptr->read; ptr++) {
57 if (strcmp(var, ptr->name) == 0) {
58 val = ptr->read(IDENT_ERROR_ON_NO_NAME);
59 break;
60 }
61 }
62 return val;
63}
64
65static int show_config(const char *var, const char *value, void *cb)
66{
67 if (value)
68 printf("%s=%s\n", var, value);
69 else
70 printf("%s\n", var);
71 return git_default_config(var, value, cb);
72}
73
74int cmd_var(int argc, const char **argv, const char *prefix)
75{
76 const char *val = NULL;
77 if (argc != 2)
78 usage(var_usage);
79
80 if (strcmp(argv[1], "-l") == 0) {
81 git_config(show_config, NULL);
82 list_vars();
83 return 0;
84 }
85 git_config(git_default_config, NULL);
86 val = read_var(argv[1]);
87 if (!val)
88 usage(var_usage);
89
90 printf("%s\n", val);
91
92 return 0;
93}