Skip to content

Commit 89d62d5

Browse files
committed
Merge branch 'bc/more-git-var'
Add more "git var" for toolsmiths to learn various locations Git is configured with either via the configuration or hardcoded defaults. * bc/more-git-var: var: add config file locations var: add attributes files locations attr: expose and rename accessor functions var: adjust memory allocation for strings var: format variable structure with C99 initializers var: add support for listing the shell t: add a function to check executable bit var: mark unused parameters in git_var callbacks
2 parents 812907d + ed773a1 commit 89d62d5

File tree

7 files changed

+303
-27
lines changed

7 files changed

+303
-27
lines changed

Documentation/git-var.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ endif::git-default-pager[]
7171
GIT_DEFAULT_BRANCH::
7272
The name of the first branch created in newly initialized repositories.
7373

74+
GIT_SHELL_PATH::
75+
The path of the binary providing the POSIX shell for commands which use the shell.
76+
77+
GIT_ATTR_SYSTEM::
78+
The path to the system linkgit:gitattributes[5] file, if one is enabled.
79+
80+
GIT_ATTR_GLOBAL::
81+
The path to the global (per-user) linkgit:gitattributes[5] file.
82+
83+
GIT_CONFIG_SYSTEM::
84+
The path to the system configuration file, if one is enabled.
85+
86+
GIT_CONFIG_GLOBAL::
87+
The path to the global (per-user) configuration files, if any.
88+
89+
Most path values contain only one value. However, some can contain multiple
90+
values, which are separated by newlines, and are listed in order from highest to
91+
lowest priority. Callers should be prepared for any such path value to contain
92+
multiple items.
93+
94+
Note that paths are printed even if they do not exist, but not if they are
95+
disabled by other environment variables.
96+
7497
SEE ALSO
7598
--------
7699
linkgit:git-commit-tree[1]

attr.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -872,23 +872,23 @@ static struct attr_stack *read_attr(struct index_state *istate,
872872
return res;
873873
}
874874

875-
static const char *git_etc_gitattributes(void)
875+
const char *git_attr_system_file(void)
876876
{
877877
static const char *system_wide;
878878
if (!system_wide)
879879
system_wide = system_path(ETC_GITATTRIBUTES);
880880
return system_wide;
881881
}
882882

883-
static const char *get_home_gitattributes(void)
883+
const char *git_attr_global_file(void)
884884
{
885885
if (!git_attributes_file)
886886
git_attributes_file = xdg_config_home("attributes");
887887

888888
return git_attributes_file;
889889
}
890890

