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