Imported Upstream version 2.12.3
[platform/upstream/git.git] / t / t9802-git-p4-filetype.sh
index 992bb8c..eb9a8ed 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-test_description='git-p4 p4 filetype tests'
+test_description='git p4 filetype tests'
 
 . ./lib-git-p4.sh
 
@@ -8,6 +8,123 @@ test_expect_success 'start p4d' '
        start_p4d
 '
 
+#
+# This series of tests checks newline handling  Both p4 and
+# git store newlines as \n, and have options to choose how
+# newlines appear in checked-out files.
+#
+test_expect_success 'p4 client newlines, unix' '
+       (
+               cd "$cli" &&
+               p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+               printf "unix\ncrlf\n" >f-unix &&
+               printf "unix\r\ncrlf\r\n" >f-unix-as-crlf &&
+               p4 add -t text f-unix &&
+               p4 submit -d f-unix &&
+
+               # LineEnd: unix; should be no change after sync
+               cp f-unix f-unix-orig &&
+               p4 sync -f &&
+               test_cmp f-unix-orig f-unix &&
+
+               # make sure stored in repo as unix newlines
+               # use sed to eat python-appended newline
+               p4 -G print //depot/f-unix | marshal_dump data 2 |\
+                   sed \$d >f-unix-p4-print &&
+               test_cmp f-unix-orig f-unix-p4-print &&
+
+               # switch to win, make sure lf -> crlf
+               p4 client -o | sed "/LineEnd/s/:.*/:win/" | p4 client -i &&
+               p4 sync -f &&
+               test_cmp f-unix-as-crlf f-unix
+       )
+'
+
+test_expect_success 'p4 client newlines, win' '
+       (
+               cd "$cli" &&
+               p4 client -o | sed "/LineEnd/s/:.*/:win/" | p4 client -i &&
+               printf "win\r\ncrlf\r\n" >f-win &&
+               printf "win\ncrlf\n" >f-win-as-lf &&
+               p4 add -t text f-win &&
+               p4 submit -d f-win &&
+
+               # LineEnd: win; should be no change after sync
+               cp f-win f-win-orig &&
+               p4 sync -f &&
+               test_cmp f-win-orig f-win &&
+
+               # make sure stored in repo as unix newlines
+               # use sed to eat python-appened newline
+               p4 -G print //depot/f-win | marshal_dump data 2 |\
+                   sed \$d >f-win-p4-print &&
+               test_cmp f-win-as-lf f-win-p4-print &&
+
+               # switch to unix, make sure lf -> crlf
+               p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+               p4 sync -f &&
+               test_cmp f-win-as-lf f-win
+       )
+'
+
+test_expect_success 'ensure blobs store only lf newlines' '
+       test_when_finished cleanup_git &&
+       (
+               cd "$git" &&
+               git init &&
+               git p4 sync //depot@all &&
+
+               # verify the files in .git are stored only with newlines
+               o=$(git ls-tree p4/master -- f-unix | cut -f1 | cut -d\  -f3) &&
+               git cat-file blob $o >f-unix-blob &&
+               test_cmp "$cli"/f-unix-orig f-unix-blob &&
+
+               o=$(git ls-tree p4/master -- f-win | cut -f1 | cut -d\  -f3) &&
+               git cat-file blob $o >f-win-blob &&
+               test_cmp "$cli"/f-win-as-lf f-win-blob &&
+
+               rm f-unix-blob f-win-blob
+       )
+'
+
+test_expect_success 'gitattributes setting eol=lf produces lf newlines' '
+       test_when_finished cleanup_git &&
+       (
+               # checkout the files and make sure core.eol works as planned
+               cd "$git" &&
+               git init &&
+               echo "* eol=lf" >.gitattributes &&
+               git p4 sync //depot@all &&
+               git checkout -b master p4/master &&
+               test_cmp "$cli"/f-unix-orig f-unix &&
+               test_cmp "$cli"/f-win-as-lf f-win
+       )
+'
+
+test_expect_success 'gitattributes setting eol=crlf produces crlf newlines' '
+       test_when_finished cleanup_git &&
+       (
+               # checkout the files and make sure core.eol works as planned
+               cd "$git" &&
+               git init &&
+               echo "* eol=crlf" >.gitattributes &&
+               git p4 sync //depot@all &&
+               git checkout -b master p4/master &&
+               test_cmp "$cli"/f-unix-as-crlf f-unix &&
+               test_cmp "$cli"/f-win-orig f-win
+       )
+'
+
+test_expect_success 'crlf cleanup' '
+       (
+               cd "$cli" &&
+               rm f-unix-orig f-unix-as-crlf &&
+               rm f-win-orig f-win-as-lf &&
+               p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+               p4 sync -f
+       )
+'
+
 test_expect_success 'utf-16 file create' '
        (
                cd "$cli" &&
@@ -37,7 +154,7 @@ test_expect_success 'utf-16 file create' '
 
 test_expect_success 'utf-16 file test' '
        test_when_finished cleanup_git &&
-       "$GITP4" clone --dest="$git" //depot@all &&
+       git p4 clone --dest="$git" //depot@all &&
        (
                cd "$git" &&
 
@@ -84,7 +201,7 @@ test_expect_success 'keyword file test' '
        build_smush &&
        test_when_finished rm -f k_smush.py ko_smush.py &&
        test_when_finished cleanup_git &&
-       "$GITP4" clone --dest="$git" //depot@all &&
+       git p4 clone --dest="$git" //depot@all &&
        (
                cd "$git" &&
 
@@ -94,7 +211,7 @@ test_expect_success 'keyword file test' '
                "$PYTHON_PATH" "$TRASH_DIRECTORY/ko_smush.py" <"$cli/k-text-ko" >cli-k-text-ko-smush &&
                test_cmp cli-k-text-ko-smush k-text-ko &&
 
-               # utf16, even though p4 expands keywords, git-p4 does not
+               # utf16, even though p4 expands keywords, git p4 does not
                # try to undo that
                test_cmp "$cli/k-utf16-k" k-utf16-k &&
                test_cmp "$cli/k-utf16-ko" k-utf16-ko
@@ -105,12 +222,13 @@ build_gendouble() {
        cat >gendouble.py <<-\EOF
        import sys
        import struct
-       import array
 
-       s = array.array("c", '\0' * 26)
-       struct.pack_into(">L", s,  0, 0x00051607)  # AppleDouble
-       struct.pack_into(">L", s,  4, 0x00020000)  # version 2
-       s.tofile(sys.stdout)
+       s = struct.pack(b">LL18s",
+                       0x00051607,  # AppleDouble
+                       0x00020000,  # version 2
+                       b""          # pad to 26 bytes
+       )
+       getattr(sys.stdout, 'buffer', sys.stdout).write(s)
        EOF
 }
 
@@ -125,13 +243,96 @@ test_expect_success 'ignore apple' '
                p4 submit -d appledouble
        ) &&
        test_when_finished cleanup_git &&
-       "$GITP4" clone --dest="$git" //depot@all &&
+       git p4 clone --dest="$git" //depot@all &&
        (
                cd "$git" &&
                test ! -f double.png
        )
 '
 
+test_expect_success SYMLINKS 'create p4 symlink' '
+       cd "$cli" &&
+       ln -s symlink-target symlink &&
+       p4 add symlink &&
+       p4 submit -d "add symlink"
+'
+
+test_expect_success SYMLINKS 'ensure p4 symlink parsed correctly' '
+       test_when_finished cleanup_git &&
+       git p4 clone --dest="$git" //depot@all &&
+       (
+               cd "$git" &&
+               test -L symlink &&
+               test $(readlink symlink) = symlink-target
+       )
+'
+
+test_expect_success SYMLINKS 'empty symlink target' '
+       (
+               # first create the file as a file
+               cd "$cli" &&
+               >empty-symlink &&
+               p4 add empty-symlink &&
+               p4 submit -d "add empty-symlink as a file"
+       ) &&
+       (
+               # now change it to be a symlink to "target1"
+               cd "$cli" &&
+               p4 edit empty-symlink &&
+               p4 reopen -t symlink empty-symlink &&
+               rm empty-symlink &&
+               ln -s target1 empty-symlink &&
+               p4 add empty-symlink &&
+               p4 submit -d "make empty-symlink point to target1"
+       ) &&
+       (
+               # Hack the p4 depot to make the symlink point to nothing;
+               # this should not happen in reality, but shows up
+               # in p4 repos in the wild.
+               #
+               # The sed expression changes this:
+               #     @@
+               #     text
+               #     @target1
+               #     @
+               # to this:
+               #     @@
+               #     text
+               #     @@
+               #
+               cd "$db/depot" &&
+               sed "/@target1/{; s/target1/@/; n; d; }" \
+                   empty-symlink,v >empty-symlink,v.tmp &&
+               mv empty-symlink,v.tmp empty-symlink,v
+       ) &&
+       (
+               # Make sure symlink really is empty.  Asking
+               # p4 to sync here will make it generate errors.
+               cd "$cli" &&
+               p4 print -q //depot/empty-symlink#2 >out &&
+               test ! -s out
+       ) &&
+       test_when_finished cleanup_git &&
+
+       # make sure git p4 handles it without error
+       git p4 clone --dest="$git" //depot@all &&
+
+       # fix the symlink, make it point to "target2"
+       (
+               cd "$cli" &&
+               p4 open empty-symlink &&
+               rm empty-symlink &&
+               ln -s target2 empty-symlink &&
+               p4 submit -d "make empty-symlink point to target2"
+       ) &&
+       cleanup_git &&
+       git p4 clone --dest="$git" //depot@all &&
+       (
+               cd "$git" &&
+               test $(readlink empty-symlink) = target2
+       )
+'
+
 test_expect_success 'kill p4d' '
        kill_p4d
 '