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