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