6 #include "parse-options.h"
8 static int quiet
, verbose
, stdin_paths
, show_non_matching
, no_index
;
9 static const char * const check_ignore_usage
[] = {
10 "git check-ignore [<options>] <pathname>...",
11 "git check-ignore [<options>] --stdin",
15 static int nul_term_line
;
17 static const struct option check_ignore_options
[] = {
18 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
19 OPT__VERBOSE(&verbose
, N_("be verbose")),
21 OPT_BOOL(0, "stdin", &stdin_paths
,
22 N_("read file names from stdin")),
23 OPT_BOOL('z', NULL
, &nul_term_line
,
24 N_("terminate input and output records by a NUL character")),
25 OPT_BOOL('n', "non-matching", &show_non_matching
,
26 N_("show non-matching input paths")),
27 OPT_BOOL(0, "no-index", &no_index
,
28 N_("ignore index when checking")),
32 static void output_exclude(const char *path
, struct exclude
*exclude
)
34 char *bang
= (exclude
&& exclude
->flags
& EXC_FLAG_NEGATIVE
) ? "!" : "";
35 char *slash
= (exclude
&& exclude
->flags
& EXC_FLAG_MUSTBEDIR
) ? "/" : "";
38 write_name_quoted(path
, stdout
, '\n');
41 quote_c_style(exclude
->el
->src
, NULL
, stdout
, 0);
42 printf(":%d:%s%s%s\t",
44 bang
, exclude
->pattern
, slash
);
49 quote_c_style(path
, NULL
, stdout
, 0);
54 printf("%s%c", path
, '\0');
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',
63 printf("%c%c%c%s%c", '\0', '\0', '\0', path
, '\0');
68 static int check_ignore(struct dir_struct
*dir
,
69 const char *prefix
, int argc
, const char **argv
)
71 const char *full_path
;
73 int num_ignored
= 0, dtype
= DT_UNKNOWN
, i
;
74 struct exclude
*exclude
;
75 struct pathspec pathspec
;
79 fprintf(stderr
, "no pathspec given.\n");
84 * check-ignore just needs paths. Magic beyond :/ is really
87 parse_pathspec(&pathspec
,
88 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
89 PATHSPEC_SYMLINK_LEADING_PATH
|
90 PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE
|
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.
99 seen
= find_pathspecs_matching_against_index(&pathspec
);
100 for (i
= 0; i
< pathspec
.nr
; i
++) {
101 full_path
= pathspec
.items
[i
].match
;
104 exclude
= last_exclude_matching(dir
, &the_index
,
107 if (!quiet
&& (exclude
|| show_non_matching
))
108 output_exclude(pathspec
.items
[i
].original
, exclude
);
117 static int check_ignore_stdin_paths(struct dir_struct
*dir
, const char *prefix
)
119 struct strbuf buf
= STRBUF_INIT
;
120 struct strbuf unquoted
= STRBUF_INIT
;
121 char *pathspec
[2] = { NULL
, NULL
};
122 strbuf_getline_fn getline_fn
;
125 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
126 while (getline_fn(&buf
, stdin
) != EOF
) {
127 if (!nul_term_line
&& buf
.buf
[0] == '"') {
128 strbuf_reset(&unquoted
);
129 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
130 die("line is badly quoted");
131 strbuf_swap(&buf
, &unquoted
);
133 pathspec
[0] = buf
.buf
;
134 num_ignored
+= check_ignore(dir
, prefix
,
135 1, (const char **)pathspec
);
136 maybe_flush_or_die(stdout
, "check-ignore to stdout");
138 strbuf_release(&buf
);
139 strbuf_release(&unquoted
);
143 int cmd_check_ignore(int argc
, const char **argv
, const char *prefix
)
146 struct dir_struct dir
;
148 git_config(git_default_config
, NULL
);
150 argc
= parse_options(argc
, argv
, prefix
, check_ignore_options
,
151 check_ignore_usage
, 0);
155 die(_("cannot specify pathnames with --stdin"));
158 die(_("-z only makes sense with --stdin"));
160 die(_("no path specified"));
164 die(_("--quiet is only valid with a single pathname"));
166 die(_("cannot have both --quiet and --verbose"));
168 if (show_non_matching
&& !verbose
)
169 die(_("--non-matching is only valid with --verbose"));
171 /* read_cache() is only necessary so we can watch out for submodules. */
172 if (!no_index
&& read_cache() < 0)
173 die(_("index file corrupt"));
175 memset(&dir
, 0, sizeof(dir
));
176 setup_standard_excludes(&dir
);
179 num_ignored
= check_ignore_stdin_paths(&dir
, prefix
);
181 num_ignored
= check_ignore(&dir
, prefix
, argc
, argv
);
182 maybe_flush_or_die(stdout
, "ignore to stdout");
185 clear_directory(&dir
);