891-
static int git_attr_system(void)
891+
int git_attr_system_is_enabled(void)
892892
{
893893
return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
894894
}
@@ -922,14 +922,14 @@ static void bootstrap_attr_stack(struct index_state *istate,
922922
push_stack(stack, e, NULL, 0);
923923

924924
/* system-wide frame */
925-
if (git_attr_system()) {
926-
e = read_attr_from_file(git_etc_gitattributes(), flags);
925+
if (git_attr_system_is_enabled()) {
926+
e = read_attr_from_file(git_attr_system_file(), flags);
927927
push_stack(stack, e, NULL, 0);
928928
}
929929

930930
/* home directory */
931-
if (get_home_gitattributes()) {
932-
e = read_attr_from_file(get_home_gitattributes(), flags);
931+
if (git_attr_global_file()) {
932+
e = read_attr_from_file(git_attr_global_file(), flags);
933933
push_stack(stack, e, NULL, 0);
934934
}
935935

attr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,13 @@ void git_attr_set_direction(enum git_attr_direction new_direction);
227227

228228
void attr_start(void);
229229

230+
/* Return the system gitattributes file. */
231+
const char *git_attr_system_file(void);
232+
233+
/* Return the global gitattributes file, if any. */
234+
const char *git_attr_global_file(void);
235+
236+
/* Return whether the system gitattributes file is enabled and should be used. */
237+
int git_attr_system_is_enabled(void);
238+
230239
#endif /* ATTR_H */

builtin/var.c

Lines changed: 149 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,188 @@
44
* Copyright (C) Eric Biederman, 2005
55
*/
66
#include "builtin.h"
7+
#include "attr.h"
78
#include "config.h"
89
#include "editor.h"
910
#include "ident.h"
1011
#include "pager.h"
1112
#include "refs.h"
13+
#include "path.h"
14+
#include "strbuf.h"
1215

1316
static const char var_usage[] = "git var (-l | <variable>)";
1417

15-
static const char *editor(int flag)
18+
static char *committer(int ident_flag)
1619
{
17-
return git_editor();
20+
return xstrdup_or_null(git_committer_info(ident_flag));
1821
}
1922

20-
static const char *sequence_editor(int flag)
23+
static char *author(int ident_flag)
2124
{
22-
return git_sequence_editor();
25+
return xstrdup_or_null(git_author_info(ident_flag));
2326
}
2427

25-
static const char *pager(int flag)
28+
static char *editor(int ident_flag UNUSED)
29+
{
30+
return xstrdup_or_null(git_editor());
31+
}
32+
33+
static char *sequence_editor(int ident_flag UNUSED)
34+
{
35+
return xstrdup_or_null(git_sequence_editor());
36+
}
37+
38+
static char *pager(int ident_flag UNUSED)
2639
{
2740
const char *pgm = git_pager(1);
2841

2942
if (!pgm)
3043
pgm = "cat";
31-
return pgm;
44+
return xstrdup(pgm);
45+
}
46+
47+
static char *default_branch(int ident_flag UNUSED)
48+
{
49+
return xstrdup_or_null(git_default_branch_name(1));
50+
}
51+
52+
static char *shell_path(int ident_flag UNUSED)
53+
{
54+
return xstrdup(SHELL_PATH);
3255
}
3356

34-
static const char *default_branch(int flag)
57+
static char *git_attr_val_system(int ident_flag UNUSED)
3558
{
36-
return git_default_branch_name(1);
59+
if (git_attr_system_is_enabled()) {
60+
char *file = xstrdup(git_attr_system_file());
61+
normalize_path_copy(file, file);
62+
return file;
63+
}
64+
return NULL;
65+
}
66+
67+
static char *git_attr_val_global(int ident_flag UNUSED)
68+
{
69+
char *file = xstrdup(git_attr_global_file());
70+
if (file) {
71+
normalize_path_copy(file, file);
72+
return file;
73+
}
74+
return NULL;
75+
}
76+
77+
static char *git_config_val_system(int ident_flag UNUSED)
78+
{
79+
if (git_config_system()) {
80+
char *file = git_system_config();
81+
normalize_path_copy(file, file);
82+
return file;
83+
}
84+
return NULL;
85+
}
86+
87+
static char *git_config_val_global(int ident_flag UNUSED)
88+
{
89+
struct strbuf buf = STRBUF_INIT;
90+
char *user, *xdg;
91+
size_t unused;
92+
93+
git_global_config(&user, &xdg);
94+
if (xdg && *xdg) {
95+
normalize_path_copy(xdg, xdg);
96+
strbuf_addf(&buf, "%s\n", xdg);
97+
}
98+
if (user && *user) {
99+
normalize_path_copy(user, user);
100+
strbuf_addf(&buf, "%s\n", user);
101+
}
102+
free(xdg);
103+
free(user);
104+
strbuf_trim_trailing_newline(&buf);
105+
if (buf.len == 0) {
106+
strbuf_release(&buf);
107+
return NULL;
108+
}
109+
return strbuf_detach(&buf, &unused);
37110
}
38111

39112
struct git_var {
40113
const char *name;
41-
const char *(*read)(int);
114+
char *(*read)(int);
115+
int multivalued;
42116
};
43117
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 },
118+
{
119+
.name = "GIT_COMMITTER_IDENT",
120+
.read = committer,
121+
},
122+
{
123+
.name = "GIT_AUTHOR_IDENT",
124+
.read = author,
125+
},
126+
{
127+
.name = "GIT_EDITOR",
128+
.read = editor,
129+
},
130+
{
131+
.name = "GIT_SEQUENCE_EDITOR",
132+
.read = sequence_editor,
133+
},
134+
{
135+
.name = "GIT_PAGER",
136+
.read = pager,
137+
},
138+
{
139+
.name = "GIT_DEFAULT_BRANCH",
140+
.read = default_branch,
141+
},
142+
{
143+
.name = "GIT_SHELL_PATH",
144+
.read = shell_path,
145+
},
146+
{
147+
.name = "GIT_ATTR_SYSTEM",
148+
.read = git_attr_val_system,
149+
},
150+
{
151+
.name = "GIT_ATTR_GLOBAL",
152+
.read = git_attr_val_global,
153+
},
154+
{
155+
.name = "GIT_CONFIG_SYSTEM",
156+
.read = git_config_val_system,
157+
},
158+
{
159+
.name = "GIT_CONFIG_GLOBAL",
160+
.read = git_config_val_global,
161+
.multivalued = 1,
162+
},
163+
{
164+
.name = "",
165+
.read = NULL,
166+
},
51167
};
52168

53169
static void list_vars(void)
54170
{
55171
struct git_var *ptr;
56-
const char *val;
172+
char *val;
57173

58174
for (ptr = git_vars; ptr->read; ptr++)
59-
if ((val = ptr->read(0)))
60-
printf("%s=%s\n", ptr->name, val);
175+
if ((val = ptr->read(0))) {
176+
if (ptr->multivalued && *val) {
177+
struct string_list list = STRING_LIST_INIT_DUP;
178+
int i;
179+
180+
string_list_split(&list, val, '\n', -1);
181+
for (i = 0; i < list.nr; i++)
182+
printf("%s=%s\n", ptr->name, list.items[i].string);
183+
string_list_clear(&list, 0);
184+
} else {
185+
printf("%s=%s\n", ptr->name, val);
186+
}
187+
free(val);
188+
}
61189
}
62190

63191
static const struct git_var *get_git_var(const char *var)
@@ -83,7 +211,7 @@ static int show_config(const char *var, const char *value, void *cb)
83211
int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
84212
{
85213
const struct git_var *git_var;
86-
const char *val;
214+
char *val;
87215

88216
if (argc != 2)
89217
usage(var_usage);
@@ -104,6 +232,7 @@ int cmd_var(int argc, const char **argv, const char *prefix UNUSED)
104232
return 1;
105233

106234
printf("%s\n", val);
235+
free(val);
107236

108237
return 0;
109238
}

t/README

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,12 @@ see test-lib-functions.sh for the full list and their options.
11021102
the symbolic link in the file system and a part that does; then only
11031103
the latter part need be protected by a SYMLINKS prerequisite (see below).
11041104

1105+
- test_path_is_executable
1106+
1107+
This tests whether a file is executable and prints an error message
1108+
if not. This must be used only under the POSIXPERM prerequisite
1109+
(see below).
1110+
11051111
- test_oid_init
11061112

11071113
This function loads facts and useful object IDs related to the hash

0 commit comments

Comments
 (0)