t / t9824-git-p4-git-lfs.shon commit i18n: rebase-interactive: mark comments of squash for translation (f2d1706)
   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                echo "content 1 txt 23 bytes" >file1.txt &&
  46                p4 add file1.txt &&
  47                echo "content 2-3 bin 25 bytes" >file2.dat &&
  48                p4 add file2.dat &&
  49                p4 submit -d "Add text and binary file" &&
  50
  51                mkdir "path with spaces" &&
  52                echo "content 2-3 bin 25 bytes" >"path with spaces/file3.bin" &&
  53                p4 add "path with spaces/file3.bin" &&
  54                p4 submit -d "Add another binary file with same content and spaces in path" &&
  55
  56                echo "content 4 bin 26 bytes XX" >file4.bin &&
  57                p4 add file4.bin &&
  58                p4 submit -d "Add another binary file with different content"
  59        )
  60'
  61
  62test_expect_success 'Store files in LFS based on size (>24 bytes)' '
  63        client_view "//depot/... //client/..." &&
  64        test_when_finished cleanup_git &&
  65        (
  66                cd "$git" &&
  67                git init . &&
  68                git config git-p4.useClientSpec true &&
  69                git config git-p4.largeFileSystem GitLFS &&
  70                git config git-p4.largeFileThreshold 24 &&
  71                git p4 clone --destination="$git" //depot@all &&
  72
  73                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
  74                test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
  75                test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
  76
  77                test_file_count_in_dir ".git/lfs/objects" 2 &&
  78
  79                cat >expect <<-\EOF &&
  80
  81                #
  82                # Git LFS (see https://git-lfs.github.com/)
  83                #
  84                /file2.dat filter=lfs -text
  85                /file4.bin filter=lfs -text
  86                /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs -text
  87                EOF
  88                test_path_is_file .gitattributes &&
  89                test_cmp expect .gitattributes
  90        )
  91'
  92
  93test_expect_success 'Store files in LFS based on size (>25 bytes)' '
  94        client_view "//depot/... //client/..." &&
  95        test_when_finished cleanup_git &&
  96        (
  97                cd "$git" &&
  98                git init . &&
  99                git config git-p4.useClientSpec true &&
 100                git config git-p4.largeFileSystem GitLFS &&
 101                git config git-p4.largeFileThreshold 25 &&
 102                git p4 clone --destination="$git" //depot@all &&
 103
 104                test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
 105                test_file_count_in_dir ".git/lfs/objects" 1 &&
 106
 107                cat >expect <<-\EOF &&
 108
 109                #
 110                # Git LFS (see https://git-lfs.github.com/)
 111                #
 112                /file4.bin filter=lfs -text
 113                EOF
 114                test_path_is_file .gitattributes &&
 115                test_cmp expect .gitattributes
 116        )
 117'
 118
 119test_expect_success 'Store files in LFS based on extension (dat)' '
 120        client_view "//depot/... //client/..." &&
 121        test_when_finished cleanup_git &&
 122        (
 123                cd "$git" &&
 124                git init . &&
 125                git config git-p4.useClientSpec true &&
 126                git config git-p4.largeFileSystem GitLFS &&
 127                git config git-p4.largeFileExtensions dat &&
 128                git p4 clone --destination="$git" //depot@all &&
 129
 130                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 131                test_file_count_in_dir ".git/lfs/objects" 1 &&
 132
 133                cat >expect <<-\EOF &&
 134
 135                #
 136                # Git LFS (see https://git-lfs.github.com/)
 137                #
 138                *.dat filter=lfs -text
 139                EOF
 140                test_path_is_file .gitattributes &&
 141                test_cmp expect .gitattributes
 142        )
 143'
 144
 145test_expect_success 'Store files in LFS based on size (>25 bytes) and extension (dat)' '
 146        client_view "//depot/... //client/..." &&
 147        test_when_finished cleanup_git &&
 148        (
 149                cd "$git" &&
 150                git init . &&
 151                git config git-p4.useClientSpec true &&
 152                git config git-p4.largeFileSystem GitLFS &&
 153                git config git-p4.largeFileExtensions dat &&
 154                git config git-p4.largeFileThreshold 25 &&
 155                git p4 clone --destination="$git" //depot@all &&
 156
 157                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 158                test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
 159                test_file_count_in_dir ".git/lfs/objects" 2 &&
 160
 161                cat >expect <<-\EOF &&
 162
 163                #
 164                # Git LFS (see https://git-lfs.github.com/)
 165                #
 166                *.dat filter=lfs -text
 167                /file4.bin filter=lfs -text
 168                EOF
 169                test_path_is_file .gitattributes &&
 170                test_cmp expect .gitattributes
 171        )
 172'
 173
 174test_expect_success 'Remove file from repo and store files in LFS based on size (>24 bytes)' '
 175        client_view "//depot/... //client/..." &&
 176        (
 177                cd "$cli" &&
 178                p4 delete file4.bin &&
 179                p4 submit -d "Remove file"
 180        ) &&
 181
 182        client_view "//depot/... //client/..." &&
 183        test_when_finished cleanup_git &&
 184        (
 185                cd "$git" &&
 186                git init . &&
 187                git config git-p4.useClientSpec true &&
 188                git config git-p4.largeFileSystem GitLFS &&
 189                git config git-p4.largeFileThreshold 24 &&
 190                git p4 clone --destination="$git" //depot@all &&
 191
 192                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 193                test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
 194                test_path_is_missing file4.bin &&
 195                test_file_count_in_dir ".git/lfs/objects" 2 &&
 196
 197                cat >expect <<-\EOF &&
 198
 199                #
 200                # Git LFS (see https://git-lfs.github.com/)
 201                #
 202                /file2.dat filter=lfs -text
 203                /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs -text
 204                EOF
 205                test_path_is_file .gitattributes &&
 206                test_cmp expect .gitattributes
 207        )
 208'
 209
 210test_expect_success 'Add .gitattributes and store files in LFS based on size (>24 bytes)' '
 211        client_view "//depot/... //client/..." &&
 212        (
 213                cd "$cli" &&
 214                echo "*.txt text" >.gitattributes &&
 215                p4 add .gitattributes &&
 216                p4 submit -d "Add .gitattributes"
 217        ) &&
 218
 219        client_view "//depot/... //client/..." &&
 220        test_when_finished cleanup_git &&
 221        (
 222                cd "$git" &&
 223                git init . &&
 224                git config git-p4.useClientSpec true &&
 225                git config git-p4.largeFileSystem GitLFS &&
 226                git config git-p4.largeFileThreshold 24 &&
 227                git p4 clone --destination="$git" //depot@all &&
 228
 229                test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
 230                test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
 231                test_path_is_missing file4.bin &&
 232                test_file_count_in_dir ".git/lfs/objects" 2 &&
 233
 234                cat >expect <<-\EOF &&
 235                *.txt text
 236
 237                #
 238                # Git LFS (see https://git-lfs.github.com/)
 239                #
 240                /file2.dat filter=lfs -text
 241                /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs -text
 242                EOF
 243                test_path_is_file .gitattributes &&
 244                test_cmp expect .gitattributes
 245        )
 246'
 247
 248test_expect_success 'Add big files to repo and store files in LFS based on compressed size (>28 bytes)' '
 249        client_view "//depot/... //client/..." &&
 250        (
 251                cd "$cli" &&
 252                echo "content 5 bin 40 bytes XXXXXXXXXXXXXXXX" >file5.bin &&
 253                p4 add file5.bin &&
 254                p4 submit -d "Add file with small footprint after compression" &&
 255
 256                echo "content 6 bin 39 bytes XXXXXYYYYYZZZZZ" >file6.bin &&
 257                p4 add file6.bin &&
 258                p4 submit -d "Add file with large footprint after compression"
 259        ) &&
 260
 261        client_view "//depot/... //client/..." &&
 262        test_when_finished cleanup_git &&
 263        (
 264                cd "$git" &&
 265                git init . &&
 266                git config git-p4.useClientSpec true &&
 267                git config git-p4.largeFileSystem GitLFS &&
 268                git config git-p4.largeFileCompressedThreshold 28 &&
 269                # We only import HEAD here ("@all" is missing!)
 270                git p4 clone --destination="$git" //depot &&
 271
 272                test_file_in_lfs file6.bin 39 "content 6 bin 39 bytes XXXXXYYYYYZZZZZ" &&
 273                test_file_count_in_dir ".git/lfs/objects" 1 &&
 274
 275                cat >expect <<-\EOF &&
 276                *.txt text
 277
 278                #
 279                # Git LFS (see https://git-lfs.github.com/)
 280                #
 281                /file6.bin filter=lfs -text
 282                EOF
 283                test_path_is_file .gitattributes &&
 284                test_cmp expect .gitattributes
 285        )
 286'
 287
 288test_expect_success 'kill p4d' '
 289        kill_p4d
 290'
 291
 292test_done