6 #include "parse-options.h"
8 static int quiet, verbose, stdin_paths, show_non_matching;
9 static const char * const check_ignore_usage[] = {
10 "git check-ignore [options] pathname...",
11 "git check-ignore [options] --stdin < <list-of-paths>",
15 static int null_term_line;
17 static const struct option check_ignore_options[] = {
18 OPT__QUIET(&quiet, N_("suppress progress reporting")),
19 OPT__VERBOSE(&verbose, N_("be verbose")),
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")),
30 static void output_exclude(const char *path, struct exclude *exclude)
32 char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
33 char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
34 if (!null_term_line) {
36 write_name_quoted(path, stdout, '\n');
39 quote_c_style(exclude->el->src, NULL, stdout, 0);
40 printf(":%d:%s%s%s\t",
42 bang, exclude->pattern, slash);
47 quote_c_style(path, NULL, stdout, 0);
52 printf("%s%c", path, '\0');
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',
61 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
66 static int check_ignore(struct dir_struct *dir,
67 const char *prefix, int argc, const char **argv)
69 const char *full_path;
71 int num_ignored = 0, dtype = DT_UNKNOWN, i;
72 struct exclude *exclude;
73 struct pathspec pathspec;
77 fprintf(stderr, "no pathspec given.\n");
82 * check-ignore just needs paths. Magic beyond :/ is really
85 parse_pathspec(&pathspec,
86 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
87 PATHSPEC_SYMLINK_LEADING_PATH |
88 PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |
93 * look for pathspecs matching entries in the index, since these
94 * should not be ignored, in order to be consistent with
95 * 'git status', 'git add' etc.
97 seen = find_pathspecs_matching_against_index(pathspec.raw);
98 for (i = 0; i < pathspec.nr; i++) {
99 full_path = pathspec.raw[i];
102 exclude = last_exclude_matching(dir, full_path, &dtype);
104 if (!quiet && (exclude || show_non_matching))
105 output_exclude(pathspec.items[i].original, exclude);
114 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
116 struct strbuf buf, nbuf;
117 char *pathspec[2] = { NULL, NULL };
118 int line_termination = null_term_line ? 0 : '\n';
121 strbuf_init(&buf, 0);
122 strbuf_init(&nbuf, 0);
123 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
124 if (line_termination && buf.buf[0] == '"') {
126 if (unquote_c_style(&nbuf, buf.buf, NULL))
127 die("line is badly quoted");
128 strbuf_swap(&buf, &nbuf);
130 pathspec[0] = buf.buf;
131 num_ignored += check_ignore(dir, prefix,
132 1, (const char **)pathspec);
133 maybe_flush_or_die(stdout, "check-ignore to stdout");
135 strbuf_release(&buf);
136 strbuf_release(&nbuf);
140 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
143 struct dir_struct dir;
145 git_config(git_default_config, NULL);
147 argc = parse_options(argc, argv, prefix, check_ignore_options,
148 check_ignore_usage, 0);
152 die(_("cannot specify pathnames with --stdin"));
155 die(_("-z only makes sense with --stdin"));
157 die(_("no path specified"));
161 die(_("--quiet is only valid with a single pathname"));
163 die(_("cannot have both --quiet and --verbose"));
165 if (show_non_matching && !verbose)
166 die(_("--non-matching is only valid with --verbose"));
168 /* read_cache() is only necessary so we can watch out for submodules. */
169 if (read_cache() < 0)
170 die(_("index file corrupt"));
172 memset(&dir, 0, sizeof(dir));
173 setup_standard_excludes(&dir);
176 num_ignored = check_ignore_stdin_paths(&dir, prefix);
178 num_ignored = check_ignore(&dir, prefix, argc, argv);
179 maybe_flush_or_die(stdout, "ignore to stdout");
182 clear_directory(&dir);