builtin/bundle: have unbundle check for repo before opening its bundle
[git/gitster.git] / builtin / check-ignore.c
blob2bda6a1d46291676533765925720b66ce3463a6f
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "gettext.h"
5 #include "quote.h"
6 #include "pathspec.h"
7 #include "parse-options.h"
8 #include "repository.h"
9 #include "submodule.h"
10 #include "write-or-die.h"
12 static int quiet, verbose, stdin_paths, show_non_matching, no_index;
13 static const char * const check_ignore_usage[] = {
14 "git check-ignore [<options>] <pathname>...",
15 "git check-ignore [<options>] --stdin",
16 NULL
19 static int nul_term_line;
21 static const struct option check_ignore_options[] = {
22 OPT__QUIET(&quiet, N_("suppress progress reporting")),
23 OPT__VERBOSE(&verbose, N_("be verbose")),
24 OPT_GROUP(""),
25 OPT_BOOL(0, "stdin", &stdin_paths,
26 N_("read file names from stdin")),
27 OPT_BOOL('z', NULL, &nul_term_line,
28 N_("terminate input and output records by a NUL character")),
29 OPT_BOOL('n', "non-matching", &show_non_matching,
30 N_("show non-matching input paths")),
31 OPT_BOOL(0, "no-index", &no_index,
32 N_("ignore index when checking")),
33 OPT_END()
36 static void output_pattern(const char *path, struct path_pattern *pattern)
38 const char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
39 const char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : "";
40 if (!nul_term_line) {
41 if (!verbose) {
42 write_name_quoted(path, stdout, '\n');
43 } else {
44 if (pattern) {
45 quote_c_style(pattern->pl->src, NULL, stdout, 0);
46 printf(":%d:%s%s%s\t",
47 pattern->srcpos,
48 bang, pattern->pattern, slash);
50 else {
51 printf("::\t");
53 quote_c_style(path, NULL, stdout, 0);
54 fputc('\n', stdout);
56 } else {
57 if (!verbose) {
58 printf("%s%c", path, '\0');
59 } else {
60 if (pattern)
61 printf("%s%c%d%c%s%s%s%c%s%c",
62 pattern->pl->src, '\0',
63 pattern->srcpos, '\0',
64 bang, pattern->pattern, slash, '\0',
65 path, '\0');
66 else
67 printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
72 static int check_ignore(struct dir_struct *dir,
73 const char *prefix, int argc, const char **argv)
75 const char *full_path;
76 char *seen;
77 int num_ignored = 0, i;
78 struct path_pattern *pattern;
79 struct pathspec pathspec;
81 if (!argc) {
82 if (!quiet)
83 fprintf(stderr, "no pathspec given.\n");
84 return 0;
88 * check-ignore just needs paths. Magic beyond :/ is really
89 * irrelevant.
91 parse_pathspec(&pathspec,
92 PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
93 PATHSPEC_SYMLINK_LEADING_PATH |
94 PATHSPEC_KEEP_ORDER,
95 prefix, argv);
97 die_path_inside_submodule(the_repository->index, &pathspec);
100 * look for pathspecs matching entries in the index, since these
101 * should not be ignored, in order to be consistent with
102 * 'git status', 'git add' etc.
104 seen = find_pathspecs_matching_against_index(&pathspec, the_repository->index,
105 PS_HEED_SKIP_WORKTREE);
106 for (i = 0; i < pathspec.nr; i++) {
107 full_path = pathspec.items[i].match;
108 pattern = NULL;
109 if (!seen[i]) {
110 int dtype = DT_UNKNOWN;
111 pattern = last_matching_pattern(dir, the_repository->index,
112 full_path, &dtype);
113 if (!verbose && pattern &&
114 pattern->flags & PATTERN_FLAG_NEGATIVE)
115 pattern = NULL;
117 if (!quiet && (pattern || show_non_matching))
118 output_pattern(pathspec.items[i].original, pattern);
119 if (pattern)
120 num_ignored++;
122 free(seen);
123 clear_pathspec(&pathspec);
125 return num_ignored;
128 static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
130 struct strbuf buf = STRBUF_INIT;
131 struct strbuf unquoted = STRBUF_INIT;
132 char *pathspec[2] = { NULL, NULL };
133 strbuf_getline_fn getline_fn;
134 int num_ignored = 0;
136 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
137 while (getline_fn(&buf, stdin) != EOF) {
138 if (!nul_term_line && buf.buf[0] == '"') {
139 strbuf_reset(&unquoted);
140 if (unquote_c_style(&unquoted, buf.buf, NULL))
141 die("line is badly quoted");
142 strbuf_swap(&buf, &unquoted);
144 pathspec[0] = buf.buf;
145 num_ignored += check_ignore(dir, prefix,
146 1, (const char **)pathspec);
147 maybe_flush_or_die(stdout, "check-ignore to stdout");
149 strbuf_release(&buf);
150 strbuf_release(&unquoted);
151 return num_ignored;
154 int cmd_check_ignore(int argc, const char **argv, const char *prefix)
156 int num_ignored;
157 struct dir_struct dir = DIR_INIT;
159 git_config(git_default_config, NULL);
161 argc = parse_options(argc, argv, prefix, check_ignore_options,
162 check_ignore_usage, 0);
164 if (stdin_paths) {
165 if (argc > 0)
166 die(_("cannot specify pathnames with --stdin"));
167 } else {
168 if (nul_term_line)
169 die(_("-z only makes sense with --stdin"));
170 if (argc == 0)
171 die(_("no path specified"));
173 if (quiet) {
174 if (argc > 1)
175 die(_("--quiet is only valid with a single pathname"));
176 if (verbose)
177 die(_("cannot have both --quiet and --verbose"));
179 if (show_non_matching && !verbose)
180 die(_("--non-matching is only valid with --verbose"));
182 /* read_cache() is only necessary so we can watch out for submodules. */
183 if (!no_index && repo_read_index(the_repository) < 0)
184 die(_("index file corrupt"));
186 setup_standard_excludes(&dir);
188 if (stdin_paths) {
189 num_ignored = check_ignore_stdin_paths(&dir, prefix);
190 } else {
191 num_ignored = check_ignore(&dir, prefix, argc, argv);
192 maybe_flush_or_die(stdout, "ignore to stdout");
195 dir_clear(&dir);
197 return !num_ignored;