t / t9824-git-p4-git-lfs.shon commit Merge branch 'pb/test-must-fail-is-for-git' (c7f352f)
   1#!/bin/sh
   2
   3test_description='Clone repositories and store files in Git LFS'
   4
   5. ./lib-git-p4.sh
   6
   7git lfs help >/dev/null 2>&1 || {
   8        skip_all='skipping git p4 Git LFS tests; Git LFS not found'
   9        test_done
  10}
  11
  12test_file_in_lfs () {
  13        FILE="$1" &&
  14        SIZE="$2" &&
  15        EXPECTED_CONTENT="$3" &&
  16        sed -n '1,1 p' "$FILE" | grep "^version " &&
  17        sed -n '2,2 p' "$FILE" | grep "^oid " &&
  18        sed -n '3,3 p' "$FILE" | grep "^size " &&
  19        test_line_count = 3 "$FILE" &&
  20        cat "$FILE" | grep "size $SIZE" &&
  21        HASH=$(cat "$FILE" | grep "oid sha256:" | sed -e "s/oid sha256://g") &&
  22        LFS_FILE=".git/lfs/objects/$(echo "$HASH" | cut -c1-2)/$(echo "$HASH" | cut -c3-4)/$HASH" &&
  23        echo $EXPECTED_CONTENT >expect &&
  24        test_path_is_file "$FILE" &&
  25        test_path_is_file "$LFS_FILE" &&
  26        test_cmp expect "$LFS_FILE"
  27}
  28
  29test_file_count_in_dir () {
  30        DIR="$1" &&
  31        EXPECTED_COUNT="$2" &&
  32        find "$DIR" -type f >actual &&
  33        test_line_count = $EXPECTED_COUNT actual
  34}
  35
  36test_expect_success 'start p4d' '
  37        start_p4d
  38'
  39
  40test_expect_success 'Create repo with binary files' '
  41        client_view "//depot/... //client/..." &&
  42        (
  43                cd "$cli" &&
  44
  45                >file0.dat &&
  46                p4 add file0.dat &&
  47                echo "content 1 txt 23 bytes" >file1.txt &&
  48                p4 add file1.txt &&
  49                echo "content 2-3 bin 25 bytes" >file2.dat &&
  50                p4 add file2.dat &&
  51                p4 submit -d "Add text and binary file" &&
  52
  53                mkdir "path with spaces" &&
  54                echo "content 2-3 bin 25 bytes" >"path with spaces/file3.bin" &&
  55                p4 add "path with spaces/file3.bin" &&
  56                p4 submit -d "Add another binary file with same content and spaces in path" &&
  57
  58                echo "content 4 bin 26 bytes XX" >file4.bin &&
  59                p4 add file4.bin &&
  60                p4 submit -d "Add another binary file with different content"
  61        )
  62'
  63
  64test_expect_success 'Store files in LFS based on size (>24 bytes)' '
  65        client_view "//depot/... //client/..." &&
  66        test_when_finished cleanup_git &&
  67        (
  68                cd "$git" &&
  69                git init . &&
  70                git config git-p4.useClientSpec true &&
  71                git config git-p4.largeFileSystem GitLFS &&
  72                git config git-p4.largeFileThreshold 24 &&
  73                git p4 clone --destination="$git" //depot@all &&
  74
  75                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
  76                test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
  77                test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
  78
  79                test_file_count_in_dir ".git/lfs/objects" 2 &&
  80
  81                cat >expect <<-\EOF &&
  82
  83                #
  84                # Git LFS (see https://git-lfs.github.com/)
  85                #
  86                /file2.dat filter=lfs diff=lfs merge=lfs -text
  87                /file4.bin filter=lfs diff=lfs merge=lfs -text
  88                /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs diff=lfs merge=lfs -text
  89                EOF
  90                test_path_is_file .gitattributes &&
  91                test_cmp expect .gitattributes
  92        )
  93'
  94
  95test_expect_success 'Store files in LFS based on size (>25 bytes)' '
  96        client_view "//depot/... //client/..." &&
  97        test_when_finished cleanup_git &&
  98        (
  99                cd "$git" &&
 100                git init . &&
 101                git config git-p4.useClientSpec true &&
 102                git config git-p4.largeFileSystem GitLFS &&
 103                git config git-p4.largeFileThreshold 25 &&
 104                git p4 clone --destination="$git" //depot@all &&
 105
 106                test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
 107                test_file_count_in_dir ".git/lfs/objects" 1 &&
 108
 109                cat >expect <<-\EOF &&
 110
 111                #
 112                # Git LFS (see https://git-lfs.github.com/)
 113                #
 114                /file4.bin filter=lfs diff=lfs merge=lfs -text
 115                EOF
 116                test_path_is_file .gitattributes &&
 117                test_cmp expect .gitattributes
 118        )
 119'
 120
 121test_expect_success 'Store files in LFS based on extension (dat)' '
 122        client_view "//depot/... //client/..." &&
 123        test_when_finished cleanup_git &&
 124        (
 125                cd "$git" &&
 126                git init . &&
 127                git config git-p4.useClientSpec true &&
 128                git config git-p4.largeFileSystem GitLFS &&
 129                git config git-p4.largeFileExtensions dat &&
 130                git p4 clone --destination="$git" //depot@all &&
 131
 132                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 133                test_file_count_in_dir ".git/lfs/objects" 1 &&
 134
 135                cat >expect <<-\EOF &&
 136
 137                #
 138                # Git LFS (see https://git-lfs.github.com/)
 139                #
 140                *.dat filter=lfs diff=lfs merge=lfs -text
 141                EOF
 142                test_path_is_file .gitattributes &&
 143                test_cmp expect .gitattributes
 144        )
 145'
 146
 147test_expect_success 'Store files in LFS based on size (>25 bytes) and extension (dat)' '
 148        client_view "//depot/... //client/..." &&
 149        test_when_finished cleanup_git &&
 150        (
 151                cd "$git" &&
 152                git init . &&
 153                git config git-p4.useClientSpec true &&
 154                git config git-p4.largeFileSystem GitLFS &&
 155                git config git-p4.largeFileExtensions dat &&
 156                git config git-p4.largeFileThreshold 25 &&
 157                git p4 clone --destination="$git" //depot@all &&
 158
 159                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 160                test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
 161                test_file_count_in_dir ".git/lfs/objects" 2 &&
 162
 163                cat >expect <<-\EOF &&
 164
 165                #
 166                # Git LFS (see https://git-lfs.github.com/)
 167                #
 168                *.dat filter=lfs diff=lfs merge=lfs -text
 169                /file4.bin filter=lfs diff=lfs merge=lfs -text
 170                EOF
 171                test_path_is_file .gitattributes &&
 172                test_cmp expect .gitattributes
 173        )
 174'
 175
 176test_expect_success 'Remove file from repo and store files in LFS based on size (>24 bytes)' '
 177        client_view "//depot/... //client/..." &&
 178        (
 179                cd "$cli" &&
 180                p4 delete file4.bin &&
 181                p4 submit -d "Remove file"
 182        ) &&
 183
 184        client_view "//depot/... //client/..." &&
 185        test_when_finished cleanup_git &&
 186        (
 187                cd "$git" &&
 188                git init . &&
 189                git config git-p4.useClientSpec true &&
 190                git config git-p4.largeFileSystem GitLFS &&
 191                git config git-p4.largeFileThreshold 24 &&
 192                git p4 clone --destination="$git" //depot@all &&
 193
 194                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 195                test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
 196                test_path_is_missing file4.bin &&
 197                test_file_count_in_dir ".git/lfs/objects" 2 &&
 198
 199                cat >expect <<-\EOF &&
 200
 201                #
 202                # Git LFS (see https://git-lfs.github.com/)
 203                #
 204                /file2.dat filter=lfs diff=lfs merge=lfs -text
 205                /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs diff=lfs merge=lfs -text
 206                EOF
 207                test_path_is_file .gitattributes &&
 208                test_cmp expect .gitattributes
 209        )
 210'
 211
 212test_expect_success 'Add .gitattributes and store files in LFS based on size (>24 bytes)' '
 213        client_view "//depot/... //client/..." &&
 214        (
 215                cd "$cli" &&
 216                echo "*.txt text" >.gitattributes &&
 217                p4 add .gitattributes &&
 218                p4 submit -d "Add .gitattributes"
 219        ) &&
 220
 221        client_view "//depot/... //client/..." &&
 222        test_when_finished cleanup_git &&
 223        (
 224                cd "$git" &&
 225                git init . &&
 226                git config git-p4.useClientSpec true &&
 227                git config git-p4.largeFileSystem GitLFS &&
 228                git config git-p4.largeFileThreshold 24 &&
 229                git p4 clone --destination="$git" //depot@all &&
 230
 231                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 232                test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
 233                test_path_is_missing file4.bin &&
 234                test_file_count_in_dir ".git/lfs/objects" 2 &&
 235
 236                cat >expect <<-\EOF &&
 237                *.txt text
 238
 239                #
 240                # Git LFS (see https://git-lfs.github.com/)
 241                #
 242                /file2.dat filter=lfs diff=lfs merge=lfs -text
 243                /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs diff=lfs merge=lfs -text
 244                EOF
 245                test_path_is_file .gitattributes &&
 246                test_cmp expect .gitattributes
 247        )
 248'
 249
 250test_expect_success 'Add big files to repo and store files in LFS based on compressed size (>28 bytes)' '
 251        client_view "//depot/... //client/..." &&
 252        (
 253                cd "$cli" &&
 254                echo "content 5 bin 40 bytes XXXXXXXXXXXXXXXX" >file5.bin &&
 255                p4 add file5.bin &&
 256                p4 submit -d "Add file with small footprint after compression" &&
 257
 258                echo "content 6 bin 39 bytes XXXXXYYYYYZZZZZ" >file6.bin &&
 259                p4 add file6.bin &&
 260                p4 submit -d "Add file with large footprint after compression"
 261        ) &&
 262
 263        client_view "//depot/... //client/..." &&
 264        test_when_finished cleanup_git &&
 265        (
 266                cd "$git" &&
 267                git init . &&
 268                git config git-p4.useClientSpec true &&
 269                git config git-p4.largeFileSystem GitLFS &&
 270                git config git-p4.largeFileCompressedThreshold 28 &&
 271                # We only import HEAD here ("@all" is missing!)
 272                git p4 clone --destination="$git" //depot &&
 273
 274                test_file_in_lfs file6.bin 39 "content 6 bin 39 bytes XXXXXYYYYYZZZZZ" &&
 275                test_file_count_in_dir ".git/lfs/objects" 1 &&
 276
 277                cat >expect <<-\EOF &&
 278                *.txt text
 279
 280                #
 281                # Git LFS (see https://git-lfs.github.com/)
 282                #
 283                /file6.bin filter=lfs diff=lfs merge=lfs -text
 284                EOF
 285                test_path_is_file .gitattributes &&
 286                test_cmp expect .gitattributes
 287        )
 288'
 289
 290test_expect_success 'kill p4d' '
 291        kill_p4d
 292'
 293
 294test_done