5 #include "parse-options.h"
8 static int stdin_paths;
9 static const char * const check_attr_usage[] = {
10 "git check-attr [-a | --all | attr...] [--] pathname...",
11 "git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
15 static int null_term_line;
17 static const struct option check_attr_options[] = {
18 OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
19 OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
20 OPT_BOOLEAN('z', NULL, &null_term_line,
21 "input paths are terminated by a null character"),
25 static void output_attr(int cnt, struct git_attr_check *check,
29 for (j = 0; j < cnt; j++) {
30 const char *value = check[j].value;
34 else if (ATTR_FALSE(value))
36 else if (ATTR_UNSET(value))
37 value = "unspecified";
39 quote_c_style(file, NULL, stdout, 0);
40 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
44 static void check_attr(const char *prefix, int cnt,
45 struct git_attr_check *check, const char *file)
48 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
50 if (git_check_attr(full_path, cnt, check))
51 die("git_check_attr died");
52 output_attr(cnt, check, file);
54 if (git_all_attrs(full_path, &cnt, &check))
55 die("git_all_attrs died");
56 output_attr(cnt, check, file);
62 static void check_attr_stdin_paths(const char *prefix, int cnt,
63 struct git_attr_check *check)
65 struct strbuf buf, nbuf;
66 int line_termination = null_term_line ? 0 : '\n';
69 strbuf_init(&nbuf, 0);
70 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
71 if (line_termination && buf.buf[0] == '"') {
73 if (unquote_c_style(&nbuf, buf.buf, NULL))
74 die("line is badly quoted");
75 strbuf_swap(&buf, &nbuf);
77 check_attr(prefix, cnt, check, buf.buf);
78 maybe_flush_or_die(stdout, "attribute to stdout");
81 strbuf_release(&nbuf);
84 static NORETURN void error_with_usage(const char *msg)
87 usage_with_options(check_attr_usage, check_attr_options);
90 int cmd_check_attr(int argc, const char **argv, const char *prefix)
92 struct git_attr_check *check;
93 int cnt, i, doubledash, filei;
95 argc = parse_options(argc, argv, prefix, check_attr_options,
96 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
98 if (read_cache() < 0) {
103 for (i = 0; doubledash < 0 && i < argc; i++) {
104 if (!strcmp(argv[i], "--"))
108 /* Process --all and/or attribute arguments: */
111 error_with_usage("Attributes and --all both specified");
114 filei = doubledash + 1;
115 } else if (doubledash == 0) {
116 error_with_usage("No attribute specified");
117 } else if (doubledash < 0) {
119 error_with_usage("No attribute specified");
122 /* Treat all arguments as attribute names. */
126 /* Treat exactly one argument as an attribute name. */
132 filei = doubledash + 1;
135 /* Check file argument(s): */
138 error_with_usage("Can't specify files with --stdin");
141 error_with_usage("No file specified");
147 check = xcalloc(cnt, sizeof(*check));
148 for (i = 0; i < cnt; i++) {
154 return error("%s: not a valid attribute name",
161 check_attr_stdin_paths(prefix, cnt, check);
163 for (i = filei; i < argc; i++)
164 check_attr(prefix, cnt, check, argv[i]);
165 maybe_flush_or_die(stdout, "attribute to stdout");