5 #include "parse-options.h"
8 static int cached_attrs
;
9 static int stdin_paths
;
10 static const char * const check_attr_usage
[] = {
11 "git check-attr [-a | --all | attr...] [--] pathname...",
12 "git check-attr --stdin [-z] [-a | --all | attr...] < <list-of-paths>",
16 static int null_term_line
;
18 static const struct option check_attr_options
[] = {
19 OPT_BOOLEAN('a', "all", &all_attrs
, "report all attributes set on file"),
20 OPT_BOOLEAN(0, "cached", &cached_attrs
, "use .gitattributes only from the index"),
21 OPT_BOOLEAN(0 , "stdin", &stdin_paths
, "read file names from stdin"),
22 OPT_BOOLEAN('z', NULL
, &null_term_line
,
23 "input paths are terminated by a null character"),
27 static void output_attr(int cnt
, struct git_attr_check
*check
,
31 for (j
= 0; j
< cnt
; j
++) {
32 const char *value
= check
[j
].value
;
36 else if (ATTR_FALSE(value
))
38 else if (ATTR_UNSET(value
))
39 value
= "unspecified";
41 quote_c_style(file
, NULL
, stdout
, 0);
42 printf(": %s: %s\n", git_attr_name(check
[j
].attr
), value
);
46 static void check_attr(const char *prefix
, int cnt
,
47 struct git_attr_check
*check
, const char *file
)
50 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
52 if (git_check_attr(full_path
, cnt
, check
))
53 die("git_check_attr died");
54 output_attr(cnt
, check
, file
);
56 if (git_all_attrs(full_path
, &cnt
, &check
))
57 die("git_all_attrs died");
58 output_attr(cnt
, check
, file
);
64 static void check_attr_stdin_paths(const char *prefix
, int cnt
,
65 struct git_attr_check
*check
)
67 struct strbuf buf
, nbuf
;
68 int line_termination
= null_term_line
? 0 : '\n';
71 strbuf_init(&nbuf
, 0);
72 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
73 if (line_termination
&& buf
.buf
[0] == '"') {
75 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
76 die("line is badly quoted");
77 strbuf_swap(&buf
, &nbuf
);
79 check_attr(prefix
, cnt
, check
, buf
.buf
);
80 maybe_flush_or_die(stdout
, "attribute to stdout");
83 strbuf_release(&nbuf
);
86 static NORETURN
void error_with_usage(const char *msg
)
89 usage_with_options(check_attr_usage
, check_attr_options
);
92 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
94 struct git_attr_check
*check
;
95 int cnt
, i
, doubledash
, filei
;
97 git_config(git_default_config
, NULL
);
99 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
100 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
102 if (read_cache() < 0) {
103 die("invalid cache");
107 git_attr_set_direction(GIT_ATTR_INDEX
, NULL
);
110 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
111 if (!strcmp(argv
[i
], "--"))
115 /* Process --all and/or attribute arguments: */
118 error_with_usage("Attributes and --all both specified");
121 filei
= doubledash
+ 1;
122 } else if (doubledash
== 0) {
123 error_with_usage("No attribute specified");
124 } else if (doubledash
< 0) {
126 error_with_usage("No attribute specified");
129 /* Treat all arguments as attribute names. */
133 /* Treat exactly one argument as an attribute name. */
139 filei
= doubledash
+ 1;
142 /* Check file argument(s): */
145 error_with_usage("Can't specify files with --stdin");
148 error_with_usage("No file specified");
154 check
= xcalloc(cnt
, sizeof(*check
));
155 for (i
= 0; i
< cnt
; i
++) {
161 return error("%s: not a valid attribute name",
168 check_attr_stdin_paths(prefix
, cnt
, check
);
170 for (i
= filei
; i
< argc
; i
++)
171 check_attr(prefix
, cnt
, check
, argv
[i
]);
172 maybe_flush_or_die(stdout
, "attribute to stdout");