7 #include "parse-options.h"
10 static int quiet
, verbose
, stdin_paths
, show_non_matching
, no_index
;
11 static const char * const check_ignore_usage
[] = {
12 "git check-ignore [<options>] <pathname>...",
13 "git check-ignore [<options>] --stdin",
17 static int nul_term_line
;
19 static const struct option check_ignore_options
[] = {
20 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
21 OPT__VERBOSE(&verbose
, N_("be verbose")),
23 OPT_BOOL(0, "stdin", &stdin_paths
,
24 N_("read file names from stdin")),
25 OPT_BOOL('z', NULL
, &nul_term_line
,
26 N_("terminate input and output records by a NUL character")),
27 OPT_BOOL('n', "non-matching", &show_non_matching
,
28 N_("show non-matching input paths")),
29 OPT_BOOL(0, "no-index", &no_index
,
30 N_("ignore index when checking")),
34 static void output_exclude(const char *path
, struct exclude
*exclude
)
36 char *bang
= (exclude
&& exclude
->flags
& EXC_FLAG_NEGATIVE
) ? "!" : "";
37 char *slash
= (exclude
&& exclude
->flags
& EXC_FLAG_MUSTBEDIR
) ? "/" : "";
40 write_name_quoted(path
, stdout
, '\n');
43 quote_c_style(exclude
->el
->src
, NULL
, stdout
, 0);
44 printf(":%d:%s%s%s\t",
46 bang
, exclude
->pattern
, slash
);
51 quote_c_style(path
, NULL
, stdout
, 0);
56 printf("%s%c", path
, '\0');
59 printf("%s%c%d%c%s%s%s%c%s%c",
60 exclude
->el
->src
, '\0',
61 exclude
->srcpos
, '\0',
62 bang
, exclude
->pattern
, slash
, '\0',
65 printf("%c%c%c%s%c", '\0', '\0', '\0', path
, '\0');
70 static int check_ignore(struct dir_struct
*dir
,
71 const char *prefix
, int argc
, const char **argv
)
73 const char *full_path
;
75 int num_ignored
= 0, i
;
76 struct exclude
*exclude
;
77 struct pathspec pathspec
;
81 fprintf(stderr
, "no pathspec given.\n");
86 * check-ignore just needs paths. Magic beyond :/ is really
89 parse_pathspec(&pathspec
,
90 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
91 PATHSPEC_SYMLINK_LEADING_PATH
|
95 die_path_inside_submodule(&the_index
, &pathspec
);
98 * look for pathspecs matching entries in the index, since these
99 * should not be ignored, in order to be consistent with
100 * 'git status', 'git add' etc.
102 seen
= find_pathspecs_matching_against_index(&pathspec
, &the_index
);
103 for (i
= 0; i
< pathspec
.nr
; i
++) {
104 full_path
= pathspec
.items
[i
].match
;
107 int dtype
= DT_UNKNOWN
;
108 exclude
= last_exclude_matching(dir
, &the_index
,
111 if (!quiet
&& (exclude
|| show_non_matching
))
112 output_exclude(pathspec
.items
[i
].original
, exclude
);
121 static int check_ignore_stdin_paths(struct dir_struct
*dir
, const char *prefix
)
123 struct strbuf buf
= STRBUF_INIT
;
124 struct strbuf unquoted
= STRBUF_INIT
;
125 char *pathspec
[2] = { NULL
, NULL
};
126 strbuf_getline_fn getline_fn
;
129 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
130 while (getline_fn(&buf
, stdin
) != EOF
) {
131 if (!nul_term_line
&& buf
.buf
[0] == '"') {
132 strbuf_reset(&unquoted
);
133 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
134 die("line is badly quoted");
135 strbuf_swap(&buf
, &unquoted
);
137 pathspec
[0] = buf
.buf
;
138 num_ignored
+= check_ignore(dir
, prefix
,
139 1, (const char **)pathspec
);
140 maybe_flush_or_die(stdout
, "check-ignore to stdout");
142 strbuf_release(&buf
);
143 strbuf_release(&unquoted
);
147 int cmd_check_ignore(int argc
, const char **argv
, const char *prefix
)
150 struct dir_struct dir
;
152 git_config(git_default_config
, NULL
);
154 argc
= parse_options(argc
, argv
, prefix
, check_ignore_options
,
155 check_ignore_usage
, 0);
159 die(_("cannot specify pathnames with --stdin"));
162 die(_("-z only makes sense with --stdin"));
164 die(_("no path specified"));
168 die(_("--quiet is only valid with a single pathname"));
170 die(_("cannot have both --quiet and --verbose"));
172 if (show_non_matching
&& !verbose
)
173 die(_("--non-matching is only valid with --verbose"));
175 /* read_cache() is only necessary so we can watch out for submodules. */
176 if (!no_index
&& read_cache() < 0)
177 die(_("index file corrupt"));
179 memset(&dir
, 0, sizeof(dir
));
180 setup_standard_excludes(&dir
);
183 num_ignored
= check_ignore_stdin_paths(&dir
, prefix
);
185 num_ignored
= check_ignore(&dir
, prefix
, argc
, argv
);
186 maybe_flush_or_die(stdout
, "ignore to stdout");
189 clear_directory(&dir
);