check-attr: Add --stdin option
This allows multiple paths to be specified on stdin. Signed-off-by: Dmitry Potapov <dpotapov@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Dmitry Potapov committed
Oct 7, 2008 at 04:16 UTC
b4666852a06af0c969ad8f1c444d0b48c4451e76
3 files changed
+89
-11
Documentation/git-check-attr.txt
+8
@@ -9,6 +9,7 @@ git-check-attr - Display gitattributes information.
9
SYNOPSIS
10
--------
11
'git check-attr' attr... [--] pathname...
12
+'git check-attr' --stdin [-z] attr... < <list-of-paths
13
14
DESCRIPTION
15
-----------
@@ -17,6 +18,13 @@ For every pathname, this command will list if each attr is 'unspecified',
18
19
OPTIONS
20
-------
21
+--stdin::
22
+ Read file names from stdin instead of from the command-line.
23
+
24
+-z::
25
+ Only meaningful with `--stdin`; paths are separated with
26
+ NUL character instead of LF.
27
+
28
\--::
29
Interpret all preceding arguments as attributes, and all following
30
arguments as path names. If not supplied, only the first argument will
builtin-check-attr.c
+64
-11
@@ -2,9 +2,23 @@
2
#include "cache.h"
3
#include "attr.h"
4
#include "quote.h"
5
+#include "parse-options.h"
6
6
-static const char check_attr_usage[] =
7
-"git check-attr attr... [--] pathname...";
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
+};
22
23
static void check_attr(int cnt, struct git_attr_check *check,
24
const char** name, const char *file)
@@ -27,17 +41,45 @@ static void check_attr(int cnt, struct git_attr_check *check,
41
}
42
}
43
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
+
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;
70
+ const char *errstr = NULL;
71
+
72
+ argc = parse_options(argc, argv, check_attr_options, check_attr_usage,
73
+ PARSE_OPT_KEEP_DASHDASH);
74
+ if (!argc)
75
+ usage_with_options(check_attr_usage, check_attr_options);
76
77
if (read_cache() < 0) {
78
die("invalid cache");
79
}
80
81
doubledash = -1;
40
- for (i = 1; doubledash < 0 && i < argc; i++) {
82
+ for (i = 0; doubledash < 0 && i < argc; i++) {
83
if (!strcmp(argv[i], "--"))
84
doubledash = i;
85
}
@@ -45,26 +87,37 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
87
/* If there is no double dash, we handle only one attribute */
88
if (doubledash < 0) {
89
cnt = 1;
48
- doubledash = 1;
90
+ doubledash = 0;
91
} else
50
- cnt = doubledash - 1;
92
+ cnt = doubledash;
93
doubledash++;
94
53
- if (cnt <= 0 || argc < doubledash)
54
- usage(check_attr_usage);
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) {
100
+ error (errstr);
101
+ usage_with_options(check_attr_usage, check_attr_options);
102
+ }
103
+
104
check = xcalloc(cnt, sizeof(*check));
105
for (i = 0; i < cnt; i++) {
106
const char *name;
107
struct git_attr *a;
59
- name = argv[i + 1];
108
+ name = argv[i];
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;
113
}
114
66
- for (i = doubledash; i < argc; i++)
67
- check_attr(cnt, check, argv+1, argv[i]);
68
- maybe_flush_or_die(stdout, "attribute to stdout");
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
+ }
122
return 0;
123
}
t/t0003-attributes.sh
+17
@@ -47,6 +47,23 @@ test_expect_success 'attribute test' '
47
48
'
49
50
+test_expect_success 'attribute test: read paths from stdin' '
51
+
52
+ cat <<EOF > expect
53
+f: test: f
54
+a/f: test: f
55
+a/c/f: test: f
56
+a/g: test: a/g
57
+a/b/g: test: a/b/g
58
+b/g: test: unspecified
59
+a/b/h: test: a/b/h
60
+a/b/d/g: test: a/b/d/*
61
+EOF
62
+
63
+ sed -e "s/:.*//" < expect | git check-attr --stdin test > actual &&
64
+ test_cmp expect actual
65
+'
66
+
67
test_expect_success 'root subdir attribute test' '
68
69
attr_check a/i a/i &&