From b2b3e9c2d623359ed680a5eef850ef2fd313b73c Mon Sep 17 00:00:00 2001 From: Jay Soffian Date: Thu, 22 Sep 2011 17:44:20 -0400 Subject: [PATCH 1/2] 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 Signed-off-by: Junio C Hamano --- Documentation/git-check-attr.txt | 3 +++ builtin/check-attr.c | 5 +++++ t/t0003-attributes.sh | 25 +++++++++++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Documentation/git-check-attr.txt b/Documentation/git-check-attr.txt index 1f7312a189..5abdbaa51c 100644 --- a/Documentation/git-check-attr.txt +++ b/Documentation/git-check-attr.txt @@ -24,6 +24,9 @@ OPTIONS paths. If this option is used, then 'unspecified' attributes will not be included in the output. +--cached:: + Consider `.gitattributes` in the index only, ignoring the working tree. + --stdin:: Read file names from stdin instead of from the command-line. diff --git a/builtin/check-attr.c b/builtin/check-attr.c index 708988a0e1..ded0d836d3 100644 --- a/builtin/check-attr.c +++ b/builtin/check-attr.c @@ -5,6 +5,7 @@ #include "parse-options.h" static int all_attrs; +static int cached_attrs; static int stdin_paths; static const char * const check_attr_usage[] = { "git check-attr [-a | --all | attr...] [--] pathname...", @@ -16,6 +17,7 @@ static int null_term_line; static const struct option check_attr_options[] = { OPT_BOOLEAN('a', "all", &all_attrs, "report all attributes set on file"), + OPT_BOOLEAN(0, "cached", &cached_attrs, "use .gitattributes only from the index"), OPT_BOOLEAN(0 , "stdin", &stdin_paths, "read file names from stdin"), OPT_BOOLEAN('z', NULL, &null_term_line, "input paths are terminated by a null character"), @@ -99,6 +101,9 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix) die("invalid cache"); } + if (cached_attrs) + git_attr_set_direction(GIT_ATTR_INDEX, NULL); + doubledash = -1; for (i = 0; doubledash < 0 && i < argc; i++) { if (!strcmp(argv[i], "--")) diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh index ae2f1da28f..c0a45630be 100755 --- a/t/t0003-attributes.sh +++ b/t/t0003-attributes.sh @@ -134,10 +134,20 @@ test_expect_success 'attribute test: read paths from stdin' ' test_expect_success 'attribute test: --all option' ' - grep -v unspecified < expect-all | sort > expect && - sed -e "s/:.*//" < expect-all | uniq | - git check-attr --stdin --all | sort > actual && - test_cmp expect actual + grep -v unspecified specified-all && + sed -e "s/:.*//" stdin-all && + git check-attr --stdin --all actual && + test_cmp specified-all actual +' + +test_expect_success 'attribute test: --cached option' ' + + : >empty && + git check-attr --cached --stdin --all actual && + test_cmp empty actual && + git add .gitattributes a/.gitattributes a/b/.gitattributes && + git check-attr --cached --stdin --all actual && + test_cmp specified-all actual ' test_expect_success 'root subdir attribute test' ' @@ -168,6 +178,13 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' ' ' +test_expect_success 'bare repository: check that --cached honors index' ' + GIT_INDEX_FILE=../.git/index \ + git check-attr --cached --stdin --all <../stdin-all | + sort >actual && + test_cmp ../specified-all actual +' + test_expect_success 'bare repository: test info/attributes' ' ( From 78cec75747995a8613a8f27625aeaa4ec4b19651 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 22 Sep 2011 16:34:05 -0700 Subject: [PATCH 2/2] t0003: remove extra whitespaces The test had excess whitespaces everywhere that made it harder to read than necessary. Remove them. Signed-off-by: Junio C Hamano --- t/t0003-attributes.sh | 68 ++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 46 deletions(-) diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh index c0a45630be..46b0736b35 100755 --- a/t/t0003-attributes.sh +++ b/t/t0003-attributes.sh @@ -5,20 +5,16 @@ test_description=gitattributes . ./test-lib.sh attr_check () { - - path="$1" - expect="$2" + path="$1" expect="$2" git check-attr test -- "$path" >actual 2>err && echo "$path: test: $2" >expect && test_cmp expect actual && test_line_count = 0 err - } test_expect_success 'setup' ' - mkdir -p a/b/d a/c b && ( echo "[attr]notest !test" @@ -40,29 +36,27 @@ test_expect_success 'setup' ' ( echo "global test=global" ) >"$HOME"/global-gitattributes && - cat <expect-all -f: test: f -a/f: test: f -a/c/f: test: f -a/g: test: a/g -a/b/g: test: a/b/g -b/g: test: unspecified -a/b/h: test: a/b/h -a/b/d/g: test: a/b/d/* -onoff: test: unset -offon: test: set -no: notest: set -no: test: unspecified -a/b/d/no: notest: set -a/b/d/no: test: a/b/d/* -a/b/d/yes: notest: set -a/b/d/yes: test: unspecified -EOF - + cat <<-EOF >expect-all + f: test: f + a/f: test: f + a/c/f: test: f + a/g: test: a/g + a/b/g: test: a/b/g + b/g: test: unspecified + a/b/h: test: a/b/h + a/b/d/g: test: a/b/d/* + onoff: test: unset + offon: test: set + no: notest: set + no: test: unspecified + a/b/d/no: notest: set + a/b/d/no: test: a/b/d/* + a/b/d/yes: notest: set + a/b/d/yes: test: unspecified + EOF ' test_expect_success 'command line checks' ' - test_must_fail git check-attr && test_must_fail git check-attr -- && test_must_fail git check-attr test && @@ -72,11 +66,9 @@ test_expect_success 'command line checks' ' echo "f" | test_must_fail git check-attr --stdin -- f && echo "f" | test_must_fail git check-attr --stdin test -- f && test_must_fail git check-attr "" -- f - ' test_expect_success 'attribute test' ' - attr_check f f && attr_check a/f f && attr_check a/c/f f && @@ -90,20 +82,16 @@ test_expect_success 'attribute test' ' attr_check no unspecified && attr_check a/b/d/no "a/b/d/*" && attr_check a/b/d/yes unspecified - ' test_expect_success 'unnormalized paths' ' - attr_check ./f f && attr_check ./a/g a/g && attr_check a/./g a/g && attr_check a/c/../b/g a/b/g - ' test_expect_success 'relative paths' ' - (cd a && attr_check ../f f) && (cd a && attr_check f f) && (cd a && attr_check i a/i) && @@ -112,7 +100,6 @@ test_expect_success 'relative paths' ' (cd b && attr_check ../a/f f) && (cd b && attr_check ../a/g a/g) && (cd b && attr_check ../a/b/g a/b/g) - ' test_expect_success 'core.attributesfile' ' @@ -121,19 +108,17 @@ test_expect_success 'core.attributesfile' ' attr_check global global && git config core.attributesfile "~/global-gitattributes" && attr_check global global && - echo "global test=precedence" >> .gitattributes && + echo "global test=precedence" >>.gitattributes && attr_check global precedence ' test_expect_success 'attribute test: read paths from stdin' ' - - grep -v notest < expect-all > expect && - sed -e "s/:.*//" < expect | git check-attr --stdin test > actual && + grep -v notest expect && + sed -e "s/:.*//" actual && test_cmp expect actual ' test_expect_success 'attribute test: --all option' ' - grep -v unspecified specified-all && sed -e "s/:.*//" stdin-all && git check-attr --stdin --all actual && @@ -141,7 +126,6 @@ test_expect_success 'attribute test: --all option' ' ' test_expect_success 'attribute test: --cached option' ' - : >empty && git check-attr --cached --stdin --all actual && test_cmp empty actual && @@ -151,21 +135,16 @@ test_expect_success 'attribute test: --cached option' ' ' test_expect_success 'root subdir attribute test' ' - attr_check a/i a/i && attr_check subdir/a/i unspecified - ' test_expect_success 'setup bare' ' - git clone --bare . bare.git && cd bare.git - ' test_expect_success 'bare repository: check that .gitattribute is ignored' ' - ( echo "f test=f" echo "a/i test=a/i" @@ -175,7 +154,6 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' ' attr_check a/c/f unspecified && attr_check a/i unspecified && attr_check subdir/a/i unspecified - ' test_expect_success 'bare repository: check that --cached honors index' ' @@ -186,7 +164,6 @@ test_expect_success 'bare repository: check that --cached honors index' ' ' test_expect_success 'bare repository: test info/attributes' ' - ( echo "f test=f" echo "a/i test=a/i" @@ -196,7 +173,6 @@ test_expect_success 'bare repository: test info/attributes' ' attr_check a/c/f f && attr_check a/i a/i && attr_check subdir/a/i unspecified - ' test_done