Revert "Add new @ shortcut for HEAD"
[git/raj.git] / builtin / check-ignore.c
blob4a8fc707c747596e31dcc6f57abf5f965cdf612f
1 #include "builtin.h"
2 #include "cache.h"
3 #include "dir.h"
4 #include "quote.h"
5 #include "pathspec.h"
6 #include "parse-options.h"
8 static int quiet, verbose, stdin_paths, show_non_matching;
9 static const char * const check_ignore_usage[] = {
10 "git check-ignore [options] pathname...",
11 "git check-ignore [options] --stdin < <list-of-paths>",
12 NULL
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")),
20 OPT_GROUP(""),
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")),
25 OPT_BOOLEAN('n', "non-matching", &show_non_matching,
26 N_("show non-matching input paths")),
27 OPT_END()
30 static void output_exclude(const char *path, struct exclude *exclude)
32 char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
33 char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
34 if (!null_term_line) {
35 if (!verbose) {
36 write_name_quoted(path, stdout, '\n');
37 } else {
38 if (exclude) {
39 quote_c_style(exclude->el->src, NULL, stdout, 0);
40 printf(":%d:%s%s%s\t",
41 exclude->srcpos,
42 bang, exclude->pattern, slash);
44 else {
45 printf("::\t");
47 quote_c_style(path, NULL, stdout, 0);
48 fputc('\n', stdout);
50 } else {
51 if (!verbose) {
52 printf("%s%c", path, '\0');
53 } else {
54 if (exclude)
55 printf("%s%c%d%c%s%s%s%c%s%c",
56 exclude->el->src, '\0',
57 exclude->srcpos, '\0',
58 bang, exclude->pattern, slash, '\0',
59 path, '\0');
60 else
61 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
66 static int check_ignore(struct dir_struct *dir,
67 const char *prefix, const char **pathspec)
69 const char *path, *full_path;
70 char *seen;
71 int num_ignored = 0, dtype = DT_UNKNOWN, i;
72 struct exclude *exclude;
74 if (!pathspec || !*pathspec) {
75 if (!quiet)
76 fprintf(stderr, "no pathspec given.\n");
77 return 0;
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++) {
87 path = pathspec[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);
92 exclude = NULL;
93 if (!seen[i]) {
94 exclude = last_exclude_matching(dir, full_path, &dtype);
96 if (!quiet && (exclude || show_non_matching))
97 output_exclude(path, exclude);
98 if (exclude)
99 num_ignored++;
101 free(seen);
103 return num_ignored;
106 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
108 struct strbuf buf, nbuf;
109 char *pathspec[2] = { NULL, NULL };
110 int line_termination = null_term_line ? 0 : '\n';
111 int num_ignored = 0;
113 strbuf_init(&buf, 0);
114 strbuf_init(&nbuf, 0);
115 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
116 if (line_termination && buf.buf[0] == '"') {
117 strbuf_reset(&nbuf);
118 if (unquote_c_style(&nbuf, buf.buf, NULL))
119 die("line is badly quoted");
120 strbuf_swap(&buf, &nbuf);
122 pathspec[0] = buf.buf;
123 num_ignored += check_ignore(dir, prefix, (const char **)pathspec);
124 maybe_flush_or_die(stdout, "check-ignore to stdout");
126 strbuf_release(&buf);
127 strbuf_release(&nbuf);
128 return num_ignored;
131 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
133 int num_ignored;
134 struct dir_struct dir;
136 git_config(git_default_config, NULL);
138 argc = parse_options(argc, argv, prefix, check_ignore_options,
139 check_ignore_usage, 0);
141 if (stdin_paths) {
142 if (argc > 0)
143 die(_("cannot specify pathnames with --stdin"));
144 } else {
145 if (null_term_line)
146 die(_("-z only makes sense with --stdin"));
147 if (argc == 0)
148 die(_("no path specified"));
150 if (quiet) {
151 if (argc > 1)
152 die(_("--quiet is only valid with a single pathname"));
153 if (verbose)
154 die(_("cannot have both --quiet and --verbose"));
156 if (show_non_matching && !verbose)
157 die(_("--non-matching is only valid with --verbose"));
159 /* read_cache() is only necessary so we can watch out for submodules. */
160 if (read_cache() < 0)
161 die(_("index file corrupt"));
163 memset(&dir, 0, sizeof(dir));
164 setup_standard_excludes(&dir);
166 if (stdin_paths) {
167 num_ignored = check_ignore_stdin_paths(&dir, prefix);
168 } else {
169 num_ignored = check_ignore(&dir, prefix, argv);
170 maybe_flush_or_die(stdout, "ignore to stdout");
173 clear_directory(&dir);
175 return !num_ignored;