5 #include "parse-options.h"
8 static int cached_attrs
;
9 static int stdin_paths
;
10 static const char * const check_attr_usage
[] = {
11 N_("git check-attr [-a | --all | <attr>...] [--] <pathname>..."),
12 N_("git check-attr --stdin [-z] [-a | --all | <attr>...]"),
16 static int nul_term_line
;
18 static const struct option check_attr_options
[] = {
19 OPT_BOOL('a', "all", &all_attrs
, N_("report all attributes set on file")),
20 OPT_BOOL(0, "cached", &cached_attrs
, N_("use .gitattributes only from the index")),
21 OPT_BOOL(0 , "stdin", &stdin_paths
, N_("read file names from stdin")),
22 OPT_BOOL('z', NULL
, &nul_term_line
,
23 N_("terminate input and output records by a NUL 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";
42 printf("%s%c" /* path */
44 "%s%c" /* attrvalue */,
45 file
, 0, git_attr_name(check
[j
].attr
), 0, value
, 0);
47 quote_c_style(file
, NULL
, stdout
, 0);
48 printf(": %s: %s\n", git_attr_name(check
[j
].attr
), value
);
54 static void check_attr(const char *prefix
, int cnt
,
55 struct git_attr_check
*check
, const char *file
)
58 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
60 if (git_check_attr(full_path
, cnt
, check
))
61 die("git_check_attr died");
62 output_attr(cnt
, check
, file
);
64 if (git_all_attrs(full_path
, &cnt
, &check
))
65 die("git_all_attrs died");
66 output_attr(cnt
, check
, file
);
72 static void check_attr_stdin_paths(const char *prefix
, int cnt
,
73 struct git_attr_check
*check
)
75 struct strbuf buf
, nbuf
;
76 strbuf_getline_fn getline_fn
;
78 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
80 strbuf_init(&nbuf
, 0);
81 while (getline_fn(&buf
, stdin
) != EOF
) {
82 if (!nul_term_line
&& buf
.buf
[0] == '"') {
84 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
85 die("line is badly quoted");
86 strbuf_swap(&buf
, &nbuf
);
88 check_attr(prefix
, cnt
, check
, buf
.buf
);
89 maybe_flush_or_die(stdout
, "attribute to stdout");
92 strbuf_release(&nbuf
);
95 static NORETURN
void error_with_usage(const char *msg
)
98 usage_with_options(check_attr_usage
, check_attr_options
);
101 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
103 struct git_attr_check
*check
;
104 int cnt
, i
, doubledash
, filei
;
106 if (!is_bare_repository())
109 git_config(git_default_config
, NULL
);
111 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
112 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
114 if (read_cache() < 0) {
115 die("invalid cache");
119 git_attr_set_direction(GIT_ATTR_INDEX
, NULL
);
122 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
123 if (!strcmp(argv
[i
], "--"))
127 /* Process --all and/or attribute arguments: */
130 error_with_usage("Attributes and --all both specified");
133 filei
= doubledash
+ 1;
134 } else if (doubledash
== 0) {
135 error_with_usage("No attribute specified");
136 } else if (doubledash
< 0) {
138 error_with_usage("No attribute specified");
141 /* Treat all arguments as attribute names. */
145 /* Treat exactly one argument as an attribute name. */
151 filei
= doubledash
+ 1;
154 /* Check file argument(s): */
157 error_with_usage("Can't specify files with --stdin");
160 error_with_usage("No file specified");
166 check
= xcalloc(cnt
, sizeof(*check
));
167 for (i
= 0; i
< cnt
; i
++) {
173 return error("%s: not a valid attribute name",
180 check_attr_stdin_paths(prefix
, cnt
, check
);
182 for (i
= filei
; i
< argc
; i
++)
183 check_attr(prefix
, cnt
, check
, argv
[i
]);
184 maybe_flush_or_die(stdout
, "attribute to stdout");