Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 1 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 2 | #include "builtin.h" |
| 3 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 4 | #include "config.h" |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 5 | #include "dir.h" |
| 6 | #include "quote.h" |
| 7 | #include "pathspec.h" |
| 8 | #include "parse-options.h" |
Brandon Williams | c08397e | 2017-05-11 15:04:24 -0700 | [diff] [blame] | 9 | #include "submodule.h" |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 10 | |
Dave Williams | 8231fa6 | 2013-09-05 17:08:01 +0100 | [diff] [blame] | 11 | static int quiet, verbose, stdin_paths, show_non_matching, no_index; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 12 | static const char * const check_ignore_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 13 | "git check-ignore [<options>] <pathname>...", |
Junio C Hamano | 33e8fc8 | 2015-10-16 11:27:42 -0700 | [diff] [blame] | 14 | "git check-ignore [<options>] --stdin", |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 15 | NULL |
| 16 | }; |
| 17 | |
Junio C Hamano | 800531a | 2013-07-11 23:05:48 -0700 | [diff] [blame] | 18 | static int nul_term_line; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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(""), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 24 | OPT_BOOL(0, "stdin", &stdin_paths, |
| 25 | N_("read file names from stdin")), |
Junio C Hamano | a86a8b9 | 2013-09-04 12:39:02 -0700 | [diff] [blame] | 26 | OPT_BOOL('z', NULL, &nul_term_line, |
| 27 | N_("terminate input and output records by a NUL character")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 28 | OPT_BOOL('n', "non-matching", &show_non_matching, |
| 29 | N_("show non-matching input paths")), |
Dave Williams | 8231fa6 | 2013-09-05 17:08:01 +0100 | [diff] [blame] | 30 | OPT_BOOL(0, "no-index", &no_index, |
| 31 | N_("ignore index when checking")), |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 32 | OPT_END() |
| 33 | }; |
| 34 | |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 35 | static void output_pattern(const char *path, struct path_pattern *pattern) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 36 | { |
Derrick Stolee | 4ff89ee | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 37 | char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : ""; |
| 38 | char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : ""; |
Junio C Hamano | 800531a | 2013-07-11 23:05:48 -0700 | [diff] [blame] | 39 | if (!nul_term_line) { |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 40 | if (!verbose) { |
| 41 | write_name_quoted(path, stdout, '\n'); |
| 42 | } else { |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 43 | if (pattern) { |
Derrick Stolee | caa3d55 | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 44 | quote_c_style(pattern->pl->src, NULL, stdout, 0); |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 45 | printf(":%d:%s%s%s\t", |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 46 | pattern->srcpos, |
| 47 | bang, pattern->pattern, slash); |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 48 | } |
| 49 | else { |
| 50 | printf("::\t"); |
| 51 | } |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 { |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 59 | if (pattern) |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 60 | printf("%s%c%d%c%s%s%s%c%s%c", |
Derrick Stolee | caa3d55 | 2019-09-03 11:04:56 -0700 | [diff] [blame] | 61 | pattern->pl->src, '\0', |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 62 | pattern->srcpos, '\0', |
| 63 | bang, pattern->pattern, slash, '\0', |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 64 | path, '\0'); |
| 65 | else |
| 66 | printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0'); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 71 | static int check_ignore(struct dir_struct *dir, |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 72 | const char *prefix, int argc, const char **argv) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 73 | { |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 74 | const char *full_path; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 75 | char *seen; |
René Scharfe | d60771e | 2018-02-10 13:38:29 +0100 | [diff] [blame] | 76 | int num_ignored = 0, i; |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 77 | struct path_pattern *pattern; |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 78 | struct pathspec pathspec; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 79 | |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 80 | if (!argc) { |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 81 | if (!quiet) |
| 82 | fprintf(stderr, "no pathspec given.\n"); |
| 83 | return 0; |
| 84 | } |
| 85 | |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 86 | /* |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 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 | |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 93 | PATHSPEC_KEEP_ORDER, |
| 94 | prefix, argv); |
| 95 | |
Brandon Williams | c08397e | 2017-05-11 15:04:24 -0700 | [diff] [blame] | 96 | die_path_inside_submodule(&the_index, &pathspec); |
| 97 | |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 98 | /* |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 | */ |
Brandon Williams | 08de915 | 2017-05-11 15:04:27 -0700 | [diff] [blame] | 103 | seen = find_pathspecs_matching_against_index(&pathspec, &the_index); |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 104 | for (i = 0; i < pathspec.nr; i++) { |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 105 | full_path = pathspec.items[i].match; |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 106 | pattern = NULL; |
Junio C Hamano | c19387e | 2013-02-19 11:56:44 -0800 | [diff] [blame] | 107 | if (!seen[i]) { |
René Scharfe | d60771e | 2018-02-10 13:38:29 +0100 | [diff] [blame] | 108 | int dtype = DT_UNKNOWN; |
Derrick Stolee | 65edd96 | 2019-09-03 11:04:57 -0700 | [diff] [blame] | 109 | pattern = last_matching_pattern(dir, &the_index, |
Brandon Williams | a0bba65 | 2017-05-05 12:53:30 -0700 | [diff] [blame] | 110 | full_path, &dtype); |
Elijah Newren | 7ec8125 | 2020-02-18 23:05:37 +0000 | [diff] [blame] | 111 | if (!verbose && pattern && |
| 112 | pattern->flags & PATTERN_FLAG_NEGATIVE) |
| 113 | pattern = NULL; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 114 | } |
Derrick Stolee | ab8db61 | 2019-09-03 11:04:55 -0700 | [diff] [blame] | 115 | if (!quiet && (pattern || show_non_matching)) |
| 116 | output_pattern(pathspec.items[i].original, pattern); |
| 117 | if (pattern) |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 118 | num_ignored++; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 119 | } |
| 120 | free(seen); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 121 | |
| 122 | return num_ignored; |
| 123 | } |
| 124 | |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 125 | static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 126 | { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 127 | struct strbuf buf = STRBUF_INIT; |
| 128 | struct strbuf unquoted = STRBUF_INIT; |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 129 | char *pathspec[2] = { NULL, NULL }; |
Junio C Hamano | dca9003 | 2016-01-14 13:31:17 -0800 | [diff] [blame] | 130 | strbuf_getline_fn getline_fn; |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 131 | int num_ignored = 0; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 132 | |
Junio C Hamano | dca9003 | 2016-01-14 13:31:17 -0800 | [diff] [blame] | 133 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
Junio C Hamano | dca9003 | 2016-01-14 13:31:17 -0800 | [diff] [blame] | 134 | while (getline_fn(&buf, stdin) != EOF) { |
| 135 | if (!nul_term_line && buf.buf[0] == '"') { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 136 | strbuf_reset(&unquoted); |
| 137 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 138 | die("line is badly quoted"); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 139 | strbuf_swap(&buf, &unquoted); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 140 | } |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 141 | pathspec[0] = buf.buf; |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 142 | num_ignored += check_ignore(dir, prefix, |
| 143 | 1, (const char **)pathspec); |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 144 | maybe_flush_or_die(stdout, "check-ignore to stdout"); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 145 | } |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 146 | strbuf_release(&buf); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 147 | strbuf_release(&unquoted); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 148 | return num_ignored; |
| 149 | } |
| 150 | |
| 151 | int cmd_check_ignore(int argc, const char **argv, const char *prefix) |
| 152 | { |
| 153 | int num_ignored; |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 154 | struct dir_struct dir; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 155 | |
| 156 | git_config(git_default_config, NULL); |
| 157 | |
| 158 | argc = parse_options(argc, argv, prefix, check_ignore_options, |
| 159 | check_ignore_usage, 0); |
| 160 | |
| 161 | if (stdin_paths) { |
| 162 | if (argc > 0) |
| 163 | die(_("cannot specify pathnames with --stdin")); |
| 164 | } else { |
Junio C Hamano | 800531a | 2013-07-11 23:05:48 -0700 | [diff] [blame] | 165 | if (nul_term_line) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 166 | die(_("-z only makes sense with --stdin")); |
| 167 | if (argc == 0) |
| 168 | die(_("no path specified")); |
| 169 | } |
| 170 | if (quiet) { |
| 171 | if (argc > 1) |
| 172 | die(_("--quiet is only valid with a single pathname")); |
| 173 | if (verbose) |
| 174 | die(_("cannot have both --quiet and --verbose")); |
| 175 | } |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 176 | if (show_non_matching && !verbose) |
| 177 | die(_("--non-matching is only valid with --verbose")); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 178 | |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 179 | /* read_cache() is only necessary so we can watch out for submodules. */ |
Dave Williams | 8231fa6 | 2013-09-05 17:08:01 +0100 | [diff] [blame] | 180 | if (!no_index && read_cache() < 0) |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 181 | die(_("index file corrupt")); |
| 182 | |
Elijah Newren | eceba53 | 2020-08-18 22:58:26 +0000 | [diff] [blame] | 183 | dir_init(&dir); |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 184 | setup_standard_excludes(&dir); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 185 | |
| 186 | if (stdin_paths) { |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 187 | num_ignored = check_ignore_stdin_paths(&dir, prefix); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 188 | } else { |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 189 | num_ignored = check_ignore(&dir, prefix, argc, argv); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 190 | maybe_flush_or_die(stdout, "ignore to stdout"); |
| 191 | } |
| 192 | |
Elijah Newren | eceba53 | 2020-08-18 22:58:26 +0000 | [diff] [blame] | 193 | dir_clear(&dir); |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 194 | |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 195 | return !num_ignored; |
| 196 | } |