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