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