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