builtin / check-attr.con commit Sync with 1.7.6.6 (110c511)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "attr.h"
   4#include "quote.h"
   5#include "parse-options.h"
   6
   7static int all_attrs;
   8static int stdin_paths;
   9static const char * const check_attr_usage[] = {
  10"git check-attr [-a | --all | attr...] [--] pathname...",
  11"git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
  12NULL
  13};
  14
  15static int null_term_line;
  16
  17static const struct option check_attr_options[] = {
  18        OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
  19        OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
  20        OPT_BOOLEAN('z', NULL, &null_term_line,
  21                "input paths are terminated by a null character"),
  22        OPT_END()
  23};
  24
  25static void output_attr(int cnt, struct git_attr_check *check,
  26        const char *file)
  27{
  28        int j;
  29        for (j = 0; j < cnt; j++) {
  30                const char *value = check[j].value;
  31
  32                if (ATTR_TRUE(value))
  33                        value = "set";
  34                else if (ATTR_FALSE(value))
  35                        value = "unset";
  36                else if (ATTR_UNSET(value))
  37                        value = "unspecified";
  38
  39                quote_c_style(file, NULL, stdout, 0);
  40                printf(": %s: %s\n", git_attr_name(check[j].attr), value);
  41        }
  42}
  43
  44static void check_attr(const char *prefix, int cnt,
  45        struct git_attr_check *check, const char *file)
  46{
  47        char *full_path =
  48                prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
  49        if (check != NULL) {
  50                if (git_check_attr(full_path, cnt, check))
  51                        die("git_check_attr died");
  52                output_attr(cnt, check, file);
  53        } else {
  54                if (git_all_attrs(full_path, &cnt, &check))
  55                        die("git_all_attrs died");
  56                output_attr(cnt, check, file);
  57                free(check);
  58        }
  59        free(full_path);
  60}
  61
  62static void check_attr_stdin_paths(const char *prefix, int cnt,
  63        struct git_attr_check *check)
  64{
  65        struct strbuf buf, nbuf;
  66        int line_termination = null_term_line ? 0 : '\n';
  67
  68        strbuf_init(&buf, 0);
  69        strbuf_init(&nbuf, 0);
  70        while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
  71                if (line_termination && buf.buf[0] == '"') {
  72                        strbuf_reset(&nbuf);
  73                        if (unquote_c_style(&nbuf, buf.buf, NULL))
  74                                die("line is badly quoted");
  75                        strbuf_swap(&buf, &nbuf);
  76                }
  77                check_attr(prefix, cnt, check, buf.buf);
  78                maybe_flush_or_die(stdout, "attribute to stdout");
  79        }
  80        strbuf_release(&buf);
  81        strbuf_release(&nbuf);
  82}
  83
  84static NORETURN void error_with_usage(const char *msg)
  85{
  86        error("%s", msg);
  87        usage_with_options(check_attr_usage, check_attr_options);
  88}
  89
  90int cmd_check_attr(int argc, const char **argv, const char *prefix)
  91{
  92        struct git_attr_check *check;
  93        int cnt, i, doubledash, filei;
  94
  95        git_config(git_default_config, NULL);
  96
  97        argc = parse_options(argc, argv, prefix, check_attr_options,
  98                             check_attr_usage, PARSE_OPT_KEEP_DASHDASH);
  99
 100        if (read_cache() < 0) {
 101                die("invalid cache");
 102        }
 103
 104        doubledash = -1;
 105        for (i = 0; doubledash < 0 && i < argc; i++) {
 106                if (!strcmp(argv[i], "--"))
 107                        doubledash = i;
 108        }
 109
 110        /* Process --all and/or attribute arguments: */
 111        if (all_attrs) {
 112                if (doubledash >= 1)
 113                        error_with_usage("Attributes and --all both specified");
 114
 115                cnt = 0;
 116                filei = doubledash + 1;
 117        } else if (doubledash == 0) {
 118                error_with_usage("No attribute specified");
 119        } else if (doubledash < 0) {
 120                if (!argc)
 121                        error_with_usage("No attribute specified");
 122
 123                if (stdin_paths) {
 124                        /* Treat all arguments as attribute names. */
 125                        cnt = argc;
 126                        filei = argc;
 127                } else {
 128                        /* Treat exactly one argument as an attribute name. */
 129                        cnt = 1;
 130                        filei = 1;
 131                }
 132        } else {
 133                cnt = doubledash;
 134                filei = doubledash + 1;
 135        }
 136
 137        /* Check file argument(s): */
 138        if (stdin_paths) {
 139                if (filei < argc)
 140                        error_with_usage("Can't specify files with --stdin");
 141        } else {
 142                if (filei >= argc)
 143                        error_with_usage("No file specified");
 144        }
 145
 146        if (all_attrs) {
 147                check = NULL;
 148        } else {
 149                check = xcalloc(cnt, sizeof(*check));
 150                for (i = 0; i < cnt; i++) {
 151                        const char *name;
 152                        struct git_attr *a;
 153                        name = argv[i];
 154                        a = git_attr(name);
 155                        if (!a)
 156                                return error("%s: not a valid attribute name",
 157                                        name);
 158                        check[i].attr = a;
 159                }
 160        }
 161
 162        if (stdin_paths)
 163                check_attr_stdin_paths(prefix, cnt, check);
 164        else {
 165                for (i = filei; i < argc; i++)
 166                        check_attr(prefix, cnt, check, argv[i]);
 167                maybe_flush_or_die(stdout, "attribute to stdout");
 168        }
 169        return 0;
 170}