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