Commit | Line | Data |
---|---|---|
aed022ab EB |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Eric Biederman, 2005 | |
5 | */ | |
6 | #include "cache.h" | |
2fb3f6db | 7 | #include "exec_cmd.h" |
aed022ab | 8 | |
5354a56f | 9 | static const char var_usage[] = "git var [-l | <variable>]"; |
aed022ab EB |
10 | |
11 | struct git_var { | |
12 | const char *name; | |
749be728 | 13 | const char *(*read)(int); |
aed022ab EB |
14 | }; |
15 | static struct git_var git_vars[] = { | |
16 | { "GIT_COMMITTER_IDENT", git_committer_info }, | |
17 | { "GIT_AUTHOR_IDENT", git_author_info }, | |
18 | { "", NULL }, | |
19 | }; | |
20 | ||
21 | static void list_vars(void) | |
22 | { | |
23 | struct git_var *ptr; | |
24 | for(ptr = git_vars; ptr->read; ptr++) { | |
774751a8 | 25 | printf("%s=%s\n", ptr->name, ptr->read(IDENT_WARN_ON_NO_NAME)); |
aed022ab EB |
26 | } |
27 | } | |
28 | ||
29 | static const char *read_var(const char *var) | |
30 | { | |
31 | struct git_var *ptr; | |
32 | const char *val; | |
33 | val = NULL; | |
34 | for(ptr = git_vars; ptr->read; ptr++) { | |
35 | if (strcmp(var, ptr->name) == 0) { | |
774751a8 | 36 | val = ptr->read(IDENT_ERROR_ON_NO_NAME); |
aed022ab EB |
37 | break; |
38 | } | |
39 | } | |
40 | return val; | |
41 | } | |
42 | ||
ef90d6d4 | 43 | static int show_config(const char *var, const char *value, void *cb) |
e1b10391 LT |
44 | { |
45 | if (value) | |
46 | printf("%s=%s\n", var, value); | |
47 | else | |
48 | printf("%s\n", var); | |
ef90d6d4 | 49 | return git_default_config(var, value, cb); |
e1b10391 LT |
50 | } |
51 | ||
aed022ab EB |
52 | int main(int argc, char **argv) |
53 | { | |
54 | const char *val; | |
2fba8366 | 55 | int nongit; |
aed022ab EB |
56 | if (argc != 2) { |
57 | usage(var_usage); | |
58 | } | |
5d318ce6 | 59 | |
2fb3f6db SP |
60 | git_extract_argv0_path(argv[0]); |
61 | ||
2fba8366 | 62 | setup_git_directory_gently(&nongit); |
aed022ab EB |
63 | val = NULL; |
64 | ||
65 | if (strcmp(argv[1], "-l") == 0) { | |
ef90d6d4 | 66 | git_config(show_config, NULL); |
aed022ab EB |
67 | list_vars(); |
68 | return 0; | |
69 | } | |
ef90d6d4 | 70 | git_config(git_default_config, NULL); |
aed022ab EB |
71 | val = read_var(argv[1]); |
72 | if (!val) | |
73 | usage(var_usage); | |
a6080a0a | 74 | |
aed022ab | 75 | printf("%s\n", val); |
a6080a0a | 76 | |
aed022ab EB |
77 | return 0; |
78 | } |