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