2013-01-06 20:58:13 +04:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "quote.h"
|
|
|
|
#include "pathspec.h"
|
|
|
|
#include "parse-options.h"
|
|
|
|
|
2013-09-05 20:08:01 +04:00
|
|
|
static int quiet, verbose, stdin_paths, show_non_matching, no_index;
|
2013-01-06 20:58:13 +04:00
|
|
|
static const char * const check_ignore_usage[] = {
|
2015-01-13 10:44:47 +03:00
|
|
|
"git check-ignore [<options>] <pathname>...",
|
usage: do not insist that standard input must come from a file
The synopsys text and the usage string of subcommands that read list
of things from the standard input are often shown like this:
git gostak [--distim] < <list-of-doshes>
This is problematic in a number of ways:
* The way to use these commands is more often to feed them the
output from another command, not feed them from a file.
* Manual pages outside Git, commands that operate on the data read
from the standard input, e.g "sort", "grep", "sed", etc., are not
described with such a "< redirection-from-file" in their synopsys
text. Our doing so introduces inconsistency.
* We do not insist on where the output should go, by saying
git gostak [--distim] < <list-of-doshes> > <output>
* As it is our convention to enclose placeholders inside <braket>,
the redirection operator followed by a placeholder filename
becomes very hard to read, both in the documentation and in the
help text.
Let's clean them all up, after making sure that the documentation
clearly describes the modes that take information from the standard
input and what kind of things are expected on the input.
[jc: stole example for fmt-merge-msg from Jonathan]
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-10-16 21:27:42 +03:00
|
|
|
"git check-ignore [<options>] --stdin",
|
2013-01-06 20:58:13 +04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-07-12 10:05:48 +04:00
|
|
|
static int nul_term_line;
|
2013-01-06 20:58:13 +04:00
|
|
|
|
|
|
|
static const struct option check_ignore_options[] = {
|
|
|
|
OPT__QUIET(&quiet, N_("suppress progress reporting")),
|
|
|
|
OPT__VERBOSE(&verbose, N_("be verbose")),
|
|
|
|
OPT_GROUP(""),
|
2013-08-03 15:51:19 +04:00
|
|
|
OPT_BOOL(0, "stdin", &stdin_paths,
|
|
|
|
N_("read file names from stdin")),
|
2013-09-04 23:39:02 +04:00
|
|
|
OPT_BOOL('z', NULL, &nul_term_line,
|
|
|
|
N_("terminate input and output records by a NUL character")),
|
2013-08-03 15:51:19 +04:00
|
|
|
OPT_BOOL('n', "non-matching", &show_non_matching,
|
|
|
|
N_("show non-matching input paths")),
|
2013-09-05 20:08:01 +04:00
|
|
|
OPT_BOOL(0, "no-index", &no_index,
|
|
|
|
N_("ignore index when checking")),
|
2013-01-06 20:58:13 +04:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void output_exclude(const char *path, struct exclude *exclude)
|
|
|
|
{
|
2013-04-11 16:05:10 +04:00
|
|
|
char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
|
|
|
|
char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
|
2013-07-12 10:05:48 +04:00
|
|
|
if (!nul_term_line) {
|
2013-01-06 20:58:13 +04:00
|
|
|
if (!verbose) {
|
|
|
|
write_name_quoted(path, stdout, '\n');
|
|
|
|
} else {
|
2013-04-11 16:05:10 +04:00
|
|
|
if (exclude) {
|
|
|
|
quote_c_style(exclude->el->src, NULL, stdout, 0);
|
|
|
|
printf(":%d:%s%s%s\t",
|
|
|
|
exclude->srcpos,
|
|
|
|
bang, exclude->pattern, slash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("::\t");
|
|
|
|
}
|
2013-01-06 20:58:13 +04:00
|
|
|
quote_c_style(path, NULL, stdout, 0);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!verbose) {
|
|
|
|
printf("%s%c", path, '\0');
|
|
|
|
} else {
|
2013-04-11 16:05:10 +04:00
|
|
|
if (exclude)
|
|
|
|
printf("%s%c%d%c%s%s%s%c%s%c",
|
|
|
|
exclude->el->src, '\0',
|
|
|
|
exclude->srcpos, '\0',
|
|
|
|
bang, exclude->pattern, slash, '\0',
|
|
|
|
path, '\0');
|
|
|
|
else
|
|
|
|
printf("%c%c%c%s%c", '\0', '\0', '\0', path, '\0');
|
2013-01-06 20:58:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 01:23:39 +04:00
|
|
|
static int check_ignore(struct dir_struct *dir,
|
2013-07-14 12:35:45 +04:00
|
|
|
const char *prefix, int argc, const char **argv)
|
2013-01-06 20:58:13 +04:00
|
|
|
{
|
2013-07-14 12:35:45 +04:00
|
|
|
const char *full_path;
|
2013-01-06 20:58:13 +04:00
|
|
|
char *seen;
|
|
|
|
int num_ignored = 0, dtype = DT_UNKNOWN, i;
|
|
|
|
struct exclude *exclude;
|
2013-07-14 12:35:45 +04:00
|
|
|
struct pathspec pathspec;
|
2013-01-06 20:58:13 +04:00
|
|
|
|
2013-07-14 12:35:45 +04:00
|
|
|
if (!argc) {
|
2013-01-06 20:58:13 +04:00
|
|
|
if (!quiet)
|
|
|
|
fprintf(stderr, "no pathspec given.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-14 12:35:45 +04:00
|
|
|
/*
|
|
|
|
* check-ignore just needs paths. Magic beyond :/ is really
|
|
|
|
* irrelevant.
|
|
|
|
*/
|
|
|
|
parse_pathspec(&pathspec,
|
|
|
|
PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
|
|
|
|
PATHSPEC_SYMLINK_LEADING_PATH |
|
|
|
|
PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |
|
|
|
|
PATHSPEC_KEEP_ORDER,
|
|
|
|
prefix, argv);
|
|
|
|
|
2013-01-06 20:58:13 +04:00
|
|
|
/*
|
|
|
|
* look for pathspecs matching entries in the index, since these
|
|
|
|
* should not be ignored, in order to be consistent with
|
|
|
|
* 'git status', 'git add' etc.
|
|
|
|
*/
|
2013-07-14 12:36:00 +04:00
|
|
|
seen = find_pathspecs_matching_against_index(&pathspec);
|
2013-07-14 12:35:45 +04:00
|
|
|
for (i = 0; i < pathspec.nr; i++) {
|
2013-07-14 12:36:00 +04:00
|
|
|
full_path = pathspec.items[i].match;
|
2013-04-11 16:05:10 +04:00
|
|
|
exclude = NULL;
|
2013-02-19 23:56:44 +04:00
|
|
|
if (!seen[i]) {
|
2013-05-30 01:23:39 +04:00
|
|
|
exclude = last_exclude_matching(dir, full_path, &dtype);
|
2013-01-06 20:58:13 +04:00
|
|
|
}
|
2013-04-11 16:05:10 +04:00
|
|
|
if (!quiet && (exclude || show_non_matching))
|
2013-07-14 12:35:45 +04:00
|
|
|
output_exclude(pathspec.items[i].original, exclude);
|
2013-04-11 16:05:10 +04:00
|
|
|
if (exclude)
|
|
|
|
num_ignored++;
|
2013-01-06 20:58:13 +04:00
|
|
|
}
|
|
|
|
free(seen);
|
|
|
|
|
|
|
|
return num_ignored;
|
|
|
|
}
|
|
|
|
|
2013-05-30 01:23:39 +04:00
|
|
|
static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
|
2013-01-06 20:58:13 +04:00
|
|
|
{
|
|
|
|
struct strbuf buf, nbuf;
|
2013-04-11 16:05:12 +04:00
|
|
|
char *pathspec[2] = { NULL, NULL };
|
2016-01-15 00:31:17 +03:00
|
|
|
strbuf_getline_fn getline_fn;
|
2013-04-11 16:05:12 +04:00
|
|
|
int num_ignored = 0;
|
2013-01-06 20:58:13 +04:00
|
|
|
|
2016-01-15 00:31:17 +03:00
|
|
|
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
|
2013-01-06 20:58:13 +04:00
|
|
|
strbuf_init(&buf, 0);
|
|
|
|
strbuf_init(&nbuf, 0);
|
2016-01-15 00:31:17 +03:00
|
|
|
while (getline_fn(&buf, stdin) != EOF) {
|
|
|
|
if (!nul_term_line && buf.buf[0] == '"') {
|
2013-01-06 20:58:13 +04:00
|
|
|
strbuf_reset(&nbuf);
|
|
|
|
if (unquote_c_style(&nbuf, buf.buf, NULL))
|
|
|
|
die("line is badly quoted");
|
|
|
|
strbuf_swap(&buf, &nbuf);
|
|
|
|
}
|
2013-04-11 16:05:12 +04:00
|
|
|
pathspec[0] = buf.buf;
|
2013-07-14 12:35:45 +04:00
|
|
|
num_ignored += check_ignore(dir, prefix,
|
|
|
|
1, (const char **)pathspec);
|
2013-04-11 16:05:12 +04:00
|
|
|
maybe_flush_or_die(stdout, "check-ignore to stdout");
|
2013-01-06 20:58:13 +04:00
|
|
|
}
|
|
|
|
strbuf_release(&buf);
|
|
|
|
strbuf_release(&nbuf);
|
|
|
|
return num_ignored;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_check_ignore(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
|
|
|
int num_ignored;
|
2013-04-11 16:05:11 +04:00
|
|
|
struct dir_struct dir;
|
2013-01-06 20:58:13 +04:00
|
|
|
|
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, prefix, check_ignore_options,
|
|
|
|
check_ignore_usage, 0);
|
|
|
|
|
|
|
|
if (stdin_paths) {
|
|
|
|
if (argc > 0)
|
|
|
|
die(_("cannot specify pathnames with --stdin"));
|
|
|
|
} else {
|
2013-07-12 10:05:48 +04:00
|
|
|
if (nul_term_line)
|
2013-01-06 20:58:13 +04:00
|
|
|
die(_("-z only makes sense with --stdin"));
|
|
|
|
if (argc == 0)
|
|
|
|
die(_("no path specified"));
|
|
|
|
}
|
|
|
|
if (quiet) {
|
|
|
|
if (argc > 1)
|
|
|
|
die(_("--quiet is only valid with a single pathname"));
|
|
|
|
if (verbose)
|
|
|
|
die(_("cannot have both --quiet and --verbose"));
|
|
|
|
}
|
2013-04-11 16:05:10 +04:00
|
|
|
if (show_non_matching && !verbose)
|
|
|
|
die(_("--non-matching is only valid with --verbose"));
|
2013-01-06 20:58:13 +04:00
|
|
|
|
2013-04-11 16:05:11 +04:00
|
|
|
/* read_cache() is only necessary so we can watch out for submodules. */
|
2013-09-05 20:08:01 +04:00
|
|
|
if (!no_index && read_cache() < 0)
|
2013-04-11 16:05:11 +04:00
|
|
|
die(_("index file corrupt"));
|
|
|
|
|
|
|
|
memset(&dir, 0, sizeof(dir));
|
|
|
|
setup_standard_excludes(&dir);
|
2013-01-06 20:58:13 +04:00
|
|
|
|
|
|
|
if (stdin_paths) {
|
2013-05-30 01:23:39 +04:00
|
|
|
num_ignored = check_ignore_stdin_paths(&dir, prefix);
|
2013-01-06 20:58:13 +04:00
|
|
|
} else {
|
2013-07-14 12:35:45 +04:00
|
|
|
num_ignored = check_ignore(&dir, prefix, argc, argv);
|
2013-01-06 20:58:13 +04:00
|
|
|
maybe_flush_or_die(stdout, "ignore to stdout");
|
|
|
|
}
|
|
|
|
|
2013-04-11 16:05:11 +04:00
|
|
|
clear_directory(&dir);
|
|
|
|
|
2013-01-06 20:58:13 +04:00
|
|
|
return !num_ignored;
|
|
|
|
}
|