]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/check-attr.c
Git 1.7.9.5
[thirdparty/git.git] / builtin / check-attr.c
CommitLineData
d0bfd026 1#include "builtin.h"
6b06d518 2#include "cache.h"
d0bfd026
JH
3#include "attr.h"
4#include "quote.h"
b4666852 5#include "parse-options.h"
d0bfd026 6
4ca0f188 7static int all_attrs;
b2b3e9c2 8static int cached_attrs;
b4666852
DP
9static int stdin_paths;
10static const char * const check_attr_usage[] = {
4ca0f188
MH
11"git check-attr [-a | --all | attr...] [--] pathname...",
12"git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
b4666852
DP
13NULL
14};
15
16static int null_term_line;
17
18static const struct option check_attr_options[] = {
4ca0f188 19 OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
b2b3e9c2 20 OPT_BOOLEAN(0, "cached", &cached_attrs, "use .gitattributes only from the index"),
b4666852
DP
21 OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
22 OPT_BOOLEAN('z', NULL, &null_term_line,
23 "input paths are terminated by a null character"),
24 OPT_END()
25};
d0bfd026 26
46f96a6d 27static void output_attr(int cnt, struct git_attr_check *check,
66a1fb30 28 const char *file)
41038c5e
DP
29{
30 int j;
41038c5e
DP
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);
66a1fb30 42 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
41038c5e
DP
43 }
44}
45
f5114a40
MH
46static void check_attr(const char *prefix, int cnt,
47 struct git_attr_check *check, const char *file)
46f96a6d 48{
f5114a40
MH
49 char *full_path =
50 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
4ca0f188 51 if (check != NULL) {
f5114a40 52 if (git_check_attr(full_path, cnt, check))
d932f4eb 53 die("git_check_attr died");
4ca0f188
MH
54 output_attr(cnt, check, file);
55 } else {
f5114a40 56 if (git_all_attrs(full_path, &cnt, &check))
4ca0f188
MH
57 die("git_all_attrs died");
58 output_attr(cnt, check, file);
59 free(check);
60 }
f5114a40 61 free(full_path);
46f96a6d
MH
62}
63
f5114a40
MH
64static void check_attr_stdin_paths(const char *prefix, int cnt,
65 struct git_attr_check *check)
b4666852
DP
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 }
f5114a40 79 check_attr(prefix, cnt, check, buf.buf);
b4666852
DP
80 maybe_flush_or_die(stdout, "attribute to stdout");
81 }
82 strbuf_release(&buf);
83 strbuf_release(&nbuf);
84}
85
9e37a7e1
MH
86static 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
d0bfd026
JH
92int cmd_check_attr(int argc, const char **argv, const char *prefix)
93{
94 struct git_attr_check *check;
d6541bb1 95 int cnt, i, doubledash, filei;
b4666852 96
64589a03
JH
97 git_config(git_default_config, NULL);
98
37782920
SB
99 argc = parse_options(argc, argv, prefix, check_attr_options,
100 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
d0bfd026 101
6b06d518
BD
102 if (read_cache() < 0) {
103 die("invalid cache");
104 }
105
b2b3e9c2
JS
106 if (cached_attrs)
107 git_attr_set_direction(GIT_ATTR_INDEX, NULL);
108
d0bfd026 109 doubledash = -1;
b4666852 110 for (i = 0; doubledash < 0 && i < argc; i++) {
d0bfd026
JH
111 if (!strcmp(argv[i], "--"))
112 doubledash = i;
113 }
114
4ca0f188
MH
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) {
72541040
MH
123 error_with_usage("No attribute specified");
124 } else if (doubledash < 0) {
72541040
MH
125 if (!argc)
126 error_with_usage("No attribute specified");
127
ca64d061
MH
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 }
d6541bb1 137 } else {
b4666852 138 cnt = doubledash;
d6541bb1
MH
139 filei = doubledash + 1;
140 }
d0bfd026 141
72541040 142 /* Check file argument(s): */
fdf6be82
MH
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 }
b4666852 150
4ca0f188
MH
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 }
d0bfd026
JH
165 }
166
b4666852 167 if (stdin_paths)
f5114a40 168 check_attr_stdin_paths(prefix, cnt, check);
b4666852 169 else {
d6541bb1 170 for (i = filei; i < argc; i++)
f5114a40 171 check_attr(prefix, cnt, check, argv[i]);
b4666852
DP
172 maybe_flush_or_die(stdout, "attribute to stdout");
173 }
d0bfd026
JH
174 return 0;
175}