dir.c: replace is_path_excluded with now equivalent is_excluded API
[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 N_("git check-attr [-a | --all | attr...] [--] pathname..."),
12 N_("git check-attr --stdin [-z] [-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, N_("report all attributes set on file")),
20         OPT_BOOLEAN(0,  "cached", &cached_attrs, N_("use .gitattributes only from the index")),
21         OPT_BOOLEAN(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
22         OPT_BOOLEAN('z', NULL, &null_term_line,
23                 N_("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         git_config(git_default_config, NULL);
98
99         argc = parse_options(argc, argv, prefix, check_attr_options,
100                              check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
101
102         if (read_cache() < 0) {
103                 die("invalid cache");
104         }
105
106         if (cached_attrs)
107                 git_attr_set_direction(GIT_ATTR_INDEX, NULL);
108
109         doubledash = -1;
110         for (i = 0; doubledash < 0 && i < argc; i++) {
111                 if (!strcmp(argv[i], "--"))
112                         doubledash = i;
113         }
114
115         /* Process --all and/or attribute arguments: */
116         if (all_attrs) {
117                 if (doubledash >= 1)
118                         error_with_usage("Attributes and --all both specified");
119
120                 cnt = 0;
121                 filei = doubledash + 1;
122         } else if (doubledash == 0) {
123                 error_with_usage("No attribute specified");
124         } else if (doubledash < 0) {
125                 if (!argc)
126                         error_with_usage("No attribute specified");
127
128                 if (stdin_paths) {
129                         /* Treat all arguments as attribute names. */
130                         cnt = argc;
131                         filei = argc;
132                 } else {
133                         /* Treat exactly one argument as an attribute name. */
134                         cnt = 1;
135                         filei = 1;
136                 }
137         } else {
138                 cnt = doubledash;
139                 filei = doubledash + 1;
140         }
141
142         /* Check file argument(s): */
143         if (stdin_paths) {
144                 if (filei < argc)
145                         error_with_usage("Can't specify files with --stdin");
146         } else {
147                 if (filei >= argc)
148                         error_with_usage("No file specified");
149         }
150
151         if (all_attrs) {
152                 check = NULL;
153         } else {
154                 check = xcalloc(cnt, sizeof(*check));
155                 for (i = 0; i < cnt; i++) {
156                         const char *name;
157                         struct git_attr *a;
158                         name = argv[i];
159                         a = git_attr(name);
160                         if (!a)
161                                 return error("%s: not a valid attribute name",
162                                         name);
163                         check[i].attr = a;
164                 }
165         }
166
167         if (stdin_paths)
168                 check_attr_stdin_paths(prefix, cnt, check);
169         else {
170                 for (i = filei; i < argc; i++)
171                         check_attr(prefix, cnt, check, argv[i]);
172                 maybe_flush_or_die(stdout, "attribute to stdout");
173         }
174         return 0;
175 }