]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #include "builtin.h" | |
3 | #include "config.h" | |
4 | #include "dir.h" | |
5 | #include "gettext.h" | |
6 | #include "quote.h" | |
7 | #include "pathspec.h" | |
8 | #include "parse-options.h" | |
9 | #include "submodule.h" | |
10 | #include "write-or-die.h" | |
11 | ||
12 | static int quiet, verbose, stdin_paths, show_non_matching, no_index; | |
13 | static const char * const check_ignore_usage[] = { | |
14 | "git check-ignore [<options>] <pathname>...", | |
15 | "git check-ignore [<options>] --stdin", | |
16 | NULL | |
17 | }; | |
18 | ||
19 | static int nul_term_line; | |
20 | ||
21 | static const struct option check_ignore_options[] = { | |
22 | OPT__QUIET(&quiet, N_("suppress progress reporting")), | |
23 | OPT__VERBOSE(&verbose, N_("be verbose")), | |
24 | OPT_GROUP(""), | |
25 | OPT_BOOL(0, "stdin", &stdin_paths, | |
26 | N_("read file names from stdin")), | |
27 | OPT_BOOL('z', NULL, &nul_term_line, | |
28 | N_("terminate input and output records by a NUL character")), | |
29 | OPT_BOOL('n', "non-matching", &show_non_matching, | |
30 | N_("show non-matching input paths")), | |
31 | OPT_BOOL(0, "no-index", &no_index, | |
32 | N_("ignore index when checking")), | |
33 | OPT_END() | |
34 | }; | |
35 | ||
36 | static void output_pattern(const char *path, struct path_pattern *pattern) | |
37 | { | |
38 | const char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : ""; | |
39 | const char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : ""; | |
40 | if (!nul_term_line) { | |
41 | if (!verbose) { | |
42 | write_name_quoted(path, stdout, '\n'); | |
43 | } else { | |
44 | if (pattern) { | |
45 | quote_c_style(pattern->pl->src, NULL, stdout, 0); | |
46 | printf(":%d:%s%s%s\t", | |
47 | pattern->srcpos, | |
48 | bang, pattern->pattern, slash); | |
49 | } | |
50 | else { | |
51 | printf("::\t"); | |
52 | } | |
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 { | |
60 | if (pattern) | |
61 | printf("%s%c%d%c%s%s%s%c%s%c", | |
62 | pattern->pl->src, '\0', | |
63 | pattern->srcpos, '\0', | |
64 | bang, pattern->pattern, slash, '\0', | |
65 | path, '\0'); | |
66 | else | |
67 | printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0'); | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | static int check_ignore(struct dir_struct *dir, | |
73 | const char *prefix, int argc, const char **argv) | |
74 | { | |
75 | const char *full_path; | |
76 | char *seen; | |
77 | int num_ignored = 0, i; | |
78 | struct path_pattern *pattern; | |
79 | struct pathspec pathspec; | |
80 | ||
81 | if (!argc) { | |
82 | if (!quiet) | |
83 | fprintf(stderr, "no pathspec given.\n"); | |
84 | return 0; | |
85 | } | |
86 | ||
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 | | |
94 | PATHSPEC_KEEP_ORDER, | |
95 | prefix, argv); | |
96 | ||
97 | die_path_inside_submodule(the_repository->index, &pathspec); | |
98 | ||
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 | */ | |
104 | seen = find_pathspecs_matching_against_index(&pathspec, the_repository->index, | |
105 | PS_HEED_SKIP_WORKTREE); | |
106 | for (i = 0; i < pathspec.nr; i++) { | |
107 | full_path = pathspec.items[i].match; | |
108 | pattern = NULL; | |
109 | if (!seen[i]) { | |
110 | int dtype = DT_UNKNOWN; | |
111 | pattern = last_matching_pattern(dir, the_repository->index, | |
112 | full_path, &dtype); | |
113 | if (!verbose && pattern && | |
114 | pattern->flags & PATTERN_FLAG_NEGATIVE) | |
115 | pattern = NULL; | |
116 | } | |
117 | if (!quiet && (pattern || show_non_matching)) | |
118 | output_pattern(pathspec.items[i].original, pattern); | |
119 | if (pattern) | |
120 | num_ignored++; | |
121 | } | |
122 | free(seen); | |
123 | clear_pathspec(&pathspec); | |
124 | ||
125 | return num_ignored; | |
126 | } | |
127 | ||
128 | static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix) | |
129 | { | |
130 | struct strbuf buf = STRBUF_INIT; | |
131 | struct strbuf unquoted = STRBUF_INIT; | |
132 | char *pathspec[2] = { NULL, NULL }; | |
133 | strbuf_getline_fn getline_fn; | |
134 | int num_ignored = 0; | |
135 | ||
136 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; | |
137 | while (getline_fn(&buf, stdin) != EOF) { | |
138 | if (!nul_term_line && buf.buf[0] == '"') { | |
139 | strbuf_reset(&unquoted); | |
140 | if (unquote_c_style(&unquoted, buf.buf, NULL)) | |
141 | die("line is badly quoted"); | |
142 | strbuf_swap(&buf, &unquoted); | |
143 | } | |
144 | pathspec[0] = buf.buf; | |
145 | num_ignored += check_ignore(dir, prefix, | |
146 | 1, (const char **)pathspec); | |
147 | maybe_flush_or_die(stdout, "check-ignore to stdout"); | |
148 | } | |
149 | strbuf_release(&buf); | |
150 | strbuf_release(&unquoted); | |
151 | return num_ignored; | |
152 | } | |
153 | ||
154 | int cmd_check_ignore(int argc, | |
155 | const char **argv, | |
156 | const char *prefix, | |
157 | struct repository *repo UNUSED) | |
158 | { | |
159 | int num_ignored; | |
160 | struct dir_struct dir = DIR_INIT; | |
161 | ||
162 | git_config(git_default_config, NULL); | |
163 | ||
164 | argc = parse_options(argc, argv, prefix, check_ignore_options, | |
165 | check_ignore_usage, 0); | |
166 | ||
167 | if (stdin_paths) { | |
168 | if (argc > 0) | |
169 | die(_("cannot specify pathnames with --stdin")); | |
170 | } else { | |
171 | if (nul_term_line) | |
172 | die(_("-z only makes sense with --stdin")); | |
173 | if (argc == 0) | |
174 | die(_("no path specified")); | |
175 | } | |
176 | if (quiet) { | |
177 | if (argc > 1) | |
178 | die(_("--quiet is only valid with a single pathname")); | |
179 | if (verbose) | |
180 | die(_("cannot have both --quiet and --verbose")); | |
181 | } | |
182 | if (show_non_matching && !verbose) | |
183 | die(_("--non-matching is only valid with --verbose")); | |
184 | ||
185 | /* read_cache() is only necessary so we can watch out for submodules. */ | |
186 | if (!no_index && repo_read_index(the_repository) < 0) | |
187 | die(_("index file corrupt")); | |
188 | ||
189 | setup_standard_excludes(&dir); | |
190 | ||
191 | if (stdin_paths) { | |
192 | num_ignored = check_ignore_stdin_paths(&dir, prefix); | |
193 | } else { | |
194 | num_ignored = check_ignore(&dir, prefix, argc, argv); | |
195 | maybe_flush_or_die(stdout, "ignore to stdout"); | |
196 | } | |
197 | ||
198 | dir_clear(&dir); | |
199 | ||
200 | return !num_ignored; | |
201 | } |