safe_create_leading_directories(): fix format of "if" chaining
[git.git] / builtin / check-attr.c
blobe9af7b2bfb932d75cdface880be9814885068a50
1 #include "builtin.h"
2 #include "cache.h"
3 #include "attr.h"
4 #include "quote.h"
5 #include "parse-options.h"
7 static int all_attrs;
8 static int cached_attrs;
9 static int stdin_paths;
10 static const char * const check_attr_usage[] = {
11 N_("git check-attr [-a | --all | attr...] [--] pathname..."),
12 N_("git check-attr --stdin [-z] [-a | --all | attr...] < <list-of-paths>"),
13 NULL
16 static int nul_term_line;
18 static const struct option check_attr_options[] = {
19 OPT_BOOL('a', "all", &all_attrs, N_("report all attributes set on file")),
20 OPT_BOOL(0, "cached", &cached_attrs, N_("use .gitattributes only from the index")),
21 OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
22 OPT_BOOL('z', NULL, &nul_term_line,
23 N_("terminate input and output records by a NUL character")),
24 OPT_END()
27 static void output_attr(int cnt, struct git_attr_check *check,
28 const char *file)
30 int j;
31 for (j = 0; j < cnt; j++) {
32 const char *value = check[j].value;
34 if (ATTR_TRUE(value))
35 value = "set";
36 else if (ATTR_FALSE(value))
37 value = "unset";
38 else if (ATTR_UNSET(value))
39 value = "unspecified";
41 if (nul_term_line) {
42 printf("%s%c" /* path */
43 "%s%c" /* attrname */
44 "%s%c" /* attrvalue */,
45 file, 0, git_attr_name(check[j].attr), 0, value, 0);
46 } else {
47 quote_c_style(file, NULL, stdout, 0);
48 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
54 static void check_attr(const char *prefix, int cnt,
55 struct git_attr_check *check, const char *file)
57 char *full_path =
58 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
59 if (check != NULL) {
60 if (git_check_attr(full_path, cnt, check))
61 die("git_check_attr died");
62 output_attr(cnt, check, file);
63 } else {
64 if (git_all_attrs(full_path, &cnt, &check))
65 die("git_all_attrs died");
66 output_attr(cnt, check, file);
67 free(check);
69 free(full_path);
72 static void check_attr_stdin_paths(const char *prefix, int cnt,
73 struct git_attr_check *check)
75 struct strbuf buf, nbuf;
76 int line_termination = nul_term_line ? 0 : '\n';
78 strbuf_init(&buf, 0);
79 strbuf_init(&nbuf, 0);
80 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
81 if (line_termination && buf.buf[0] == '"') {
82 strbuf_reset(&nbuf);
83 if (unquote_c_style(&nbuf, buf.buf, NULL))
84 die("line is badly quoted");
85 strbuf_swap(&buf, &nbuf);
87 check_attr(prefix, cnt, check, buf.buf);
88 maybe_flush_or_die(stdout, "attribute to stdout");
90 strbuf_release(&buf);
91 strbuf_release(&nbuf);
94 static NORETURN void error_with_usage(const char *msg)
96 error("%s", msg);
97 usage_with_options(check_attr_usage, check_attr_options);
100 int cmd_check_attr(int argc, const char **argv, const char *prefix)
102 struct git_attr_check *check;
103 int cnt, i, doubledash, filei;
105 git_config(git_default_config, NULL);
107 argc = parse_options(argc, argv, prefix, check_attr_options,
108 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
110 if (read_cache() < 0) {
111 die("invalid cache");
114 if (cached_attrs)
115 git_attr_set_direction(GIT_ATTR_INDEX, NULL);
117 doubledash = -1;
118 for (i = 0; doubledash < 0 && i < argc; i++) {
119 if (!strcmp(argv[i], "--"))
120 doubledash = i;
123 /* Process --all and/or attribute arguments: */
124 if (all_attrs) {
125 if (doubledash >= 1)
126 error_with_usage("Attributes and --all both specified");
128 cnt = 0;
129 filei = doubledash + 1;
130 } else if (doubledash == 0) {
131 error_with_usage("No attribute specified");
132 } else if (doubledash < 0) {
133 if (!argc)
134 error_with_usage("No attribute specified");
136 if (stdin_paths) {
137 /* Treat all arguments as attribute names. */
138 cnt = argc;
139 filei = argc;
140 } else {
141 /* Treat exactly one argument as an attribute name. */
142 cnt = 1;
143 filei = 1;
145 } else {
146 cnt = doubledash;
147 filei = doubledash + 1;
150 /* Check file argument(s): */
151 if (stdin_paths) {
152 if (filei < argc)
153 error_with_usage("Can't specify files with --stdin");
154 } else {
155 if (filei >= argc)
156 error_with_usage("No file specified");
159 if (all_attrs) {
160 check = NULL;
161 } else {
162 check = xcalloc(cnt, sizeof(*check));
163 for (i = 0; i < cnt; i++) {
164 const char *name;
165 struct git_attr *a;
166 name = argv[i];
167 a = git_attr(name);
168 if (!a)
169 return error("%s: not a valid attribute name",
170 name);
171 check[i].attr = a;
175 if (stdin_paths)
176 check_attr_stdin_paths(prefix, cnt, check);
177 else {
178 for (i = filei; i < argc; i++)
179 check_attr(prefix, cnt, check, argv[i]);
180 maybe_flush_or_die(stdout, "attribute to stdout");
182 return 0;