ls-files: prevent prune_cache from overeagerly pruning submodules
[git] / builtin / check-ignore.c
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"
7 #include "submodule.h"
8
9 static int quiet, verbose, stdin_paths, show_non_matching, no_index;
10 static const char * const check_ignore_usage[] = {
11 "git check-ignore [<options>] <pathname>...",
12 "git check-ignore [<options>] --stdin",
13 NULL
14 };
15
16 static int nul_term_line;
17
18 static const struct option check_ignore_options[] = {
19         OPT__QUIET(&quiet, N_("suppress progress reporting")),
20         OPT__VERBOSE(&verbose, N_("be verbose")),
21         OPT_GROUP(""),
22         OPT_BOOL(0, "stdin", &stdin_paths,
23                  N_("read file names from stdin")),
24         OPT_BOOL('z', NULL, &nul_term_line,
25                  N_("terminate input and output records by a NUL character")),
26         OPT_BOOL('n', "non-matching", &show_non_matching,
27                  N_("show non-matching input paths")),
28         OPT_BOOL(0, "no-index", &no_index,
29                  N_("ignore index when checking")),
30         OPT_END()
31 };
32
33 static void output_exclude(const char *path, struct exclude *exclude)
34 {
35         char *bang  = (exclude && exclude->flags & EXC_FLAG_NEGATIVE)  ? "!" : "";
36         char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
37         if (!nul_term_line) {
38                 if (!verbose) {
39                         write_name_quoted(path, stdout, '\n');
40                 } else {
41                         if (exclude) {
42                                 quote_c_style(exclude->el->src, NULL, stdout, 0);
43                                 printf(":%d:%s%s%s\t",
44                                        exclude->srcpos,
45                                        bang, exclude->pattern, slash);
46                         }
47                         else {
48                                 printf("::\t");
49                         }
50                         quote_c_style(path, NULL, stdout, 0);
51                         fputc('\n', stdout);
52                 }
53         } else {
54                 if (!verbose) {
55                         printf("%s%c", path, '\0');
56                 } else {
57                         if (exclude)
58                                 printf("%s%c%d%c%s%s%s%c%s%c",
59                                        exclude->el->src, '\0',
60                                        exclude->srcpos, '\0',
61                                        bang, exclude->pattern, slash, '\0',
62                                        path, '\0');
63                         else
64                                 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
65                 }
66         }
67 }
68
69 static int check_ignore(struct dir_struct *dir,
70                         const char *prefix, int argc, const char **argv)
71 {
72         const char *full_path;
73         char *seen;
74         int num_ignored = 0, dtype = DT_UNKNOWN, i;
75         struct exclude *exclude;
76         struct pathspec pathspec;
77
78         if (!argc) {
79                 if (!quiet)
80                         fprintf(stderr, "no pathspec given.\n");
81                 return 0;
82         }
83
84         /*
85          * check-ignore just needs paths. Magic beyond :/ is really
86          * irrelevant.
87          */
88         parse_pathspec(&pathspec,
89                        PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
90                        PATHSPEC_SYMLINK_LEADING_PATH |
91                        PATHSPEC_KEEP_ORDER,
92                        prefix, argv);
93
94         die_path_inside_submodule(&the_index, &pathspec);
95
96         /*
97          * look for pathspecs matching entries in the index, since these
98          * should not be ignored, in order to be consistent with
99          * 'git status', 'git add' etc.
100          */
101         seen = find_pathspecs_matching_against_index(&pathspec);
102         for (i = 0; i < pathspec.nr; i++) {
103                 full_path = pathspec.items[i].match;
104                 exclude = NULL;
105                 if (!seen[i]) {
106                         exclude = last_exclude_matching(dir, full_path, &dtype);
107                 }
108                 if (!quiet && (exclude || show_non_matching))
109                         output_exclude(pathspec.items[i].original, exclude);
110                 if (exclude)
111                         num_ignored++;
112         }
113         free(seen);
114
115         return num_ignored;
116 }
117
118 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
119 {
120         struct strbuf buf = STRBUF_INIT;
121         struct strbuf unquoted = STRBUF_INIT;
122         char *pathspec[2] = { NULL, NULL };
123         strbuf_getline_fn getline_fn;
124         int num_ignored = 0;
125
126         getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
127         while (getline_fn(&buf, stdin) != EOF) {
128                 if (!nul_term_line && buf.buf[0] == '"') {
129                         strbuf_reset(&unquoted);
130                         if (unquote_c_style(&unquoted, buf.buf, NULL))
131                                 die("line is badly quoted");
132                         strbuf_swap(&buf, &unquoted);
133                 }
134                 pathspec[0] = buf.buf;
135                 num_ignored += check_ignore(dir, prefix,
136                                             1, (const char **)pathspec);
137                 maybe_flush_or_die(stdout, "check-ignore to stdout");
138         }
139         strbuf_release(&buf);
140         strbuf_release(&unquoted);
141         return num_ignored;
142 }
143
144 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
145 {
146         int num_ignored;
147         struct dir_struct dir;
148
149         git_config(git_default_config, NULL);
150
151         argc = parse_options(argc, argv, prefix, check_ignore_options,
152                              check_ignore_usage, 0);
153
154         if (stdin_paths) {
155                 if (argc > 0)
156                         die(_("cannot specify pathnames with --stdin"));
157         } else {
158                 if (nul_term_line)
159                         die(_("-z only makes sense with --stdin"));
160                 if (argc == 0)
161                         die(_("no path specified"));
162         }
163         if (quiet) {
164                 if (argc > 1)
165                         die(_("--quiet is only valid with a single pathname"));
166                 if (verbose)
167                         die(_("cannot have both --quiet and --verbose"));
168         }
169         if (show_non_matching && !verbose)
170                 die(_("--non-matching is only valid with --verbose"));
171
172         /* read_cache() is only necessary so we can watch out for submodules. */
173         if (!no_index && read_cache() < 0)
174                 die(_("index file corrupt"));
175
176         memset(&dir, 0, sizeof(dir));
177         setup_standard_excludes(&dir);
178
179         if (stdin_paths) {
180                 num_ignored = check_ignore_stdin_paths(&dir, prefix);
181         } else {
182                 num_ignored = check_ignore(&dir, prefix, argc, argv);
183                 maybe_flush_or_die(stdout, "ignore to stdout");
184         }
185
186         clear_directory(&dir);
187
188         return !num_ignored;
189 }