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(int cnt
, struct git_attr_check
*check
,
48 if (git_checkattr(file
, cnt
, check
))
49 die("git_checkattr died");
50 output_attr(cnt
, check
, file
);
52 if (git_all_attrs(file
, &cnt
, &check
))
53 die("git_all_attrs died");
54 output_attr(cnt
, check
, file
);
59 static void check_attr_stdin_paths(int cnt
, struct git_attr_check
*check
)
61 struct strbuf buf
, nbuf
;
62 int line_termination
= null_term_line
? 0 : '\n';
65 strbuf_init(&nbuf
, 0);
66 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
67 if (line_termination
&& buf
.buf
[0] == '"') {
69 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
70 die("line is badly quoted");
71 strbuf_swap(&buf
, &nbuf
);
73 check_attr(cnt
, check
, buf
.buf
);
74 maybe_flush_or_die(stdout
, "attribute to stdout");
77 strbuf_release(&nbuf
);
80 static NORETURN
void error_with_usage(const char *msg
)
83 usage_with_options(check_attr_usage
, check_attr_options
);
86 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
88 struct git_attr_check
*check
;
89 int cnt
, i
, doubledash
, filei
;
91 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
92 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
94 if (read_cache() < 0) {
99 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
100 if (!strcmp(argv
[i
], "--"))
104 /* Process --all and/or attribute arguments: */
107 error_with_usage("Attributes and --all both specified");
110 filei
= doubledash
+ 1;
111 } else if (doubledash
== 0) {
112 error_with_usage("No attribute specified");
113 } else if (doubledash
< 0) {
115 * There is no double dash; treat the first
116 * argument as an attribute.
119 error_with_usage("No attribute specified");
125 filei
= doubledash
+ 1;
128 /* Check file argument(s): */
131 error_with_usage("Can't specify files with --stdin");
134 error_with_usage("No file specified");
140 check
= xcalloc(cnt
, sizeof(*check
));
141 for (i
= 0; i
< cnt
; i
++) {
147 return error("%s: not a valid attribute name",
154 check_attr_stdin_paths(cnt
, check
);
156 for (i
= filei
; i
< argc
; i
++)
157 check_attr(cnt
, check
, argv
[i
]);
158 maybe_flush_or_die(stdout
, "attribute to stdout");