Git 1.7.12.4
[git/mjg.git] / builtin / var.c
blobaedbb53a2da5e02b2c8fc29a1a4dc22dc76fbefb
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
5 */
6 #include "builtin.h"
8 static const char var_usage[] = "git var (-l | <variable>)";
10 static const char *editor(int flag)
12 const char *pgm = git_editor();
14 if (!pgm && flag & IDENT_STRICT)
15 die("Terminal is dumb, but EDITOR unset");
17 return pgm;
20 static const char *pager(int flag)
22 const char *pgm = git_pager(1);
24 if (!pgm)
25 pgm = "cat";
26 return pgm;
29 struct git_var {
30 const char *name;
31 const char *(*read)(int);
33 static 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 },
41 static void list_vars(void)
43 struct git_var *ptr;
44 const char *val;
46 for (ptr = git_vars; ptr->read; ptr++)
47 if ((val = ptr->read(0)))
48 printf("%s=%s\n", ptr->name, val);
51 static const char *read_var(const char *var)
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_STRICT);
59 break;
62 return val;
65 static int show_config(const char *var, const char *value, void *cb)
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);
74 int cmd_var(int argc, const char **argv, const char *prefix)
76 const char *val = NULL;
77 if (argc != 2)
78 usage(var_usage);
80 if (strcmp(argv[1], "-l") == 0) {
81 git_config(show_config, NULL);
82 list_vars();
83 return 0;
85 git_config(git_default_config, NULL);
86 val = read_var(argv[1]);
87 if (!val)
88 usage(var_usage);
90 printf("%s\n", val);
92 return 0;