]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/check-ignore.c
Merge branch 'js/empty-index-fixes'
[thirdparty/git.git] / builtin / check-ignore.c
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "gettext.h"
6 #include "quote.h"
7 #include "pathspec.h"
8 #include "parse-options.h"
9 #include "repository.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 [<options>] <pathname>...",
16 "git check-ignore [<options>] --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 char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
40 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_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_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_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, const char **argv, const char *prefix)
156 {
157 int num_ignored;
158 struct dir_struct dir = DIR_INIT;
159
160 git_config(git_default_config, NULL);
161
162 argc = parse_options(argc, argv, prefix, check_ignore_options,
163 check_ignore_usage, 0);
164
165 if (stdin_paths) {
166 if (argc > 0)
167 die(_("cannot specify pathnames with --stdin"));
168 } else {
169 if (nul_term_line)
170 die(_("-z only makes sense with --stdin"));
171 if (argc == 0)
172 die(_("no path specified"));
173 }
174 if (quiet) {
175 if (argc > 1)
176 die(_("--quiet is only valid with a single pathname"));
177 if (verbose)
178 die(_("cannot have both --quiet and --verbose"));
179 }
180 if (show_non_matching && !verbose)
181 die(_("--non-matching is only valid with --verbose"));
182
183 /* read_cache() is only necessary so we can watch out for submodules. */
184 if (!no_index && repo_read_index(the_repository) < 0)
185 die(_("index file corrupt"));
186
187 setup_standard_excludes(&dir);
188
189 if (stdin_paths) {
190 num_ignored = check_ignore_stdin_paths(&dir, prefix);
191 } else {
192 num_ignored = check_ignore(&dir, prefix, argc, argv);
193 maybe_flush_or_die(stdout, "ignore to stdout");
194 }
195
196 dir_clear(&dir);
197
198 return !num_ignored;
199 }