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