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