]>
| Commit | Line | Data |
|---|---|---|
| 03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 368aa529 | 2 | #include "builtin.h" |
| b2141fc1 | 3 | #include "config.h" |
| 368aa529 | 4 | #include "dir.h" |
| 08b77586 | 5 | #include "environment.h" |
| f394e093 | 6 | #include "gettext.h" |
| 368aa529 AS |
7 | #include "quote.h" |
| 8 | #include "pathspec.h" | |
| 9 | #include "parse-options.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 | { |
| b567004b PS |
39 | const char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : ""; |
| 40 | const 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 | ||
| f59aa5e0 | 98 | die_path_inside_submodule(the_repository->index, &pathspec); |
| c08397e3 | 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 | */ | |
| f59aa5e0 | 105 | seen = find_pathspecs_matching_against_index(&pathspec, the_repository->index, |
| 719630eb | 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; |
| f59aa5e0 | 112 | pattern = last_matching_pattern(dir, the_repository->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 | ||
| 9b1cb507 JC |
155 | int cmd_check_ignore(int argc, |
| 156 | const char **argv, | |
| 157 | const char *prefix, | |
| 158 | struct repository *repo UNUSED) | |
| 368aa529 AS |
159 | { |
| 160 | int num_ignored; | |
| ce93a4c6 | 161 | struct dir_struct dir = DIR_INIT; |
| 368aa529 | 162 | |
| 9ce196e8 | 163 | repo_config(the_repository, git_default_config, NULL); |
| 368aa529 AS |
164 | |
| 165 | argc = parse_options(argc, argv, prefix, check_ignore_options, | |
| 166 | check_ignore_usage, 0); | |
| 167 | ||
| 168 | if (stdin_paths) { | |
| 169 | if (argc > 0) | |
| 170 | die(_("cannot specify pathnames with --stdin")); | |
| 171 | } else { | |
| 800531a8 | 172 | if (nul_term_line) |
| 368aa529 AS |
173 | die(_("-z only makes sense with --stdin")); |
| 174 | if (argc == 0) | |
| 175 | die(_("no path specified")); | |
| 176 | } | |
| 177 | if (quiet) { | |
| 178 | if (argc > 1) | |
| 179 | die(_("--quiet is only valid with a single pathname")); | |
| 180 | if (verbose) | |
| 181 | die(_("cannot have both --quiet and --verbose")); | |
| 182 | } | |
| ae3caf4c AS |
183 | if (show_non_matching && !verbose) |
| 184 | die(_("--non-matching is only valid with --verbose")); | |
| 368aa529 | 185 | |
| 0006d85c | 186 | /* read_cache() is only necessary so we can watch out for submodules. */ |
| 07047d68 | 187 | if (!no_index && repo_read_index(the_repository) < 0) |
| 0006d85c AS |
188 | die(_("index file corrupt")); |
| 189 | ||
| 0006d85c | 190 | setup_standard_excludes(&dir); |
| 368aa529 AS |
191 | |
| 192 | if (stdin_paths) { | |
| c51afbbd | 193 | num_ignored = check_ignore_stdin_paths(&dir, prefix); |
| 368aa529 | 194 | } else { |
| 931eab64 | 195 | num_ignored = check_ignore(&dir, prefix, argc, argv); |
| 368aa529 AS |
196 | maybe_flush_or_die(stdout, "ignore to stdout"); |
| 197 | } | |
| 198 | ||
| eceba532 | 199 | dir_clear(&dir); |
| 0006d85c | 200 | |
| 368aa529 AS |
201 | return !num_ignored; |
| 202 | } |