Merge branch 'jc/check-x-z'
authorJunio C Hamano <gitster@pobox.com>
Wed, 4 Sep 2013 19:23:24 +0000 (12:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 4 Sep 2013 19:23:25 +0000 (12:23 -0700)
"git check-ignore -z" applied the NUL termination to both its input
(with --stdin) and its output, but "git check-attr -z" ignored the
option on the output side.

This is potentially a backward incompatible fix. Let's see if
anybody screams before deciding if we want to do anything to help
existing users (there may be none).

* jc/check-x-z:
check-attr -z: a single -z should apply to both input and output
check-ignore -z: a single -z should apply to both input and output
check-attr: the name of the character is NUL, not NULL
check-ignore: the name of the character is NUL, not NULL

1  2 
Documentation/git-check-attr.txt
builtin/check-ignore.c
index a7be80d48bf6fd6928186116f00f3b48cf564041,760aca9fa9dfe0d80ba28f0c5e59f246724c2fc6..00e2aa2df259d449602f59cbec33599346ebd8a0
@@@ -31,8 -31,9 +31,9 @@@ OPTION
        Read file names from stdin instead of from the command-line.
  
  -z::
-       Only meaningful with `--stdin`; paths are separated with a
-       NUL character instead of a linefeed character.
+       The output format is modified to be machine-parseable.
+       If `--stdin` is also given, input paths are separated
+       with a NUL character instead of a linefeed character.
  
  \--::
        Interpret all preceding arguments as attributes and all following
@@@ -48,6 -49,10 +49,10 @@@ OUTPU
  The output is of the form:
  <path> COLON SP <attribute> COLON SP <info> LF
  
+ unless `-z` is in effect, in which case NUL is used as delimiter:
+ <path> NUL <attribute> NUL <info> NUL
  <path> is the path of a file being queried, <attribute> is an attribute
  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
  --------
  
diff --combined builtin/check-ignore.c
index 4a8fc707c747596e31dcc6f57abf5f965cdf612f,03e509e43d2766b8ae665f44f8ac87ba2fc4e7a8..503a559b18bf03aa7d78c2b408289da6297e0cff
@@@ -5,14 -5,14 +5,14 @@@
  #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>",
  NULL
  };
  
- static int null_term_line;
+ static int nul_term_line;
  
  static const struct option check_ignore_options[] = {
        OPT__QUIET(&quiet, N_("suppress progress reporting")),
        OPT_GROUP(""),
        OPT_BOOLEAN(0, "stdin", &stdin_paths,
                    N_("read file names from stdin")),
-       OPT_BOOLEAN('z', NULL, &null_term_line,
-                   N_("input paths are terminated by a null character")),
+       OPT_BOOLEAN('z', NULL, &nul_term_line,
+                   N_("terminate input and output records by a NUL 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 (!nul_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);
                }
                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 path_exclude_check check;
        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));
 -      dir.flags |= DIR_COLLECT_IGNORED;
 -      setup_standard_excludes(&dir);
 -
        if (!pathspec || !*pathspec) {
                if (!quiet)
                        fprintf(stderr, "no pathspec given.\n");
                return 0;
        }
  
 -      path_exclude_check_init(&check, &dir);
        /*
         * look for pathspecs matching entries in the index, since these
         * should not be ignored, in order to be consistent with
                                        ? 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_path(&check, full_path,
 -                                                           -1, &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);
 -      path_exclude_check_clear(&check);
  
        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 line_termination = nul_term_line ? 0 : '\n';
 -      int num_ignored;
 +      int num_ignored = 0;
  
        strbuf_init(&buf, 0);
        strbuf_init(&nbuf, 0);
                                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);
  
                if (argc > 0)
                        die(_("cannot specify pathnames with --stdin"));
        } else {
-               if (null_term_line)
+               if (nul_term_line)
                        die(_("-z only makes sense with --stdin"));
                if (argc == 0)
                        die(_("no path specified"));
                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;
  }