]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/check-ignore.c
builtin: stop using `the_index`
[thirdparty/git.git] / builtin / check-ignore.c
CommitLineData
368aa529 1#include "builtin.h"
b2141fc1 2#include "config.h"
368aa529 3#include "dir.h"
f394e093 4#include "gettext.h"
368aa529
AS
5#include "quote.h"
6#include "pathspec.h"
7#include "parse-options.h"
d1cbe1e6 8#include "repository.h"
c08397e3 9#include "submodule.h"
d48be35c 10#include "write-or-die.h"
368aa529 11
8231fa6a 12static int quiet, verbose, stdin_paths, show_non_matching, no_index;
368aa529 13static const char * const check_ignore_usage[] = {
9c9b4f2f 14"git check-ignore [<options>] <pathname>...",
33e8fc87 15"git check-ignore [<options>] --stdin",
368aa529
AS
16NULL
17};
18
800531a8 19static int nul_term_line;
368aa529
AS
20
21static const struct option check_ignore_options[] = {
22 OPT__QUIET(&quiet, N_("suppress progress reporting")),
23 OPT__VERBOSE(&verbose, N_("be verbose")),
24 OPT_GROUP(""),
d5d09d47
SB
25 OPT_BOOL(0, "stdin", &stdin_paths,
26 N_("read file names from stdin")),
a86a8b97
JH
27 OPT_BOOL('z', NULL, &nul_term_line,
28 N_("terminate input and output records by a NUL character")),
d5d09d47
SB
29 OPT_BOOL('n', "non-matching", &show_non_matching,
30 N_("show non-matching input paths")),
8231fa6a
DW
31 OPT_BOOL(0, "no-index", &no_index,
32 N_("ignore index when checking")),
368aa529
AS
33 OPT_END()
34};
35
ab8db613 36static void output_pattern(const char *path, struct path_pattern *pattern)
368aa529 37{
4ff89ee5
DS
38 char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
39 char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : "";
800531a8 40 if (!nul_term_line) {
368aa529
AS
41 if (!verbose) {
42 write_name_quoted(path, stdout, '\n');
43 } else {
ab8db613 44 if (pattern) {
caa3d554 45 quote_c_style(pattern->pl->src, NULL, stdout, 0);
ae3caf4c 46 printf(":%d:%s%s%s\t",
ab8db613
DS
47 pattern->srcpos,
48 bang, pattern->pattern, slash);
ae3caf4c
AS
49 }
50 else {
51 printf("::\t");
52 }
368aa529
AS
53 quote_c_style(path, NULL, stdout, 0);
54 fputc('\n', stdout);
55 }
56 } else {
57 if (!verbose) {
58 printf("%s%c", path, '\0');
59 } else {
ab8db613 60 if (pattern)
ae3caf4c 61 printf("%s%c%d%c%s%s%s%c%s%c",
caa3d554 62 pattern->pl->src, '\0',
ab8db613
DS
63 pattern->srcpos, '\0',
64 bang, pattern->pattern, slash, '\0',
ae3caf4c
AS
65 path, '\0');
66 else
67 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
368aa529
AS
68 }
69 }
70}
71
c51afbbd 72static int check_ignore(struct dir_struct *dir,
931eab64 73 const char *prefix, int argc, const char **argv)
368aa529 74{
931eab64 75 const char *full_path;
368aa529 76 char *seen;
d60771e9 77 int num_ignored = 0, i;
ab8db613 78 struct path_pattern *pattern;
931eab64 79 struct pathspec pathspec;
368aa529 80
931eab64 81 if (!argc) {
368aa529
AS
82 if (!quiet)
83 fprintf(stderr, "no pathspec given.\n");
84 return 0;
85 }
86
931eab64
NTND
87 /*
88 * check-ignore just needs paths. Magic beyond :/ is really
89 * irrelevant.
90 */
91 parse_pathspec(&pathspec,
92 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
93 PATHSPEC_SYMLINK_LEADING_PATH |
931eab64
NTND
94 PATHSPEC_KEEP_ORDER,
95 prefix, argv);
96
f59aa5e0 97 die_path_inside_submodule(the_repository->index, &pathspec);
c08397e3 98
368aa529
AS
99 /*
100 * look for pathspecs matching entries in the index, since these
101 * should not be ignored, in order to be consistent with
102 * 'git status', 'git add' etc.
103 */
f59aa5e0 104 seen = find_pathspecs_matching_against_index(&pathspec, the_repository->index,
719630eb 105 PS_HEED_SKIP_WORKTREE);
931eab64 106 for (i = 0; i < pathspec.nr; i++) {
84b8b5d1 107 full_path = pathspec.items[i].match;
ab8db613 108 pattern = NULL;
c19387e7 109 if (!seen[i]) {
d60771e9 110 int dtype = DT_UNKNOWN;
f59aa5e0 111 pattern = last_matching_pattern(dir, the_repository->index,
a0bba65b 112 full_path, &dtype);
7ec8125f
EN
113 if (!verbose && pattern &&
114 pattern->flags & PATTERN_FLAG_NEGATIVE)
115 pattern = NULL;
368aa529 116 }
ab8db613
DS
117 if (!quiet && (pattern || show_non_matching))
118 output_pattern(pathspec.items[i].original, pattern);
119 if (pattern)
ae3caf4c 120 num_ignored++;
368aa529
AS
121 }
122 free(seen);
26564436 123 clear_pathspec(&pathspec);
368aa529
AS
124
125 return num_ignored;
126}
127
c51afbbd 128static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
368aa529 129{
0d4cc1b4
JK
130 struct strbuf buf = STRBUF_INIT;
131 struct strbuf unquoted = STRBUF_INIT;
0c8e8c08 132 char *pathspec[2] = { NULL, NULL };
dca90031 133 strbuf_getline_fn getline_fn;
0c8e8c08 134 int num_ignored = 0;
368aa529 135
dca90031 136 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
dca90031
JH
137 while (getline_fn(&buf, stdin) != EOF) {
138 if (!nul_term_line && buf.buf[0] == '"') {
0d4cc1b4
JK
139 strbuf_reset(&unquoted);
140 if (unquote_c_style(&unquoted, buf.buf, NULL))
368aa529 141 die("line is badly quoted");
0d4cc1b4 142 strbuf_swap(&buf, &unquoted);
368aa529 143 }
0c8e8c08 144 pathspec[0] = buf.buf;
931eab64
NTND
145 num_ignored += check_ignore(dir, prefix,
146 1, (const char **)pathspec);
0c8e8c08 147 maybe_flush_or_die(stdout, "check-ignore to stdout");
368aa529 148 }
368aa529 149 strbuf_release(&buf);
0d4cc1b4 150 strbuf_release(&unquoted);
368aa529
AS
151 return num_ignored;
152}
153
154int cmd_check_ignore(int argc, const char **argv, const char *prefix)
155{
156 int num_ignored;
ce93a4c6 157 struct dir_struct dir = DIR_INIT;
368aa529
AS
158
159 git_config(git_default_config, NULL);
160
161 argc = parse_options(argc, argv, prefix, check_ignore_options,
162 check_ignore_usage, 0);
163
164 if (stdin_paths) {
165 if (argc > 0)
166 die(_("cannot specify pathnames with --stdin"));
167 } else {
800531a8 168 if (nul_term_line)
368aa529
AS
169 die(_("-z only makes sense with --stdin"));
170 if (argc == 0)
171 die(_("no path specified"));
172 }
173 if (quiet) {
174 if (argc > 1)
175 die(_("--quiet is only valid with a single pathname"));
176 if (verbose)
177 die(_("cannot have both --quiet and --verbose"));
178 }
ae3caf4c
AS
179 if (show_non_matching && !verbose)
180 die(_("--non-matching is only valid with --verbose"));
368aa529 181
0006d85c 182 /* read_cache() is only necessary so we can watch out for submodules. */
07047d68 183 if (!no_index && repo_read_index(the_repository) < 0)
0006d85c
AS
184 die(_("index file corrupt"));
185
0006d85c 186 setup_standard_excludes(&dir);
368aa529
AS
187
188 if (stdin_paths) {
c51afbbd 189 num_ignored = check_ignore_stdin_paths(&dir, prefix);
368aa529 190 } else {
931eab64 191 num_ignored = check_ignore(&dir, prefix, argc, argv);
368aa529
AS
192 maybe_flush_or_die(stdout, "ignore to stdout");
193 }
194
eceba532 195 dir_clear(&dir);
0006d85c 196
368aa529
AS
197 return !num_ignored;
198}