Imported Upstream version 2.19.4
[platform/upstream/git.git] / t / t1007-hash-object.sh
index 7d2baa1..a377530 100755 (executable)
@@ -9,13 +9,13 @@ echo_without_newline() {
 }
 
 test_blob_does_not_exist() {
-       test_expect_success 'blob does not exist in database' "
+       test_expect_success SHA1 'blob does not exist in database' "
                test_must_fail git cat-file blob $1
        "
 }
 
 test_blob_exists() {
-       test_expect_success 'blob exists in database' "
+       test_expect_success SHA1 'blob exists in database' "
                git cat-file blob $1
        "
 }
@@ -73,19 +73,19 @@ test_expect_success "Can't use --path with --no-filters" '
 
 push_repo
 
-test_expect_success 'hash a file' '
+test_expect_success SHA1 'hash a file' '
        test $hello_sha1 = $(git hash-object hello)
 '
 
 test_blob_does_not_exist $hello_sha1
 
-test_expect_success 'hash from stdin' '
+test_expect_success SHA1 '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_expect_success SHA1 'hash a file and write to database' '
        test $hello_sha1 = $(git hash-object -w hello)
 '
 
@@ -101,7 +101,7 @@ test_expect_success 'git hash-object --stdin file1 <file0 first operates on file
        test "$obname1" = "$obname1new"
 '
 
-test_expect_success 'check that appropriate filter is invoke when --path is used' '
+test_expect_success 'set up crlf tests' '
        echo fooQ | tr Q "\\015" >file0 &&
        cp file0 file1 &&
        echo "file0 -crlf" >.gitattributes &&
@@ -109,7 +109,10 @@ test_expect_success 'check that appropriate filter is invoke when --path is used
        git config core.autocrlf true &&
        file0_sha=$(git hash-object file0) &&
        file1_sha=$(git hash-object file1) &&
-       test "$file0_sha" != "$file1_sha" &&
+       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" &&
@@ -117,38 +120,40 @@ test_expect_success 'check that appropriate filter is invoke when --path is used
        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" &&
-       git config --unset core.autocrlf
+       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 '--path works in a subdirectory' '
+       (
+               cd subdir &&
+               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"
+       )
 '
 
 test_expect_success 'check that --no-filters option works' '
-       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" &&
        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" &&
-       git config --unset core.autocrlf
+       test "$file0_sha" = "$nofilters_file1"
 '
 
 test_expect_success 'check that --no-filters option works with --stdin-paths' '
-       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" &&
        nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
-       test "$file0_sha" = "$nofilters_file1" &&
-       git config --unset core.autocrlf
+       test "$file0_sha" = "$nofilters_file1"
 '
 
 pop_repo
@@ -156,7 +161,7 @@ pop_repo
 for args in "-w --stdin" "--stdin -w"; do
        push_repo
 
-       test_expect_success "hash from stdin and write to database ($args)" '
+       test_expect_success SHA1 "hash from stdin and write to database ($args)" '
                test $example_sha1 = $(git hash-object $args < example)
        '
 
@@ -171,14 +176,14 @@ example"
 sha1s="$hello_sha1
 $example_sha1"
 
-test_expect_success "hash two files with names on stdin" '
+test_expect_success SHA1 "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"; do
        push_repo
 
-       test_expect_success "hash two files with names on stdin and write to database ($args)" '
+       test_expect_success SHA1 "hash two files with names on stdin and write to database ($args)" '
                test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)"
        '
 
@@ -188,9 +193,30 @@ for args in "-w --stdin-paths" "--stdin-paths -w"; do
        pop_repo
 done
 
-test_expect_success 'corrupt tree' '
+test_expect_success 'too-short tree' '
        echo abc >malformed-tree &&
-       test_must_fail git hash-object -t tree malformed-tree
+       test_must_fail git hash-object -t tree malformed-tree 2>err &&
+       test_i18ngrep "too-short tree object" err
+'
+
+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' '