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
,
72 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
75 git_all_attrs(&the_index
, full_path
, check
);
77 git_check_attr(&the_index
, full_path
, check
);
79 output_attr(check
, file
);
84 static void check_attr_stdin_paths(const char *prefix
, struct attr_check
*check
,
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
, 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 initialized_oid
;
116 int cnt
, i
, doubledash
, filei
;
118 if (!is_bare_repository())
121 git_config(git_default_config
, NULL
);
123 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
124 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
126 if (repo_read_index(the_repository
) < 0) {
127 die("invalid cache");
131 git_attr_set_direction(GIT_ATTR_INDEX
);
134 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
135 if (!strcmp(argv
[i
], "--"))
139 /* Process --all and/or attribute arguments: */
142 error_with_usage("Attributes and --all both specified");
145 filei
= doubledash
+ 1;
146 } else if (doubledash
== 0) {
147 error_with_usage("No attribute specified");
148 } else if (doubledash
< 0) {
150 error_with_usage("No attribute specified");
153 /* Treat all arguments as attribute names. */
157 /* Treat exactly one argument as an attribute name. */
163 filei
= doubledash
+ 1;
166 /* Check file argument(s): */
169 error_with_usage("Can't specify files with --stdin");
172 error_with_usage("No file specified");
175 check
= attr_check_alloc();
177 for (i
= 0; i
< cnt
; i
++) {
178 const struct git_attr
*a
= git_attr(argv
[i
]);
181 return error("%s: not a valid attribute name",
183 attr_check_append(check
, a
);
188 if (repo_get_oid_tree(the_repository
, source
, &initialized_oid
))
189 die("%s: not a valid tree-ish source", source
);
190 set_git_attr_source(source
);
194 check_attr_stdin_paths(prefix
, check
, all_attrs
);
196 for (i
= filei
; i
< argc
; i
++)
197 check_attr(prefix
, check
, all_attrs
, argv
[i
]);
198 maybe_flush_or_die(stdout
, "attribute to stdout");
201 attr_check_free(check
);