git-check-attr: Add an --all option to show all attributes
Add new usage patterns git check-attr [-a | --all] [--] pathname... git check-attr --stdin [-a | --all] < <list-of-paths> which display all attributes associated with the specified file(s). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Aug 4, 2011 at 06:36 UTC
4ca0f188f6ece3098b60b6ba732e1e8551466f60
3 files changed
+74
-18
Documentation/git-check-attr.txt
+14
-2
@@ -9,8 +9,8 @@ git-check-attr - Display gitattributes information
9
SYNOPSIS
10
--------
11
[verse]
12
-'git check-attr' attr... [--] pathname...
13
-'git check-attr' --stdin [-z] attr... < <list-of-paths>
12
+'git check-attr' [-a | --all | attr...] [--] pathname...
13
+'git check-attr' --stdin [-z] [-a | --all | attr...] < <list-of-paths>
14
15
DESCRIPTION
16
-----------
@@ -19,6 +19,11 @@ For every pathname, this command will list if each attribute is 'unspecified',
19
20
OPTIONS
21
-------
22
+-a, --all::
23
+ List all attributes that are associated with the specified
24
+ paths. If this option is used, then 'unspecified' attributes
25
+ will not be included in the output.
26
+
27
--stdin::
28
Read file names from stdin instead of from the command-line.
29
@@ -69,6 +74,13 @@ org/example/MyClass.java: diff: java
74
org/example/MyClass.java: myAttr: set
75
---------------
76
77
+* Listing all attributes for a file:
78
+---------------
79
+$ git check-attr --all -- org/example/MyClass.java
80
+org/example/MyClass.java: diff: java
81
+org/example/MyClass.java: myAttr: set
82
+---------------
83
+
84
* Listing an attribute for multiple files:
85
---------------
86
$ git check-attr myAttr -- org/example/MyClass.java org/example/NoMyAttr.java
builtin/check-attr.c
+36
-16
@@ -4,16 +4,18 @@
4
#include "quote.h"
5
#include "parse-options.h"
6
7
+static int all_attrs;
8
static int stdin_paths;
9
static const char * const check_attr_usage[] = {
9
-"git check-attr attr... [--] pathname...",
10
-"git check-attr --stdin attr... < <list-of-paths>",
10
+"git check-attr [-a | --all | attr...] [--] pathname...",
11
+"git check-attr --stdin [-a | --all | attr...] < <list-of-paths>",
12
NULL
13
};
14
15
static int null_term_line;
16
17
static 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"),
@@ -42,9 +44,16 @@ static void output_attr(int cnt, struct git_attr_check *check,
44
static void check_attr(int cnt, struct git_attr_check *check,
45
const char *file)
46
{
45
- if (git_checkattr(file, cnt, check))
46
- die("git_checkattr died");
47
- output_attr(cnt, check, file);
47
+ if (check != NULL) {
48
+ if (git_checkattr(file, cnt, check))
49
+ die("git_checkattr died");
50
+ output_attr(cnt, check, file);
51
+ } else {
52
+ if (git_all_attrs(file, &cnt, &check))
53
+ die("git_all_attrs died");
54
+ output_attr(cnt, check, file);
55
+ free(check);
56
+ }
57
}
58
59
static void check_attr_stdin_paths(int cnt, struct git_attr_check *check)
@@ -92,8 +101,14 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
101
doubledash = i;
102
}
103
95
- /* Check attribute argument(s): */
96
- if (doubledash == 0) {
104
+ /* Process --all and/or attribute arguments: */
105
+ if (all_attrs) {
106
+ if (doubledash >= 1)
107
+ error_with_usage("Attributes and --all both specified");
108
+
109
+ cnt = 0;
110
+ filei = doubledash + 1;
111
+ } else if (doubledash == 0) {
112
error_with_usage("No attribute specified");
113
} else if (doubledash < 0) {
114
/*
@@ -119,15 +134,20 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
134
error_with_usage("No file specified");
135
}
136
122
- check = xcalloc(cnt, sizeof(*check));
123
- for (i = 0; i < cnt; i++) {
124
- const char *name;
125
- struct git_attr *a;
126
- name = argv[i];
127
- a = git_attr(name);
128
- if (!a)
129
- return error("%s: not a valid attribute name", name);
130
- check[i].attr = a;
137
+ if (all_attrs) {
138
+ check = NULL;
139
+ } else {
140
+ check = xcalloc(cnt, sizeof(*check));
141
+ for (i = 0; i < cnt; i++) {
142
+ const char *name;
143
+ struct git_attr *a;
144
+ name = argv[i];
145
+ a = git_attr(name);
146
+ if (!a)
147
+ return error("%s: not a valid attribute name",
148
+ name);
149
+ check[i].attr = a;
150
+ }
151
}
152
153
if (stdin_paths)
t/t0003-attributes.sh
+24
@@ -107,6 +107,30 @@ EOF
107
test_cmp expect actual
108
'
109
110
+test_expect_success 'attribute test: --all option' '
111
+
112
+ cat <<EOF > all &&
113
+f: test: f
114
+a/f: test: f
115
+a/c/f: test: f
116
+a/g: test: a/g
117
+a/b/g: test: a/b/g
118
+b/g: test: unspecified
119
+a/b/h: test: a/b/h
120
+a/b/d/g: test: a/b/d/*
121
+onoff: test: unset
122
+offon: test: set
123
+no: notest: set
124
+a/b/d/no: test: a/b/d/*
125
+a/b/d/no: notest: set
126
+a/b/d/yes: notest: set
127
+EOF
128
+
129
+ grep -v unspecified < all | sort > expect &&
130
+ sed -e "s/:.*//" < all | uniq | git check-attr --stdin --all | sort > actual &&
131
+ test_cmp expect actual
132
+'
133
+
134
test_expect_success 'root subdir attribute test' '
135
136
attr_check a/i a/i &&