Merge branch 'jc/maint-name-rev-all' into maint-1.7.6
[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 stdin_paths;
8 static const char * const check_attr_usage[] = {
9 "git check-attr attr... [--] pathname...",
10 "git check-attr --stdin attr... < <list-of-paths>",
11 NULL
12 };
13
14 static int null_term_line;
15
16 static const struct option check_attr_options[] = {
17         OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
18         OPT_BOOLEAN('z', NULL, &null_term_line,
19                 "input paths are terminated by a null character"),
20         OPT_END()
21 };
22
23 static void check_attr(int cnt, struct git_attr_check *check,
24         const char** name, const char *file)
25 {
26         int j;
27         if (git_checkattr(file, cnt, check))
28                 die("git_checkattr died");
29         for (j = 0; j < cnt; j++) {
30                 const char *value = check[j].value;
31
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";
38
39                 quote_c_style(file, NULL, stdout, 0);
40                 printf(": %s: %s\n", name[j], value);
41         }
42 }
43
44 static void check_attr_stdin_paths(int cnt, struct git_attr_check *check,
45         const char** name)
46 {
47         struct strbuf buf, nbuf;
48         int line_termination = null_term_line ? 0 : '\n';
49
50         strbuf_init(&buf, 0);
51         strbuf_init(&nbuf, 0);
52         while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
53                 if (line_termination && buf.buf[0] == '"') {
54                         strbuf_reset(&nbuf);
55                         if (unquote_c_style(&nbuf, buf.buf, NULL))
56                                 die("line is badly quoted");
57                         strbuf_swap(&buf, &nbuf);
58                 }
59                 check_attr(cnt, check, name, buf.buf);
60                 maybe_flush_or_die(stdout, "attribute to stdout");
61         }
62         strbuf_release(&buf);
63         strbuf_release(&nbuf);
64 }
65
66 int cmd_check_attr(int argc, const char **argv, const char *prefix)
67 {
68         struct git_attr_check *check;
69         int cnt, i, doubledash;
70         const char *errstr = NULL;
71
72         argc = parse_options(argc, argv, prefix, check_attr_options,
73                              check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
74         if (!argc)
75                 usage_with_options(check_attr_usage, check_attr_options);
76
77         if (read_cache() < 0) {
78                 die("invalid cache");
79         }
80
81         doubledash = -1;
82         for (i = 0; doubledash < 0 && i < argc; i++) {
83                 if (!strcmp(argv[i], "--"))
84                         doubledash = i;
85         }
86
87         /* If there is no double dash, we handle only one attribute */
88         if (doubledash < 0) {
89                 cnt = 1;
90                 doubledash = 0;
91         } else
92                 cnt = doubledash;
93         doubledash++;
94
95         if (cnt <= 0)
96                 errstr = "No attribute specified";
97         else if (stdin_paths && doubledash < argc)
98                 errstr = "Can't specify files with --stdin";
99         if (errstr) {
100                 error("%s", errstr);
101                 usage_with_options(check_attr_usage, check_attr_options);
102         }
103
104         check = xcalloc(cnt, sizeof(*check));
105         for (i = 0; i < cnt; i++) {
106                 const char *name;
107                 struct git_attr *a;
108                 name = argv[i];
109                 a = git_attr(name);
110                 if (!a)
111                         return error("%s: not a valid attribute name", name);
112                 check[i].attr = a;
113         }
114
115         if (stdin_paths)
116                 check_attr_stdin_paths(cnt, check, argv);
117         else {
118                 for (i = doubledash; i < argc; i++)
119                         check_attr(cnt, check, argv, argv[i]);
120                 maybe_flush_or_die(stdout, "attribute to stdout");
121         }
122         return 0;
123 }