1 #define USE_THE_INDEX_VARIABLE
8 #include "parse-options.h"
9 #include "repository.h"
10 #include "submodule.h"
11 #include "write-or-die.h"
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",
20 static int nul_term_line
;
22 static const struct option check_ignore_options
[] = {
23 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
24 OPT__VERBOSE(&verbose
, N_("be verbose")),
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")),
37 static void output_pattern(const char *path
, struct path_pattern
*pattern
)
39 char *bang
= (pattern
&& pattern
->flags
& PATTERN_FLAG_NEGATIVE
) ? "!" : "";
40 char *slash
= (pattern
&& pattern
->flags
& PATTERN_FLAG_MUSTBEDIR
) ? "/" : "";
43 write_name_quoted(path
, stdout
, '\n');
46 quote_c_style(pattern
->pl
->src
, NULL
, stdout
, 0);
47 printf(":%d:%s%s%s\t",
49 bang
, pattern
->pattern
, slash
);
54 quote_c_style(path
, NULL
, stdout
, 0);
59 printf("%s%c", path
, '\0');
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',
68 printf("%c%c%c%s%c", '\0', '\0', '\0', path
, '\0');
73 static int check_ignore(struct dir_struct
*dir
,
74 const char *prefix
, int argc
, const char **argv
)
76 const char *full_path
;
78 int num_ignored
= 0, i
;
79 struct path_pattern
*pattern
;
80 struct pathspec pathspec
;
84 fprintf(stderr
, "no pathspec given.\n");
89 * check-ignore just needs paths. Magic beyond :/ is really
92 parse_pathspec(&pathspec
,
93 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
94 PATHSPEC_SYMLINK_LEADING_PATH
|
98 die_path_inside_submodule(&the_index
, &pathspec
);
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.
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
;
111 int dtype
= DT_UNKNOWN
;
112 pattern
= last_matching_pattern(dir
, &the_index
,
114 if (!verbose
&& pattern
&&
115 pattern
->flags
& PATTERN_FLAG_NEGATIVE
)
118 if (!quiet
&& (pattern
|| show_non_matching
))
119 output_pattern(pathspec
.items
[i
].original
, pattern
);
124 clear_pathspec(&pathspec
);
129 static int check_ignore_stdin_paths(struct dir_struct
*dir
, const char *prefix
)
131 struct strbuf buf
= STRBUF_INIT
;
132 struct strbuf unquoted
= STRBUF_INIT
;
133 char *pathspec
[2] = { NULL
, NULL
};
134 strbuf_getline_fn getline_fn
;
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
);
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");
150 strbuf_release(&buf
);
151 strbuf_release(&unquoted
);
155 int cmd_check_ignore(int argc
, const char **argv
, const char *prefix
)
158 struct dir_struct dir
= DIR_INIT
;
160 git_config(git_default_config
, NULL
);
162 argc
= parse_options(argc
, argv
, prefix
, check_ignore_options
,
163 check_ignore_usage
, 0);
167 die(_("cannot specify pathnames with --stdin"));
170 die(_("-z only makes sense with --stdin"));
172 die(_("no path specified"));
176 die(_("--quiet is only valid with a single pathname"));
178 die(_("cannot have both --quiet and --verbose"));
180 if (show_non_matching
&& !verbose
)
181 die(_("--non-matching is only valid with --verbose"));
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"));
187 setup_standard_excludes(&dir
);
190 num_ignored
= check_ignore_stdin_paths(&dir
, prefix
);
192 num_ignored
= check_ignore(&dir
, prefix
, argc
, argv
);
193 maybe_flush_or_die(stdout
, "ignore to stdout");