59acf74c58cff87842734664050204ff6130447b
1#include "builtin.h"
2#include "cache.h"
3#include "dir.h"
4#include "quote.h"
5#include "pathspec.h"
6#include "parse-options.h"
7
8static int quiet, verbose, stdin_paths, show_non_matching;
9static const char * const check_ignore_usage[] = {
10"git check-ignore [options] pathname...",
11"git check-ignore [options] --stdin < <list-of-paths>",
12NULL
13};
14
15static int null_term_line;
16
17static const struct option check_ignore_options[] = {
18 OPT__QUIET(&quiet, N_("suppress progress reporting")),
19 OPT__VERBOSE(&verbose, N_("be verbose")),
20 OPT_GROUP(""),
21 OPT_BOOLEAN(0, "stdin", &stdin_paths,
22 N_("read file names from stdin")),
23 OPT_BOOLEAN('z', NULL, &null_term_line,
24 N_("input paths are terminated by a null character")),
25 OPT_BOOLEAN('n', "non-matching", &show_non_matching,
26 N_("show non-matching input paths")),
27 OPT_END()
28};
29
30static void output_exclude(const char *path, struct exclude *exclude)
31{
32 char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
33 char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
34 if (!null_term_line) {
35 if (!verbose) {
36 write_name_quoted(path, stdout, '\n');
37 } else {
38 if (exclude) {
39 quote_c_style(exclude->el->src, NULL, stdout, 0);
40 printf(":%d:%s%s%s\t",
41 exclude->srcpos,
42 bang, exclude->pattern, slash);
43 }
44 else {
45 printf("::\t");
46 }
47 quote_c_style(path, NULL, stdout, 0);
48 fputc('\n', stdout);
49 }
50 } else {
51 if (!verbose) {
52 printf("%s%c", path, '\0');
53 } else {
54 if (exclude)
55 printf("%s%c%d%c%s%s%s%c%s%c",
56 exclude->el->src, '\0',
57 exclude->srcpos, '\0',
58 bang, exclude->pattern, slash, '\0',
59 path, '\0');
60 else
61 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
62 }
63 }
64}
65
66static int check_ignore(const char *prefix, const char **pathspec)
67{
68 struct dir_struct dir;
69 const char *path, *full_path;
70 char *seen;
71 int num_ignored = 0, dtype = DT_UNKNOWN, i;
72 struct path_exclude_check check;
73 struct exclude *exclude;
74
75 /* read_cache() is only necessary so we can watch out for submodules. */
76 if (read_cache() < 0)
77 die(_("index file corrupt"));
78
79 memset(&dir, 0, sizeof(dir));
80 dir.flags |= DIR_COLLECT_IGNORED;
81 setup_standard_excludes(&dir);
82
83 if (!pathspec || !*pathspec) {
84 if (!quiet)
85 fprintf(stderr, "no pathspec given.\n");
86 return 0;
87 }
88
89 path_exclude_check_init(&check, &dir);
90 /*
91 * look for pathspecs matching entries in the index, since these
92 * should not be ignored, in order to be consistent with
93 * 'git status', 'git add' etc.
94 */
95 seen = find_pathspecs_matching_against_index(pathspec);
96 for (i = 0; pathspec[i]; i++) {
97 path = pathspec[i];
98 full_path = prefix_path(prefix, prefix
99 ? strlen(prefix) : 0, path);
100 full_path = check_path_for_gitlink(full_path);
101 die_if_path_beyond_symlink(full_path, prefix);
102 exclude = NULL;
103 if (!seen[i]) {
104 exclude = last_exclude_matching_path(&check, full_path,
105 -1, &dtype);
106 }
107 if (!quiet && (exclude || show_non_matching))
108 output_exclude(path, exclude);
109 if (exclude)
110 num_ignored++;
111 }
112 free(seen);
113 clear_directory(&dir);
114 path_exclude_check_clear(&check);
115
116 return num_ignored;
117}
118
119static int check_ignore_stdin_paths(const char *prefix)
120{
121 struct strbuf buf, nbuf;
122 char **pathspec = NULL;
123 size_t nr = 0, alloc = 0;
124 int line_termination = null_term_line ? 0 : '\n';
125 int num_ignored;
126
127 strbuf_init(&buf, 0);
128 strbuf_init(&nbuf, 0);
129 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
130 if (line_termination && buf.buf[0] == '"') {
131 strbuf_reset(&nbuf);
132 if (unquote_c_style(&nbuf, buf.buf, NULL))
133 die("line is badly quoted");
134 strbuf_swap(&buf, &nbuf);
135 }
136 ALLOC_GROW(pathspec, nr + 1, alloc);
137 pathspec[nr] = xcalloc(strlen(buf.buf) + 1, sizeof(*buf.buf));
138 strcpy(pathspec[nr++], buf.buf);
139 }
140 ALLOC_GROW(pathspec, nr + 1, alloc);
141 pathspec[nr] = NULL;
142 num_ignored = check_ignore(prefix, (const char **)pathspec);
143 maybe_flush_or_die(stdout, "attribute to stdout");
144 strbuf_release(&buf);
145 strbuf_release(&nbuf);
146 free(pathspec);
147 return num_ignored;
148}
149
150int cmd_check_ignore(int argc, const char **argv, const char *prefix)
151{
152 int num_ignored;
153
154 git_config(git_default_config, NULL);
155
156 argc = parse_options(argc, argv, prefix, check_ignore_options,
157 check_ignore_usage, 0);
158
159 if (stdin_paths) {
160 if (argc > 0)
161 die(_("cannot specify pathnames with --stdin"));
162 } else {
163 if (null_term_line)
164 die(_("-z only makes sense with --stdin"));
165 if (argc == 0)
166 die(_("no path specified"));
167 }
168 if (quiet) {
169 if (argc > 1)
170 die(_("--quiet is only valid with a single pathname"));
171 if (verbose)
172 die(_("cannot have both --quiet and --verbose"));
173 }
174 if (show_non_matching && !verbose)
175 die(_("--non-matching is only valid with --verbose"));
176
177 if (stdin_paths) {
178 num_ignored = check_ignore_stdin_paths(prefix);
179 } else {
180 num_ignored = check_ignore(prefix, argv);
181 maybe_flush_or_die(stdout, "ignore to stdout");
182 }
183
184 return !num_ignored;
185}