Merge branch 'nd/document-err-packet'
[git] / builtin / check-attr.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "attr.h"
4 #include "quote.h"
5 #include "parse-options.h"
6
7 static int all_attrs;
8 static int cached_attrs;
9 static int stdin_paths;
10 static const char * const check_attr_usage[] = {
11 "git check-attr [-a | --all | attr...] [--] pathname...",
12 "git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
13 NULL
14 };
15
16 static int null_term_line;
17
18 static const struct option check_attr_options[] = {
19         OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
20         OPT_BOOLEAN(0,  "cached", &cached_attrs, "use .gitattributes only from the index"),
21         OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
22         OPT_BOOLEAN('z', NULL, &null_term_line,
23                 "input paths are terminated by a null character"),
24         OPT_END()
25 };
26
27 static void output_attr(int cnt, struct git_attr_check *check,
28         const char *file)
29 {
30         int j;
31         for (j = 0; j < cnt; j++) {
32                 const char *value = check[j].value;
33
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";
40
41                 quote_c_style(file, NULL, stdout, 0);
42                 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
43         }
44 }
45
46 static void check_attr(const char *prefix, int cnt,
47         struct git_attr_check *check, const char *file)
48 {
49         char *full_path =
50                 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
51         if (check != NULL) {
52                 if (git_check_attr(full_path, cnt, check))
53                         die("git_check_attr died");
54                 output_attr(cnt, check, file);
55         } else {
56                 if (git_all_attrs(full_path, &cnt, &check))
57                         die("git_all_attrs died");
58                 output_attr(cnt, check, file);
59                 free(check);
60         }
61         free(full_path);
62 }
63
64 static void check_attr_stdin_paths(const char *prefix, int cnt,
65         struct git_attr_check *check)
66 {
67         struct strbuf buf, nbuf;
68         int line_termination = null_term_line ? 0 : '\n';
69
70         strbuf_init(&buf, 0);
71         strbuf_init(&nbuf, 0);
72         while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
73                 if (line_termination && buf.buf[0] == '"') {
74                         strbuf_reset(&nbuf);
75                         if (unquote_c_style(&nbuf, buf.buf, NULL))
76                                 die("line is badly quoted");
77                         strbuf_swap(&buf, &nbuf);
78                 }
79                 check_attr(prefix, cnt, check, buf.buf);
80                 maybe_flush_or_die(stdout, "attribute to stdout");
81         }
82         strbuf_release(&buf);
83         strbuf_release(&nbuf);
84 }
85
86 static NORETURN void error_with_usage(const char *msg)
87 {
88         error("%s", msg);
89         usage_with_options(check_attr_usage, check_attr_options);
90 }
91
92 int cmd_check_attr(int argc, const char **argv, const char *prefix)
93 {
94         struct git_attr_check *check;
95         int cnt, i, doubledash, filei;
96
97         argc = parse_options(argc, argv, prefix, check_attr_options,
98                              check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
99
100         if (read_cache() < 0) {
101                 die("invalid cache");
102         }
103
104         if (cached_attrs)
105                 git_attr_set_direction(GIT_ATTR_INDEX, NULL);
106
107         doubledash = -1;
108         for (i = 0; doubledash < 0 && i < argc; i++) {
109                 if (!strcmp(argv[i], "--"))
110                         doubledash = i;
111         }
112
113         /* Process --all and/or attribute arguments: */
114         if (all_attrs) {
115                 if (doubledash >= 1)
116                         error_with_usage("Attributes and --all both specified");
117
118                 cnt = 0;
119                 filei = doubledash + 1;
120         } else if (doubledash == 0) {
121                 error_with_usage("No attribute specified");
122         } else if (doubledash < 0) {
123                 if (!argc)
124                         error_with_usage("No attribute specified");
125
126                 if (stdin_paths) {
127                         /* Treat all arguments as attribute names. */
128                         cnt = argc;
129                         filei = argc;
130                 } else {
131                         /* Treat exactly one argument as an attribute name. */
132                         cnt = 1;
133                         filei = 1;
134                 }
135         } else {
136                 cnt = doubledash;
137                 filei = doubledash + 1;
138         }
139
140         /* Check file argument(s): */
141         if (stdin_paths) {
142                 if (filei < argc)
143                         error_with_usage("Can't specify files with --stdin");
144         } else {
145                 if (filei >= argc)
146                         error_with_usage("No file specified");
147         }
148
149         if (all_attrs) {
150                 check = NULL;
151         } else {
152                 check = xcalloc(cnt, sizeof(*check));
153                 for (i = 0; i < cnt; i++) {
154                         const char *name;
155                         struct git_attr *a;
156                         name = argv[i];
157                         a = git_attr(name);
158                         if (!a)
159                                 return error("%s: not a valid attribute name",
160                                         name);
161                         check[i].attr = a;
162                 }
163         }
164
165         if (stdin_paths)
166                 check_attr_stdin_paths(prefix, cnt, check);
167         else {
168                 for (i = filei; i < argc; i++)
169                         check_attr(prefix, cnt, check, argv[i]);
170                 maybe_flush_or_die(stdout, "attribute to stdout");
171         }
172         return 0;
173 }