]>
Commit | Line | Data |
---|---|---|
aed022ab EB |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Eric Biederman, 2005 | |
5 | */ | |
c2e86add | 6 | #include "builtin.h" |
b2141fc1 | 7 | #include "config.h" |
4e120823 | 8 | #include "editor.h" |
b5fa6081 | 9 | #include "ident.h" |
ca4eed70 | 10 | #include "pager.h" |
e06c9e1d | 11 | #include "refs.h" |
aed022ab | 12 | |
9fabb6d7 | 13 | static const char var_usage[] = "git var (-l | <variable>)"; |
aed022ab | 14 | |
44fcb497 JN |
15 | static const char *editor(int flag) |
16 | { | |
2ad150e3 | 17 | return git_editor(); |
44fcb497 JN |
18 | } |
19 | ||
4c3dd930 SA |
20 | static const char *sequence_editor(int flag) |
21 | { | |
22 | return git_sequence_editor(); | |
23 | } | |
24 | ||
63618245 JN |
25 | static const char *pager(int flag) |
26 | { | |
64778d24 | 27 | const char *pgm = git_pager(1); |
63618245 JN |
28 | |
29 | if (!pgm) | |
30 | pgm = "cat"; | |
31 | return pgm; | |
32 | } | |
33 | ||
e06c9e1d TW |
34 | static const char *default_branch(int flag) |
35 | { | |
36 | return git_default_branch_name(1); | |
37 | } | |
38 | ||
aed022ab EB |
39 | struct git_var { |
40 | const char *name; | |
749be728 | 41 | const char *(*read)(int); |
aed022ab EB |
42 | }; |
43 | static struct git_var git_vars[] = { | |
44 | { "GIT_COMMITTER_IDENT", git_committer_info }, | |
45 | { "GIT_AUTHOR_IDENT", git_author_info }, | |
44fcb497 | 46 | { "GIT_EDITOR", editor }, |
4c3dd930 | 47 | { "GIT_SEQUENCE_EDITOR", sequence_editor }, |
63618245 | 48 | { "GIT_PAGER", pager }, |
e06c9e1d | 49 | { "GIT_DEFAULT_BRANCH", default_branch }, |
aed022ab EB |
50 | { "", NULL }, |
51 | }; | |
52 | ||
53 | static void list_vars(void) | |
54 | { | |
55 | struct git_var *ptr; | |
44fcb497 JN |
56 | const char *val; |
57 | ||
eeefa7c9 | 58 | for (ptr = git_vars; ptr->read; ptr++) |
44fcb497 JN |
59 | if ((val = ptr->read(0))) |
60 | printf("%s=%s\n", ptr->name, val); | |
aed022ab EB |
61 | } |
62 | ||
26b8abc7 | 63 | static const struct git_var *get_git_var(const char *var) |
aed022ab EB |
64 | { |
65 | struct git_var *ptr; | |
eeefa7c9 | 66 | for (ptr = git_vars; ptr->read; ptr++) { |
aed022ab | 67 | if (strcmp(var, ptr->name) == 0) { |
26b8abc7 | 68 | return ptr; |
aed022ab EB |
69 | } |
70 | } | |
26b8abc7 | 71 | return NULL; |
aed022ab EB |
72 | } |
73 | ||
ef90d6d4 | 74 | static int show_config(const char *var, const char *value, void *cb) |
e1b10391 LT |
75 | { |
76 | if (value) | |
77 | printf("%s=%s\n", var, value); | |
78 | else | |
79 | printf("%s\n", var); | |
ef90d6d4 | 80 | return git_default_config(var, value, cb); |
e1b10391 LT |
81 | } |
82 | ||
55b6745d | 83 | int cmd_var(int argc, const char **argv, const char *prefix) |
aed022ab | 84 | { |
26b8abc7 SA |
85 | const struct git_var *git_var; |
86 | const char *val; | |
87 | ||
2bc8c1a8 | 88 | if (argc != 2) |
aed022ab | 89 | usage(var_usage); |
aed022ab EB |
90 | |
91 | if (strcmp(argv[1], "-l") == 0) { | |
ef90d6d4 | 92 | git_config(show_config, NULL); |
aed022ab EB |
93 | list_vars(); |
94 | return 0; | |
95 | } | |
ef90d6d4 | 96 | git_config(git_default_config, NULL); |
26b8abc7 SA |
97 | |
98 | git_var = get_git_var(argv[1]); | |
99 | if (!git_var) | |
aed022ab | 100 | usage(var_usage); |
a6080a0a | 101 | |
26b8abc7 SA |
102 | val = git_var->read(IDENT_STRICT); |
103 | if (!val) | |
104 | return 1; | |
105 | ||
aed022ab | 106 | printf("%s\n", val); |
a6080a0a | 107 | |
aed022ab EB |
108 | return 0; |
109 | } |