#!/bin/sh global test_description := '"git hash-object'" source ./test-lib.sh proc echo_without_newline { printf '%s' "$ifsjoin(Argv)" } proc test_blob_does_not_exist { test_expect_success 'blob does not exist in database' " test_must_fail git cat-file blob $1 " } proc test_blob_exists { test_expect_success 'blob exists in database' " git cat-file blob $1 " } global hello_content := '"Hello World'" global hello_sha1 := '5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689' global example_content := '"This is an example'" global example_sha1 := 'ddd3f836d3e3fbb7ae289aa9ae83536f76956399' proc setup_repo { echo_without_newline $hello_content > hello echo_without_newline $example_content > example } global test_repo := 'test' proc push_repo { test_create_repo $test_repo cd $test_repo setup_repo } proc pop_repo { cd .. rm -rf $test_repo } setup_repo # Argument checking test_expect_success "multiple '--stdin's are rejected" ' echo example | test_must_fail git hash-object --stdin --stdin ' test_expect_success "Can't use --stdin and --stdin-paths together" ' echo example | test_must_fail git hash-object --stdin --stdin-paths && echo example | test_must_fail git hash-object --stdin-paths --stdin ' test_expect_success "Can't pass filenames as arguments with --stdin-paths" ' echo example | test_must_fail git hash-object --stdin-paths hello ' test_expect_success "Can't use --path with --stdin-paths" ' echo example | test_must_fail git hash-object --stdin-paths --path=foo ' test_expect_success "Can't use --path with --no-filters" ' test_must_fail git hash-object --no-filters --path=foo ' # Behavior push_repo test_expect_success 'hash a file' ' test $hello_sha1 = $(git hash-object hello) ' test_blob_does_not_exist $hello_sha1 test_expect_success 'hash from stdin' ' test $example_sha1 = $(git hash-object --stdin < example) ' test_blob_does_not_exist $example_sha1 test_expect_success 'hash a file and write to database' ' test $hello_sha1 = $(git hash-object -w hello) ' test_blob_exists $hello_sha1 test_expect_success 'git hash-object --stdin file1 file1 && obname0=$(echo bar | git hash-object --stdin) && obname1=$(git hash-object file1) && obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && test "$obname0" = "$obname0new" && test "$obname1" = "$obname1new" ' test_expect_success 'set up crlf tests' ' echo fooQ | tr Q "\\015" >file0 && cp file0 file1 && echo "file0 -crlf" >.gitattributes && echo "file1 crlf" >>.gitattributes && git config core.autocrlf true && file0_sha=$(git hash-object file0) && file1_sha=$(git hash-object file1) && test "$file0_sha" != "$file1_sha" ' test_expect_success 'check that appropriate filter is invoke when --path is used' ' path1_sha=$(git hash-object --path=file1 file0) && path0_sha=$(git hash-object --path=file0 file1) && test "$file0_sha" = "$path0_sha" && test "$file1_sha" = "$path1_sha" && path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) && path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) && test "$file0_sha" = "$path0_sha" && test "$file1_sha" = "$path1_sha" ' test_expect_success 'gitattributes also work in a subdirectory' ' mkdir subdir && ( cd subdir && subdir_sha0=$(git hash-object ../file0) && subdir_sha1=$(git hash-object ../file1) && test "$file0_sha" = "$subdir_sha0" && test "$file1_sha" = "$subdir_sha1" ) ' test_expect_success 'check that --no-filters option works' ' nofilters_file1=$(git hash-object --no-filters file1) && test "$file0_sha" = "$nofilters_file1" && nofilters_file1=$(cat file1 | git hash-object --stdin) && test "$file0_sha" = "$nofilters_file1" ' test_expect_success 'check that --no-filters option works with --stdin-paths' ' nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) && test "$file0_sha" = "$nofilters_file1" ' pop_repo for args in ["-w --stdin" "--stdin -w]" { push_repo test_expect_success "hash from stdin and write to database ($args)" ' test $example_sha1 = $(git hash-object $args < example) ' test_blob_exists $example_sha1 pop_repo } global filenames := '"hello example'" global sha1s := ""$hello_sha1 $example_sha1"" test_expect_success "hash two files with names on stdin" ' test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)" ' for args in ["-w --stdin-paths" "--stdin-paths -w]" { push_repo test_expect_success "hash two files with names on stdin and write to database ($args)" ' test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)" ' test_blob_exists $hello_sha1 test_blob_exists $example_sha1 pop_repo } test_expect_success 'too-short tree' ' echo abc >malformed-tree && test_must_fail git hash-object -t tree malformed-tree 2>err && test_i18ngrep "too-short tree object" err ' proc hex2oct { perl -ne 'printf "\\%03o", hex for /../g' } test_expect_success 'malformed mode in tree' ' hex_sha1=$(echo foo | git hash-object --stdin -w) && bin_sha1=$(echo $hex_sha1 | hex2oct) && printf "9100644 \0$bin_sha1" >tree-with-malformed-mode && test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err && test_i18ngrep "malformed mode in tree entry" err ' test_expect_success 'empty filename in tree' ' hex_sha1=$(echo foo | git hash-object --stdin -w) && bin_sha1=$(echo $hex_sha1 | hex2oct) && printf "100644 \0$bin_sha1" >tree-with-empty-filename && test_must_fail git hash-object -t tree tree-with-empty-filename 2>err && test_i18ngrep "empty filename in tree entry" err ' test_expect_success 'corrupt commit' ' test_must_fail git hash-object -t commit --stdin )} {(DQ ($ VSub_Star "$*"))})] spids:[19]) spids: [15 18] ) (FuncDef name: test_blob_does_not_exist body: (BraceGroup children: [ (C {(test_expect_success)} {(SQ <"blob does not exist in database">)} { (DQ ("\n") ("\t\ttest_must_fail git cat-file blob ") ($ VSub_Number "$1") ("\n") ("\t")) } ) ] spids: [39] ) spids: [35 38] ) (FuncDef name: test_blob_exists body: (BraceGroup children: [ (C {(test_expect_success)} {(SQ <"blob exists in database">)} {(DQ ("\n") ("\t\tgit cat-file blob ") ($ VSub_Number "$1") ("\n") ("\t"))} ) ] spids: [63] ) spids: [59 62] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:hello_content) op: Equal rhs: {(DQ ("Hello World"))} spids: [83] ) ] spids: [83] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:hello_sha1) op: Equal rhs: {(5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689)} spids: [88] ) ] spids: [88] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:example_content) op: Equal rhs: {(DQ ("This is an example"))} spids: [92] ) ] spids: [92] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:example_sha1) op: Equal rhs: {(ddd3f836d3e3fbb7ae289aa9ae83536f76956399)} spids: [97] ) ] spids: [97] ) (FuncDef name: setup_repo body: (BraceGroup children: [ (SimpleCommand words: [{(echo_without_newline)} {(DQ ($ VSub_Name "$hello_content"))}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(hello)} spids:[114])] ) (SimpleCommand words: [{(echo_without_newline)} {(DQ ($ VSub_Name "$example_content"))}] redirects: [(Redir op_id:Redir_Great fd:-1 arg_word:{(example)} spids:[125])] ) ] spids: [105] ) spids: [101 104] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:test_repo) op:Equal rhs:{(test)} spids:[132])] spids: [132] ) (FuncDef name: push_repo body: (BraceGroup children: [ (C {(test_create_repo)} {($ VSub_Name "$test_repo")}) (C {(cd)} {($ VSub_Name "$test_repo")}) (C {(setup_repo)}) ] spids: [139] ) spids: [135 138] ) (FuncDef name: pop_repo body: (BraceGroup children: [(C {(cd)} {(..)}) (C {(rm)} {(-rf)} {($ VSub_Name "$test_repo")})] spids: [162] ) spids: [158 161] ) (C {(setup_repo)}) (C {(test_expect_success)} {(DQ ("multiple '--stdin's are rejected"))} {(SQ <"\n"> <"\techo example | test_must_fail git hash-object --stdin --stdin\n">)} ) (C {(test_expect_success)} {(DQ ("Can't use --stdin and --stdin-paths together"))} { (SQ <"\n"> <"\techo example | test_must_fail git hash-object --stdin --stdin-paths &&\n"> <"\techo example | test_must_fail git hash-object --stdin-paths --stdin\n"> ) } ) (C {(test_expect_success)} {(DQ ("Can't pass filenames as arguments with --stdin-paths"))} {(SQ <"\n"> <"\techo example | test_must_fail git hash-object --stdin-paths hello\n">)} ) (C {(test_expect_success)} {(DQ ("Can't use --path with --stdin-paths"))} {(SQ <"\n"> <"\techo example | test_must_fail git hash-object --stdin-paths --path=foo\n">)} ) (C {(test_expect_success)} {(DQ ("Can't use --path with --no-filters"))} {(SQ <"\n"> <"\ttest_must_fail git hash-object --no-filters --path=foo\n">)} ) (C {(push_repo)}) (C {(test_expect_success)} {(SQ <"hash a file">)} {(SQ <"\n"> <"\ttest $hello_sha1 = $(git hash-object hello)\n">)} ) (C {(test_blob_does_not_exist)} {($ VSub_Name "$hello_sha1")}) (C {(test_expect_success)} {(SQ <"hash from stdin">)} {(SQ <"\n"> <"\ttest $example_sha1 = $(git hash-object --stdin < example)\n">)} ) (C {(test_blob_does_not_exist)} {($ VSub_Name "$example_sha1")}) (C {(test_expect_success)} {(SQ <"hash a file and write to database">)} {(SQ <"\n"> <"\ttest $hello_sha1 = $(git hash-object -w hello)\n">)} ) (C {(test_blob_exists)} {($ VSub_Name "$hello_sha1")}) (C {(test_expect_success)} {(SQ <"git hash-object --stdin file1 )} { (SQ <"\n"> <"\techo foo > file1 &&\n"> <"\tobname0=$(echo bar | git hash-object --stdin) &&\n"> <"\tobname1=$(git hash-object file1) &&\n"> <"\tobname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&\n"> <"\tobname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&\n"> <"\ttest \"$obname0\" = \"$obname0new\" &&\n"> <"\ttest \"$obname1\" = \"$obname1new\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"set up crlf tests">)} { (SQ <"\n"> <"\techo fooQ | tr Q \"\\\\015\" >file0 &&\n"> <"\tcp file0 file1 &&\n"> <"\techo \"file0 -crlf\" >.gitattributes &&\n"> <"\techo \"file1 crlf\" >>.gitattributes &&\n"> <"\tgit config core.autocrlf true &&\n"> <"\tfile0_sha=$(git hash-object file0) &&\n"> <"\tfile1_sha=$(git hash-object file1) &&\n"> <"\ttest \"$file0_sha\" != \"$file1_sha\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"check that appropriate filter is invoke when --path is used">)} { (SQ <"\n"> <"\tpath1_sha=$(git hash-object --path=file1 file0) &&\n"> <"\tpath0_sha=$(git hash-object --path=file0 file1) &&\n"> <"\ttest \"$file0_sha\" = \"$path0_sha\" &&\n"> <"\ttest \"$file1_sha\" = \"$path1_sha\" &&\n"> <"\tpath1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&\n"> <"\tpath0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&\n"> <"\ttest \"$file0_sha\" = \"$path0_sha\" &&\n"> <"\ttest \"$file1_sha\" = \"$path1_sha\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"gitattributes also work in a subdirectory">)} { (SQ <"\n"> <"\tmkdir subdir &&\n"> <"\t(\n"> <"\t\tcd subdir &&\n"> <"\t\tsubdir_sha0=$(git hash-object ../file0) &&\n"> <"\t\tsubdir_sha1=$(git hash-object ../file1) &&\n"> <"\t\ttest \"$file0_sha\" = \"$subdir_sha0\" &&\n"> <"\t\ttest \"$file1_sha\" = \"$subdir_sha1\"\n"> <"\t)\n"> ) } ) (C {(test_expect_success)} {(SQ <"check that --no-filters option works">)} { (SQ <"\n"> <"\tnofilters_file1=$(git hash-object --no-filters file1) &&\n"> <"\ttest \"$file0_sha\" = \"$nofilters_file1\" &&\n"> <"\tnofilters_file1=$(cat file1 | git hash-object --stdin) &&\n"> <"\ttest \"$file0_sha\" = \"$nofilters_file1\"\n"> ) } ) (C {(test_expect_success)} {(SQ <"check that --no-filters option works with --stdin-paths">)} { (SQ <"\n"> <"\tnofilters_file1=$(echo \"file1\" | git hash-object --stdin-paths --no-filters) &&\n"> <"\ttest \"$file0_sha\" = \"$nofilters_file1\"\n"> ) } ) (C {(pop_repo)}) (ForEach iter_name: args iter_words: [{(DQ ("-w --stdin"))} {(DQ ("--stdin -w"))}] do_arg_iter: False body: (DoGroup children: [ (C {(push_repo)}) (C {(test_expect_success)} {(DQ ("hash from stdin and write to database (") ($ VSub_Name "$args") (")"))} {(SQ <"\n"> <"\t\ttest $example_sha1 = $(git hash-object $args < example)\n"> <"\t">)} ) (C {(test_blob_exists)} {($ VSub_Name "$example_sha1")}) (C {(pop_repo)}) ] spids: [426 457] ) spids: [416 424] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:filenames) op: Equal rhs: {(DQ ("hello\n") (example))} spids: [460] ) ] spids: [460] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:sha1s) op: Equal rhs: {(DQ ($ VSub_Name "$hello_sha1") ("\n") ($ VSub_Name "$example_sha1"))} spids: [467] ) ] spids: [467] ) (C {(test_expect_success)} {(DQ ("hash two files with names on stdin"))} { (SQ <"\n"> < "\ttest \"$sha1s\" = \"$(echo_without_newline \"$filenames\" | git hash-object --stdin-paths)\"\n" > ) } ) (ForEach iter_name: args iter_words: [{(DQ ("-w --stdin-paths"))} {(DQ ("--stdin-paths -w"))}] do_arg_iter: False body: (DoGroup children: [ (C {(push_repo)}) (C {(test_expect_success)} { (DQ ("hash two files with names on stdin and write to database (") ($ VSub_Name "$args") (")") ) } { (SQ <"\n"> < "\t\ttest \"$sha1s\" = \"$(echo_without_newline \"$filenames\" | git hash-object $args)\"\n" > <"\t"> ) } ) (C {(test_blob_exists)} {($ VSub_Name "$hello_sha1")}) (C {(test_blob_exists)} {($ VSub_Name "$example_sha1")}) (C {(pop_repo)}) ] spids: [502 538] ) spids: [492 500] ) (C {(test_expect_success)} {(SQ <"too-short tree">)} { (SQ <"\n"> <"\techo abc >malformed-tree &&\n"> <"\ttest_must_fail git hash-object -t tree malformed-tree 2>err &&\n"> <"\ttest_i18ngrep \"too-short tree object\" err\n"> ) } ) (FuncDef name: hex2oct body: (BraceGroup children: [(C {(perl)} {(-ne)} {(SQ <"printf \"\\\\%03o\", hex for /../g">)})] spids: [559] ) spids: [555 558] ) (C {(test_expect_success)} {(SQ <"malformed mode in tree">)} { (SQ <"\n"> <"\thex_sha1=$(echo foo | git hash-object --stdin -w) &&\n"> <"\tbin_sha1=$(echo $hex_sha1 | hex2oct) &&\n"> <"\tprintf \"9100644 \\0$bin_sha1\" >tree-with-malformed-mode &&\n"> <"\ttest_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&\n"> <"\ttest_i18ngrep \"malformed mode in tree entry\" err\n"> ) } ) (C {(test_expect_success)} {(SQ <"empty filename in tree">)} { (SQ <"\n"> <"\thex_sha1=$(echo foo | git hash-object --stdin -w) &&\n"> <"\tbin_sha1=$(echo $hex_sha1 | hex2oct) &&\n"> <"\tprintf \"100644 \\0$bin_sha1\" >tree-with-empty-filename &&\n"> <"\ttest_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&\n"> <"\ttest_i18ngrep \"empty filename in tree entry\" err\n"> ) } ) (C {(test_expect_success)} {(SQ <"corrupt commit">)} {(SQ <"\n"> <"\ttest_must_fail git hash-object -t commit --stdin )} ) (C {(test_expect_success)} {(SQ <"corrupt tag">)} {(SQ <"\n"> <"\ttest_must_fail git hash-object -t tag --stdin )} ) (C {(test_expect_success)} {(SQ <"hash-object complains about bogus type name">)} {(SQ <"\n"> <"\ttest_must_fail git hash-object -t bogus --stdin )} ) (C {(test_expect_success)} {(SQ <"hash-object complains about truncated type name">)} {(SQ <"\n"> <"\ttest_must_fail git hash-object -t bl --stdin )} ) (C {(test_expect_success)} {(SQ <--literally>)} { (SQ <"\n"> <"\tt=1234567890 &&\n"> <"\techo example | git hash-object -t $t --literally --stdin\n"> ) } ) (C {(test_expect_success)} {(SQ <"--literally with extra-long type">)} { (SQ <"\n"> <"\tt=12345678901234567890123456789012345678901234567890 &&\n"> <"\tt=\"$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t\" &&\n"> <"\techo example | git hash-object -t $t --literally --stdin\n"> ) } ) (C {(test_done)}) ] )