hash-ll.h: split out of hash.h to remove dependency on repository.h
[alt-git.git] / builtin / check-attr.c
blobb2b678847f5b9dc0994e87883540fba6041447ee
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 "object-name.h"
9 #include "quote.h"
10 #include "repository.h"
11 #include "setup.h"
12 #include "parse-options.h"
13 #include "write-or-die.h"
15 static int all_attrs;
16 static int cached_attrs;
17 static int stdin_paths;
18 static char *source;
19 static const char * const check_attr_usage[] = {
20 N_("git check-attr [--source <tree-ish>] [-a | --all | <attr>...] [--] <pathname>..."),
21 N_("git check-attr --stdin [-z] [--source <tree-ish>] [-a | --all | <attr>...]"),
22 NULL
25 static int nul_term_line;
27 static const struct option check_attr_options[] = {
28 OPT_BOOL('a', "all", &all_attrs, N_("report all attributes set on file")),
29 OPT_BOOL(0, "cached", &cached_attrs, N_("use .gitattributes only from the index")),
30 OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
31 OPT_BOOL('z', NULL, &nul_term_line,
32 N_("terminate input and output records by a NUL character")),
33 OPT_STRING(0, "source", &source, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
34 OPT_END()
37 static void output_attr(struct attr_check *check, const char *file)
39 int j;
40 int cnt = check->nr;
42 for (j = 0; j < cnt; j++) {
43 const char *value = check->items[j].value;
45 if (ATTR_TRUE(value))
46 value = "set";
47 else if (ATTR_FALSE(value))
48 value = "unset";
49 else if (ATTR_UNSET(value))
50 value = "unspecified";
52 if (nul_term_line) {
53 printf("%s%c" /* path */
54 "%s%c" /* attrname */
55 "%s%c" /* attrvalue */,
56 file, 0,
57 git_attr_name(check->items[j].attr), 0, value, 0);
58 } else {
59 quote_c_style(file, NULL, stdout, 0);
60 printf(": %s: %s\n",
61 git_attr_name(check->items[j].attr), value);
66 static void check_attr(const char *prefix, struct attr_check *check,
67 const struct object_id *tree_oid, int collect_all,
68 const char *file)
71 char *full_path =
72 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
74 if (collect_all) {
75 git_all_attrs(&the_index, tree_oid, full_path, check);
76 } else {
77 git_check_attr(&the_index, tree_oid, full_path, check);
79 output_attr(check, file);
81 free(full_path);
84 static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
85 const struct object_id *tree_oid, int collect_all)
87 struct strbuf buf = STRBUF_INIT;
88 struct strbuf unquoted = STRBUF_INIT;
89 strbuf_getline_fn getline_fn;
91 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
92 while (getline_fn(&buf, stdin) != EOF) {
93 if (!nul_term_line && buf.buf[0] == '"') {
94 strbuf_reset(&unquoted);
95 if (unquote_c_style(&unquoted, buf.buf, NULL))
96 die("line is badly quoted");
97 strbuf_swap(&buf, &unquoted);
99 check_attr(prefix, check, tree_oid, collect_all, buf.buf);
100 maybe_flush_or_die(stdout, "attribute to stdout");
102 strbuf_release(&buf);
103 strbuf_release(&unquoted);
106 static NORETURN void error_with_usage(const char *msg)
108 error("%s", msg);
109 usage_with_options(check_attr_usage, check_attr_options);
112 int cmd_check_attr(int argc, const char **argv, const char *prefix)
114 struct attr_check *check;
115 struct object_id *tree_oid = NULL;
116 struct object_id initialized_oid;
117 int cnt, i, doubledash, filei;
119 if (!is_bare_repository())
120 setup_work_tree();
122 git_config(git_default_config, NULL);
124 argc = parse_options(argc, argv, prefix, check_attr_options,
125 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
127 if (repo_read_index(the_repository) < 0) {
128 die("invalid cache");
131 if (cached_attrs)
132 git_attr_set_direction(GIT_ATTR_INDEX);
134 doubledash = -1;
135 for (i = 0; doubledash < 0 && i < argc; i++) {
136 if (!strcmp(argv[i], "--"))
137 doubledash = i;
140 /* Process --all and/or attribute arguments: */
141 if (all_attrs) {
142 if (doubledash >= 1)
143 error_with_usage("Attributes and --all both specified");
145 cnt = 0;
146 filei = doubledash + 1;
147 } else if (doubledash == 0) {
148 error_with_usage("No attribute specified");
149 } else if (doubledash < 0) {
150 if (!argc)
151 error_with_usage("No attribute specified");
153 if (stdin_paths) {
154 /* Treat all arguments as attribute names. */
155 cnt = argc;
156 filei = argc;
157 } else {
158 /* Treat exactly one argument as an attribute name. */
159 cnt = 1;
160 filei = 1;
162 } else {
163 cnt = doubledash;
164 filei = doubledash + 1;
167 /* Check file argument(s): */
168 if (stdin_paths) {
169 if (filei < argc)
170 error_with_usage("Can't specify files with --stdin");
171 } else {
172 if (filei >= argc)
173 error_with_usage("No file specified");
176 check = attr_check_alloc();
177 if (!all_attrs) {
178 for (i = 0; i < cnt; i++) {
179 const struct git_attr *a = git_attr(argv[i]);
181 if (!a)
182 return error("%s: not a valid attribute name",
183 argv[i]);
184 attr_check_append(check, a);
188 if (source) {
189 if (repo_get_oid_tree(the_repository, source, &initialized_oid))
190 die("%s: not a valid tree-ish source", source);
191 tree_oid = &initialized_oid;
194 if (stdin_paths)
195 check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
196 else {
197 for (i = filei; i < argc; i++)
198 check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
199 maybe_flush_or_die(stdout, "attribute to stdout");
202 attr_check_free(check);
203 return 0;