Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Brian Downing | 6b06d51 | 2007-08-14 08:18:38 -0500 | [diff] [blame] | 2 | #include "cache.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 3 | #include "attr.h" |
| 4 | #include "quote.h" |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 5 | #include "parse-options.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 6 | |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 7 | static int stdin_paths; |
| 8 | static const char * const check_attr_usage[] = { |
| 9 | "git check-attr attr... [--] pathname...", |
| 10 | "git check-attr --stdin attr... < <list-of-paths>", |
| 11 | NULL |
| 12 | }; |
| 13 | |
| 14 | static int null_term_line; |
| 15 | |
| 16 | static const struct option check_attr_options[] = { |
| 17 | OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"), |
| 18 | OPT_BOOLEAN('z', NULL, &null_term_line, |
| 19 | "input paths are terminated by a null character"), |
| 20 | OPT_END() |
| 21 | }; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 22 | |
Dmitry Potapov | 41038c5 | 2008-10-07 04:14:18 +0400 | [diff] [blame] | 23 | static void check_attr(int cnt, struct git_attr_check *check, |
| 24 | const char** name, const char *file) |
| 25 | { |
| 26 | int j; |
| 27 | if (git_checkattr(file, cnt, check)) |
| 28 | die("git_checkattr died"); |
| 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", name[j], value); |
| 41 | } |
| 42 | } |
| 43 | |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 44 | static void check_attr_stdin_paths(int cnt, struct git_attr_check *check, |
| 45 | const char** name) |
| 46 | { |
| 47 | struct strbuf buf, nbuf; |
| 48 | int line_termination = null_term_line ? 0 : '\n'; |
| 49 | |
| 50 | strbuf_init(&buf, 0); |
| 51 | strbuf_init(&nbuf, 0); |
| 52 | while (strbuf_getline(&buf, stdin, line_termination) != EOF) { |
| 53 | if (line_termination && buf.buf[0] == '"') { |
| 54 | strbuf_reset(&nbuf); |
| 55 | if (unquote_c_style(&nbuf, buf.buf, NULL)) |
| 56 | die("line is badly quoted"); |
| 57 | strbuf_swap(&buf, &nbuf); |
| 58 | } |
| 59 | check_attr(cnt, check, name, buf.buf); |
| 60 | maybe_flush_or_die(stdout, "attribute to stdout"); |
| 61 | } |
| 62 | strbuf_release(&buf); |
| 63 | strbuf_release(&nbuf); |
| 64 | } |
| 65 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 66 | int cmd_check_attr(int argc, const char **argv, const char *prefix) |
| 67 | { |
| 68 | struct git_attr_check *check; |
| 69 | int cnt, i, doubledash; |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 70 | const char *errstr = NULL; |
| 71 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 72 | argc = parse_options(argc, argv, prefix, check_attr_options, |
| 73 | check_attr_usage, PARSE_OPT_KEEP_DASHDASH); |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 74 | if (!argc) |
| 75 | usage_with_options(check_attr_usage, check_attr_options); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 76 | |
Brian Downing | 6b06d51 | 2007-08-14 08:18:38 -0500 | [diff] [blame] | 77 | if (read_cache() < 0) { |
| 78 | die("invalid cache"); |
| 79 | } |
| 80 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 81 | doubledash = -1; |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 82 | for (i = 0; doubledash < 0 && i < argc; i++) { |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 83 | if (!strcmp(argv[i], "--")) |
| 84 | doubledash = i; |
| 85 | } |
| 86 | |
| 87 | /* If there is no double dash, we handle only one attribute */ |
| 88 | if (doubledash < 0) { |
| 89 | cnt = 1; |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 90 | doubledash = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 91 | } else |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 92 | cnt = doubledash; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 93 | doubledash++; |
| 94 | |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 95 | if (cnt <= 0) |
| 96 | errstr = "No attribute specified"; |
| 97 | else if (stdin_paths && doubledash < argc) |
| 98 | errstr = "Can't specify files with --stdin"; |
| 99 | if (errstr) { |
Daniel Lowe | 42fc113 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 100 | error("%s", errstr); |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 101 | usage_with_options(check_attr_usage, check_attr_options); |
| 102 | } |
| 103 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 104 | check = xcalloc(cnt, sizeof(*check)); |
| 105 | for (i = 0; i < cnt; i++) { |
| 106 | const char *name; |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 107 | struct git_attr *a; |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 108 | name = argv[i]; |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 109 | a = git_attr(name, strlen(name)); |
| 110 | if (!a) |
| 111 | return error("%s: not a valid attribute name", name); |
| 112 | check[i].attr = a; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Dmitry Potapov | b466685 | 2008-10-07 04:16:52 +0400 | [diff] [blame] | 115 | if (stdin_paths) |
| 116 | check_attr_stdin_paths(cnt, check, argv); |
| 117 | else { |
| 118 | for (i = doubledash; i < argc; i++) |
| 119 | check_attr(cnt, check, argv, argv[i]); |
| 120 | maybe_flush_or_die(stdout, "attribute to stdout"); |
| 121 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |