1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "environment.h"
7 #include "object-name.h"
10 #include "parse-options.h"
11 #include "write-or-die.h"
14 static int cached_attrs
;
15 static int stdin_paths
;
17 static const char * const check_attr_usage
[] = {
18 N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
19 N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
23 static int nul_term_line
;
25 static const struct option check_attr_options
[] = {
26 OPT_BOOL('a', "all", &all_attrs
, N_("report all attributes set on file")),
27 OPT_BOOL(0, "cached", &cached_attrs
, N_("use .gitattributes only from the index")),
28 OPT_BOOL(0 , "stdin", &stdin_paths
, N_("read file names from stdin")),
29 OPT_BOOL('z', NULL
, &nul_term_line
,
30 N_("terminate input and output records by a NUL character")),
31 OPT_STRING(0, "source", &source
, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
35 static void output_attr(struct attr_check
*check
, const char *file
)
40 for (j
= 0; j
< cnt
; j
++) {
41 const char *value
= check
->items
[j
].value
;
45 else if (ATTR_FALSE(value
))
47 else if (ATTR_UNSET(value
))
48 value
= "unspecified";
51 printf("%s%c" /* path */
53 "%s%c" /* attrvalue */,
55 git_attr_name(check
->items
[j
].attr
), 0, value
, 0);
57 quote_c_style(file
, NULL
, stdout
, 0);
59 git_attr_name(check
->items
[j
].attr
), value
);
64 static void check_attr(const char *prefix
, struct attr_check
*check
,
70 prefix_path(prefix
, prefix
? strlen(prefix
) : 0, file
);
73 git_all_attrs(the_repository
->index
, full_path
, check
);
75 git_check_attr(the_repository
->index
, full_path
, check
);
77 output_attr(check
, file
);
82 static void check_attr_stdin_paths(const char *prefix
, struct attr_check
*check
,
85 struct strbuf buf
= STRBUF_INIT
;
86 struct strbuf unquoted
= STRBUF_INIT
;
87 strbuf_getline_fn getline_fn
;
89 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
90 while (getline_fn(&buf
, stdin
) != EOF
) {
91 if (!nul_term_line
&& buf
.buf
[0] == '"') {
92 strbuf_reset(&unquoted
);
93 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
94 die("line is badly quoted");
95 strbuf_swap(&buf
, &unquoted
);
97 check_attr(prefix
, check
, collect_all
, buf
.buf
);
98 maybe_flush_or_die(stdout
, "attribute to stdout");
100 strbuf_release(&buf
);
101 strbuf_release(&unquoted
);
104 static NORETURN
void error_with_usage(const char *msg
)
107 usage_with_options(check_attr_usage
, check_attr_options
);
110 int cmd_check_attr(int argc
,
113 struct repository
*repo UNUSED
)
115 struct attr_check
*check
;
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 prepare_repo_settings(the_repository
);
128 the_repository
->settings
.command_requires_full_index
= 0;
130 if (repo_read_index(the_repository
) < 0) {
131 die("invalid cache");
135 git_attr_set_direction(GIT_ATTR_INDEX
);
138 for (i
= 0; doubledash
< 0 && i
< argc
; i
++) {
139 if (!strcmp(argv
[i
], "--"))
143 /* Process --all and/or attribute arguments: */
146 error_with_usage("Attributes and --all both specified");
149 filei
= doubledash
+ 1;
150 } else if (doubledash
== 0) {
151 error_with_usage("No attribute specified");
152 } else if (doubledash
< 0) {
154 error_with_usage("No attribute specified");
157 /* Treat all arguments as attribute names. */
161 /* Treat exactly one argument as an attribute name. */
167 filei
= doubledash
+ 1;
170 /* Check file argument(s): */
173 error_with_usage("Can't specify files with --stdin");
176 error_with_usage("No file specified");
179 check
= attr_check_alloc();
181 for (i
= 0; i
< cnt
; i
++) {
182 const struct git_attr
*a
= git_attr(argv
[i
]);
185 return error("%s: not a valid attribute name",
187 attr_check_append(check
, a
);
192 if (repo_get_oid_tree(the_repository
, source
, &initialized_oid
))
193 die("%s: not a valid tree-ish source", source
);
194 set_git_attr_source(source
);
198 check_attr_stdin_paths(prefix
, check
, all_attrs
);
200 for (i
= filei
; i
< argc
; i
++)
201 check_attr(prefix
, check
, all_attrs
, argv
[i
]);
202 maybe_flush_or_die(stdout
, "attribute to stdout");
205 attr_check_free(check
);