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