PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/t/t1007-hash-object.sh

https://gitlab.com/Blueprint-Marketing/git
Shell | 223 lines | 172 code | 48 blank | 3 comment | 2 complexity | 53c52eeb6686c10c06f0ab09942061ef MD5 | raw file
  1. #!/bin/sh
  2. test_description="git hash-object"
  3. . ./test-lib.sh
  4. echo_without_newline() {
  5. printf '%s' "$*"
  6. }
  7. test_blob_does_not_exist() {
  8. test_expect_success 'blob does not exist in database' "
  9. test_must_fail git cat-file blob $1
  10. "
  11. }
  12. test_blob_exists() {
  13. test_expect_success 'blob exists in database' "
  14. git cat-file blob $1
  15. "
  16. }
  17. hello_content="Hello World"
  18. hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
  19. example_content="This is an example"
  20. example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
  21. setup_repo() {
  22. echo_without_newline "$hello_content" > hello
  23. echo_without_newline "$example_content" > example
  24. }
  25. test_repo=test
  26. push_repo() {
  27. test_create_repo $test_repo
  28. cd $test_repo
  29. setup_repo
  30. }
  31. pop_repo() {
  32. cd ..
  33. rm -rf $test_repo
  34. }
  35. setup_repo
  36. # Argument checking
  37. test_expect_success "multiple '--stdin's are rejected" '
  38. echo example | test_must_fail git hash-object --stdin --stdin
  39. '
  40. test_expect_success "Can't use --stdin and --stdin-paths together" '
  41. echo example | test_must_fail git hash-object --stdin --stdin-paths &&
  42. echo example | test_must_fail git hash-object --stdin-paths --stdin
  43. '
  44. test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
  45. echo example | test_must_fail git hash-object --stdin-paths hello
  46. '
  47. test_expect_success "Can't use --path with --stdin-paths" '
  48. echo example | test_must_fail git hash-object --stdin-paths --path=foo
  49. '
  50. test_expect_success "Can't use --path with --no-filters" '
  51. test_must_fail git hash-object --no-filters --path=foo
  52. '
  53. # Behavior
  54. push_repo
  55. test_expect_success 'hash a file' '
  56. test $hello_sha1 = $(git hash-object hello)
  57. '
  58. test_blob_does_not_exist $hello_sha1
  59. test_expect_success 'hash from stdin' '
  60. test $example_sha1 = $(git hash-object --stdin < example)
  61. '
  62. test_blob_does_not_exist $example_sha1
  63. test_expect_success 'hash a file and write to database' '
  64. test $hello_sha1 = $(git hash-object -w hello)
  65. '
  66. test_blob_exists $hello_sha1
  67. test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
  68. echo foo > file1 &&
  69. obname0=$(echo bar | git hash-object --stdin) &&
  70. obname1=$(git hash-object file1) &&
  71. obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
  72. obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
  73. test "$obname0" = "$obname0new" &&
  74. test "$obname1" = "$obname1new"
  75. '
  76. test_expect_success 'check that appropriate filter is invoke when --path is used' '
  77. echo fooQ | tr Q "\\015" >file0 &&
  78. cp file0 file1 &&
  79. echo "file0 -crlf" >.gitattributes &&
  80. echo "file1 crlf" >>.gitattributes &&
  81. git config core.autocrlf true &&
  82. file0_sha=$(git hash-object file0) &&
  83. file1_sha=$(git hash-object file1) &&
  84. test "$file0_sha" != "$file1_sha" &&
  85. path1_sha=$(git hash-object --path=file1 file0) &&
  86. path0_sha=$(git hash-object --path=file0 file1) &&
  87. test "$file0_sha" = "$path0_sha" &&
  88. test "$file1_sha" = "$path1_sha" &&
  89. path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
  90. path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
  91. test "$file0_sha" = "$path0_sha" &&
  92. test "$file1_sha" = "$path1_sha" &&
  93. git config --unset core.autocrlf
  94. '
  95. test_expect_success 'check that --no-filters option works' '
  96. echo fooQ | tr Q "\\015" >file0 &&
  97. cp file0 file1 &&
  98. echo "file0 -crlf" >.gitattributes &&
  99. echo "file1 crlf" >>.gitattributes &&
  100. git config core.autocrlf true &&
  101. file0_sha=$(git hash-object file0) &&
  102. file1_sha=$(git hash-object file1) &&
  103. test "$file0_sha" != "$file1_sha" &&
  104. nofilters_file1=$(git hash-object --no-filters file1) &&
  105. test "$file0_sha" = "$nofilters_file1" &&
  106. nofilters_file1=$(cat file1 | git hash-object --stdin) &&
  107. test "$file0_sha" = "$nofilters_file1" &&
  108. git config --unset core.autocrlf
  109. '
  110. test_expect_success 'check that --no-filters option works with --stdin-paths' '
  111. echo fooQ | tr Q "\\015" >file0 &&
  112. cp file0 file1 &&
  113. echo "file0 -crlf" >.gitattributes &&
  114. echo "file1 crlf" >>.gitattributes &&
  115. git config core.autocrlf true &&
  116. file0_sha=$(git hash-object file0) &&
  117. file1_sha=$(git hash-object file1) &&
  118. test "$file0_sha" != "$file1_sha" &&
  119. nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
  120. test "$file0_sha" = "$nofilters_file1" &&
  121. git config --unset core.autocrlf
  122. '
  123. pop_repo
  124. for args in "-w --stdin" "--stdin -w"; do
  125. push_repo
  126. test_expect_success "hash from stdin and write to database ($args)" '
  127. test $example_sha1 = $(git hash-object $args < example)
  128. '
  129. test_blob_exists $example_sha1
  130. pop_repo
  131. done
  132. filenames="hello
  133. example"
  134. sha1s="$hello_sha1
  135. $example_sha1"
  136. test_expect_success "hash two files with names on stdin" '
  137. test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
  138. '
  139. for args in "-w --stdin-paths" "--stdin-paths -w"; do
  140. push_repo
  141. test_expect_success "hash two files with names on stdin and write to database ($args)" '
  142. test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
  143. '
  144. test_blob_exists $hello_sha1
  145. test_blob_exists $example_sha1
  146. pop_repo
  147. done
  148. test_expect_success 'corrupt tree' '
  149. echo abc >malformed-tree &&
  150. test_must_fail git hash-object -t tree malformed-tree
  151. '
  152. test_expect_success 'corrupt commit' '
  153. test_must_fail git hash-object -t commit --stdin </dev/null
  154. '
  155. test_expect_success 'corrupt tag' '
  156. test_must_fail git hash-object -t tag --stdin </dev/null
  157. '
  158. test_expect_success 'hash-object complains about bogus type name' '
  159. test_must_fail git hash-object -t bogus --stdin </dev/null
  160. '
  161. test_expect_success 'hash-object complains about truncated type name' '
  162. test_must_fail git hash-object -t bl --stdin </dev/null
  163. '
  164. test_expect_success '--literally' '
  165. t=1234567890 &&
  166. echo example | git hash-object -t $t --literally --stdin
  167. '
  168. test_expect_success '--literally with extra-long type' '
  169. t=12345678901234567890123456789012345678901234567890 &&
  170. 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$t" &&
  171. echo example | git hash-object -t $t --literally --stdin
  172. '
  173. test_done