Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 | |
Dave Williams | 8231fa6 | 2013-09-05 17:08:01 +0100 | [diff] [blame] | 8 | static int quiet, verbose, stdin_paths, show_non_matching, no_index; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 9 | static const char * const check_ignore_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 10 | "git check-ignore [<options>] <pathname>...", |
Junio C Hamano | 33e8fc8 | 2015-10-16 11:27:42 -0700 | [diff] [blame] | 11 | "git check-ignore [<options>] --stdin", |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 12 | NULL |
| 13 | }; |
| 14 | |
Junio C Hamano | 800531a | 2013-07-11 23:05:48 -0700 | [diff] [blame] | 15 | static int nul_term_line; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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(""), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 21 | OPT_BOOL(0, "stdin", &stdin_paths, |
| 22 | N_("read file names from stdin")), |
Junio C Hamano | a86a8b9 | 2013-09-04 12:39:02 -0700 | [diff] [blame] | 23 | OPT_BOOL('z', NULL, &nul_term_line, |
| 24 | N_("terminate input and output records by a NUL character")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 25 | OPT_BOOL('n', "non-matching", &show_non_matching, |
| 26 | N_("show non-matching input paths")), |
Dave Williams | 8231fa6 | 2013-09-05 17:08:01 +0100 | [diff] [blame] | 27 | OPT_BOOL(0, "no-index", &no_index, |
| 28 | N_("ignore index when checking")), |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 29 | OPT_END() |
| 30 | }; |
| 31 | |
| 32 | static void output_exclude(const char *path, struct exclude *exclude) |
| 33 | { |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 34 | char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : ""; |
| 35 | char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : ""; |
Junio C Hamano | 800531a | 2013-07-11 23:05:48 -0700 | [diff] [blame] | 36 | if (!nul_term_line) { |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 37 | if (!verbose) { |
| 38 | write_name_quoted(path, stdout, '\n'); |
| 39 | } else { |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 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 | } |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 { |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 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'); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 68 | 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] | 69 | const char *prefix, int argc, const char **argv) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 70 | { |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 71 | const char *full_path; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 72 | char *seen; |
| 73 | int num_ignored = 0, dtype = DT_UNKNOWN, i; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 74 | struct exclude *exclude; |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 75 | struct pathspec pathspec; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 76 | |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 77 | if (!argc) { |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 78 | if (!quiet) |
| 79 | fprintf(stderr, "no pathspec given.\n"); |
| 80 | return 0; |
| 81 | } |
| 82 | |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 83 | /* |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 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 | |
| 94 | /* |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 | */ |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 99 | seen = find_pathspecs_matching_against_index(&pathspec); |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 100 | for (i = 0; i < pathspec.nr; i++) { |
Nguyễn Thái Ngọc Duy | 84b8b5d | 2013-07-14 15:36:00 +0700 | [diff] [blame] | 101 | full_path = pathspec.items[i].match; |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 102 | exclude = NULL; |
Junio C Hamano | c19387e | 2013-02-19 11:56:44 -0800 | [diff] [blame] | 103 | if (!seen[i]) { |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 104 | exclude = last_exclude_matching(dir, full_path, &dtype); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 105 | } |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 106 | if (!quiet && (exclude || show_non_matching)) |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 107 | output_exclude(pathspec.items[i].original, exclude); |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 108 | if (exclude) |
| 109 | num_ignored++; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 110 | } |
| 111 | free(seen); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 112 | |
| 113 | return num_ignored; |
| 114 | } |
| 115 | |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 116 | 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] | 117 | { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 118 | struct strbuf buf = STRBUF_INIT; |
| 119 | struct strbuf unquoted = STRBUF_INIT; |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 120 | char *pathspec[2] = { NULL, NULL }; |
Junio C Hamano | dca9003 | 2016-01-14 13:31:17 -0800 | [diff] [blame] | 121 | strbuf_getline_fn getline_fn; |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 122 | int num_ignored = 0; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 123 | |
Junio C Hamano | dca9003 | 2016-01-14 13:31:17 -0800 | [diff] [blame] | 124 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
Junio C Hamano | dca9003 | 2016-01-14 13:31:17 -0800 | [diff] [blame] | 125 | while (getline_fn(&buf, stdin) != EOF) { |
| 126 | if (!nul_term_line && buf.buf[0] == '"') { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 127 | strbuf_reset(&unquoted); |
| 128 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 129 | die("line is badly quoted"); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 130 | strbuf_swap(&buf, &unquoted); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 131 | } |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 132 | pathspec[0] = buf.buf; |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 133 | num_ignored += check_ignore(dir, prefix, |
| 134 | 1, (const char **)pathspec); |
Adam Spiers | 0c8e8c0 | 2013-04-11 13:05:12 +0100 | [diff] [blame] | 135 | maybe_flush_or_die(stdout, "check-ignore to stdout"); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 136 | } |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 137 | strbuf_release(&buf); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 138 | strbuf_release(&unquoted); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 139 | return num_ignored; |
| 140 | } |
| 141 | |
| 142 | int cmd_check_ignore(int argc, const char **argv, const char *prefix) |
| 143 | { |
| 144 | int num_ignored; |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 145 | struct dir_struct dir; |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 { |
Junio C Hamano | 800531a | 2013-07-11 23:05:48 -0700 | [diff] [blame] | 156 | if (nul_term_line) |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 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 | } |
Adam Spiers | ae3caf4 | 2013-04-11 13:05:10 +0100 | [diff] [blame] | 167 | if (show_non_matching && !verbose) |
| 168 | die(_("--non-matching is only valid with --verbose")); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 169 | |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 170 | /* read_cache() is only necessary so we can watch out for submodules. */ |
Dave Williams | 8231fa6 | 2013-09-05 17:08:01 +0100 | [diff] [blame] | 171 | if (!no_index && read_cache() < 0) |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 172 | die(_("index file corrupt")); |
| 173 | |
| 174 | memset(&dir, 0, sizeof(dir)); |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 175 | setup_standard_excludes(&dir); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 176 | |
| 177 | if (stdin_paths) { |
Junio C Hamano | c51afbb | 2013-05-29 14:23:39 -0700 | [diff] [blame] | 178 | num_ignored = check_ignore_stdin_paths(&dir, prefix); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 179 | } else { |
Nguyễn Thái Ngọc Duy | 931eab6 | 2013-07-14 15:35:45 +0700 | [diff] [blame] | 180 | num_ignored = check_ignore(&dir, prefix, argc, argv); |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 181 | maybe_flush_or_die(stdout, "ignore to stdout"); |
| 182 | } |
| 183 | |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 184 | clear_directory(&dir); |
Adam Spiers | 0006d85 | 2013-04-11 13:05:11 +0100 | [diff] [blame] | 185 | |
Adam Spiers | 368aa52 | 2013-01-06 16:58:13 +0000 | [diff] [blame] | 186 | return !num_ignored; |
| 187 | } |