1 #define USE_THE_INDEX_VARIABLE
7 #include "parse-options.h"
10 static int cached_attrs
;
11 static int stdin_paths
;
13 static const char * const check_attr_usage
[] = {
14 N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
15 N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
19 static int nul_term_line
;
21 static const struct option check_attr_options
[] = {
22 OPT_BOOL('a', "all", &all_attrs
, N_("report all attributes set on file")),
23 OPT_BOOL(0, "cached", &cached_attrs
, N_("use .gitattributes only from the index")),
24 OPT_BOOL(0 , "stdin", &stdin_paths
, N_("read file names from stdin")),
25 OPT_BOOL('z', NULL
, &nul_term_line
,
26 N_("terminate input and output records by a NUL character")),
27 OPT_STRING(0, "source", &source
, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
31 static void output_attr(struct attr_check
*check
, const char *file
)
36 for (j
= 0; j
< cnt
; j
++) {
37 const char *value
= check
->items
[j
].value
;
41 else if (ATTR_FALSE(value
))
43 else if (ATTR_UNSET(value
))
44 value
= "unspecified";
47 printf("%s%c" /* path */
49 "%s%c" /* attrvalue */,
51 git_attr_name(check
->items
[j
].attr
), 0, value
, 0);
53 quote_c_style(file
, NULL
, stdout
, 0);
55 git_attr_name(check
->items
[j
].attr
), value
);
60 static void check_attr(const char *prefix
, struct attr_check
*check
,
61 const struct object_id
*tree_oid
, int collect_all
,
66 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
69 git_all_attrs(&the_index
, tree_oid
, full_path
, check
);
71 git_check_attr(&the_index
, tree_oid
, full_path
, check
);
73 output_attr(check
, file
);
78 static void check_attr_stdin_paths(const char *prefix
, struct attr_check
*check
,
79 const struct object_id
*tree_oid
, int collect_all
)
81 struct strbuf buf
= STRBUF_INIT
;
82 struct strbuf unquoted
= STRBUF_INIT
;
83 strbuf_getline_fn getline_fn
;
85 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
86 while (getline_fn(&buf
, stdin
) != EOF
) {
87 if (!nul_term_line
&& buf
.buf
[0] == '"') {
88 strbuf_reset(&unquoted
);
89 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
90 die("line is badly quoted");
91 strbuf_swap(&buf
, &unquoted
);
93 check_attr(prefix
, check
, tree_oid
, collect_all
, buf
.buf
);
94 maybe_flush_or_die(stdout
, "attribute to stdout");
97 strbuf_release(&unquoted
);
100 static NORETURN
void error_with_usage(const char *msg
)
103 usage_with_options(check_attr_usage
, check_attr_options
);
106 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
108 struct attr_check
*check
;
109 struct object_id
*tree_oid
= NULL
;
110 struct object_id initialized_oid
;
111 int cnt
, i
, doubledash
, filei
;
113 if (!is_bare_repository())
116 git_config(git_default_config
, NULL
);
118 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
119 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
121 if (repo_read_index(the_repository
) < 0) {
122 die("invalid cache");
126 git_attr_set_direction(GIT_ATTR_INDEX
);
129 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
130 if (!strcmp(argv
[i
], "--"))
134 /* Process --all and/or attribute arguments: */
137 error_with_usage("Attributes and --all both specified");
140 filei
= doubledash
+ 1;
141 } else if (doubledash
== 0) {
142 error_with_usage("No attribute specified");
143 } else if (doubledash
< 0) {
145 error_with_usage("No attribute specified");
148 /* Treat all arguments as attribute names. */
152 /* Treat exactly one argument as an attribute name. */
158 filei
= doubledash
+ 1;
161 /* Check file argument(s): */
164 error_with_usage("Can't specify files with --stdin");
167 error_with_usage("No file specified");
170 check
= attr_check_alloc();
172 for (i
= 0; i
< cnt
; i
++) {
173 const struct git_attr
*a
= git_attr(argv
[i
]);
176 return error("%s: not a valid attribute name",
178 attr_check_append(check
, a
);
183 if (repo_get_oid_tree(the_repository
, source
, &initialized_oid
))
184 die("%s: not a valid tree-ish source", source
);
185 tree_oid
= &initialized_oid
;
189 check_attr_stdin_paths(prefix
, check
, tree_oid
, all_attrs
);
191 for (i
= filei
; i
< argc
; i
++)
192 check_attr(prefix
, check
, tree_oid
, all_attrs
, argv
[i
]);
193 maybe_flush_or_die(stdout
, "attribute to stdout");
196 attr_check_free(check
);