7 #include "parse-options.h"
8 #include "repository.h"
10 #include "write-or-die.h"
12 static int quiet
, verbose
, stdin_paths
, show_non_matching
, no_index
;
13 static const char * const check_ignore_usage
[] = {
14 "git check-ignore [<options>] <pathname>...",
15 "git check-ignore [<options>] --stdin",
19 static int nul_term_line
;
21 static const struct option check_ignore_options
[] = {
22 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
23 OPT__VERBOSE(&verbose
, N_("be verbose")),
25 OPT_BOOL(0, "stdin", &stdin_paths
,
26 N_("read file names from stdin")),
27 OPT_BOOL('z', NULL
, &nul_term_line
,
28 N_("terminate input and output records by a NUL character")),
29 OPT_BOOL('n', "non-matching", &show_non_matching
,
30 N_("show non-matching input paths")),
31 OPT_BOOL(0, "no-index", &no_index
,
32 N_("ignore index when checking")),
36 static void output_pattern(const char *path
, struct path_pattern
*pattern
)
38 const char *bang
= (pattern
&& pattern
->flags
& PATTERN_FLAG_NEGATIVE
) ? "!" : "";
39 const char *slash
= (pattern
&& pattern
->flags
& PATTERN_FLAG_MUSTBEDIR
) ? "/" : "";
42 write_name_quoted(path
, stdout
, '\n');
45 quote_c_style(pattern
->pl
->src
, NULL
, stdout
, 0);
46 printf(":%d:%s%s%s\t",
48 bang
, pattern
->pattern
, slash
);
53 quote_c_style(path
, NULL
, stdout
, 0);
58 printf("%s%c", path
, '\0');
61 printf("%s%c%d%c%s%s%s%c%s%c",
62 pattern
->pl
->src
, '\0',
63 pattern
->srcpos
, '\0',
64 bang
, pattern
->pattern
, slash
, '\0',
67 printf("%c%c%c%s%c", '\0', '\0', '\0', path
, '\0');
72 static int check_ignore(struct dir_struct
*dir
,
73 const char *prefix
, int argc
, const char **argv
)
75 const char *full_path
;
77 int num_ignored
= 0, i
;
78 struct path_pattern
*pattern
;
79 struct pathspec pathspec
;
83 fprintf(stderr
, "no pathspec given.\n");
88 * check-ignore just needs paths. Magic beyond :/ is really
91 parse_pathspec(&pathspec
,
92 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
93 PATHSPEC_SYMLINK_LEADING_PATH
|
97 die_path_inside_submodule(the_repository
->index
, &pathspec
);
100 * look for pathspecs matching entries in the index, since these
101 * should not be ignored, in order to be consistent with
102 * 'git status', 'git add' etc.
104 seen
= find_pathspecs_matching_against_index(&pathspec
, the_repository
->index
,
105 PS_HEED_SKIP_WORKTREE
);
106 for (i
= 0; i
< pathspec
.nr
; i
++) {
107 full_path
= pathspec
.items
[i
].match
;
110 int dtype
= DT_UNKNOWN
;
111 pattern
= last_matching_pattern(dir
, the_repository
->index
,
113 if (!verbose
&& pattern
&&
114 pattern
->flags
& PATTERN_FLAG_NEGATIVE
)
117 if (!quiet
&& (pattern
|| show_non_matching
))
118 output_pattern(pathspec
.items
[i
].original
, pattern
);
123 clear_pathspec(&pathspec
);
128 static int check_ignore_stdin_paths(struct dir_struct
*dir
, const char *prefix
)
130 struct strbuf buf
= STRBUF_INIT
;
131 struct strbuf unquoted
= STRBUF_INIT
;
132 char *pathspec
[2] = { NULL
, NULL
};
133 strbuf_getline_fn getline_fn
;
136 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
137 while (getline_fn(&buf
, stdin
) != EOF
) {
138 if (!nul_term_line
&& buf
.buf
[0] == '"') {
139 strbuf_reset(&unquoted
);
140 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
141 die("line is badly quoted");
142 strbuf_swap(&buf
, &unquoted
);
144 pathspec
[0] = buf
.buf
;
145 num_ignored
+= check_ignore(dir
, prefix
,
146 1, (const char **)pathspec
);
147 maybe_flush_or_die(stdout
, "check-ignore to stdout");
149 strbuf_release(&buf
);
150 strbuf_release(&unquoted
);
154 int cmd_check_ignore(int argc
, const char **argv
, const char *prefix
)
157 struct dir_struct dir
= DIR_INIT
;
159 git_config(git_default_config
, NULL
);
161 argc
= parse_options(argc
, argv
, prefix
, check_ignore_options
,
162 check_ignore_usage
, 0);
166 die(_("cannot specify pathnames with --stdin"));
169 die(_("-z only makes sense with --stdin"));
171 die(_("no path specified"));
175 die(_("--quiet is only valid with a single pathname"));
177 die(_("cannot have both --quiet and --verbose"));
179 if (show_non_matching
&& !verbose
)
180 die(_("--non-matching is only valid with --verbose"));
182 /* read_cache() is only necessary so we can watch out for submodules. */
183 if (!no_index
&& repo_read_index(the_repository
) < 0)
184 die(_("index file corrupt"));
186 setup_standard_excludes(&dir
);
189 num_ignored
= check_ignore_stdin_paths(&dir
, prefix
);
191 num_ignored
= check_ignore(&dir
, prefix
, argc
, argv
);
192 maybe_flush_or_die(stdout
, "ignore to stdout");