]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/check-attr.c
Allow querying all attributes on a file
[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
b4666852
DP
7static int stdin_paths;
8static const char * const check_attr_usage[] = {
9"git check-attr attr... [--] pathname...",
10"git check-attr --stdin attr... < <list-of-paths>",
11NULL
12};
13
14static int null_term_line;
15
16static 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};
d0bfd026 22
41038c5e 23static void check_attr(int cnt, struct git_attr_check *check,
66a1fb30 24 const char *file)
41038c5e
DP
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);
66a1fb30 40 printf(": %s: %s\n", git_attr_name(check[j].attr), value);
41038c5e
DP
41 }
42}
43
66a1fb30 44static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
b4666852
DP
45{
46 struct strbuf buf, nbuf;
47 int line_termination = null_term_line ? 0 : '\n';
48
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 }
66a1fb30 58 check_attr(cnt, check, buf.buf);
b4666852
DP
59 maybe_flush_or_die(stdout, "attribute to stdout");
60 }
61 strbuf_release(&buf);
62 strbuf_release(&nbuf);
63}
64
d0bfd026
JH
65int cmd_check_attr(int argc, const char **argv, const char *prefix)
66{
67 struct git_attr_check *check;
68 int cnt, i, doubledash;
b4666852
DP
69 const char *errstr = NULL;
70
37782920
SB
71 argc = parse_options(argc, argv, prefix, check_attr_options,
72 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
b4666852
DP
73 if (!argc)
74 usage_with_options(check_attr_usage, check_attr_options);
d0bfd026 75
6b06d518
BD
76 if (read_cache() < 0) {
77 die("invalid cache");
78 }
79
d0bfd026 80 doubledash = -1;
b4666852 81 for (i = 0; doubledash < 0 && i < argc; i++) {
d0bfd026
JH
82 if (!strcmp(argv[i], "--"))
83 doubledash = i;
84 }
85
86 /* If there is no double dash, we handle only one attribute */
87 if (doubledash < 0) {
88 cnt = 1;
b4666852 89 doubledash = 0;
d0bfd026 90 } else
b4666852 91 cnt = doubledash;
d0bfd026
JH
92 doubledash++;
93
b4666852
DP
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) {
42fc1139 99 error("%s", errstr);
b4666852
DP
100 usage_with_options(check_attr_usage, check_attr_options);
101 }
102
d0bfd026
JH
103 check = xcalloc(cnt, sizeof(*check));
104 for (i = 0; i < cnt; i++) {
105 const char *name;
e4aee10a 106 struct git_attr *a;
b4666852 107 name = argv[i];
7fb0eaa2 108 a = git_attr(name);
e4aee10a
JH
109 if (!a)
110 return error("%s: not a valid attribute name", name);
111 check[i].attr = a;
d0bfd026
JH
112 }
113
b4666852 114 if (stdin_paths)
66a1fb30 115 check_attr_stdin_paths(cnt, check);
b4666852
DP
116 else {
117 for (i = doubledash; i < argc; i++)
66a1fb30 118 check_attr(cnt, check, argv[i]);
b4666852
DP
119 maybe_flush_or_die(stdout, "attribute to stdout");
120 }
d0bfd026
JH
121 return 0;
122}