Skip to content

Commit 83d6b33

Browse files
committed
Merge branch 'js/check-attr-cached'
* js/check-attr-cached: t0003: remove extra whitespaces Teach '--cached' option to check-attr
2 parents 18445fd + 78cec75 commit 83d6b33

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

Documentation/git-check-attr.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ OPTIONS
2424
paths. If this option is used, then 'unspecified' attributes
2525
will not be included in the output.
2626

27+
--cached::
28+
Consider `.gitattributes` in the index only, ignoring the working tree.
29+
2730
--stdin::
2831
Read file names from stdin instead of from the command-line.
2932

builtin/check-attr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "parse-options.h"
66

77
static int all_attrs;
8+
static int cached_attrs;
89
static int stdin_paths;
910
static const char * const check_attr_usage[] = {
1011
"git check-attr [-a | --all | attr...] [--] pathname...",
@@ -16,6 +17,7 @@ static int null_term_line;
1617

1718
static const struct option check_attr_options[] = {
1819
OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"),
20+
OPT_BOOLEAN(0, "cached", &cached_attrs, "use .gitattributes only from the index"),
1921
OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"),
2022
OPT_BOOLEAN('z', NULL, &null_term_line,
2123
"input paths are terminated by a null character"),
@@ -99,6 +101,9 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
99101
die("invalid cache");
100102
}
101103

104+
if (cached_attrs)
105+
git_attr_set_direction(GIT_ATTR_INDEX, NULL);
106+
102107
doubledash = -1;
103108
for (i = 0; doubledash < 0 && i < argc; i++) {
104109
if (!strcmp(argv[i], "--"))

t/t0003-attributes.sh

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@ test_description=gitattributes
55
. ./test-lib.sh
66

77
attr_check () {
8-
9-
path="$1"
10-
expect="$2"
8+
path="$1" expect="$2"
119

1210
git check-attr test -- "$path" >actual 2>err &&
1311
echo "$path: test: $2" >expect &&
1412
test_cmp expect actual &&
1513
test_line_count = 0 err
16-
1714
}
1815

1916

2017
test_expect_success 'setup' '
21-
2218
mkdir -p a/b/d a/c b &&
2319
(
2420
echo "[attr]notest !test"
@@ -40,29 +36,27 @@ test_expect_success 'setup' '
4036
(
4137
echo "global test=global"
4238
) >"$HOME"/global-gitattributes &&
43-
cat <<EOF >expect-all
44-
f: test: f
45-
a/f: test: f
46-
a/c/f: test: f
47-
a/g: test: a/g
48-
a/b/g: test: a/b/g
49-
b/g: test: unspecified
50-
a/b/h: test: a/b/h
51-
a/b/d/g: test: a/b/d/*
52-
onoff: test: unset
53-
offon: test: set
54-
no: notest: set
55-
no: test: unspecified
56-
a/b/d/no: notest: set
57-
a/b/d/no: test: a/b/d/*
58-
a/b/d/yes: notest: set
59-
a/b/d/yes: test: unspecified
60-
EOF
61-
39+
cat <<-EOF >expect-all
40+
f: test: f
41+
a/f: test: f
42+
a/c/f: test: f
43+
a/g: test: a/g
44+
a/b/g: test: a/b/g
45+
b/g: test: unspecified
46+
a/b/h: test: a/b/h
47+
a/b/d/g: test: a/b/d/*
48+
onoff: test: unset
49+
offon: test: set
50+
no: notest: set
51+
no: test: unspecified
52+
a/b/d/no: notest: set
53+
a/b/d/no: test: a/b/d/*
54+
a/b/d/yes: notest: set
55+
a/b/d/yes: test: unspecified
56+
EOF
6257
'
6358

6459
test_expect_success 'command line checks' '
65-
6660
test_must_fail git check-attr &&
6761
test_must_fail git check-attr -- &&
6862
test_must_fail git check-attr test &&
@@ -72,11 +66,9 @@ test_expect_success 'command line checks' '
7266
echo "f" | test_must_fail git check-attr --stdin -- f &&
7367
echo "f" | test_must_fail git check-attr --stdin test -- f &&
7468
test_must_fail git check-attr "" -- f
75-
7669
'
7770

7871
test_expect_success 'attribute test' '
79-
8072
attr_check f f &&
8173
attr_check a/f f &&
8274
attr_check a/c/f f &&
@@ -90,20 +82,16 @@ test_expect_success 'attribute test' '
9082
attr_check no unspecified &&
9183
attr_check a/b/d/no "a/b/d/*" &&
9284
attr_check a/b/d/yes unspecified
93-
9485
'
9586

9687
test_expect_success 'unnormalized paths' '
97-
9888
attr_check ./f f &&
9989
attr_check ./a/g a/g &&
10090
attr_check a/./g a/g &&
10191
attr_check a/c/../b/g a/b/g
102-
10392
'
10493

10594
test_expect_success 'relative paths' '
106-
10795
(cd a && attr_check ../f f) &&
10896
(cd a && attr_check f f) &&
10997
(cd a && attr_check i a/i) &&
@@ -112,7 +100,6 @@ test_expect_success 'relative paths' '
112100
(cd b && attr_check ../a/f f) &&
113101
(cd b && attr_check ../a/g a/g) &&
114102
(cd b && attr_check ../a/b/g a/b/g)
115-
116103
'
117104

118105
test_expect_success 'core.attributesfile' '
@@ -121,41 +108,43 @@ test_expect_success 'core.attributesfile' '
121108
attr_check global global &&
122109
git config core.attributesfile "~/global-gitattributes" &&
123110
attr_check global global &&
124-
echo "global test=precedence" >> .gitattributes &&
111+
echo "global test=precedence" >>.gitattributes &&
125112
attr_check global precedence
126113
'
127114

128115
test_expect_success 'attribute test: read paths from stdin' '
129-
130-
grep -v notest < expect-all > expect &&
131-
sed -e "s/:.*//" < expect | git check-attr --stdin test > actual &&
116+
grep -v notest <expect-all >expect &&
117+
sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
132118
test_cmp expect actual
133119
'
134120

135121
test_expect_success 'attribute test: --all option' '
122+
grep -v unspecified <expect-all | sort >specified-all &&
123+
sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
124+
git check-attr --stdin --all <stdin-all | sort >actual &&
125+
test_cmp specified-all actual
126+
'
136127

137-
grep -v unspecified < expect-all | sort > expect &&
138-
sed -e "s/:.*//" < expect-all | uniq |
139-
git check-attr --stdin --all | sort > actual &&
140-
test_cmp expect actual
128+
test_expect_success 'attribute test: --cached option' '
129+
: >empty &&
130+
git check-attr --cached --stdin --all <stdin-all | sort >actual &&
131+
test_cmp empty actual &&
132+
git add .gitattributes a/.gitattributes a/b/.gitattributes &&
133+
git check-attr --cached --stdin --all <stdin-all | sort >actual &&
134+
test_cmp specified-all actual
141135
'
142136

143137
test_expect_success 'root subdir attribute test' '
144-
145138
attr_check a/i a/i &&
146139
attr_check subdir/a/i unspecified
147-
148140
'
149141

150142
test_expect_success 'setup bare' '
151-
152143
git clone --bare . bare.git &&
153144
cd bare.git
154-
155145
'
156146

157147
test_expect_success 'bare repository: check that .gitattribute is ignored' '
158-
159148
(
160149
echo "f test=f"
161150
echo "a/i test=a/i"
@@ -165,11 +154,16 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
165154
attr_check a/c/f unspecified &&
166155
attr_check a/i unspecified &&
167156
attr_check subdir/a/i unspecified
157+
'
168158

159+
test_expect_success 'bare repository: check that --cached honors index' '
160+
GIT_INDEX_FILE=../.git/index \
161+
git check-attr --cached --stdin --all <../stdin-all |
162+
sort >actual &&
163+
test_cmp ../specified-all actual
169164
'
170165

171166
test_expect_success 'bare repository: test info/attributes' '
172-
173167
(
174168
echo "f test=f"
175169
echo "a/i test=a/i"
@@ -179,7 +173,6 @@ test_expect_success 'bare repository: test info/attributes' '
179173
attr_check a/c/f f &&
180174
attr_check a/i a/i &&
181175
attr_check subdir/a/i unspecified
182-
183176
'
184177

185178
test_done

0 commit comments

Comments
 (0)