Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/check-ignore.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / check-ignore.c` 202 lines ยท 5463 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #include "builtin.h" 3 #include "config.h" 4 #include "dir.h" 5 #include "environment.h" 6 #include "gettext.h" 7 #include "quote.h" 8 #include "pathspec.h" 9 #include "parse-options.h" 10 #include "submodule.h" 11 #include "write-or-die.h" 12 13 static int quiet, verbose, stdin_paths, show_non_matching, no_index; 14 static const char * const check_ignore_usage[] = { 15 "git check-ignore [] ...", 16 "git check-ignore [] --stdin", 17 NULL 18 }; 19 20 static int nul_term_line; 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(""), 26 OPT_BOOL(0, "stdin", &stdin_paths, 27 N_("read file names from stdin")), 28 OPT_BOOL('z', NULL, &nul_term_line, 29 N_("terminate input and output records by a NUL character")), 30 OPT_BOOL('n', "non-matching", &show_non_matching, 31 N_("show non-matching input paths")), 32 OPT_BOOL(0, "no-index", &no_index, 33 N_("ignore index when checking")), 34 OPT_END() 35 }; 36 37 static void output_pattern(const char *path, struct path_pattern *pattern) 38 { 39 const char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : ""; 40 const char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : ""; 41 if (!nul_term_line) { 42 if (!verbose) { 43 write_name_quoted(path, stdout, '\n'); 44 } else { 45 if (pattern) { 46 quote_c_style(pattern->pl->src, NULL, stdout, 0); 47 printf(":%d:%s%s%s\t", 48 pattern->srcpos, 49 bang, pattern->pattern, slash); 50 } 51 else { 52 printf("::\t"); 53 } 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 { 61 if (pattern) 62 printf("%s%c%d%c%s%s%s%c%s%c", 63 pattern->pl->src, '\0', 64 pattern->srcpos, '\0', 65 bang, pattern->pattern, slash, '\0', 66 path, '\0'); 67 else 68 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0'); 69 } 70 } 71 } 72 73 static int check_ignore(struct dir_struct *dir, 74 const char *prefix, int argc, const char **argv) 75 { 76 const char *full_path; 77 char *seen; 78 int num_ignored = 0, i; 79 struct path_pattern *pattern; 80 struct pathspec pathspec; 81 82 if (!argc) { 83 if (!quiet) 84 fprintf(stderr, "no pathspec given.\n"); 85 return 0; 86 } 87 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 | 95 PATHSPEC_KEEP_ORDER, 96 prefix, argv); 97 98 die_path_inside_submodule(the_repository->index, &pathspec); 99 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 */ 105 seen = find_pathspecs_matching_against_index(&pathspec, the_repository->index, 106 PS_HEED_SKIP_WORKTREE); 107 for (i = 0; i < pathspec.nr; i++) { 108 full_path = pathspec.items[i].match; 109 pattern = NULL; 110 if (!seen[i]) { 111 int dtype = DT_UNKNOWN; 112 pattern = last_matching_pattern(dir, the_repository->index, 113 full_path, &dtype); 114 if (!verbose && pattern && 115 pattern->flags & PATTERN_FLAG_NEGATIVE) 116 pattern = NULL; 117 } 118 if (!quiet && (pattern || show_non_matching)) 119 output_pattern(pathspec.items[i].original, pattern); 120 if (pattern) 121 num_ignored++; 122 } 123 free(seen); 124 clear_pathspec(&pathspec); 125 126 return num_ignored; 127 } 128 129 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix) 130 { 131 struct strbuf buf = STRBUF_INIT; 132 struct strbuf unquoted = STRBUF_INIT; 133 char *pathspec[2] = { NULL, NULL }; 134 strbuf_getline_fn getline_fn; 135 int num_ignored = 0; 136 137 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; 138 while (getline_fn(&buf, stdin) != EOF) { 139 if (!nul_term_line && buf.buf[0] == '"') { 140 strbuf_reset(&unquoted); 141 if (unquote_c_style(&unquoted, buf.buf, NULL)) 142 die("line is badly quoted"); 143 strbuf_swap(&buf, &unquoted); 144 } 145 pathspec[0] = buf.buf; 146 num_ignored += check_ignore(dir, prefix, 147 1, (const char **)pathspec); 148 maybe_flush_or_die(stdout, "check-ignore to stdout"); 149 } 150 strbuf_release(&buf); 151 strbuf_release(&unquoted); 152 return num_ignored; 153 } 154 155 int cmd_check_ignore(int argc, 156 const char **argv, 157 const char *prefix, 158 struct repository *repo UNUSED) 159 { 160 int num_ignored; 161 struct dir_struct dir = DIR_INIT; 162 163 repo_config(the_repository, git_default_config, NULL); 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 { 172 if (nul_term_line) 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 } 183 if (show_non_matching && !verbose) 184 die(_("--non-matching is only valid with --verbose")); 185 186 /* read_cache() is only necessary so we can watch out for submodules. */ 187 if (!no_index && repo_read_index(the_repository) < 0) 188 die(_("index file corrupt")); 189 190 setup_standard_excludes(&dir); 191 192 if (stdin_paths) { 193 num_ignored = check_ignore_stdin_paths(&dir, prefix); 194 } else { 195 num_ignored = check_ignore(&dir, prefix, argc, argv); 196 maybe_flush_or_die(stdout, "ignore to stdout"); 197 } 198 199 dir_clear(&dir); 200 201 return !num_ignored; 202 }