1 #define USE_THE_INDEX_VARIABLE
5 #include "environment.h"
7 #include "object-name.h"
9 #include "repository.h"
11 #include "parse-options.h"
12 #include "write-or-die.h"
15 static int cached_attrs
;
16 static int stdin_paths
;
18 static const char * const check_attr_usage
[] = {
19 N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
20 N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
24 static int nul_term_line
;
26 static const struct option check_attr_options
[] = {
27 OPT_BOOL('a', "all", &all_attrs
, N_("report all attributes set on file")),
28 OPT_BOOL(0, "cached", &cached_attrs
, N_("use .gitattributes only from the index")),
29 OPT_BOOL(0 , "stdin", &stdin_paths
, N_("read file names from stdin")),
30 OPT_BOOL('z', NULL
, &nul_term_line
,
31 N_("terminate input and output records by a NUL character")),
32 OPT_STRING(0, "source", &source
, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
36 static void output_attr(struct attr_check
*check
, const char *file
)
41 for (j
= 0; j
< cnt
; j
++) {
42 const char *value
= check
->items
[j
].value
;
46 else if (ATTR_FALSE(value
))
48 else if (ATTR_UNSET(value
))
49 value
= "unspecified";
52 printf("%s%c" /* path */
54 "%s%c" /* attrvalue */,
56 git_attr_name(check
->items
[j
].attr
), 0, value
, 0);
58 quote_c_style(file
, NULL
, stdout
, 0);
60 git_attr_name(check
->items
[j
].attr
), value
);
65 static void check_attr(const char *prefix
, struct attr_check
*check
,
71 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
74 git_all_attrs(&the_index
, full_path
, check
);
76 git_check_attr(&the_index
, full_path
, check
);
78 output_attr(check
, file
);
83 static void check_attr_stdin_paths(const char *prefix
, struct attr_check
*check
,
86 struct strbuf buf
= STRBUF_INIT
;
87 struct strbuf unquoted
= STRBUF_INIT
;
88 strbuf_getline_fn getline_fn
;
90 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
91 while (getline_fn(&buf
, stdin
) != EOF
) {
92 if (!nul_term_line
&& buf
.buf
[0] == '"') {
93 strbuf_reset(&unquoted
);
94 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
95 die("line is badly quoted");
96 strbuf_swap(&buf
, &unquoted
);
98 check_attr(prefix
, check
, collect_all
, buf
.buf
);
99 maybe_flush_or_die(stdout
, "attribute to stdout");
101 strbuf_release(&buf
);
102 strbuf_release(&unquoted
);
105 static NORETURN
void error_with_usage(const char *msg
)
108 usage_with_options(check_attr_usage
, check_attr_options
);
111 int cmd_check_attr(int argc
, const char **argv
, const char *prefix
)
113 struct attr_check
*check
;
114 struct object_id initialized_oid
;
115 int cnt
, i
, doubledash
, filei
;
117 if (!is_bare_repository())
120 git_config(git_default_config
, NULL
);
122 argc
= parse_options(argc
, argv
, prefix
, check_attr_options
,
123 check_attr_usage
, PARSE_OPT_KEEP_DASHDASH
);
125 prepare_repo_settings(the_repository
);
126 the_repository
->settings
.command_requires_full_index
= 0;
128 if (repo_read_index(the_repository
) < 0) {
129 die("invalid cache");
133 git_attr_set_direction(GIT_ATTR_INDEX
);
136 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
137 if (!strcmp(argv
[i
], "--"))
141 /* Process --all and/or attribute arguments: */
144 error_with_usage("Attributes and --all both specified");
147 filei
= doubledash
+ 1;
148 } else if (doubledash
== 0) {
149 error_with_usage("No attribute specified");
150 } else if (doubledash
< 0) {
152 error_with_usage("No attribute specified");
155 /* Treat all arguments as attribute names. */
159 /* Treat exactly one argument as an attribute name. */
165 filei
= doubledash
+ 1;
168 /* Check file argument(s): */
171 error_with_usage("Can't specify files with --stdin");
174 error_with_usage("No file specified");
177 check
= attr_check_alloc();
179 for (i
= 0; i
< cnt
; i
++) {
180 const struct git_attr
*a
= git_attr(argv
[i
]);
183 return error("%s: not a valid attribute name",
185 attr_check_append(check
, a
);
190 if (repo_get_oid_tree(the_repository
, source
, &initialized_oid
))
191 die("%s: not a valid tree-ish source", source
);
192 set_git_attr_source(source
);
196 check_attr_stdin_paths(prefix
, check
, all_attrs
);
198 for (i
= filei
; i
< argc
; i
++)
199 check_attr(prefix
, check
, all_attrs
, argv
[i
]);
200 maybe_flush_or_die(stdout
, "attribute to stdout");
203 attr_check_free(check
);