1 #define USE_THE_INDEX_VARIABLE
6 #include "environment.h"
10 #include "parse-options.h"
13 static int cached_attrs
;
14 static int stdin_paths
;
16 static const char * const check_attr_usage
[] = {
17 N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
18 N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
22 static int nul_term_line
;
24 static const struct option check_attr_options
[] = {
25 OPT_BOOL('a', "all", &all_attrs
, N_("report all attributes set on file")),
26 OPT_BOOL(0, "cached", &cached_attrs
, N_("use .gitattributes only from the index")),
27 OPT_BOOL(0 , "stdin", &stdin_paths
, N_("read file names from stdin")),
28 OPT_BOOL('z', NULL
, &nul_term_line
,
29 N_("terminate input and output records by a NUL character")),
30 OPT_STRING(0, "source", &source
, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
34 static void output_attr(struct attr_check
*check
, const char *file
)
39 for (j
= 0; j
< cnt
; j
++) {
40 const char *value
= check
->items
[j
].value
;
44 else if (ATTR_FALSE(value
))
46 else if (ATTR_UNSET(value
))
47 value
= "unspecified";
50 printf("%s%c" /* path */
52 "%s%c" /* attrvalue */,
54 git_attr_name(check
->items
[j
].attr
), 0, value
, 0);
56 quote_c_style(file
, NULL
, stdout
, 0);
58 git_attr_name(check
->items
[j
].attr
), value
);
63 static void check_attr(const char *prefix
, struct attr_check
*check
,
64 const struct object_id
*tree_oid
, int collect_all
,
69 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
72 git_all_attrs(&the_index
, tree_oid
, full_path
, check
);
74 git_check_attr(&the_index
, tree_oid
, full_path
, check
);
76 output_attr(check
, file
);
81 static void check_attr_stdin_paths(const char *prefix
, struct attr_check
*check
,
82 const struct object_id
*tree_oid
, int collect_all
)
84 struct strbuf buf
= STRBUF_INIT
;
85 struct strbuf unquoted
= STRBUF_INIT
;
86 strbuf_getline_fn getline_fn
;
88 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
89 while (getline_fn(&buf
, stdin
) != EOF
) {
90 if (!nul_term_line
&& buf
.buf
[0] == '"') {
91 strbuf_reset(&unquoted
);
92 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
93 die("line is badly quoted");
94 strbuf_swap(&buf
, &unquoted
);
96 check_attr(prefix
, check
, tree_oid
, collect_all
, buf
.buf
);
97 maybe_flush_or_die(stdout
, "attribute to stdout");
100 strbuf_release(&unquoted
);
103 static NORETURN
void error_with_usage(const char *msg
)
106 usage_with_options(check_attr_usage
, check_attr_options
);
109 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
111 struct attr_check
*check
;
112 struct object_id
*tree_oid
= NULL
;
113 struct object_id initialized_oid
;
114 int cnt
, i
, doubledash
, filei
;
116 if (!is_bare_repository())
119 git_config(git_default_config
, NULL
);
121 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
122 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
124 if (repo_read_index(the_repository
) < 0) {
125 die("invalid cache");
129 git_attr_set_direction(GIT_ATTR_INDEX
);
132 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
133 if (!strcmp(argv
[i
], "--"))
137 /* Process --all and/or attribute arguments: */
140 error_with_usage("Attributes and --all both specified");
143 filei
= doubledash
+ 1;
144 } else if (doubledash
== 0) {
145 error_with_usage("No attribute specified");
146 } else if (doubledash
< 0) {
148 error_with_usage("No attribute specified");
151 /* Treat all arguments as attribute names. */
155 /* Treat exactly one argument as an attribute name. */
161 filei
= doubledash
+ 1;
164 /* Check file argument(s): */
167 error_with_usage("Can't specify files with --stdin");
170 error_with_usage("No file specified");
173 check
= attr_check_alloc();
175 for (i
= 0; i
< cnt
; i
++) {
176 const struct git_attr
*a
= git_attr(argv
[i
]);
179 return error("%s: not a valid attribute name",
181 attr_check_append(check
, a
);
186 if (repo_get_oid_tree(the_repository
, source
, &initialized_oid
))
187 die("%s: not a valid tree-ish source", source
);
188 tree_oid
= &initialized_oid
;
192 check_attr_stdin_paths(prefix
, check
, tree_oid
, all_attrs
);
194 for (i
= filei
; i
< argc
; i
++)
195 check_attr(prefix
, check
, tree_oid
, all_attrs
, argv
[i
]);
196 maybe_flush_or_die(stdout
, "attribute to stdout");
199 attr_check_free(check
);