4
#include "environment.h"
6
#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, const char **argv, const char *prefix)
112
struct attr_check *check;
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
prepare_repo_settings(the_repository);
125
the_repository->settings.command_requires_full_index = 0;
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
set_git_attr_source(source);
195
check_attr_stdin_paths(prefix, check, all_attrs);
197
for (i = filei; i < argc; i++)
198
check_attr(prefix, check, all_attrs, argv[i]);
199
maybe_flush_or_die(stdout, "attribute to stdout");
202
attr_check_free(check);