notes-utils: convert internals to struct object_id
[alt-git.git] / builtin / check-attr.c
blob4d01ca0c8ba0ba2a1d2c209b766e19d9ca9c8ac0
1 #include "builtin.h"
2 #include "cache.h"
3 #include "attr.h"
4 #include "quote.h"
5 #include "parse-options.h"
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>...]"),
13 NULL
16 static int nul_term_line;
18 static const struct option check_attr_options[] = {
19 OPT_BOOL('a', "all", &all_attrs, N_("report all attributes set on file")),
20 OPT_BOOL(0, "cached", &cached_attrs, N_("use .gitattributes only from the index")),
21 OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
22 OPT_BOOL('z', NULL, &nul_term_line,
23 N_("terminate input and output records by a NUL character")),
24 OPT_END()
27 static void output_attr(struct attr_check *check, const char *file)
29 int j;
30 int cnt = check->nr;
32 for (j = 0; j < cnt; j++) {
33 const char *value = check->items[j].value;
35 if (ATTR_TRUE(value))
36 value = "set";
37 else if (ATTR_FALSE(value))
38 value = "unset";
39 else if (ATTR_UNSET(value))
40 value = "unspecified";
42 if (nul_term_line) {
43 printf("%s%c" /* path */
44 "%s%c" /* attrname */
45 "%s%c" /* attrvalue */,
46 file, 0,
47 git_attr_name(check->items[j].attr), 0, value, 0);
48 } else {
49 quote_c_style(file, NULL, stdout, 0);
50 printf(": %s: %s\n",
51 git_attr_name(check->items[j].attr), value);
56 static void check_attr(const char *prefix,
57 struct attr_check *check,
58 int collect_all,
59 const char *file)
61 char *full_path =
62 prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
64 if (collect_all) {
65 git_all_attrs(full_path, check);
66 } else {
67 if (git_check_attr(full_path, check))
68 die("git_check_attr died");
70 output_attr(check, file);
72 free(full_path);
75 static void check_attr_stdin_paths(const char *prefix,
76 struct attr_check *check,
77 int collect_all)
79 struct strbuf buf = STRBUF_INIT;
80 struct strbuf unquoted = STRBUF_INIT;
81 strbuf_getline_fn getline_fn;
83 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
84 while (getline_fn(&buf, stdin) != EOF) {
85 if (!nul_term_line && buf.buf[0] == '"') {
86 strbuf_reset(&unquoted);
87 if (unquote_c_style(&unquoted, buf.buf, NULL))
88 die("line is badly quoted");
89 strbuf_swap(&buf, &unquoted);
91 check_attr(prefix, check, collect_all, buf.buf);
92 maybe_flush_or_die(stdout, "attribute to stdout");
94 strbuf_release(&buf);
95 strbuf_release(&unquoted);
98 static NORETURN void error_with_usage(const char *msg)
100 error("%s", msg);
101 usage_with_options(check_attr_usage, check_attr_options);
104 int cmd_check_attr(int argc, const char **argv, const char *prefix)
106 struct attr_check *check;
107 int cnt, i, doubledash, filei;
109 if (!is_bare_repository())
110 setup_work_tree();
112 git_config(git_default_config, NULL);
114 argc = parse_options(argc, argv, prefix, check_attr_options,
115 check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
117 if (read_cache() < 0) {
118 die("invalid cache");
121 if (cached_attrs)
122 git_attr_set_direction(GIT_ATTR_INDEX, NULL);
124 doubledash = -1;
125 for (i = 0; doubledash < 0 && i < argc; i++) {
126 if (!strcmp(argv[i], "--"))
127 doubledash = i;
130 /* Process --all and/or attribute arguments: */
131 if (all_attrs) {
132 if (doubledash >= 1)
133 error_with_usage("Attributes and --all both specified");
135 cnt = 0;
136 filei = doubledash + 1;
137 } else if (doubledash == 0) {
138 error_with_usage("No attribute specified");
139 } else if (doubledash < 0) {
140 if (!argc)
141 error_with_usage("No attribute specified");
143 if (stdin_paths) {
144 /* Treat all arguments as attribute names. */
145 cnt = argc;
146 filei = argc;
147 } else {
148 /* Treat exactly one argument as an attribute name. */
149 cnt = 1;
150 filei = 1;
152 } else {
153 cnt = doubledash;
154 filei = doubledash + 1;
157 /* Check file argument(s): */
158 if (stdin_paths) {
159 if (filei < argc)
160 error_with_usage("Can't specify files with --stdin");
161 } else {
162 if (filei >= argc)
163 error_with_usage("No file specified");
166 check = attr_check_alloc();
167 if (!all_attrs) {
168 for (i = 0; i < cnt; i++) {
169 const struct git_attr *a = git_attr(argv[i]);
171 if (!a)
172 return error("%s: not a valid attribute name",
173 argv[i]);
174 attr_check_append(check, a);
178 if (stdin_paths)
179 check_attr_stdin_paths(prefix, check, all_attrs);
180 else {
181 for (i = filei; i < argc; i++)
182 check_attr(prefix, check, all_attrs, argv[i]);
183 maybe_flush_or_die(stdout, "attribute to stdout");
186 attr_check_free(check);
187 return 0;