Skip to content

Commit

Permalink
Merge branch 'as/check-ignore'
Browse files Browse the repository at this point in the history
Enhance "check-ignore" (1.8.2 update) to work more like "check-attr"
over bidi-pipes.

* as/check-ignore:
  t0008: use named pipe (FIFO) to test check-ignore streaming
  Documentation: add caveats about I/O buffering for check-{attr,ignore}
  check-ignore: allow incremental streaming of queries via --stdin
  check-ignore: move setup into cmd_check_ignore()
  check-ignore: add -n / --non-matching option
  t0008: remove duplicated test fixture data
  • Loading branch information
Junio C Hamano committed May 29, 2013
2 parents 77eb44b + b96114e commit c51afbb
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 96 deletions.
5 changes: 5 additions & 0 deletions Documentation/git-check-attr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ being queried and <info> can be either:
'set';; when the attribute is defined as true.
<value>;; when a value has been assigned to the attribute.

Buffering happens as documented under the `GIT_FLUSH` option in
linkgit:git[1]. The caller is responsible for avoiding deadlocks
caused by overfilling an input buffer or reading from an empty output
buffer.

EXAMPLES
--------

Expand Down
20 changes: 20 additions & 0 deletions Documentation/git-check-ignore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ OPTIONS
below). If `--stdin` is also given, input paths are separated
with a NUL character instead of a linefeed character.

-n, --non-matching::
Show given paths which don't match any pattern. This only
makes sense when `--verbose` is enabled, otherwise it would
not be possible to distinguish between paths which match a
pattern and those which don't.

OUTPUT
------

Expand All @@ -65,6 +71,20 @@ are also used instead of colons and hard tabs:

<source> <NULL> <linenum> <NULL> <pattern> <NULL> <pathname> <NULL>

If `-n` or `--non-matching` are specified, non-matching pathnames will
also be output, in which case all fields in each output record except
for <pathname> will be empty. This can be useful when running
non-interactively, so that files can be incrementally streamed to
STDIN of a long-running check-ignore process, and for each of these
files, STDOUT will indicate whether that file matched a pattern or
not. (Without this option, it would be impossible to tell whether the
absence of output for a given file meant that it didn't match any
pattern, or that the output hadn't been generated yet.)

Buffering happens as documented under the `GIT_FLUSH` option in
linkgit:git[1]. The caller is responsible for avoiding deadlocks
caused by overfilling an input buffer or reading from an empty output
buffer.

EXIT STATUS
-----------
Expand Down
5 changes: 3 additions & 2 deletions Documentation/git.txt
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,9 @@ for further details.
'GIT_FLUSH'::
If this environment variable is set to "1", then commands such
as 'git blame' (in incremental mode), 'git rev-list', 'git log',
and 'git whatchanged' will force a flush of the output stream
after each commit-oriented record have been flushed. If this
'git check-attr', 'git check-ignore', and 'git whatchanged' will
force a flush of the output stream after each record have been
flushed. If this
variable is set to "0", the output of these commands will be done
using completely buffered I/O. If this environment variable is
not set, Git will choose buffered or record-oriented flushing
Expand Down
92 changes: 50 additions & 42 deletions builtin/check-ignore.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "pathspec.h"
#include "parse-options.h"

static int quiet, verbose, stdin_paths;
static int quiet, verbose, stdin_paths, show_non_matching;
static const char * const check_ignore_usage[] = {
"git check-ignore [options] pathname...",
"git check-ignore [options] --stdin < <list-of-paths>",
Expand All @@ -22,52 +22,55 @@ static const struct option check_ignore_options[] = {
N_("read file names from stdin")),
OPT_BOOLEAN('z', NULL, &null_term_line,
N_("input paths are terminated by a null character")),
OPT_BOOLEAN('n', "non-matching", &show_non_matching,
N_("show non-matching input paths")),
OPT_END()
};

static void output_exclude(const char *path, struct exclude *exclude)
{
char *bang = exclude->flags & EXC_FLAG_NEGATIVE ? "!" : "";
char *slash = exclude->flags & EXC_FLAG_MUSTBEDIR ? "/" : "";
char *bang = (exclude && exclude->flags & EXC_FLAG_NEGATIVE) ? "!" : "";
char *slash = (exclude && exclude->flags & EXC_FLAG_MUSTBEDIR) ? "/" : "";
if (!null_term_line) {
if (!verbose) {
write_name_quoted(path, stdout, '\n');
} else {
quote_c_style(exclude->el->src, NULL, stdout, 0);
printf(":%d:%s%s%s\t",
exclude->srcpos,
bang, exclude->pattern, slash);
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");
}
quote_c_style(path, NULL, stdout, 0);
fputc('\n', stdout);
}
} else {
if (!verbose) {
printf("%s%c", path, '\0');
} else {
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');
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');
}
}
}

