]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/check-attr.c
Merge branch 'nd/lockfile-absolute'
[thirdparty/git.git] / builtin / check-attr.c
CommitLineData
d0bfd026 1#include "builtin.h"
6b06d518 2#include "cache.h"
d0bfd026
JH
3#include "attr.h"
4#include "quote.h"
b4666852 5#include "parse-options.h"
d0bfd026 6
4ca0f188 7static int all_attrs;
b2b3e9c2 8static int cached_attrs;
b4666852
DP
9static int stdin_paths;
10static const char * const check_attr_usage[] = {
5a72beb7 11N_("git check-attr [-a | --all | attr...] [--] pathname..."),
b61f55be 12N_("git check-attr --stdin [-z] [-a | --all | attr...] < <list-of-paths>"),
b4666852
DP
13NULL
14};
15
94a55e4e 16static int nul_term_line;
b4666852
DP
17
18static const struct option check_attr_options[] = {
d5d09d47
SB
19 OPT_BOOL('a', "all", &all_attrs, N_("report all attributes set on file")),
20 OPT_BOOL(0, "cached", &cached_attrs, N_("use .gitattributes only from the index")),
21 OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
a86a8b97
JH
22 OPT_BOOL('z', NULL, &nul_term_line,
23 N_("terminate input and output records by a NUL character")),
b4666852
DP
24 OPT_END()
25};
d0bfd026 26
46f96a6d 27static void output_attr(int cnt, struct git_attr_check *check,
66a1fb30 28 const char *file)
41038c5e
DP
29{
30 int j;
41038c5e
DP
31 for (j = 0; j < cnt; j++) {
32 const char *value = check[j].value;
33
34 if (ATTR_TRUE(value))
35 value = "set";
36 else if (ATTR_FALSE(value))
37 value = "unset";
38 else if (ATTR_UNSET(value))
39 value = "unspecified";
40
f7cd8c50
JH
41 if (nul_term_line) {
42 printf("%s%c" /* path */
43 "%s%c" /* attrname */
44 "%s%c" /* attrvalue */,
45 file, 0, git_attr_name(check[j].attr), 0, value, 0);
46 } else {
47 quote_c_style(file, NULL, stdout, 0);
48 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
49 }
50
41038c5e
DP
51 }
52}
53
f5114a40
MH
54static void check_attr(const char *prefix, int cnt,
55 struct git_attr_check *check, const char *file)
46f96a6d 56{
f5114a40
MH
57 char *full_path =
58 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
4ca0f188 59 if (check != NULL) {
f5114a40 60 if (git_check_attr(full_path, cnt, check))
d932f4eb 61 die("git_check_attr died");
4ca0f188
MH
62 output_attr(cnt, check, file);
63 } else {
f5114a40 64 if (git_all_attrs(full_path, &cnt, &check))
4ca0f188
MH
65 die("git_all_attrs died");
66 output_attr(cnt, check, file);
67 free(check);
68 }
f5114a40 69 free(full_path);
46f96a6d
MH
70}
71
f5114a40
MH
72static void check_attr_stdin_paths(const char *prefix, int cnt,
73 struct git_attr_check *check)
b4666852
DP
74{
75 struct strbuf buf, nbuf;
94a55e4e 76 int line_termination = nul_term_line ? 0 : '\n';
b4666852
DP
77
78 strbuf_init(&buf, 0);
79 strbuf_init(&nbuf, 0);
80 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
81 if (line_termination && buf.buf[0] == '"') {
82 strbuf_reset(&nbuf);
83 if (unquote_c_style(&nbuf, buf.buf, NULL))
84 die("line is badly quoted");
85 strbuf_swap(&buf, &nbuf);
86 }
f5114a40 87 check_attr(prefix, cnt, check, buf.buf);
b4666852
DP
88 maybe_flush_or_die(stdout, "attribute to stdout");
89 }
90 strbuf_release(&buf);
91 strbuf_release(&nbuf);
92}
93
9e37a7e1
MH
94static NORETURN void error_with_usage(const char *msg)
95{
96 error("%s", msg);
97 usage_with_options(check_attr_usage, check_attr_options);
98}
99
d0bfd026
JH
100int cmd_check_attr(int argc, const char **argv, const char *prefix)
101{
102 struct git_attr_check *check;
d6541bb1 103 int cnt, i, doubledash, filei;
b4666852 104
cdbf6232
JH
105 if (!is_bare_repository())
106 setup_work_tree();
107
64589a03
JH
108 git_config(git_default_config, NULL);
109
37782920
SB
110 argc = parse_options(argc, argv, prefix, check_attr_options,
111 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
d0bfd026 112
6b06d518
BD
113 if (read_cache() < 0) {
114 die("invalid cache");
115 }
116
b2b3e9c2
JS
117 if (cached_attrs)
118 git_attr_set_direction(GIT_ATTR_INDEX, NULL);
119
d0bfd026 120 doubledash = -1;
b4666852 121 for (i = 0; doubledash < 0 && i < argc; i++) {
d0bfd026
JH
122 if (!strcmp(argv[i], "--"))
123 doubledash = i;
124 }
125
4ca0f188
MH
126 /* Process --all and/or attribute arguments: */
127 if (all_attrs) {
128 if (doubledash >= 1)
129 error_with_usage("Attributes and --all both specified");
130
131 cnt = 0;
132 filei = doubledash + 1;
133 } else if (doubledash == 0) {
72541040
MH
134 error_with_usage("No attribute specified");
135 } else if (doubledash < 0) {
72541040
MH
136 if (!argc)
137 error_with_usage("No attribute specified");
138
ca64d061
MH
139 if (stdin_paths) {
140 /* Treat all arguments as attribute names. */
141 cnt = argc;
142 filei = argc;
143 } else {
144 /* Treat exactly one argument as an attribute name. */
145 cnt = 1;
146 filei = 1;
147 }
d6541bb1 148 } else {
b4666852 149 cnt = doubledash;
d6541bb1
MH
150 filei = doubledash + 1;
151 }
d0bfd026 152
72541040 153 /* Check file argument(s): */
fdf6be82
MH
154 if (stdin_paths) {
155 if (filei < argc)
156 error_with_usage("Can't specify files with --stdin");
157 } else {
158 if (filei >= argc)
159 error_with_usage("No file specified");
160 }
b4666852 161
4ca0f188
MH
162 if (all_attrs) {
163 check = NULL;
164 } else {
165 check = xcalloc(cnt, sizeof(*check));
166 for (i = 0; i < cnt; i++) {
167 const char *name;
168 struct git_attr *a;
169 name = argv[i];
170 a = git_attr(name);
171 if (!a)
172 return error("%s: not a valid attribute name",
173 name);
174 check[i].attr = a;
175 }
d0bfd026
JH
176 }
177
b4666852 178 if (stdin_paths)
f5114a40 179 check_attr_stdin_paths(prefix, cnt, check);
b4666852 180 else {
d6541bb1 181 for (i = filei; i < argc; i++)
f5114a40 182 check_attr(prefix, cnt, check, argv[i]);
b4666852
DP
183 maybe_flush_or_die(stdout, "attribute to stdout");
184 }
d0bfd026
JH
185 return 0;
186}