Skip to content

Commit 4ca0f18

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdf6be8 commit 4ca0f18

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

Documentation/git-check-attr.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ git-check-attr - Display gitattributes information
99
SYNOPSIS
1010
--------
1111
[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>
1414

1515
DESCRIPTION
1616
-----------
@@ -19,6 +19,11 @@ For every pathname, this command will list if each attribute is 'unspecified',
1919

2020
OPTIONS
2121
-------
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+
2227
--stdin::
2328
Read file names from stdin instead of from the command-line.
2429

@@ -69,6 +74,13 @@ org/example/MyClass.java: diff: java
6974
org/example/MyClass.java: myAttr: set
7075
---------------
7176

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+
7284
* Listing an attribute for multiple files:
7385
---------------
7486
$ git check-attr myAttr -- org/example/MyClass.java org/example/NoMyAttr.java

builtin/check-attr.c

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
#include "quote.h"
55
#include "parse-options.h"
66

7+
static int all_attrs;
78
static int stdin_paths;
89
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>",
1112
NULL
1213
};
1314

1415
static int null_term_line;
1516

1617
static const struct option check_attr_options[] = {
18+
OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
1719
OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
1820
OPT_BOOLEAN('z', NULL, &null_term_line,
1921
"input paths are terminated by a null character"),
@@ -42,9 +44,16 @@ static void output_attr(int cnt, struct git_attr_check *check,
4244
static void check_attr(int cnt, struct git_attr_check *check,
4345
const char *file)
4446
{
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+
}
4857
}
4958

5059
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)
92101
doubledash = i;
93102
}
94103

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) {
97112
error_with_usage("No attribute specified");
98113
} else if (doubledash < 0) {
99114
/*
@@ -119,15 +134,20 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
119134
error_with_usage("No file specified");
120135
}
121136

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+
}
131151
}
132152

133153
if (stdin_paths)

t/t0003-attributes.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ EOF
107107
test_cmp expect actual
108108
'
109109

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+
110134
test_expect_success 'root subdir attribute test' '
111135
112136
attr_check a/i a/i &&

0 commit comments

Comments
 (0)