Skip to content

Commit b2b3e9c

Browse files
jaysoffiangitster
authored andcommitted
Teach '--cached' option to check-attr
This option causes check-attr to consider .gitattributes only from the index, ignoring .gitattributes from the working tree. This allows the command to be used in situations where a working tree does not exist. Signed-off-by: Jay Soffian <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 167a580 commit b2b3e9c

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
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: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,20 @@ test_expect_success 'attribute test: read paths from stdin' '
134134

135135
test_expect_success 'attribute test: --all option' '
136136
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
137+
grep -v unspecified <expect-all | sort >specified-all &&
138+
sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
139+
git check-attr --stdin --all <stdin-all | sort >actual &&
140+
test_cmp specified-all actual
141+
'
142+
143+
test_expect_success 'attribute test: --cached option' '
144+
145+
: >empty &&
146+
git check-attr --cached --stdin --all <stdin-all | sort >actual &&
147+
test_cmp empty actual &&
148+
git add .gitattributes a/.gitattributes a/b/.gitattributes &&
149+
git check-attr --cached --stdin --all <stdin-all | sort >actual &&
150+
test_cmp specified-all actual
141151
'
142152

143153
test_expect_success 'root subdir attribute test' '
@@ -168,6 +178,13 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
168178
169179
'
170180

181+
test_expect_success 'bare repository: check that --cached honors index' '
182+
GIT_INDEX_FILE=../.git/index \
183+
git check-attr --cached --stdin --all <../stdin-all |
184+
sort >actual &&
185+
test_cmp ../specified-all actual
186+
'
187+
171188
test_expect_success 'bare repository: test info/attributes' '
172189
173190
(

0 commit comments

Comments
 (0)