diff: do not use null sha1 as a sentinel value
[git/git.git] / builtin / var.c
CommitLineData
aed022ab
EB
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Eric Biederman, 2005
5 */
c2e86add 6#include "builtin.h"
aed022ab 7
9fabb6d7 8static const char var_usage[] = "git var (-l | <variable>)";
aed022ab 9
44fcb497
JN
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
63618245
JN
20static const char *pager(int flag)
21{
64778d24 22 const char *pgm = git_pager(1);
63618245
JN
23
24 if (!pgm)
25 pgm = "cat";
26 return pgm;
27}
28
aed022ab
EB
29struct git_var {
30 const char *name;
749be728 31 const char *(*read)(int);
aed022ab
EB
32};
33static struct git_var git_vars[] = {
34 { "GIT_COMMITTER_IDENT", git_committer_info },
35 { "GIT_AUTHOR_IDENT", git_author_info },
44fcb497 36 { "GIT_EDITOR", editor },
63618245 37 { "GIT_PAGER", pager },
aed022ab
EB
38 { "", NULL },
39};
40
41static void list_vars(void)
42{
43 struct git_var *ptr;
44fcb497
JN
44 const char *val;
45
eeefa7c9 46 for (ptr = git_vars; ptr->read; ptr++)
44fcb497
JN
47 if ((val = ptr->read(0)))
48 printf("%s=%s\n", ptr->name, val);
aed022ab
EB
49}
50
51static const char *read_var(const char *var)
52{
53 struct git_var *ptr;
54 const char *val;
55 val = NULL;
eeefa7c9 56 for (ptr = git_vars; ptr->read; ptr++) {
aed022ab 57 if (strcmp(var, ptr->name) == 0) {
774751a8 58 val = ptr->read(IDENT_ERROR_ON_NO_NAME);
aed022ab
EB
59 break;
60 }
61 }
62 return val;
63}
64
ef90d6d4 65static int show_config(const char *var, const char *value, void *cb)
e1b10391
LT
66{
67 if (value)
68 printf("%s=%s\n", var, value);
69 else
70 printf("%s\n", var);
ef90d6d4 71 return git_default_config(var, value, cb);
e1b10391
LT
72}
73
55b6745d 74int cmd_var(int argc, const char **argv, const char *prefix)
aed022ab 75{
2bc8c1a8
NTND
76 const char *val = NULL;
77 if (argc != 2)
aed022ab 78 usage(var_usage);
aed022ab
EB
79
80 if (strcmp(argv[1], "-l") == 0) {
ef90d6d4 81 git_config(show_config, NULL);
aed022ab
EB
82 list_vars();
83 return 0;
84 }
ef90d6d4 85 git_config(git_default_config, NULL);
aed022ab
EB
86 val = read_var(argv[1]);
87 if (!val)
88 usage(var_usage);
a6080a0a 89
aed022ab 90 printf("%s\n", val);
a6080a0a 91
aed022ab
EB
92 return 0;
93}