Code

git-check-attr: Use git_attr_name()
[git.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"
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 };
14 static int null_term_line;
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 };
23 static void check_attr(int cnt, struct git_attr_check *check,
24         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;
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);
41         }
42 }
44 static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
45 {
46         struct strbuf buf, nbuf;
47         int line_termination = null_term_line ? 0 : '\n';
49         strbuf_init(&buf, 0);
50         strbuf_init(&nbuf, 0);
51         while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
52                 if (line_termination && buf.buf[0] == '"') {
53                         strbuf_reset(&nbuf);
54                         if (unquote_c_style(&nbuf, buf.buf, NULL))
55                                 die("line is badly quoted");
56                         strbuf_swap(&buf, &nbuf);
57                 }
58                 check_attr(cnt, check, buf.buf);
59                 maybe_flush_or_die(stdout, "attribute to stdout");
60         }
61         strbuf_release(&buf);
62         strbuf_release(&nbuf);
63 }
65 int cmd_check_attr(int argc, const char **argv, const char *prefix)
66 {
67         struct git_attr_check *check;
68         int cnt, i, doubledash;
69         const char *errstr = NULL;
71         argc = parse_options(argc, argv, prefix, check_attr_options,
72                              check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
73         if (!argc)
74                 usage_with_options(check_attr_usage, check_attr_options);
76         if (read_cache() < 0) {
77                 die("invalid cache");
78         }
80         doubledash = -1;
81         for (i = 0; doubledash < 0 && i < argc; i++) {
82                 if (!strcmp(argv[i], "--"))
83                         doubledash = i;
84         }
86         /* If there is no double dash, we handle only one attribute */
87         if (doubledash < 0) {
88                 cnt = 1;
89                 doubledash = 0;
90         } else
91                 cnt = doubledash;
92         doubledash++;
94         if (cnt <= 0)
95                 errstr = "No attribute specified";
96         else if (stdin_paths && doubledash < argc)
97                 errstr = "Can't specify files with --stdin";
98         if (errstr) {
99                 error("%s", errstr);
100                 usage_with_options(check_attr_usage, check_attr_options);
101         }
103         check = xcalloc(cnt, sizeof(*check));
104         for (i = 0; i < cnt; i++) {
105                 const char *name;
106                 struct git_attr *a;
107                 name = argv[i];
108                 a = git_attr(name);
109                 if (!a)
110                         return error("%s: not a valid attribute name", name);
111                 check[i].attr = a;
112         }
114         if (stdin_paths)
115                 check_attr_stdin_paths(cnt, check);
116         else {
117                 for (i = doubledash; i < argc; i++)
118                         check_attr(cnt, check, argv[i]);
119                 maybe_flush_or_die(stdout, "attribute to stdout");
120         }
121         return 0;