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 check_attr(int cnt
, struct git_attr_check
*check
,
24 const char** name
, const char *file
)
27 if (git_checkattr(file
, cnt
, check
))
28 die("git_checkattr died");
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", name
[j
], value
);
44 static void check_attr_stdin_paths(int cnt
, struct git_attr_check
*check
,
47 struct strbuf buf
, nbuf
;
48 int line_termination
= null_term_line
? 0 : '\n';
51 strbuf_init(&nbuf
, 0);
52 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
53 if (line_termination
&& buf
.buf
[0] == '"') {
55 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
56 die("line is badly quoted");
57 strbuf_swap(&buf
, &nbuf
);
59 check_attr(cnt
, check
, name
, buf
.buf
);
60 maybe_flush_or_die(stdout
, "attribute to stdout");
63 strbuf_release(&nbuf
);
66 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
68 struct git_attr_check
*check
;
69 int cnt
, i
, doubledash
;
70 const char *errstr
= NULL
;
72 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
73 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
75 usage_with_options(check_attr_usage
, check_attr_options
);
77 if (read_cache() < 0) {
82 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
83 if (!strcmp(argv
[i
], "--"))
87 /* If there is no double dash, we handle only one attribute */
96 errstr
= "No attribute specified";
97 else if (stdin_paths
&& doubledash
< argc
)
98 errstr
= "Can't specify files with --stdin";
101 usage_with_options(check_attr_usage
, check_attr_options
);
104 check
= xcalloc(cnt
, sizeof(*check
));
105 for (i
= 0; i
< cnt
; i
++) {
109 a
= git_attr(name
, strlen(name
));
111 return error("%s: not a valid attribute name", name
);
116 check_attr_stdin_paths(cnt
, check
, argv
);
118 for (i
= doubledash
; i
< argc
; i
++)
119 check_attr(cnt
, check
, argv
, argv
[i
]);
120 maybe_flush_or_die(stdout
, "attribute to stdout");