check-ignore: Add option to ignore index contents
[alt-git.git] / builtin / check-ignore.c
blob8b3c2e3e27c036718fdf62f7af9074410f44fa51
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, show_non_matching, no_index;
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 nul_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_BOOL(0, "stdin", &stdin_paths,
22 N_("read file names from stdin")),
23 OPT_BOOL('z', NULL, &nul_term_line,
24 N_("terminate input and output records by a NUL character")),
25 OPT_BOOL('n', "non-matching", &show_non_matching,
26 N_("show non-matching input paths")),
27 OPT_BOOL(0, "no-index", &no_index,
28 N_("ignore index when checking")),
29 OPT_END()
32 static void output_exclude(const char *path, struct exclude *exclude)
34 char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
35 char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
36 if (!nul_term_line) {
37 if (!verbose) {
38 write_name_quoted(path, stdout, '\n');
39 } else {
40 if (exclude) {
41 quote_c_style(exclude->el->src, NULL, stdout, 0);
42 printf(":%d:%s%s%s\t",
43 exclude->srcpos,
44 bang, exclude->pattern, slash);
46 else {
47 printf("::\t");
49 quote_c_style(path, NULL, stdout, 0);
50 fputc('\n', stdout);
52 } else {
53 if (!verbose) {
54 printf("%s%c", path, '\0');
55 } else {
56 if (exclude)
57 printf("%s%c%d%c%s%s%s%c%s%c",
58 exclude->el->src, '\0',
59 exclude->srcpos, '\0',
60 bang, exclude->pattern, slash, '\0',
61 path, '\0');
62 else
63 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
68 static int check_ignore(struct dir_struct *dir,
69 const char *prefix, const char **pathspec)
71 const char *path, *full_path;
72 char *seen;
73 int num_ignored = 0, dtype = DT_UNKNOWN, i;
74 struct exclude *exclude;
76 if (!pathspec || !*pathspec) {
77 if (!quiet)
78 fprintf(stderr, "no pathspec given.\n");
79 return 0;
83 * look for pathspecs matching entries in the index, since these
84 * should not be ignored, in order to be consistent with
85 * 'git status', 'git add' etc.
87 seen = find_pathspecs_matching_against_index(pathspec);
88 for (i = 0; pathspec[i]; i++) {
89 path = pathspec[i];
90 full_path = prefix_path(prefix, prefix
91 ? strlen(prefix) : 0, path);
92 full_path = check_path_for_gitlink(full_path);
93 die_if_path_beyond_symlink(full_path, prefix);
94 exclude = NULL;
95 if (!seen[i]) {
96 exclude = last_exclude_matching(dir, full_path, &dtype);
98 if (!quiet && (exclude || show_non_matching))
99 output_exclude(path, exclude);
100 if (exclude)
101 num_ignored++;
103 free(seen);
105 return num_ignored;
108 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
110 struct strbuf buf, nbuf;
111 char *pathspec[2] = { NULL, NULL };
112 int line_termination = nul_term_line ? 0 : '\n';
113 int num_ignored = 0;
115 strbuf_init(&buf, 0);
116 strbuf_init(&nbuf, 0);
117 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
118 if (line_termination && buf.buf[0] == '"') {
119 strbuf_reset(&nbuf);
120 if (unquote_c_style(&nbuf, buf.buf, NULL))
121 die("line is badly quoted");
122 strbuf_swap(&buf, &nbuf);
124 pathspec[0] = buf.buf;
125 num_ignored += check_ignore(dir, prefix, (const char **)pathspec);
126 maybe_flush_or_die(stdout, "check-ignore to stdout");
128 strbuf_release(&buf);
129 strbuf_release(&nbuf);
130 return num_ignored;
133 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
135 int num_ignored;
136 struct dir_struct dir;
138 git_config(git_default_config, NULL);
140 argc = parse_options(argc, argv, prefix, check_ignore_options,
141 check_ignore_usage, 0);
143 if (stdin_paths) {
144 if (argc > 0)
145 die(_("cannot specify pathnames with --stdin"));
146 } else {
147 if (nul_term_line)
148 die(_("-z only makes sense with --stdin"));
149 if (argc == 0)
150 die(_("no path specified"));
152 if (quiet) {
153 if (argc > 1)
154 die(_("--quiet is only valid with a single pathname"));
155 if (verbose)
156 die(_("cannot have both --quiet and --verbose"));
158 if (show_non_matching && !verbose)
159 die(_("--non-matching is only valid with --verbose"));
161 /* read_cache() is only necessary so we can watch out for submodules. */
162 if (!no_index && read_cache() < 0)
163 die(_("index file corrupt"));
165 memset(&dir, 0, sizeof(dir));
166 setup_standard_excludes(&dir);
168 if (stdin_paths) {
169 num_ignored = check_ignore_stdin_paths(&dir, prefix);
170 } else {
171 num_ignored = check_ignore(&dir, prefix, argv);
172 maybe_flush_or_die(stdout, "ignore to stdout");
175 clear_directory(&dir);
177 return !num_ignored;