Merge branch 'ak/restore-both-incompatible-with-conflicts'
[git/debian.git] / builtin / check-ignore.c
blobab776061c7c4f2e1f53c3408a323db3dac6bc440
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "cache.h"
4 #include "config.h"
5 #include "dir.h"
6 #include "quote.h"
7 #include "pathspec.h"
8 #include "parse-options.h"
9 #include "submodule.h"
11 static int quiet, verbose, stdin_paths, show_non_matching, no_index;
12 static const char * const check_ignore_usage[] = {
13 "git check-ignore [<options>] <pathname>...",
14 "git check-ignore [<options>] --stdin",
15 NULL
18 static int nul_term_line;
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(""),
24 OPT_BOOL(0, "stdin", &stdin_paths,
25 N_("read file names from stdin")),
26 OPT_BOOL('z', NULL, &nul_term_line,
27 N_("terminate input and output records by a NUL character")),
28 OPT_BOOL('n', "non-matching", &show_non_matching,
29 N_("show non-matching input paths")),
30 OPT_BOOL(0, "no-index", &no_index,
31 N_("ignore index when checking")),
32 OPT_END()
35 static void output_pattern(const char *path, struct path_pattern *pattern)
37 char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
38 char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : "";
39 if (!nul_term_line) {
40 if (!verbose) {
41 write_name_quoted(path, stdout, '\n');
42 } else {
43 if (pattern) {
44 quote_c_style(pattern->pl->src, NULL, stdout, 0);
45 printf(":%d:%s%s%s\t",
46 pattern->srcpos,
47 bang, pattern->pattern, slash);
49 else {
50 printf("::\t");
52 quote_c_style(path, NULL, stdout, 0);
53 fputc('\n', stdout);
55 } else {
56 if (!verbose) {
57 printf("%s%c", path, '\0');
58 } else {
59 if (pattern)
60 printf("%s%c%d%c%s%s%s%c%s%c",
61 pattern->pl->src, '\0',
62 pattern->srcpos, '\0',
63 bang, pattern->pattern, slash, '\0',
64 path, '\0');
65 else
66 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
71 static int check_ignore(struct dir_struct *dir,
72 const char *prefix, int argc, const char **argv)
74 const char *full_path;
75 char *seen;
76 int num_ignored = 0, i;
77 struct path_pattern *pattern;
78 struct pathspec pathspec;
80 if (!argc) {
81 if (!quiet)
82 fprintf(stderr, "no pathspec given.\n");
83 return 0;
87 * check-ignore just needs paths. Magic beyond :/ is really
88 * irrelevant.
90 parse_pathspec(&pathspec,
91 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
92 PATHSPEC_SYMLINK_LEADING_PATH |
93 PATHSPEC_KEEP_ORDER,
94 prefix, argv);
96 die_path_inside_submodule(&the_index, &pathspec);
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.
103 seen = find_pathspecs_matching_against_index(&pathspec, &the_index,
104 PS_HEED_SKIP_WORKTREE);
105 for (i = 0; i < pathspec.nr; i++) {
106 full_path = pathspec.items[i].match;
107 pattern = NULL;
108 if (!seen[i]) {
109 int dtype = DT_UNKNOWN;
110 pattern = last_matching_pattern(dir, &the_index,
111 full_path, &dtype);
112 if (!verbose && pattern &&
113 pattern->flags & PATTERN_FLAG_NEGATIVE)
114 pattern = NULL;
116 if (!quiet && (pattern || show_non_matching))
117 output_pattern(pathspec.items[i].original, pattern);
118 if (pattern)
119 num_ignored++;
121 free(seen);
122 clear_pathspec(&pathspec);
124 return num_ignored;
127 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
129 struct strbuf buf = STRBUF_INIT;
130 struct strbuf unquoted = STRBUF_INIT;
131 char *pathspec[2] = { NULL, NULL };
132 strbuf_getline_fn getline_fn;
133 int num_ignored = 0;
135 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
136 while (getline_fn(&buf, stdin) != EOF) {
137 if (!nul_term_line && buf.buf[0] == '"') {
138 strbuf_reset(&unquoted);
139 if (unquote_c_style(&unquoted, buf.buf, NULL))
140 die("line is badly quoted");
141 strbuf_swap(&buf, &unquoted);
143 pathspec[0] = buf.buf;
144 num_ignored += check_ignore(dir, prefix,
145 1, (const char **)pathspec);
146 maybe_flush_or_die(stdout, "check-ignore to stdout");
148 strbuf_release(&buf);
149 strbuf_release(&unquoted);
150 return num_ignored;
153 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
155 int num_ignored;
156 struct dir_struct dir = DIR_INIT;
158 git_config(git_default_config, NULL);
160 argc = parse_options(argc, argv, prefix, check_ignore_options,
161 check_ignore_usage, 0);
163 if (stdin_paths) {
164 if (argc > 0)
165 die(_("cannot specify pathnames with --stdin"));
166 } else {
167 if (nul_term_line)
168 die(_("-z only makes sense with --stdin"));
169 if (argc == 0)
170 die(_("no path specified"));
172 if (quiet) {
173 if (argc > 1)
174 die(_("--quiet is only valid with a single pathname"));
175 if (verbose)
176 die(_("cannot have both --quiet and --verbose"));
178 if (show_non_matching && !verbose)
179 die(_("--non-matching is only valid with --verbose"));
181 /* read_cache() is only necessary so we can watch out for submodules. */
182 if (!no_index && repo_read_index(the_repository) < 0)
183 die(_("index file corrupt"));
185 setup_standard_excludes(&dir);
187 if (stdin_paths) {
188 num_ignored = check_ignore_stdin_paths(&dir, prefix);
189 } else {
190 num_ignored = check_ignore(&dir, prefix, argc, argv);
191 maybe_flush_or_die(stdout, "ignore to stdout");
194 dir_clear(&dir);
196 return !num_ignored;