git-check-attr: Add an --all option to show all attributes
[git/jnareb-git.git] / builtin / check-attr.c
blobb0d2ebc3dcff91024eed22255d6b707ad8676a79
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 stdin_paths;
9 static const char * const check_attr_usage[] = {
10 "git check-attr [-a | --all | attr...] [--] pathname...",
11 "git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
12 NULL
15 static int null_term_line;
17 static const struct option check_attr_options[] = {
18 OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
19 OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
20 OPT_BOOLEAN('z', NULL, &null_term_line,
21 "input paths are terminated by a null character"),
22 OPT_END()
25 static void output_attr(int cnt, struct git_attr_check *check,
26 const char *file)
28 int j;
29 for (j = 0; j < cnt; j++) {
30 const char *value = check[j].value;
32 if (ATTR_TRUE(value))
33 value = "set";
34 else if (ATTR_FALSE(value))
35 value = "unset";
36 else if (ATTR_UNSET(value))
37 value = "unspecified";
39 quote_c_style(file, NULL, stdout, 0);
40 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
44 static void check_attr(int cnt, struct git_attr_check *check,
45 const char *file)
47 if (check != NULL) {
48 if (git_checkattr(file, cnt, check))
49 die("git_checkattr died");
50 output_attr(cnt, check, file);
51 } else {
52 if (git_all_attrs(file, &cnt, &check))
53 die("git_all_attrs died");
54 output_attr(cnt, check, file);
55 free(check);
59 static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
61 struct strbuf buf, nbuf;
62 int line_termination = null_term_line ? 0 : '\n';
64 strbuf_init(&buf, 0);
65 strbuf_init(&nbuf, 0);
66 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
67 if (line_termination && buf.buf[0] == '"') {
68 strbuf_reset(&nbuf);
69 if (unquote_c_style(&nbuf, buf.buf, NULL))
70 die("line is badly quoted");
71 strbuf_swap(&buf, &nbuf);
73 check_attr(cnt, check, buf.buf);
74 maybe_flush_or_die(stdout, "attribute to stdout");
76 strbuf_release(&buf);
77 strbuf_release(&nbuf);
80 static NORETURN void error_with_usage(const char *msg)
82 error("%s", msg);
83 usage_with_options(check_attr_usage, check_attr_options);
86 int cmd_check_attr(int argc, const char **argv, const char *prefix)
88 struct git_attr_check *check;
89 int cnt, i, doubledash, filei;
91 argc = parse_options(argc, argv, prefix, check_attr_options,
92 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
94 if (read_cache() < 0) {
95 die("invalid cache");
98 doubledash = -1;
99 for (i = 0; doubledash < 0 && i < argc; i++) {
100 if (!strcmp(argv[i], "--"))
101 doubledash = i;
104 /* Process --all and/or attribute arguments: */
105 if (all_attrs) {
106 if (doubledash >= 1)
107 error_with_usage("Attributes and --all both specified");
109 cnt = 0;
110 filei = doubledash + 1;
111 } else if (doubledash == 0) {
112 error_with_usage("No attribute specified");
113 } else if (doubledash < 0) {
115 * There is no double dash; treat the first
116 * argument as an attribute.
118 if (!argc)
119 error_with_usage("No attribute specified");
121 cnt = 1;
122 filei = 1;
123 } else {
124 cnt = doubledash;
125 filei = doubledash + 1;
128 /* Check file argument(s): */
129 if (stdin_paths) {
130 if (filei < argc)
131 error_with_usage("Can't specify files with --stdin");
132 } else {
133 if (filei >= argc)
134 error_with_usage("No file specified");
137 if (all_attrs) {
138 check = NULL;
139 } else {
140 check = xcalloc(cnt, sizeof(*check));
141 for (i = 0; i < cnt; i++) {
142 const char *name;
143 struct git_attr *a;
144 name = argv[i];
145 a = git_attr(name);
146 if (!a)
147 return error("%s: not a valid attribute name",
148 name);
149 check[i].attr = a;
153 if (stdin_paths)
154 check_attr_stdin_paths(cnt, check);
155 else {
156 for (i = filei; i < argc; i++)
157 check_attr(cnt, check, argv[i]);
158 maybe_flush_or_die(stdout, "attribute to stdout");
160 return 0;