dir.c: git-status: avoid is_excluded checks for tracked files
[git/gitweb.git] / builtin / check-ignore.c
blob7388346ef2a0beb925d67e4933b3aacb43c3c47e
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"
8 static int quiet, verbose, stdin_paths;
9 static const char * const check_ignore_usage[] = {
10 "git check-ignore [options] pathname...",
11 "git check-ignore [options] --stdin < <list-of-paths>",
12 NULL
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")),
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_END()
28 static void output_exclude(const char *path, struct exclude *exclude)
30 char *bang = exclude->flags & EXC_FLAG_NEGATIVE ? "!" : "";
31 char *slash = exclude->flags & EXC_FLAG_MUSTBEDIR ? "/" : "";
32 if (!null_term_line) {
33 if (!verbose) {
34 write_name_quoted(path, stdout, '\n');
35 } else {
36 quote_c_style(exclude->el->src, NULL, stdout, 0);
37 printf(":%d:%s%s%s\t",
38 exclude->srcpos,
39 bang, exclude->pattern, slash);
40 quote_c_style(path, NULL, stdout, 0);
41 fputc('\n', stdout);
43 } else {
44 if (!verbose) {
45 printf("%s%c", path, '\0');
46 } else {
47 printf("%s%c%d%c%s%s%s%c%s%c",
48 exclude->el->src, '\0',
49 exclude->srcpos, '\0',
50 bang, exclude->pattern, slash, '\0',
51 path, '\0');
56 static int check_ignore(const char *prefix, const char **pathspec)
58 struct dir_struct dir;
59 const char *path, *full_path;
60 char *seen;
61 int num_ignored = 0, dtype = DT_UNKNOWN, i;
62 struct exclude *exclude;
64 /* read_cache() is only necessary so we can watch out for submodules. */
65 if (read_cache() < 0)
66 die(_("index file corrupt"));
68 memset(&dir, 0, sizeof(dir));
69 dir.flags |= DIR_COLLECT_IGNORED;
70 setup_standard_excludes(&dir);
72 if (!pathspec || !*pathspec) {
73 if (!quiet)
74 fprintf(stderr, "no pathspec given.\n");
75 return 0;
79 * look for pathspecs matching entries in the index, since these
80 * should not be ignored, in order to be consistent with
81 * 'git status', 'git add' etc.
83 seen = find_pathspecs_matching_against_index(pathspec);
84 for (i = 0; pathspec[i]; i++) {
85 path = pathspec[i];
86 full_path = prefix_path(prefix, prefix
87 ? strlen(prefix) : 0, path);
88 full_path = check_path_for_gitlink(full_path);
89 die_if_path_beyond_symlink(full_path, prefix);
90 if (!seen[i]) {
91 exclude = last_exclude_matching(&dir, full_path, &dtype);
92 if (exclude) {
93 if (!quiet)
94 output_exclude(path, exclude);
95 num_ignored++;
99 free(seen);
100 clear_directory(&dir);
102 return num_ignored;
105 static int check_ignore_stdin_paths(const char *prefix)
107 struct strbuf buf, nbuf;
108 char **pathspec = NULL;
109 size_t nr = 0, alloc = 0;
110 int line_termination = null_term_line ? 0 : '\n';
111 int num_ignored;
113 strbuf_init(&buf, 0);
114 strbuf_init(&nbuf, 0);
115 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
116 if (line_termination && buf.buf[0] == '"') {
117 strbuf_reset(&nbuf);
118 if (unquote_c_style(&nbuf, buf.buf, NULL))
119 die("line is badly quoted");
120 strbuf_swap(&buf, &nbuf);
122 ALLOC_GROW(pathspec, nr + 1, alloc);
123 pathspec[nr] = xcalloc(strlen(buf.buf) + 1, sizeof(*buf.buf));
124 strcpy(pathspec[nr++], buf.buf);
126 ALLOC_GROW(pathspec, nr + 1, alloc);
127 pathspec[nr] = NULL;
128 num_ignored = check_ignore(prefix, (const char **)pathspec);
129 maybe_flush_or_die(stdout, "attribute to stdout");
130 strbuf_release(&buf);
131 strbuf_release(&nbuf);
132 free(pathspec);
133 return num_ignored;
136 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
138 int num_ignored;
140 git_config(git_default_config, NULL);
142 argc = parse_options(argc, argv, prefix, check_ignore_options,
143 check_ignore_usage, 0);
145 if (stdin_paths) {
146 if (argc > 0)
147 die(_("cannot specify pathnames with --stdin"));
148 } else {
149 if (null_term_line)
150 die(_("-z only makes sense with --stdin"));
151 if (argc == 0)
152 die(_("no path specified"));
154 if (quiet) {
155 if (argc > 1)
156 die(_("--quiet is only valid with a single pathname"));
157 if (verbose)
158 die(_("cannot have both --quiet and --verbose"));
161 if (stdin_paths) {
162 num_ignored = check_ignore_stdin_paths(prefix);
163 } else {
164 num_ignored = check_ignore(prefix, argv);
165 maybe_flush_or_die(stdout, "ignore to stdout");
168 return !num_ignored;