Merge branch 'jk/commit-use-no-divider-with-interpret-trailers'
[alt-git.git] / builtin / check-attr.c
blobe27b86d150588b15b1884d456aba2ac17bd3e2be
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 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, full_path, check);
76 } else {
77 git_check_attr(&the_index, 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 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, 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 initialized_oid;
116 int cnt, i, doubledash, filei;
118 if (!is_bare_repository())
119 setup_work_tree();
121 git_config(git_default_config, NULL);
123 argc = parse_options(argc, argv, prefix, check_attr_options,
124 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
126 if (repo_read_index(the_repository) < 0) {
127 die("invalid cache");
130 if (cached_attrs)
131 git_attr_set_direction(GIT_ATTR_INDEX);
133 doubledash = -1;
134 for (i = 0; doubledash < 0 && i < argc; i++) {
135 if (!strcmp(argv[i], "--"))
136 doubledash = i;
139 /* Process --all and/or attribute arguments: */
140 if (all_attrs) {
141 if (doubledash >= 1)
142 error_with_usage("Attributes and --all both specified");
144 cnt = 0;
145 filei = doubledash + 1;
146 } else if (doubledash == 0) {
147 error_with_usage("No attribute specified");
148 } else if (doubledash < 0) {
149 if (!argc)
150 error_with_usage("No attribute specified");
152 if (stdin_paths) {
153 /* Treat all arguments as attribute names. */
154 cnt = argc;
155 filei = argc;
156 } else {
157 /* Treat exactly one argument as an attribute name. */
158 cnt = 1;
159 filei = 1;
161 } else {
162 cnt = doubledash;
163 filei = doubledash + 1;
166 /* Check file argument(s): */
167 if (stdin_paths) {
168 if (filei < argc)
169 error_with_usage("Can't specify files with --stdin");
170 } else {
171 if (filei >= argc)
172 error_with_usage("No file specified");
175 check = attr_check_alloc();
176 if (!all_attrs) {
177 for (i = 0; i < cnt; i++) {
178 const struct git_attr *a = git_attr(argv[i]);
180 if (!a)
181 return error("%s: not a valid attribute name",
182 argv[i]);
183 attr_check_append(check, a);
187 if (source) {
188 if (repo_get_oid_tree(the_repository, source, &initialized_oid))
189 die("%s: not a valid tree-ish source", source);
190 set_git_attr_source(source);
193 if (stdin_paths)
194 check_attr_stdin_paths(prefix, check, all_attrs);
195 else {
196 for (i = filei; i < argc; i++)
197 check_attr(prefix, check, all_attrs, argv[i]);
198 maybe_flush_or_die(stdout, "attribute to stdout");
201 attr_check_free(check);
202 return 0;