static int check_ignore(const char *prefix, const char **pathspec)
static int check_ignore(struct dir_struct *dir,
const char *prefix, const char **pathspec)
{
struct dir_struct dir;
const char *path, *full_path;
char *seen;
int num_ignored = 0, dtype = DT_UNKNOWN, i;
struct exclude *exclude;

/* read_cache() is only necessary so we can watch out for submodules. */
if (read_cache() < 0)
die(_("index file corrupt"));

memset(&dir, 0, sizeof(dir));
setup_standard_excludes(&dir);

if (!pathspec || !*pathspec) {
if (!quiet)
fprintf(stderr, "no pathspec given.\n");
Expand All @@ -86,28 +89,26 @@ static int check_ignore(const char *prefix, const char **pathspec)
? strlen(prefix) : 0, path);
full_path = check_path_for_gitlink(full_path);
die_if_path_beyond_symlink(full_path, prefix);
exclude = NULL;
if (!seen[i]) {
exclude = last_exclude_matching(&dir, full_path, &dtype);
if (exclude) {
if (!quiet)
output_exclude(path, exclude);
num_ignored++;
}
exclude = last_exclude_matching(dir, full_path, &dtype);
}
if (!quiet && (exclude || show_non_matching))
output_exclude(path, exclude);
if (exclude)
num_ignored++;
}
free(seen);
clear_directory(&dir);

return num_ignored;
}

static int check_ignore_stdin_paths(const char *prefix)
static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)
{
struct strbuf buf, nbuf;
char **pathspec = NULL;
size_t nr = 0, alloc = 0;
char *pathspec[2] = { NULL, NULL };
int line_termination = null_term_line ? 0 : '\n';
int num_ignored;
int num_ignored = 0;

strbuf_init(&buf, 0);
strbuf_init(&nbuf, 0);
Expand All @@ -118,23 +119,19 @@ static int check_ignore_stdin_paths(const char *prefix)
die("line is badly quoted");
strbuf_swap(&buf, &nbuf);
}
ALLOC_GROW(pathspec, nr + 1, alloc);
pathspec[nr] = xcalloc(strlen(buf.buf) + 1, sizeof(*buf.buf));
strcpy(pathspec[nr++], buf.buf);
pathspec[0] = buf.buf;
num_ignored += check_ignore(dir, prefix, (const char **)pathspec);
maybe_flush_or_die(stdout, "check-ignore to stdout");
}
ALLOC_GROW(pathspec, nr + 1, alloc);
pathspec[nr] = NULL;
num_ignored = check_ignore(prefix, (const char **)pathspec);
maybe_flush_or_die(stdout, "attribute to stdout");
strbuf_release(&buf);
strbuf_release(&nbuf);
free(pathspec);
return num_ignored;
}

int cmd_check_ignore(int argc, const char **argv, const char *prefix)
{
int num_ignored;
struct dir_struct dir;

git_config(git_default_config, NULL);

Expand All @@ -156,13 +153,24 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)
if (verbose)
die(_("cannot have both --quiet and --verbose"));
}
if (show_non_matching && !verbose)
die(_("--non-matching is only valid with --verbose"));

/* read_cache() is only necessary so we can watch out for submodules. */
if (read_cache() < 0)
die(_("index file corrupt"));

memset(&dir, 0, sizeof(dir));
setup_standard_excludes(&dir);

if (stdin_paths) {
num_ignored = check_ignore_stdin_paths(prefix);
num_ignored = check_ignore_stdin_paths(&dir, prefix);
} else {
num_ignored = check_ignore(prefix, argv);
num_ignored = check_ignore(&dir, prefix, argv);
maybe_flush_or_die(stdout, "ignore to stdout");
}

clear_directory(&dir);

return !num_ignored;
}
Loading

0 comments on commit c51afbb

Please sign in to comment.