2013-01-06 17:58:13 +01:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "cache.h"
|
2017-06-14 20:07:36 +02:00
|
|
|
#include "config.h"
|
2013-01-06 17:58:13 +01:00
|
|
|
#include "dir.h"
|
|
|
|
#include "quote.h"
|
|
|
|
#include "pathspec.h"
|
|
|
|
#include "parse-options.h"
|
2017-05-12 00:04:24 +02:00
|
|
|
#include "submodule.h"
|
2013-01-06 17:58:13 +01:00
|
|
|
|
2013-09-05 18:08:01 +02:00
|
|
|
static int quiet, verbose, stdin_paths, show_non_matching, no_index;
|
2013-01-06 17:58:13 +01:00
|
|
|
static const char * const check_ignore_usage[] = {
|
2015-01-13 08:44:47 +01: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 20:27:42 +02:00
|
|
|
"git check-ignore [<options>] --stdin",
|
2013-01-06 17:58:13 +01:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-07-12 08:05:48 +02:00
|
|
|
static int nul_term_line;
|
2013-01-06 17:58:13 +01: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 13:51:19 +02:00
|
|
|
OPT_BOOL(0, "stdin", &stdin_paths,
|
|
|
|
N_("read file names from stdin")),
|
2013-09-04 21:39:02 +02:00
|
|
|
OPT_BOOL('z', NULL, &nul_term_line,
|
|
|
|
N_("terminate input and output records by a NUL character")),
|
2013-08-03 13:51:19 +02:00
|
|
|
OPT_BOOL('n', "non-matching", &show_non_matching,
|
|
|
|
N_("show non-matching input paths")),
|
2013-09-05 18:08:01 +02:00
|
|
|
OPT_BOOL(0, "no-index", &no_index,
|
|
|
|
N_("ignore index when checking")),
|
2013-01-06 17:58:13 +01:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static void output_exclude(const char *path, struct exclude *exclude)
|
|
|
|
{
|
2013-04-11 14:05:10 +02:00
|
|
|
char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
|
|
|
|
char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
|
2013-07-12 08:05:48 +02:00
|
|
|
if (!nul_term_line) {
|
2013-01-06 17:58:13 +01:00
|
|
|
if (!verbose) {
|
|
|
|
write_name_quoted(path, stdout, '\n');
|
|
|
|
} else {
|
2013-04-11 14:05:10 +02: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 17:58:13 +01:00
|
|
|
quote_c_style(path, NULL, stdout, 0);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!verbose) {
|
|
|
|
printf("%s%c", path, '\0');
|
|
|
|
} else {
|
2013-04-11 14:05:10 +02: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 17:58:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 23:23:39 +02:00
|
|
|
static int check_ignore(struct dir_struct *dir,
|
2013-07-14 10:35:45 +02:00
|
|
|
const char *prefix, int argc, const char **argv)
|
2013-01-06 17:58:13 +01:00
|
|
|
{
|
2013-07-14 10:35:45 +02:00
|
|
|
const char *full_path;
|
2013-01-06 17:58:13 +01:00
|
|
|
char *seen;
|
|
|
|
int num_ignored = 0, dtype = DT_UNKNOWN, i;
|
|
|
|
struct exclude *exclude;
|
2013-07-14 10:35:45 +02:00
|
|
|
struct pathspec pathspec;
|
2013-01-06 17:58:13 +01:00
|
|
|
|
2013-07-14 10:35:45 +02:00
|
|
|
if (!argc) {
|
2013-01-06 17:58:13 +01:00
|
|
|
if (!quiet)
|
|
|
|
fprintf(stderr, "no pathspec given.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-14 10:35:45 +02:00
|
|
|
/*
|
|
|
|
* check-ignore just needs paths. Magic beyond :/ is really
|
|
|
|
* irrelevant.
|
|
|
|
*/
|
|
|
|
parse_pathspec(&pathspec,
|
|
|
|
PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
|
|
|
|
PATHSPEC_SYMLINK_LEADING_PATH |
|
|
|
|
PATHSPEC_KEEP_ORDER,
|
|
|
|
prefix, argv);
|
|
|
|
|
2017-05-12 00:04:24 +02:00
|
|
|
die_path_inside_submodule(&the_index, &pathspec);
|
|
|
|
|
2013-01-06 17:58:13 +01: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.
|
|
|
|
*/
|
2017-05-12 00:04:27 +02:00
|
|
|
seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
|
2013-07-14 10:35:45 +02:00
|
|
|
for (i = 0; i < pathspec.nr; i++) {
|
2013-07-14 10:36:00 +02:00
|
|
|
full_path = pathspec.items[i].match;
|
2013-04-11 14:05:10 +02:00
|
|
|
exclude = NULL;
|
2013-02-19 20:56:44 +01:00
|
|
|
if (!seen[i]) {
|
2017-05-05 21:53:30 +02:00
|
|
|
exclude = last_exclude_matching(dir, &the_index,
|
|
|
|
full_path, &dtype);
|
2013-01-06 17:58:13 +01:00
|
|
|
}
|
2013-04-11 14:05:10 +02:00
|
|
|
if (!quiet && (exclude || show_non_matching))
|
2013-07-14 10:35:45 +02:00
|
|
|
output_exclude(pathspec.items[i].original, exclude);
|
2013-04-11 14:05:10 +02:00
|
|
|
if (exclude)
|
|
|
|
num_ignored++;
|
2013-01-06 17:58:13 +01:00
|
|
|
}
|
|
|
|
free(seen);
|
|
|
|
|
|
|
|
return num_ignored;
|
|
|
|
}
|
|
|
|
|
2013-05-29 23:23:39 +02:00
|
|
|
static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
|
2013-01-06 17:58:13 +01:00
|
|
|
{
|
2016-01-31 12:25:26 +01:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
struct strbuf unquoted = STRBUF_INIT;
|
2013-04-11 14:05:12 +02:00
|
|
|
char *pathspec[2] = { NULL, NULL };
|
2016-01-14 22:31:17 +01:00
|
|
|
strbuf_getline_fn getline_fn;
|
2013-04-11 14:05:12 +02:00
|
|
|
int num_ignored = 0;
|
2013-01-06 17:58:13 +01:00
|
|
|
|
2016-01-14 22:31:17 +01:00
|
|
|
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
|
|
|
|
while (getline_fn(&buf, stdin) != EOF) {
|
|
|
|
if (!nul_term_line && buf.buf[0] == '"') {
|
2016-01-31 12:25:26 +01:00
|
|
|
strbuf_reset(&unquoted);
|
|
|
|
if (unquote_c_style(&unquoted, buf.buf, NULL))
|
2013-01-06 17:58:13 +01:00
|
|
|
die("line is badly quoted");
|
2016-01-31 12:25:26 +01:00
|
|
|
strbuf_swap(&buf, &unquoted);
|
2013-01-06 17:58:13 +01:00
|
|
|
}
|
2013-04-11 14:05:12 +02:00
|
|
|
pathspec[0] = buf.buf;
|
2013-07-14 10:35:45 +02:00
|
|
|
num_ignored += check_ignore(dir, prefix,
|
|
|
|
1, (const char **)pathspec);
|
2013-04-11 14:05:12 +02:00
|
|
|
maybe_flush_or_die(stdout, "check-ignore to stdout");
|
2013-01-06 17:58:13 +01:00
|
|
|
}
|
|
|
|
strbuf_release(&buf);
|
2016-01-31 12:25:26 +01:00
|
|
|
strbuf_release(&unquoted);
|
2013-01-06 17:58:13 +01:00
|
|
|
return num_ignored;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_check_ignore(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
|
|
|
int num_ignored;
|
2013-04-11 14:05:11 +02:00
|
|
|
struct dir_struct dir;
|
2013-01-06 17:58:13 +01: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 08:05:48 +02:00
|
|
|
if (nul_term_line)
|
2013-01-06 17:58:13 +01: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 14:05:10 +02:00
|
|
|
if (show_non_matching && !verbose)
|
|
|
|
die(_("--non-matching is only valid with --verbose"));
|
2013-01-06 17:58:13 +01:00
|
|
|
|
2013-04-11 14:05:11 +02:00
|
|
|
/* read_cache() is only necessary so we can watch out for submodules. */
|
2013-09-05 18:08:01 +02:00
|
|
|
if (!no_index && read_cache() < 0)
|
2013-04-11 14:05:11 +02:00
|
|
|
die(_("index file corrupt"));
|
|
|
|
|
|
|
|
memset(&dir, 0, sizeof(dir));
|
|
|
|
setup_standard_excludes(&dir);
|
2013-01-06 17:58:13 +01:00
|
|
|
|
|
|
|
if (stdin_paths) {
|
2013-05-29 23:23:39 +02:00
|
|
|
num_ignored = check_ignore_stdin_paths(&dir, prefix);
|
2013-01-06 17:58:13 +01:00
|
|
|
} else {
|
2013-07-14 10:35:45 +02:00
|
|
|
num_ignored = check_ignore(&dir, prefix, argc, argv);
|
2013-01-06 17:58:13 +01:00
|
|
|
maybe_flush_or_die(stdout, "ignore to stdout");
|
|
|
|
}
|
|
|
|
|
2013-04-11 14:05:11 +02:00
|
|
|
clear_directory(&dir);
|
|
|
|
|
2013-01-06 17:58:13 +01:00
|
|
|
return !num_ignored;
|
|
|
|
}
|