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