1 #define USE_THE_INDEX_VARIABLE
6 #include "environment.h"
8 #include "object-name.h"
10 #include "repository.h"
12 #include "parse-options.h"
13 #include "write-or-die.h"
16 static int cached_attrs
;
17 static int stdin_paths
;
19 static const char * const check_attr_usage
[] = {
20 N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
21 N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
25 static int nul_term_line
;
27 static const struct option check_attr_options
[] = {
28 OPT_BOOL('a', "all", &all_attrs
, N_("report all attributes set on file")),
29 OPT_BOOL(0, "cached", &cached_attrs
, N_("use .gitattributes only from the index")),
30 OPT_BOOL(0 , "stdin", &stdin_paths
, N_("read file names from stdin")),
31 OPT_BOOL('z', NULL
, &nul_term_line
,
32 N_("terminate input and output records by a NUL character")),
33 OPT_STRING(0, "source", &source
, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
37 static void output_attr(struct attr_check
*check
, const char *file
)
42 for (j
= 0; j
< cnt
; j
++) {
43 const char *value
= check
->items
[j
].value
;
47 else if (ATTR_FALSE(value
))
49 else if (ATTR_UNSET(value
))
50 value
= "unspecified";
53 printf("%s%c" /* path */
55 "%s%c" /* attrvalue */,
57 git_attr_name(check
->items
[j
].attr
), 0, value
, 0);
59 quote_c_style(file
, NULL
, stdout
, 0);
61 git_attr_name(check
->items
[j
].attr
), value
);
66 static void check_attr(const char *prefix
, struct attr_check
*check
,
67 const struct object_id
*tree_oid
, int collect_all
,
72 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
75 git_all_attrs(&the_index
, tree_oid
, full_path
, check
);
77 git_check_attr(&the_index
, tree_oid
, full_path
, check
);
79 output_attr(check
, file
);
84 static void check_attr_stdin_paths(const char *prefix
, struct attr_check
*check
,
85 const struct object_id
*tree_oid
, int collect_all
)
87 struct strbuf buf
= STRBUF_INIT
;
88 struct strbuf unquoted
= STRBUF_INIT
;
89 strbuf_getline_fn getline_fn
;
91 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
92 while (getline_fn(&buf
, stdin
) != EOF
) {
93 if (!nul_term_line
&& buf
.buf
[0] == '"') {
94 strbuf_reset(&unquoted
);
95 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
96 die("line is badly quoted");
97 strbuf_swap(&buf
, &unquoted
);
99 check_attr(prefix
, check
, tree_oid
, collect_all
, buf
.buf
);
100 maybe_flush_or_die(stdout
, "attribute to stdout");
102 strbuf_release(&buf
);
103 strbuf_release(&unquoted
);
106 static NORETURN
void error_with_usage(const char *msg
)
109 usage_with_options(check_attr_usage
, check_attr_options
);
112 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
114 struct attr_check
*check
;
115 struct object_id
*tree_oid
= NULL
;
116 struct object_id initialized_oid
;
117 int cnt
, i
, doubledash
, filei
;
119 if (!is_bare_repository())
122 git_config(git_default_config
, NULL
);
124 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
125 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
127 if (repo_read_index(the_repository
) < 0) {
128 die("invalid cache");
132 git_attr_set_direction(GIT_ATTR_INDEX
);
135 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
136 if (!strcmp(argv
[i
], "--"))
140 /* Process --all and/or attribute arguments: */
143 error_with_usage("Attributes and --all both specified");
146 filei
= doubledash
+ 1;
147 } else if (doubledash
== 0) {
148 error_with_usage("No attribute specified");
149 } else if (doubledash
< 0) {
151 error_with_usage("No attribute specified");
154 /* Treat all arguments as attribute names. */
158 /* Treat exactly one argument as an attribute name. */
164 filei
= doubledash
+ 1;
167 /* Check file argument(s): */
170 error_with_usage("Can't specify files with --stdin");
173 error_with_usage("No file specified");
176 check
= attr_check_alloc();
178 for (i
= 0; i
< cnt
; i
++) {
179 const struct git_attr
*a
= git_attr(argv
[i
]);
182 return error("%s: not a valid attribute name",
184 attr_check_append(check
, a
);
189 if (repo_get_oid_tree(the_repository
, source
, &initialized_oid
))
190 die("%s: not a valid tree-ish source", source
);
191 tree_oid
= &initialized_oid
;
195 check_attr_stdin_paths(prefix
, check
, tree_oid
, all_attrs
);
197 for (i
= filei
; i
< argc
; i
++)
198 check_attr(prefix
, check
, tree_oid
, all_attrs
, argv
[i
]);
199 maybe_flush_or_die(stdout
, "attribute to stdout");
202 attr_check_free(check
);