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