update-index doc: v4 is OK with JGit and libgit2
[alt-git.git] / builtin / var.c
blob214999898079454affe0882be5b4e7a24a7a569b
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Eric Biederman, 2005
5 */
6 #include "builtin.h"
7 #include "config.h"
8 #include "editor.h"
9 #include "ident.h"
10 #include "pager.h"
11 #include "refs.h"
13 static const char var_usage[] = "git var (-l | <variable>)";
15 static const char *editor(int flag)
17 return git_editor();
20 static const char *sequence_editor(int flag)
22 return git_sequence_editor();
25 static const char *pager(int flag)
27 const char *pgm = git_pager(1);
29 if (!pgm)
30 pgm = "cat";
31 return pgm;
34 static const char *default_branch(int flag)
36 return git_default_branch_name(1);
39 struct git_var {
40 const char *name;
41 const char *(*read)(int);
43 static struct git_var git_vars[] = {
44 { "GIT_COMMITTER_IDENT", git_committer_info },
45 { "GIT_AUTHOR_IDENT", git_author_info },
46 { "GIT_EDITOR", editor },
47 { "GIT_SEQUENCE_EDITOR", sequence_editor },
48 { "GIT_PAGER", pager },
49 { "GIT_DEFAULT_BRANCH", default_branch },
50 { "", NULL },
53 static void list_vars(void)
55 struct git_var *ptr;
56 const char *val;
58 for (ptr = git_vars; ptr->read; ptr++)
59 if ((val = ptr->read(0)))
60 printf("%s=%s\n", ptr->name, val);
63 static const struct git_var *get_git_var(const char *var)
65 struct git_var *ptr;
66 for (ptr = git_vars; ptr->read; ptr++) {
67 if (strcmp(var, ptr->name) == 0) {
68 return ptr;
71 return NULL;
74 static int show_config(const char *var, const char *value, void *cb)
76 if (value)
77 printf("%s=%s\n", var, value);
78 else
79 printf("%s\n", var);
80 return git_default_config(var, value, cb);
83 int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
85 const struct git_var *git_var;
86 const char *val;
88 if (argc != 2)
89 usage(var_usage);
91 if (strcmp(argv[1], "-l") == 0) {
92 git_config(show_config, NULL);
93 list_vars();
94 return 0;
96 git_config(git_default_config, NULL);
98 git_var = get_git_var(argv[1]);
99 if (!git_var)
100 usage(var_usage);
102 val = git_var->read(IDENT_STRICT);
103 if (!val)
104 return 1;
106 printf("%s\n", val);
108 return 0;