6 #include "parse-options.h"
8 static int quiet
, verbose
, stdin_paths
;
9 static const char * const check_ignore_usage
[] = {
10 "git check-ignore [options] pathname...",
11 "git check-ignore [options] --stdin < <list-of-paths>",
15 static int null_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_BOOLEAN(0, "stdin", &stdin_paths
,
22 N_("read file names from stdin")),
23 OPT_BOOLEAN('z', NULL
, &null_term_line
,
24 N_("input paths are terminated by a null character")),
28 static void output_exclude(const char *path
, struct exclude
*exclude
)
30 char *bang
= exclude
->flags
& EXC_FLAG_NEGATIVE
? "!" : "";
31 char *slash
= exclude
->flags
& EXC_FLAG_MUSTBEDIR
? "/" : "";
32 if (!null_term_line
) {
34 write_name_quoted(path
, stdout
, '\n');
36 quote_c_style(exclude
->el
->src
, NULL
, stdout
, 0);
37 printf(":%d:%s%s%s\t",
39 bang
, exclude
->pattern
, slash
);
40 quote_c_style(path
, NULL
, stdout
, 0);
45 printf("%s%c", path
, '\0');
47 printf("%s%c%d%c%s%s%s%c%s%c",
48 exclude
->el
->src
, '\0',
49 exclude
->srcpos
, '\0',
50 bang
, exclude
->pattern
, slash
, '\0',
56 static int check_ignore(const char *prefix
, const char **pathspec
)
58 struct dir_struct dir
;
59 const char *path
, *full_path
;
61 int num_ignored
= 0, dtype
= DT_UNKNOWN
, i
;
62 struct path_exclude_check check
;
63 struct exclude
*exclude
;
65 /* read_cache() is only necessary so we can watch out for submodules. */
67 die(_("index file corrupt"));
69 memset(&dir
, 0, sizeof(dir
));
70 dir
.flags
|= DIR_COLLECT_IGNORED
;
71 setup_standard_excludes(&dir
);
73 if (!pathspec
|| !*pathspec
) {
75 fprintf(stderr
, "no pathspec given.\n");
79 path_exclude_check_init(&check
, &dir
);
81 * look for pathspecs matching entries in the index, since these
82 * should not be ignored, in order to be consistent with
83 * 'git status', 'git add' etc.
85 seen
= find_pathspecs_matching_against_index(pathspec
);
86 for (i
= 0; pathspec
[i
]; i
++) {
88 full_path
= prefix_path(prefix
, prefix
89 ? strlen(prefix
) : 0, path
);
90 full_path
= check_path_for_gitlink(full_path
);
91 die_if_path_beyond_symlink(full_path
, prefix
);
93 exclude
= last_exclude_matching_path(&check
, full_path
,
97 output_exclude(path
, exclude
);
103 clear_directory(&dir
);
104 path_exclude_check_clear(&check
);
109 static int check_ignore_stdin_paths(const char *prefix
)
111 struct strbuf buf
, nbuf
;
112 char **pathspec
= NULL
;
113 size_t nr
= 0, alloc
= 0;
114 int line_termination
= null_term_line
? 0 : '\n';
117 strbuf_init(&buf
, 0);
118 strbuf_init(&nbuf
, 0);
119 while (strbuf_getline(&buf
, stdin
, line_termination
) != EOF
) {
120 if (line_termination
&& buf
.buf
[0] == '"') {
122 if (unquote_c_style(&nbuf
, buf
.buf
, NULL
))
123 die("line is badly quoted");
124 strbuf_swap(&buf
, &nbuf
);
126 ALLOC_GROW(pathspec
, nr
+ 1, alloc
);
127 pathspec
[nr
] = xcalloc(strlen(buf
.buf
) + 1, sizeof(*buf
.buf
));
128 strcpy(pathspec
[nr
++], buf
.buf
);
130 ALLOC_GROW(pathspec
, nr
+ 1, alloc
);
132 num_ignored
= check_ignore(prefix
, (const char **)pathspec
);
133 maybe_flush_or_die(stdout
, "attribute to stdout");
134 strbuf_release(&buf
);
135 strbuf_release(&nbuf
);
140 int cmd_check_ignore(int argc
, const char **argv
, const char *prefix
)
144 git_config(git_default_config
, NULL
);
146 argc
= parse_options(argc
, argv
, prefix
, check_ignore_options
,
147 check_ignore_usage
, 0);
151 die(_("cannot specify pathnames with --stdin"));
154 die(_("-z only makes sense with --stdin"));
156 die(_("no path specified"));
160 die(_("--quiet is only valid with a single pathname"));
162 die(_("cannot have both --quiet and --verbose"));
166 num_ignored
= check_ignore_stdin_paths(prefix
);
168 num_ignored
= check_ignore(prefix
, argv
);
169 maybe_flush_or_die(stdout
, "ignore to stdout");