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