]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/check-attr.c
treewide: be explicit about dependence on gettext.h
[thirdparty/git.git] / builtin / check-attr.c
CommitLineData
07047d68 1#define USE_THE_INDEX_VARIABLE
d0bfd026 2#include "builtin.h"
6b06d518 3#include "cache.h"
b2141fc1 4#include "config.h"
d0bfd026 5#include "attr.h"
f394e093 6#include "gettext.h"
d0bfd026 7#include "quote.h"
b4666852 8#include "parse-options.h"
d0bfd026 9
4ca0f188 10static int all_attrs;
b2b3e9c2 11static int cached_attrs;
b4666852 12static int stdin_paths;
47cfc9bd 13static char *source;
b4666852 14static const char * const check_attr_usage[] = {
47cfc9bd
KN
15N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
16N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
b4666852
DP
17NULL
18};
19
94a55e4e 20static int nul_term_line;
b4666852
DP
21
22static const struct option check_attr_options[] = {
d5d09d47
SB
23 OPT_BOOL('a', "all", &all_attrs, N_("report all attributes set on file")),
24 OPT_BOOL(0, "cached", &cached_attrs, N_("use .gitattributes only from the index")),
25 OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
a86a8b97
JH
26 OPT_BOOL('z', NULL, &nul_term_line,
27 N_("terminate input and output records by a NUL character")),
47cfc9bd 28 OPT_STRING(0, "source", &source, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
b4666852
DP
29 OPT_END()
30};
d0bfd026 31
7f864111 32static void output_attr(struct attr_check *check, const char *file)
41038c5e
DP
33{
34 int j;
7f864111
JH
35 int cnt = check->nr;
36
41038c5e 37 for (j = 0; j < cnt; j++) {
7f864111 38 const char *value = check->items[j].value;
41038c5e
DP
39
40 if (ATTR_TRUE(value))
41 value = "set";
42 else if (ATTR_FALSE(value))
43 value = "unset";
44 else if (ATTR_UNSET(value))
45 value = "unspecified";
46
f7cd8c50
JH
47 if (nul_term_line) {
48 printf("%s%c" /* path */
49 "%s%c" /* attrname */
50 "%s%c" /* attrvalue */,
7f864111
JH
51 file, 0,
52 git_attr_name(check->items[j].attr), 0, value, 0);
f7cd8c50
JH
53 } else {
54 quote_c_style(file, NULL, stdout, 0);
7f864111
JH
55 printf(": %s: %s\n",
56 git_attr_name(check->items[j].attr), value);
f7cd8c50 57 }
41038c5e
DP
58 }
59}
60
47cfc9bd
KN
61static void check_attr(const char *prefix, struct attr_check *check,
62 const struct object_id *tree_oid, int collect_all,
7bd18054 63 const char *file)
47cfc9bd 64
46f96a6d 65{
f5114a40
MH
66 char *full_path =
67 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
7f864111
JH
68
69 if (collect_all) {
47cfc9bd 70 git_all_attrs(&the_index, tree_oid, full_path, check);
4ca0f188 71 } else {
47cfc9bd 72 git_check_attr(&the_index, tree_oid, full_path, check);
4ca0f188 73 }
7f864111
JH
74 output_attr(check, file);
75
f5114a40 76 free(full_path);
46f96a6d
MH
77}
78
47cfc9bd
KN
79static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
80 const struct object_id *tree_oid, int collect_all)
b4666852 81{
0d4cc1b4
JK
82 struct strbuf buf = STRBUF_INIT;
83 struct strbuf unquoted = STRBUF_INIT;
f418afa9 84 strbuf_getline_fn getline_fn;
b4666852 85
f418afa9 86 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
f418afa9
JH
87 while (getline_fn(&buf, stdin) != EOF) {
88 if (!nul_term_line && buf.buf[0] == '"') {
0d4cc1b4
JK
89 strbuf_reset(&unquoted);
90 if (unquote_c_style(&unquoted, buf.buf, NULL))
b4666852 91 die("line is badly quoted");
0d4cc1b4 92 strbuf_swap(&buf, &unquoted);
b4666852 93 }
47cfc9bd 94 check_attr(prefix, check, tree_oid, collect_all, buf.buf);
b4666852
DP
95 maybe_flush_or_die(stdout, "attribute to stdout");
96 }
97 strbuf_release(&buf);
0d4cc1b4 98 strbuf_release(&unquoted);
b4666852
DP
99}
100
9e37a7e1
MH
101static NORETURN void error_with_usage(const char *msg)
102{
103 error("%s", msg);
104 usage_with_options(check_attr_usage, check_attr_options);
105}
106
d0bfd026
JH
107int cmd_check_attr(int argc, const char **argv, const char *prefix)
108{
7f864111 109 struct attr_check *check;
47cfc9bd
KN
110 struct object_id *tree_oid = NULL;
111 struct object_id initialized_oid;
d6541bb1 112 int cnt, i, doubledash, filei;
b4666852 113
cdbf6232
JH
114 if (!is_bare_repository())
115 setup_work_tree();
116
64589a03
JH
117 git_config(git_default_config, NULL);
118
37782920
SB
119 argc = parse_options(argc, argv, prefix, check_attr_options,
120 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
d0bfd026 121
07047d68 122 if (repo_read_index(the_repository) < 0) {
6b06d518
BD
123 die("invalid cache");
124 }
125
b2b3e9c2 126 if (cached_attrs)
c4500e25 127 git_attr_set_direction(GIT_ATTR_INDEX);
b2b3e9c2 128
d0bfd026 129 doubledash = -1;
b4666852 130 for (i = 0; doubledash < 0 && i < argc; i++) {
d0bfd026
JH
131 if (!strcmp(argv[i], "--"))
132 doubledash = i;
133 }
134
4ca0f188
MH
135 /* Process --all and/or attribute arguments: */
136 if (all_attrs) {
137 if (doubledash >= 1)
138 error_with_usage("Attributes and --all both specified");
139
140 cnt = 0;
141 filei = doubledash + 1;
142 } else if (doubledash == 0) {
72541040
MH
143 error_with_usage("No attribute specified");
144 } else if (doubledash < 0) {
72541040
MH
145 if (!argc)
146 error_with_usage("No attribute specified");
147
ca64d061
MH
148 if (stdin_paths) {
149 /* Treat all arguments as attribute names. */
150 cnt = argc;
151 filei = argc;
152 } else {
153 /* Treat exactly one argument as an attribute name. */
154 cnt = 1;
155 filei = 1;
156 }
d6541bb1 157 } else {
b4666852 158 cnt = doubledash;
d6541bb1
MH
159 filei = doubledash + 1;
160 }
d0bfd026 161
72541040 162 /* Check file argument(s): */
fdf6be82
MH
163 if (stdin_paths) {
164 if (filei < argc)
165 error_with_usage("Can't specify files with --stdin");
166 } else {
167 if (filei >= argc)
168 error_with_usage("No file specified");
169 }
b4666852 170
7f864111
JH
171 check = attr_check_alloc();
172 if (!all_attrs) {
4ca0f188 173 for (i = 0; i < cnt; i++) {
e810e063
BW
174 const struct git_attr *a = git_attr(argv[i]);
175
4ca0f188
MH
176 if (!a)
177 return error("%s: not a valid attribute name",
7f864111
JH
178 argv[i]);
179 attr_check_append(check, a);
4ca0f188 180 }
d0bfd026
JH
181 }
182
47cfc9bd
KN
183 if (source) {
184 if (repo_get_oid_tree(the_repository, source, &initialized_oid))
185 die("%s: not a valid tree-ish source", source);
186 tree_oid = &initialized_oid;
187 }
188
b4666852 189 if (stdin_paths)
47cfc9bd 190 check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
b4666852 191 else {
d6541bb1 192 for (i = filei; i < argc; i++)
47cfc9bd 193 check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
b4666852
DP
194 maybe_flush_or_die(stdout, "attribute to stdout");
195 }
7f864111
JH
196
197 attr_check_free(check);
d0bfd026
JH
198 return 0;
199}