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