5 #include "parse-options.h"
7 static int stdin_paths;
8 static const char * const check_attr_usage[] = {
9 "git check-attr attr... [--] pathname...",
10 "git check-attr --stdin attr... < <list-of-paths>",
14 static int null_term_line;
16 static const struct option check_attr_options[] = {
17 OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
18 OPT_BOOLEAN('z', NULL, &null_term_line,
19 "input paths are terminated by a null character"),
23 static void output_attr(int cnt, struct git_attr_check *check,
27 for (j = 0; j < cnt; j++) {
28 const char *value = check[j].value;
32 else if (ATTR_FALSE(value))
34 else if (ATTR_UNSET(value))
35 value = "unspecified";
37 quote_c_style(file, NULL, stdout, 0);
38 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
42 static void check_attr(int cnt, struct git_attr_check *check,
45 if (git_checkattr(file, cnt, check))
46 die("git_checkattr died");
47 output_attr(cnt, check, file);
50 static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
52 struct strbuf buf, nbuf;
53 int line_termination = null_term_line ? 0 : '\n';
56 strbuf_init(&nbuf, 0);
57 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
58 if (line_termination && buf.buf[0] == '"') {
60 if (unquote_c_style(&nbuf, buf.buf, NULL))
61 die("line is badly quoted");
62 strbuf_swap(&buf, &nbuf);
64 check_attr(cnt, check, buf.buf);
65 maybe_flush_or_die(stdout, "attribute to stdout");
68 strbuf_release(&nbuf);
71 static NORETURN void error_with_usage(const char *msg)
74 usage_with_options(check_attr_usage, check_attr_options);
77 int cmd_check_attr(int argc, const char **argv, const char *prefix)
79 struct git_attr_check *check;
80 int cnt, i, doubledash, filei;
82 argc = parse_options(argc, argv, prefix, check_attr_options,
83 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
85 if (read_cache() < 0) {
90 for (i = 0; doubledash < 0 && i < argc; i++) {
91 if (!strcmp(argv[i], "--"))
95 /* Check attribute argument(s): */
96 if (doubledash == 0) {
97 error_with_usage("No attribute specified");
98 } else if (doubledash < 0) {
100 * There is no double dash; treat the first
101 * argument as an attribute.
104 error_with_usage("No attribute specified");
110 filei = doubledash + 1;
113 /* Check file argument(s): */
114 if (stdin_paths && filei < argc)
115 error_with_usage("Can't specify files with --stdin");
117 check = xcalloc(cnt, sizeof(*check));
118 for (i = 0; i < cnt; i++) {
124 return error("%s: not a valid attribute name", name);
129 check_attr_stdin_paths(cnt, check);
131 for (i = filei; i < argc; i++)
132 check_attr(cnt, check, argv[i]);
133 maybe_flush_or_die(stdout, "attribute to stdout");