Merge branch 'zh/ref-filter-raw-data'

Prepare the "ref-filter" machinery that drives the "--format"
option of "git for-each-ref" and its friends to be used in "git
cat-file --batch".

* zh/ref-filter-raw-data:
  ref-filter: add %(rest) atom
  ref-filter: use non-const ref_format in *_atom_parser()
  ref-filter: --format=%(raw) support --perl
  ref-filter: add %(raw) atom
  ref-filter: add obj-type check in grab contents
This commit is contained in:
Junio C Hamano
2021-08-24 15:32:37 -07:00
10 changed files with 465 additions and 63 deletions

View File

@ -340,6 +340,10 @@ test_expect_success 'git branch --format option' '
test_cmp expect actual
'
test_expect_success 'git branch with --format=%(rest) must fail' '
test_must_fail git branch --format="%(rest)" >actual
'
test_expect_success 'worktree colors correct' '
cat >expect <<-EOF &&
* <GREEN>(HEAD detached from fromtag)<RESET>

View File

@ -130,6 +130,8 @@ test_atom head parent:short=10 ''
test_atom head numparent 0
test_atom head object ''
test_atom head type ''
test_atom head raw "$(git cat-file commit refs/heads/main)
"
test_atom head '*objectname' ''
test_atom head '*objecttype' ''
test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
@ -221,6 +223,15 @@ test_atom tag contents 'Tagging at 1151968727
'
test_atom tag HEAD ' '
test_expect_success 'basic atom: refs/tags/testtag *raw' '
git cat-file commit refs/tags/testtag^{} >expected &&
git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
sanitize_pgp <expected >expected.clean &&
echo >>expected.clean &&
sanitize_pgp <actual >actual.clean &&
test_cmp expected.clean actual.clean
'
test_expect_success 'Check invalid atoms names are errors' '
test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
'
@ -686,6 +697,15 @@ test_atom refs/tags/signed-empty contents:body ''
test_atom refs/tags/signed-empty contents:signature "$sig"
test_atom refs/tags/signed-empty contents "$sig"
test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '
git cat-file tag refs/tags/signed-empty >expected &&
git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
sanitize_pgp <expected >expected.clean &&
echo >>expected.clean &&
sanitize_pgp <actual >actual.clean &&
test_cmp expected.clean actual.clean
'
test_atom refs/tags/signed-short subject 'subject line'
test_atom refs/tags/signed-short subject:sanitize 'subject-line'
test_atom refs/tags/signed-short contents:subject 'subject line'
@ -695,6 +715,15 @@ test_atom refs/tags/signed-short contents:signature "$sig"
test_atom refs/tags/signed-short contents "subject line
$sig"
test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '
git cat-file tag refs/tags/signed-short >expected &&
git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
sanitize_pgp <expected >expected.clean &&
echo >>expected.clean &&
sanitize_pgp <actual >actual.clean &&
test_cmp expected.clean actual.clean
'
test_atom refs/tags/signed-long subject 'subject line'
test_atom refs/tags/signed-long subject:sanitize 'subject-line'
test_atom refs/tags/signed-long contents:subject 'subject line'
@ -708,6 +737,15 @@ test_atom refs/tags/signed-long contents "subject line
body contents
$sig"
test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '
git cat-file tag refs/tags/signed-long >expected &&
git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
sanitize_pgp <expected >expected.clean &&
echo >>expected.clean &&
sanitize_pgp <actual >actual.clean &&
test_cmp expected.clean actual.clean
'
test_expect_success 'set up refs pointing to tree and blob' '
git update-ref refs/mytrees/first refs/heads/main^{tree} &&
git update-ref refs/myblobs/first refs/heads/main:one
@ -720,6 +758,16 @@ test_atom refs/mytrees/first contents:body ""
test_atom refs/mytrees/first contents:signature ""
test_atom refs/mytrees/first contents ""
test_expect_success 'basic atom: refs/mytrees/first raw' '
git cat-file tree refs/mytrees/first >expected &&
echo >>expected &&
git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
test_cmp expected actual &&
git cat-file -s refs/mytrees/first >expected &&
git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
test_cmp expected actual
'
test_atom refs/myblobs/first subject ""
test_atom refs/myblobs/first contents:subject ""
test_atom refs/myblobs/first body ""
@ -727,6 +775,189 @@ test_atom refs/myblobs/first contents:body ""
test_atom refs/myblobs/first contents:signature ""
test_atom refs/myblobs/first contents ""
test_expect_success 'basic atom: refs/myblobs/first raw' '
git cat-file blob refs/myblobs/first >expected &&
echo >>expected &&
git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
test_cmp expected actual &&
git cat-file -s refs/myblobs/first >expected &&
git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
test_cmp expected actual
'
test_expect_success 'set up refs pointing to binary blob' '
printf "a\0b\0c" >blob1 &&
printf "a\0c\0b" >blob2 &&
printf "\0a\0b\0c" >blob3 &&
printf "abc" >blob4 &&
printf "\0 \0 \0 " >blob5 &&
printf "\0 \0a\0 " >blob6 &&
printf " " >blob7 &&
>blob8 &&
obj=$(git hash-object -w blob1) &&
git update-ref refs/myblobs/blob1 "$obj" &&
obj=$(git hash-object -w blob2) &&
git update-ref refs/myblobs/blob2 "$obj" &&
obj=$(git hash-object -w blob3) &&
git update-ref refs/myblobs/blob3 "$obj" &&
obj=$(git hash-object -w blob4) &&
git update-ref refs/myblobs/blob4 "$obj" &&
obj=$(git hash-object -w blob5) &&
git update-ref refs/myblobs/blob5 "$obj" &&
obj=$(git hash-object -w blob6) &&
git update-ref refs/myblobs/blob6 "$obj" &&
obj=$(git hash-object -w blob7) &&
git update-ref refs/myblobs/blob7 "$obj" &&
obj=$(git hash-object -w blob8) &&
git update-ref refs/myblobs/blob8 "$obj"
'
test_expect_success 'Verify sorts with raw' '
cat >expected <<-EOF &&
refs/myblobs/blob8
refs/myblobs/blob5
refs/myblobs/blob6
refs/myblobs/blob3
refs/myblobs/blob7
refs/mytrees/first
refs/myblobs/first
refs/myblobs/blob1
refs/myblobs/blob2
refs/myblobs/blob4
refs/heads/main
EOF
git for-each-ref --format="%(refname)" --sort=raw \
refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
test_cmp expected actual
'
test_expect_success 'Verify sorts with raw:size' '
cat >expected <<-EOF &&
refs/myblobs/blob8
refs/myblobs/first
refs/myblobs/blob7
refs/heads/main
refs/myblobs/blob4
refs/myblobs/blob1
refs/myblobs/blob2
refs/myblobs/blob3
refs/myblobs/blob5
refs/myblobs/blob6
refs/mytrees/first
EOF
git for-each-ref --format="%(refname)" --sort=raw:size \
refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
test_cmp expected actual
'
test_expect_success 'validate raw atom with %(if:equals)' '
cat >expected <<-EOF &&
not equals
not equals
not equals
not equals
not equals
not equals
refs/myblobs/blob4
not equals
not equals
not equals
not equals
not equals
EOF
git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
refs/myblobs/ refs/heads/ >actual &&
test_cmp expected actual
'
test_expect_success 'validate raw atom with %(if:notequals)' '
cat >expected <<-EOF &&
refs/heads/ambiguous
refs/heads/main
refs/heads/newtag
refs/myblobs/blob1
refs/myblobs/blob2
refs/myblobs/blob3
equals
refs/myblobs/blob5
refs/myblobs/blob6
refs/myblobs/blob7
refs/myblobs/blob8
refs/myblobs/first
EOF
git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
refs/myblobs/ refs/heads/ >actual &&
test_cmp expected actual
'
test_expect_success 'empty raw refs with %(if)' '
cat >expected <<-EOF &&
refs/myblobs/blob1 not empty
refs/myblobs/blob2 not empty
refs/myblobs/blob3 not empty
refs/myblobs/blob4 not empty
refs/myblobs/blob5 not empty
refs/myblobs/blob6 not empty
refs/myblobs/blob7 empty
refs/myblobs/blob8 empty
refs/myblobs/first not empty
EOF
git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
refs/myblobs/ >actual &&
test_cmp expected actual
'
test_expect_success '%(raw) with --python must fail' '
test_must_fail git for-each-ref --format="%(raw)" --python
'
test_expect_success '%(raw) with --tcl must fail' '
test_must_fail git for-each-ref --format="%(raw)" --tcl
'
test_expect_success '%(raw) with --perl' '
git for-each-ref --format="\$name= %(raw);
print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
cmp blob1 actual &&
git for-each-ref --format="\$name= %(raw);
print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
cmp blob3 actual &&
git for-each-ref --format="\$name= %(raw);
print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
cmp blob8 actual &&
git for-each-ref --format="\$name= %(raw);
print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
cmp one actual &&
git cat-file tree refs/mytrees/first > expected &&
git for-each-ref --format="\$name= %(raw);
print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
cmp expected actual
'
test_expect_success '%(raw) with --shell must fail' '
test_must_fail git for-each-ref --format="%(raw)" --shell
'
test_expect_success '%(raw) with --shell and --sort=raw must fail' '
test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
'
test_expect_success '%(raw:size) with --shell' '
git for-each-ref --format="%(raw:size)" | while read line
do
echo "'\''$line'\''" >>expect
done &&
git for-each-ref --format="%(raw:size)" --shell >actual &&
test_cmp expect actual
'
test_expect_success 'for-each-ref --format compare with cat-file --batch' '
git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
%(raw)" refs/mytrees/first >actual &&
test_cmp expected actual
'
test_expect_success 'set up multiple-sort tags' '
for when in 100000 200000
do
@ -980,6 +1211,10 @@ test_expect_success 'basic atom: head contents:trailers' '
test_cmp expect actual.clean
'
test_expect_success 'basic atom: rest must fail' '
test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
'
test_expect_success 'trailer parsing not fooled by --- line' '
git commit --allow-empty -F - <<-\EOF &&
this is the subject

View File

@ -1998,6 +1998,10 @@ test_expect_success '--format should list tags as per format given' '
test_cmp expect actual
'
test_expect_success 'git tag -l with --format="%(rest)" must fail' '
test_must_fail git tag -l --format="%(rest)" "v1*"
'
test_expect_success "set up color tests" '
echo "<RED>v1.0<RESET>" >expect.color &&
echo "v1.0" >expect.bare &&

View File

@ -194,6 +194,10 @@ test_expect_success GPG 'verifying tag with --format' '
test_cmp expect actual
'
test_expect_success GPG 'verifying tag with --format="%(rest)" must fail' '
test_must_fail git verify-tag --format="%(rest)" "fourth-signed"
'
test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
test_must_be_empty actual-forged