1 #define USE_THE_INDEX_VARIABLE
9 #include "parse-options.h"
10 #include "repository.h"
11 #include "submodule.h"
12 #include "write-or-die.h"
14 static int quiet
, verbose
, stdin_paths
, show_non_matching
, no_index
;
15 static const char * const check_ignore_usage
[] = {
16 "git check-ignore [<options>] <pathname>...",
17 "git check-ignore [<options>] --stdin",
21 static int nul_term_line
;
23 static const struct option check_ignore_options
[] = {
24 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
25 OPT__VERBOSE(&verbose
, N_("be verbose")),
27 OPT_BOOL(0, "stdin", &stdin_paths
,
28 N_("read file names from stdin")),
29 OPT_BOOL('z', NULL
, &nul_term_line
,
30 N_("terminate input and output records by a NUL character")),
31 OPT_BOOL('n', "non-matching", &show_non_matching
,
32 N_("show non-matching input paths")),
33 OPT_BOOL(0, "no-index", &no_index
,
34 N_("ignore index when checking")),
38 static void output_pattern(const char *path
, struct path_pattern
*pattern
)
40 char *bang
= (pattern
&& pattern
->flags
& PATTERN_FLAG_NEGATIVE
) ? "!" : "";
41 char *slash
= (pattern
&& pattern
->flags
& PATTERN_FLAG_MUSTBEDIR
) ? "/" : "";
44 write_name_quoted(path
, stdout
, '\n');
47 quote_c_style(pattern
->pl
->src
, NULL
, stdout
, 0);
48 printf(":%d:%s%s%s\t",
50 bang
, pattern
->pattern
, slash
);
55 quote_c_style(path
, NULL
, stdout
, 0);
60 printf("%s%c", path
, '\0');
63 printf("%s%c%d%c%s%s%s%c%s%c",
64 pattern
->pl
->src
, '\0',
65 pattern
->srcpos
, '\0',
66 bang
, pattern
->pattern
, slash
, '\0',
69 printf("%c%c%c%s%c", '\0', '\0', '\0', path
, '\0');
74 static int check_ignore(struct dir_struct
*dir
,
75 const char *prefix
, int argc
, const char **argv
)
77 const char *full_path
;
79 int num_ignored
= 0, i
;
80 struct path_pattern
*pattern
;
81 struct pathspec pathspec
;
85 fprintf(stderr
, "no pathspec given.\n");
90 * check-ignore just needs paths. Magic beyond :/ is really
93 parse_pathspec(&pathspec
,
94 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_FROMTOP
,
95 PATHSPEC_SYMLINK_LEADING_PATH
|
99 die_path_inside_submodule(&the_index
, &pathspec
);
102 * look for pathspecs matching entries in the index, since these
103 * should not be ignored, in order to be consistent with
104 * 'git status', 'git add' etc.
106 seen
= find_pathspecs_matching_against_index(&pathspec
, &the_index
,
107 PS_HEED_SKIP_WORKTREE
);
108 for (i
= 0; i
< pathspec
.nr
; i
++) {
109 full_path
= pathspec
.items
[i
].match
;
112 int dtype
= DT_UNKNOWN
;
113 pattern
= last_matching_pattern(dir
, &the_index
,
115 if (!verbose
&& pattern
&&
116 pattern
->flags
& PATTERN_FLAG_NEGATIVE
)
119 if (!quiet
&& (pattern
|| show_non_matching
))
120 output_pattern(pathspec
.items
[i
].original
, pattern
);
125 clear_pathspec(&pathspec
);
130 static int check_ignore_stdin_paths(struct dir_struct
*dir
, const char *prefix
)
132 struct strbuf buf
= STRBUF_INIT
;
133 struct strbuf unquoted
= STRBUF_INIT
;
134 char *pathspec
[2] = { NULL
, NULL
};
135 strbuf_getline_fn getline_fn
;
138 getline_fn
= nul_term_line
? strbuf_getline_nul
: strbuf_getline_lf
;
139 while (getline_fn(&buf
, stdin
) != EOF
) {
140 if (!nul_term_line
&& buf
.buf
[0] == '"') {
141 strbuf_reset(&unquoted
);
142 if (unquote_c_style(&unquoted
, buf
.buf
, NULL
))
143 die("line is badly quoted");
144 strbuf_swap(&buf
, &unquoted
);
146 pathspec
[0] = buf
.buf
;
147 num_ignored
+= check_ignore(dir
, prefix
,
148 1, (const char **)pathspec
);
149 maybe_flush_or_die(stdout
, "check-ignore to stdout");
151 strbuf_release(&buf
);
152 strbuf_release(&unquoted
);
156 int cmd_check_ignore(int argc
, const char **argv
, const char *prefix
)
159 struct dir_struct dir
= DIR_INIT
;
161 git_config(git_default_config
, NULL
);
163 argc
= parse_options(argc
, argv
, prefix
, check_ignore_options
,
164 check_ignore_usage
, 0);
168 die(_("cannot specify pathnames with --stdin"));
171 die(_("-z only makes sense with --stdin"));
173 die(_("no path specified"));
177 die(_("--quiet is only valid with a single pathname"));
179 die(_("cannot have both --quiet and --verbose"));
181 if (show_non_matching
&& !verbose
)
182 die(_("--non-matching is only valid with --verbose"));
184 /* read_cache() is only necessary so we can watch out for submodules. */
185 if (!no_index
&& repo_read_index(the_repository
) < 0)
186 die(_("index file corrupt"));
188 setup_standard_excludes(&dir
);
191 num_ignored
= check_ignore_stdin_paths(&dir
, prefix
);
193 num_ignored
= check_ignore(&dir
, prefix
, argc
, argv
);
194 maybe_flush_or_die(stdout
, "ignore to stdout");