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