Teach git var about GIT_EDITOR
[git/mingw.git] / var.c
blobb502487e536d76d10370182427fb0f4ab8af8a7b
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
5 */
6 #include "cache.h"
7 #include "exec_cmd.h"
9 static const char var_usage[] = "git var [-l | <variable>]";
11 static const char *editor(int flag)
13 const char *pgm = git_editor();
15 if (!pgm && flag & IDENT_ERROR_ON_NO_NAME)
16 die("Terminal is dumb, but EDITOR unset");
18 return pgm;
21 struct git_var {
22 const char *name;
23 const char *(*read)(int);
25 static struct git_var git_vars[] = {
26 { "GIT_COMMITTER_IDENT", git_committer_info },
27 { "GIT_AUTHOR_IDENT", git_author_info },
28 { "GIT_EDITOR", editor },
29 { "", NULL },
32 static void list_vars(void)
34 struct git_var *ptr;
35 const char *val;
37 for (ptr = git_vars; ptr->read; ptr++)
38 if ((val = ptr->read(0)))
39 printf("%s=%s\n", ptr->name, val);
42 static const char *read_var(const char *var)
44 struct git_var *ptr;
45 const char *val;
46 val = NULL;
47 for (ptr = git_vars; ptr->read; ptr++) {
48 if (strcmp(var, ptr->name) == 0) {
49 val = ptr->read(IDENT_ERROR_ON_NO_NAME);
50 break;
53 return val;
56 static int show_config(const char *var, const char *value, void *cb)
58 if (value)
59 printf("%s=%s\n", var, value);
60 else
61 printf("%s\n", var);
62 return git_default_config(var, value, cb);
65 int main(int argc, char **argv)
67 const char *val;
68 int nongit;
69 if (argc != 2) {
70 usage(var_usage);
73 git_extract_argv0_path(argv[0]);
75 setup_git_directory_gently(&nongit);
76 val = NULL;
78 if (strcmp(argv[1], "-l") == 0) {
79 git_config(show_config, NULL);
80 list_vars();
81 return 0;
83 git_config(git_default_config, NULL);
84 val = read_var(argv[1]);
85 if (!val)
86 usage(var_usage);
88 printf("%s\n", val);
90 return 0;