Merge branch 'rh/ishes-doc'
[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
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
13 };
14
15 static int nul_term_line;
16
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_BOOL(0, "stdin", &stdin_paths,
22                  N_("read file names from stdin")),
23         OPT_BOOL('z', NULL, &nul_term_line,
24                  N_("terminate input and output records by a NUL character")),
25         OPT_BOOL('n', "non-matching", &show_non_matching,
26                  N_("show non-matching input paths")),
27         OPT_END()
28 };
29
30 static void output_exclude(const char *path, struct exclude *exclude)
31 {
32         char *bang  = (exclude && exclude->flags & EXC_FLAG_NEGATIVE)  ? "!" : "";
33         char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
34         if (!nul_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);
43                         }
44                         else {
45                                 printf("::\t");
46                         }
47                         quote_c_style(path, NULL, stdout, 0);
48                         fputc('\n', stdout);
49                 }
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');
62                 }
63         }
64 }
65
66 static int check_ignore(struct dir_struct *dir,
67                         const char *prefix, int argc, const char **argv)
68 {
69         const char *full_path;
70         char *seen;
71         int num_ignored = 0, dtype = DT_UNKNOWN, i;
72         struct exclude *exclude;
73         struct pathspec pathspec;
74
75         if (!argc) {
76                 if (!quiet)
77                         fprintf(stderr, "no pathspec given.\n");
78                 return 0;
79         }
80
81         /*
82          * check-ignore just needs paths. Magic beyond :/ is really
83          * irrelevant.
84          */
85         parse_pathspec(&pathspec,
86                        PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
87                        PATHSPEC_SYMLINK_LEADING_PATH |
88                        PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |
89                        PATHSPEC_KEEP_ORDER,
90                        prefix, argv);
91
92         /*
93          * look for pathspecs matching entries in the index, since these
94          * should not be ignored, in order to be consistent with
95          * 'git status', 'git add' etc.
96          */
97         seen = find_pathspecs_matching_against_index(&pathspec);
98         for (i = 0; i < pathspec.nr; i++) {
99                 full_path = pathspec.items[i].match;
100                 exclude = NULL;
101                 if (!seen[i]) {
102                         exclude = last_exclude_matching(dir, full_path, &dtype);
103                 }
104                 if (!quiet && (exclude || show_non_matching))
105                         output_exclude(pathspec.items[i].original, exclude);
106                 if (exclude)
107                         num_ignored++;
108         }
109         free(seen);
110
111         return num_ignored;
112 }
113
114 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
115 {
116         struct strbuf buf, nbuf;
117         char *pathspec[2] = { NULL, NULL };
118         int line_termination = nul_term_line ? 0 : '\n';
119         int num_ignored = 0;
120
121         strbuf_init(&buf, 0);
122         strbuf_init(&nbuf, 0);
123         while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
124                 if (line_termination && buf.buf[0] == '"') {
125                         strbuf_reset(&nbuf);
126                         if (unquote_c_style(&nbuf, buf.buf, NULL))
127                                 die("line is badly quoted");
128                         strbuf_swap(&buf, &nbuf);
129                 }
130                 pathspec[0] = buf.buf;
131                 num_ignored += check_ignore(dir, prefix,
132                                             1, (const char **)pathspec);
133                 maybe_flush_or_die(stdout, "check-ignore to stdout");
134         }
135         strbuf_release(&buf);
136         strbuf_release(&nbuf);
137         return num_ignored;
138 }
139
140 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
141 {
142         int num_ignored;
143         struct dir_struct dir;
144
145         git_config(git_default_config, NULL);
146
147         argc = parse_options(argc, argv, prefix, check_ignore_options,
148                              check_ignore_usage, 0);
149
150         if (stdin_paths) {
151                 if (argc > 0)
152                         die(_("cannot specify pathnames with --stdin"));
153         } else {
154                 if (nul_term_line)
155                         die(_("-z only makes sense with --stdin"));
156                 if (argc == 0)
157                         die(_("no path specified"));
158         }
159         if (quiet) {
160                 if (argc > 1)
161                         die(_("--quiet is only valid with a single pathname"));
162                 if (verbose)
163                         die(_("cannot have both --quiet and --verbose"));
164         }
165         if (show_non_matching && !verbose)
166                 die(_("--non-matching is only valid with --verbose"));
167
168         /* read_cache() is only necessary so we can watch out for submodules. */
169         if (read_cache() < 0)
170                 die(_("index file corrupt"));
171
172         memset(&dir, 0, sizeof(dir));
173         setup_standard_excludes(&dir);
174
175         if (stdin_paths) {
176                 num_ignored = check_ignore_stdin_paths(&dir, prefix);
177         } else {
178                 num_ignored = check_ignore(&dir, prefix, argc, argv);
179                 maybe_flush_or_die(stdout, "ignore to stdout");
180         }
181
182         clear_directory(&dir);
183
184         return !num_ignored;
185 }