Sync with 2.40.2
[git.git] / builtin / check-ignore.c
blobe4b78782a321920bab57404a63d35019a26c90ac
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "cache.h"
4 #include "config.h"
5 #include "dir.h"
6 #include "gettext.h"
7 #include "quote.h"
8 #include "pathspec.h"
9 #include "parse-options.h"
10 #include "repository.h"
11 #include "submodule.h"
12 #include "write-or-die.h"
14 static int quiet, verbose, stdin_paths, show_non_matching, no_index;
15 static const char * const check_ignore_usage[] = {
16 "git check-ignore [<options>] <pathname>...",
17 "git check-ignore [<options>] --stdin",
18 NULL
21 static int nul_term_line;
23 static const struct option check_ignore_options[] = {
24 OPT__QUIET(&quiet, N_("suppress progress reporting")),
25 OPT__VERBOSE(&verbose, N_("be verbose")),
26 OPT_GROUP(""),
27 OPT_BOOL(0, "stdin", &stdin_paths,
28 N_("read file names from stdin")),
29 OPT_BOOL('z', NULL, &nul_term_line,
30 N_("terminate input and output records by a NUL character")),
31 OPT_BOOL('n', "non-matching", &show_non_matching,
32 N_("show non-matching input paths")),
33 OPT_BOOL(0, "no-index", &no_index,
34 N_("ignore index when checking")),
35 OPT_END()
38 static void output_pattern(const char *path, struct path_pattern *pattern)
40 char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
41 char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : "";
42 if (!nul_term_line) {
43 if (!verbose) {
44 write_name_quoted(path, stdout, '\n');
45 } else {
46 if (pattern) {
47 quote_c_style(pattern->pl->src, NULL, stdout, 0);
48 printf(":%d:%s%s%s\t",
49 pattern->srcpos,
50 bang, pattern->pattern, slash);
52 else {
53 printf("::\t");
55 quote_c_style(path, NULL, stdout, 0);
56 fputc('\n', stdout);
58 } else {
59 if (!verbose) {
60 printf("%s%c", path, '\0');
61 } else {
62 if (pattern)
63 printf("%s%c%d%c%s%s%s%c%s%c",
64 pattern->pl->src, '\0',
65 pattern->srcpos, '\0',
66 bang, pattern->pattern, slash, '\0',
67 path, '\0');
68 else
69 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
74 static int check_ignore(struct dir_struct *dir,
75 const char *prefix, int argc, const char **argv)
77 const char *full_path;
78 char *seen;
79 int num_ignored = 0, i;
80 struct path_pattern *pattern;
81 struct pathspec pathspec;
83 if (!argc) {
84 if (!quiet)
85 fprintf(stderr, "no pathspec given.\n");
86 return 0;
90 * check-ignore just needs paths. Magic beyond :/ is really
91 * irrelevant.
93 parse_pathspec(&pathspec,
94 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
95 PATHSPEC_SYMLINK_LEADING_PATH |
96 PATHSPEC_KEEP_ORDER,
97 prefix, argv);
99 die_path_inside_submodule(&the_index, &pathspec);
102 * look for pathspecs matching entries in the index, since these
103 * should not be ignored, in order to be consistent with
104 * 'git status', 'git add' etc.
106 seen = find_pathspecs_matching_against_index(&pathspec, &the_index,
107 PS_HEED_SKIP_WORKTREE);
108 for (i = 0; i < pathspec.nr; i++) {
109 full_path = pathspec.items[i].match;
110 pattern = NULL;
111 if (!seen[i]) {
112 int dtype = DT_UNKNOWN;
113 pattern = last_matching_pattern(dir, &the_index,
114 full_path, &dtype);
115 if (!verbose && pattern &&
116 pattern->flags & PATTERN_FLAG_NEGATIVE)
117 pattern = NULL;
119 if (!quiet && (pattern || show_non_matching))
120 output_pattern(pathspec.items[i].original, pattern);
121 if (pattern)
122 num_ignored++;
124 free(seen);
125 clear_pathspec(&pathspec);
127 return num_ignored;
130 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
132 struct strbuf buf = STRBUF_INIT;
133 struct strbuf unquoted = STRBUF_INIT;
134 char *pathspec[2] = { NULL, NULL };
135 strbuf_getline_fn getline_fn;
136 int num_ignored = 0;
138 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
139 while (getline_fn(&buf, stdin) != EOF) {
140 if (!nul_term_line && buf.buf[0] == '"') {
141 strbuf_reset(&unquoted);
142 if (unquote_c_style(&unquoted, buf.buf, NULL))
143 die("line is badly quoted");
144 strbuf_swap(&buf, &unquoted);
146 pathspec[0] = buf.buf;
147 num_ignored += check_ignore(dir, prefix,
148 1, (const char **)pathspec);
149 maybe_flush_or_die(stdout, "check-ignore to stdout");
151 strbuf_release(&buf);
152 strbuf_release(&unquoted);
153 return num_ignored;
156 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
158 int num_ignored;
159 struct dir_struct dir = DIR_INIT;
161 git_config(git_default_config, NULL);
163 argc = parse_options(argc, argv, prefix, check_ignore_options,
164 check_ignore_usage, 0);
166 if (stdin_paths) {
167 if (argc > 0)
168 die(_("cannot specify pathnames with --stdin"));
169 } else {
170 if (nul_term_line)
171 die(_("-z only makes sense with --stdin"));
172 if (argc == 0)
173 die(_("no path specified"));
175 if (quiet) {
176 if (argc > 1)
177 die(_("--quiet is only valid with a single pathname"));
178 if (verbose)
179 die(_("cannot have both --quiet and --verbose"));
181 if (show_non_matching && !verbose)
182 die(_("--non-matching is only valid with --verbose"));
184 /* read_cache() is only necessary so we can watch out for submodules. */
185 if (!no_index && repo_read_index(the_repository) < 0)
186 die(_("index file corrupt"));
188 setup_standard_excludes(&dir);
190 if (stdin_paths) {
191 num_ignored = check_ignore_stdin_paths(&dir, prefix);
192 } else {
193 num_ignored = check_ignore(&dir, prefix, argc, argv);
194 maybe_flush_or_die(stdout, "ignore to stdout");
197 dir_clear(&dir);
199 return !num_ignored;