| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # assert_pointer confirms that the pointer in the repository for $path in the |
| 4 | # given $ref matches the given $oid and $size. |
| 5 | # Note that $path is prepended with a space to match the against the start |
| 6 | # of path field in the ls-tree output, so be careful if your test involves |
| 7 | # files with spaces in their paths. |
| 8 | # |
| 9 | # $ assert_pointer "main" "path/to/file" "some-oid" 123 |
| 10 | assert_pointer() { |
| 11 | local ref="$1" |
| 12 | local path="$2" |
| 13 | local oid="$3" |
| 14 | local size="$4" |
| 15 | |
| 16 | gitblob=$(git ls-tree -lrz "$ref" | |
| 17 | while read -r -d $'\0' x; do |
| 18 | echo $x |
| 19 | done | |
| 20 | grep -F " $path" | cut -f 3 -d " ") |
| 21 | |
| 22 | actual=$(git cat-file -p $gitblob) |
| 23 | expected=$(pointer $oid $size) |
| 24 | |
| 25 | if [ "$expected" != "$actual" ]; then |
| 26 | exit 1 |
| 27 | fi |
| 28 | } |
| 29 | |
| 30 | # refute_pointer confirms that the file in the repository for $path in the |
| 31 | # given $ref is _not_ a pointer. |
| 32 | # Note that $path is prepended with a space to match the against the start |
| 33 | # of path field in the ls-tree output, so be careful if your test involves |
| 34 | # files with spaces in their paths. |
| 35 | # |
| 36 | # $ refute_pointer "main" "path/to/file" |
| 37 | refute_pointer() { |
| 38 | local ref="$1" |
| 39 | local path="$2" |
| 40 | |
| 41 | gitblob=$(git ls-tree -lrz "$ref" | |
| 42 | while read -r -d $'\0' x; do |
| 43 | echo $x |
| 44 | done | |
| 45 | grep -F " $path" | cut -f 3 -d " ") |
| 46 | |
| 47 | file=$(git cat-file -p $gitblob) |
| 48 | version="version https://git-lfs.github.com/spec/v[0-9]" |
| 49 | oid="oid sha256:[0-9a-f]\{64\}" |
| 50 | size="size [0-9]*" |
| 51 | regex="$version.*$oid.*$size" |
| 52 | |
| 53 | if echo $file | grep -q "$regex"; then |
| 54 | exit 1 |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | # local_object_path computes the path to the local storage for an oid |
| 59 | # $ local_object_path "some-oid" |
| 60 | local_object_path() { |
| 61 | local oid="$1" |
| 62 | local cfg=`git lfs env | grep LocalMediaDir` |
| 63 | echo "${cfg#LocalMediaDir=}/${oid:0:2}/${oid:2:2}/$oid" |
| 64 | } |
| 65 | |
| 66 | # assert_local_object confirms that an object file is stored for the given oid & |
| 67 | # has the correct size |
| 68 | # $ assert_local_object "some-oid" size |
| 69 | assert_local_object() { |
| 70 | local oid="$1" |
| 71 | local size="$2" |
| 72 | local f="$(local_object_path "$oid")" |
| 73 | actualsize=$(wc -c <"$f" | tr -d '[[:space:]]') |
| 74 | if [ "$size" != "$actualsize" ]; then |
| 75 | exit 1 |
| 76 | fi |
| 77 | } |
| 78 | |
| 79 | # is_valid_oid() confirms that an object ID is a valid SHA-256 hash, for use |
| 80 | # in our refute_*_object() functions which otherwise just check that an |
| 81 | # object file or record does not exist |
| 82 | is_valid_oid() { |
| 83 | local oid="$1" |
| 84 | |
| 85 | printf "%s" "$oid" | grep -q "^[0-9a-f]\{64\}$" |
| 86 | } |
| 87 | |
| 88 | # refute_local_object confirms that an object file is NOT stored for an oid. |
| 89 | # If "$size" is given as the second argument, assert that the file exists _and_ |
| 90 | # that it does _not_ the expected size |
| 91 | # |
| 92 | # $ refute_local_object "some-oid" |
| 93 | # $ refute_local_object "some-oid" "123" |
| 94 | refute_local_object() { |
| 95 | local oid="$1" |
| 96 | local size="$2" |
| 97 | |
| 98 | is_valid_oid "$oid" |
| 99 | |
| 100 | local f="$(local_object_path "$oid")" |
| 101 | if [ -e $f ]; then |
| 102 | if [ -z "$size" ]; then |
| 103 | exit 1 |
| 104 | fi |
| 105 | |
| 106 | actual_size="$(wc -c < "$f" | awk '{ print $1 }')" |
| 107 | if [ "$size" -eq "$actual_size" ]; then |
| 108 | echo >&2 "fatal: expected object $oid not to have size: $size" |
| 109 | exit 1 |
| 110 | fi |
| 111 | fi |
| 112 | } |
| 113 | |
| 114 | # delete_local_object deletes the local storage for an oid |
| 115 | # $ delete_local_object "some-oid" |
| 116 | delete_local_object() { |
| 117 | local oid="$1" |
| 118 | local f="$(local_object_path "$oid")" |
| 119 | # Note that so long as we do not use "rm -f" we do not need to first |
| 120 | # check that the object ID is valid or that file exists. |
| 121 | rm "$f" |
| 122 | } |
| 123 | |
| 124 | # corrupt_local_object corrupts the local storage for an oid |
| 125 | # $ corrupt_local_object "some-oid" |
| 126 | corrupt_local_object() { |
| 127 | local oid="$1" |
| 128 | local f="$(local_object_path "$oid")" |
| 129 | |
| 130 | [ -f "$f" ] |
| 131 | cp /dev/null "$f" |
| 132 | } |
| 133 | |
| 134 | |
| 135 | # check that the object does not exist in the git lfs server. HTTP log is |
| 136 | # written to http.log. JSON output is written to http.json. |
| 137 | # |
| 138 | # $ refute_server_object "reponame" "oid" |
| 139 | refute_server_object() { |
| 140 | local reponame="$1" |
| 141 | local oid="$2" |
| 142 | |
| 143 | [ -d "$(canonical_path "$REMOTEDIR/$reponame.git")" ] |
| 144 | is_valid_oid "$oid" |
| 145 | |
| 146 | curl -v "$GITSERVER/$reponame.git/info/lfs/objects/batch" \ |
| 147 | -u "user:pass" \ |
| 148 | -o http.json \ |
| 149 | -d "{\"operation\":\"download\",\"objects\":[{\"oid\":\"$oid\"}]}" \ |
| 150 | -H "Accept: application/vnd.git-lfs+json" \ |
| 151 | -H "Content-Type: application/vnd.git-lfs+json" \ |
| 152 | -H "X-Check-Object: 1" \ |
| 153 | -H "X-Ignore-Retries: true" 2>&1 | |
| 154 | tee http.log |
| 155 | |
| 156 | [ 0 -eq "$(grep -c "download" http.json)" ] || { |
| 157 | cat http.json |
| 158 | exit 1 |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | # Delete an object on the lfs server. HTTP log is |
| 163 | # written to http.log. JSON output is written to http.json. |
| 164 | # |
| 165 | # $ delete_server_object "reponame" "oid" |
| 166 | delete_server_object() { |
| 167 | local reponame="$1" |
| 168 | local oid="$2" |
| 169 | |
| 170 | [ -d "$(canonical_path "$REMOTEDIR/$reponame.git")" ] |
| 171 | is_valid_oid "$oid" |
| 172 | |
| 173 | curl -v "$GITSERVER/$reponame.git/info/lfs/objects/$oid" \ |
| 174 | -X DELETE \ |
| 175 | -u "user:pass" \ |
| 176 | -o http.json \ |
| 177 | -H "Accept: application/vnd.git-lfs+json" 2>&1 | |
| 178 | tee http.log |
| 179 | |
| 180 | grep "200 OK" http.log |
| 181 | } |
| 182 | |
| 183 | # check that the object does exist in the git lfs server. HTTP log is written |
| 184 | # to http.log. JSON output is written to http.json. |
| 185 | assert_server_object() { |
| 186 | local reponame="$1" |
| 187 | local oid="$2" |
| 188 | local refspec="$3" |
| 189 | curl -v "$GITSERVER/$reponame.git/info/lfs/objects/batch" \ |
| 190 | -u "user:pass" \ |
| 191 | -o http.json \ |
| 192 | -d "{\"operation\":\"download\",\"objects\":[{\"oid\":\"$oid\"}],\"ref\":{\"name\":\"$refspec\"}}" \ |
| 193 | -H "Accept: application/vnd.git-lfs+json" \ |
| 194 | -H "Content-Type: application/vnd.git-lfs+json" \ |
| 195 | -H "X-Check-Object: 1" \ |
| 196 | -H "X-Ignore-Retries: true" 2>&1 | |
| 197 | tee http.log |
| 198 | grep "200 OK" http.log |
| 199 | |
| 200 | grep "download" http.json || { |
| 201 | cat http.json |
| 202 | exit 1 |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | # assert_remote_object() confirms that an object file with the given OID and |
| 207 | # size is stored in the "remote" copy of a repository |
| 208 | assert_remote_object() { |
| 209 | local reponame="$1" |
| 210 | local oid="$2" |
| 211 | local size="$3" |
| 212 | local destination="$(canonical_path "$REMOTEDIR/$reponame.git")" |
| 213 | |
| 214 | pushd "$destination" |
| 215 | local f="$(local_object_path "$oid")" |
| 216 | actualsize="$(wc -c <"$f" | tr -d '[[:space:]]')" |
| 217 | [ "$size" -eq "$actualsize" ] |
| 218 | popd |
| 219 | } |
| 220 | |
| 221 | # refute_remote_object() confirms that an object file with the given OID |
| 222 | # is not stored in the "remote" copy of a repository |
| 223 | refute_remote_object() { |
| 224 | local reponame="$1" |
| 225 | local oid="$2" |
| 226 | |
| 227 | is_valid_oid "$oid" |
| 228 | |
| 229 | local destination="$(canonical_path "$REMOTEDIR/$reponame.git")" |
| 230 | |
| 231 | pushd "$destination" |
| 232 | local f="$(local_object_path "$oid")" |
| 233 | if [ -e $f ]; then |
| 234 | exit 1 |
| 235 | fi |
| 236 | popd |
| 237 | } |
| 238 | |
| 239 | # Set rate limit counts on the LFS server. HTTP log is written to http.log. |
| 240 | # |
| 241 | # $ reset_server_rate_limit "api" "direction" "reponame" "oid" "num-tokens" |
| 242 | set_server_rate_limit() { |
| 243 | local api="$1" |
| 244 | local direction="$2" |
| 245 | local reponame="$3" |
| 246 | local oid="$4" |
| 247 | local tokens="$5" |
| 248 | |
| 249 | local query="api=$api&direction=$direction&repo=$reponame&oid=$oid&tokens=$tokens" |
| 250 | |
| 251 | curl -v "$GITSERVER/limits/?$query" 2>&1 | tee http.log |
| 252 | |
| 253 | grep "200 OK" http.log |
| 254 | } |
| 255 | |
| 256 | check_server_lock_ssh() { |
| 257 | local reponame="$1" |
| 258 | local id="$2" |
| 259 | local refspec="$3" |
| 260 | local destination="$(canonical_path "$REMOTEDIR/$reponame.git")" |
| 261 | |
| 262 | ( |
| 263 | pktize_text 'version 1' |
| 264 | pktize_flush |
| 265 | pktize_text 'list-lock' |
| 266 | pktize_text "id=$id" |
| 267 | pktize_text "refname=$refname" |
| 268 | pktize_flush |
| 269 | pktize_text 'quit' |
| 270 | pktize_flush |
| 271 | ) | lfs-ssh-echo [email protected] "git-lfs-transfer '$destination' download" 2>&1 |
| 272 | } |
| 273 | |
| 274 | # This asserts the lock path and returns the lock ID by parsing the response of |
| 275 | # |
| 276 | # git lfs lock --json <path> |
| 277 | assert_lock() { |
| 278 | local log="$1" |
| 279 | local path="$2" |
| 280 | |
| 281 | if [ $(grep -c "\"path\":\"$path\"" "$log") -eq 0 ]; then |
| 282 | echo "path '$path' not found in:" |
| 283 | cat "$log" |
| 284 | exit 1 |
| 285 | fi |
| 286 | |
| 287 | local jsonid=$(grep -oh "\"id\":\"\w\+\"" "$log") |
| 288 | echo "${jsonid:3}" | tr -d \"\: |
| 289 | } |
| 290 | |
| 291 | # assert that a lock with the given ID exists on the test server |
| 292 | assert_server_lock() { |
| 293 | local reponame="$1" |
| 294 | local id="$2" |
| 295 | local refspec="$3" |
| 296 | |
| 297 | curl -v "$GITSERVER/$reponame.git/info/lfs/locks?refspec=$refspec" \ |
| 298 | -u "user:pass" \ |
| 299 | -o http.json \ |
| 300 | -H "Accept:application/vnd.git-lfs+json" 2>&1 | |
| 301 | tee http.log |
| 302 | |
| 303 | grep "200 OK" http.log |
| 304 | grep "$id" http.json || { |
| 305 | cat http.json |
| 306 | exit 1 |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | # assert that a lock with the given ID exists on the test server |
| 311 | assert_server_lock_ssh() { |
| 312 | local reponame="$1" |
| 313 | local id="$2" |
| 314 | local refspec="$3" |
| 315 | |
| 316 | check_server_lock_ssh "$reponame" "$id" "$refspec" | |
| 317 | tee output.log |
| 318 | |
| 319 | grep "status 200" output.log |
| 320 | grep "$id" output.log || { |
| 321 | cat output.log |
| 322 | exit 1 |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | # refute that a lock with the given ID exists on the test server |
| 327 | refute_server_lock() { |
| 328 | local reponame="$1" |
| 329 | local id="$2" |
| 330 | local refspec="$3" |
| 331 | |
| 332 | curl -v "$GITSERVER/$reponame.git/info/lfs/locks?refspec=$refspec" \ |
| 333 | -u "user:pass" \ |
| 334 | -o http.json \ |
| 335 | -H "Accept:application/vnd.git-lfs+json" 2>&1 | tee http.log |
| 336 | |
| 337 | grep "200 OK" http.log |
| 338 | |
| 339 | [ $(grep -c "$id" http.json) -eq 0 ] |
| 340 | } |
| 341 | |
| 342 | # refute that a lock with the given ID exists on the test server |
| 343 | refute_server_lock_ssh() { |
| 344 | local reponame="$1" |
| 345 | local id="$2" |
| 346 | local refspec="$3" |
| 347 | local destination="$(canonical_path "$REMOTEDIR/$reponame.git")" |
| 348 | |
| 349 | check_server_lock_ssh "$reponame" "$id" "$refspec" | |
| 350 | tee output.log |
| 351 | |
| 352 | grep "status 200" output.log |
| 353 | if grep "$id" output.log |
| 354 | then |
| 355 | cat output.log |
| 356 | exit 1 |
| 357 | fi |
| 358 | } |
| 359 | |
| 360 | # Assert that .gitattributes contains a given attribute N times |
| 361 | assert_attributes_count() { |
| 362 | local fileext="$1" |
| 363 | local attrib="$2" |
| 364 | local count="$3" |
| 365 | |
| 366 | pattern="\(*.\)\?$fileext\(.*\)$attrib" |
| 367 | actual="$(grep -c -e "$pattern" .gitattributes || true)" |
| 368 | if [ "$(printf "%d" "$actual")" -ne "$count" ]; then |
| 369 | echo "wrong number of $attrib entries for $fileext" |
| 370 | echo "expected: $count actual: $actual" |
| 371 | cat .gitattributes |
| 372 | exit 1 |
| 373 | fi |
| 374 | } |
| 375 | |
| 376 | assert_file_writeable() { |
| 377 | ls -l "$1" | grep -e "^-rw" |
| 378 | } |
| 379 | |
| 380 | refute_file_writeable() { |
| 381 | ls -l "$1" | grep -e "^-r-" |
| 382 | } |
| 383 | |
| 384 | git_root() { |
| 385 | git rev-parse --show-toplevel 2>/dev/null |
| 386 | } |
| 387 | |
| 388 | dot_git_dir() { |
| 389 | echo "$(git_root)/.git" |
| 390 | } |
| 391 | |
| 392 | assert_hooks() { |
| 393 | local git_root="$1" |
| 394 | |
| 395 | if [ -z "$git_root" ]; then |
| 396 | echo >&2 "fatal: (assert_hooks) not in git repository" |
| 397 | exit 1 |
| 398 | fi |
| 399 | |
| 400 | [ -x "$git_root/hooks/post-checkout" ] |
| 401 | [ -x "$git_root/hooks/post-commit" ] |
| 402 | [ -x "$git_root/hooks/post-merge" ] |
| 403 | [ -x "$git_root/hooks/pre-push" ] |
| 404 | } |
| 405 | |
| 406 | assert_clean_index() { |
| 407 | [ -z "$(git diff-index --cached HEAD)" ] |
| 408 | } |
| 409 | |
| 410 | assert_clean_worktree() { |
| 411 | [ -z "$(git diff-index HEAD)" ] |
| 412 | } |
| 413 | |
| 414 | assert_clean_worktree_with_exceptions() { |
| 415 | local exceptions="$1" |
| 416 | |
| 417 | [ 0 -eq "$(git diff-index HEAD | grep -c -v -E "$exceptions")" ] |
| 418 | } |
| 419 | |
| 420 | assert_clean_status() { |
| 421 | assert_clean_worktree |
| 422 | |
| 423 | status="$(git status)" |
| 424 | echo "$status" | grep "working \(directory\|tree\) clean" || { |
| 425 | echo $status |
| 426 | git lfs status |
| 427 | exit 1 |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | # pointer returns a string Git LFS pointer file. |
| 432 | # |
| 433 | # $ pointer abc-some-oid 123 <version> |
| 434 | # > version ... |
| 435 | pointer() { |
| 436 | local oid=$1 |
| 437 | local size=$2 |
| 438 | local version=${3:-https://git-lfs.github.com/spec/v1} |
| 439 | printf "version %s |
| 440 | oid sha256:%s |
| 441 | size %s |
| 442 | " "$version" "$oid" "$size" |
| 443 | } |
| 444 | |
| 445 | # wait_for_file simply sleeps until a file exists. |
| 446 | # |
| 447 | # $ wait_for_file "path/to/upcoming/file" |
| 448 | wait_for_file() { |
| 449 | local filename="$1" |
| 450 | n=0 |
| 451 | wait_time=1 |
| 452 | while [ $n -lt 17 ]; do |
| 453 | if [ -s $filename ]; then |
| 454 | return 0 |
| 455 | fi |
| 456 | |
| 457 | sleep $wait_time |
| 458 | n=`expr $n + 1` |
| 459 | if [ $wait_time -lt 4 ]; then |
| 460 | wait_time=`expr $wait_time \* 2` |
| 461 | fi |
| 462 | done |
| 463 | |
| 464 | echo "$filename did not appear after 60 seconds." |
| 465 | return 1 |
| 466 | } |
| 467 | |
| 468 | # setup_remote_repo initializes a bare Git repository that is accessible through |
| 469 | # the test Git server. The `pwd` is set to the repository's directory, in case |
| 470 | # further commands need to be run. This server is running for every test in an |
| 471 | # integration run, so every test file should setup its own remote repository to |
| 472 | # avoid conflicts. |
| 473 | # |
| 474 | # $ setup_remote_repo "some-name" |
| 475 | # |
| 476 | setup_remote_repo() { |
| 477 | local reponame="$1" |
| 478 | echo "set up remote git repository: $reponame" |
| 479 | repodir="$REMOTEDIR/$reponame.git" |
| 480 | mkdir -p "$repodir" |
| 481 | cd "$repodir" |
| 482 | git init --bare |
| 483 | git config http.receivepack true |
| 484 | git config receive.denyCurrentBranch ignore |
| 485 | } |
| 486 | |
| 487 | # creates a bare remote repository for a local clone. Useful to test pushing to |
| 488 | # a fresh remote server. |
| 489 | # |
| 490 | # $ setup_alternate_remote "$reponame-whatever" |
| 491 | # $ setup_alternate_remote "$reponame-whatever" "other-remote-name" |
| 492 | # |
| 493 | setup_alternate_remote() { |
| 494 | local newRemoteName=$1 |
| 495 | local remote=${2:-origin} |
| 496 | |
| 497 | wd=`pwd` |
| 498 | |
| 499 | setup_remote_repo "$newRemoteName" |
| 500 | cd $wd |
| 501 | git remote rm "$remote" |
| 502 | git remote add "$remote" "$GITSERVER/$newRemoteName" |
| 503 | } |
| 504 | |
| 505 | # clone_repo clones a repository from the test Git server to the subdirectory |
| 506 | # $dir under $TRASHDIR. setup_remote_repo() needs to be run first. Output is |
| 507 | # written to clone.log. |
| 508 | clone_repo() { |
| 509 | cd "$TRASHDIR" |
| 510 | |
| 511 | local reponame="$1" |
| 512 | local dir="$2" |
| 513 | echo "clone local git repository $reponame to $dir" |
| 514 | git clone "$GITSERVER/$reponame" "$dir" 2>&1 | tee clone.log |
| 515 | |
| 516 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 517 | return 1 |
| 518 | fi |
| 519 | |
| 520 | cd "$dir" |
| 521 | mv ../clone.log . |
| 522 | |
| 523 | git config credential.helper lfstest |
| 524 | } |
| 525 | |
| 526 | # clone_repo_url clones a Git repository to the subdirectory $dir under $TRASHDIR. |
| 527 | # setup_remote_repo() needs to be run first. Output is written to clone.log. |
| 528 | clone_repo_url() { |
| 529 | cd "$TRASHDIR" |
| 530 | |
| 531 | local repo="$1" |
| 532 | local dir="$2" |
| 533 | echo "clone git repository $repo to $dir" |
| 534 | git clone "$repo" "$dir" 2>&1 | tee clone.log |
| 535 | |
| 536 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 537 | return 1 |
| 538 | fi |
| 539 | |
| 540 | cd "$dir" |
| 541 | mv ../clone.log . |
| 542 | |
| 543 | git config credential.helper lfstest |
| 544 | } |
| 545 | |
| 546 | # clone_repo_ssl clones a repository from the test Git server to the subdirectory |
| 547 | # $dir under $TRASHDIR, using the SSL endpoint. |
| 548 | # setup_remote_repo() needs to be run first. Output is written to clone_ssl.log. |
| 549 | clone_repo_ssl() { |
| 550 | cd "$TRASHDIR" |
| 551 | |
| 552 | local reponame="$1" |
| 553 | local dir="$2" |
| 554 | echo "clone local git repository $reponame to $dir" |
| 555 | git clone "$SSLGITSERVER/$reponame" "$dir" 2>&1 | tee clone_ssl.log |
| 556 | |
| 557 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 558 | return 1 |
| 559 | fi |
| 560 | |
| 561 | cd "$dir" |
| 562 | mv ../clone_ssl.log . |
| 563 | |
| 564 | git config credential.helper lfstest |
| 565 | } |
| 566 | |
| 567 | # clone_repo_clientcert clones a repository from the test Git server to the subdirectory |
| 568 | # $dir under $TRASHDIR, using the client cert endpoint. |
| 569 | # setup_remote_repo() needs to be run first. Output is written to clone_client_cert.log. |
| 570 | clone_repo_clientcert() { |
| 571 | cd "$TRASHDIR" |
| 572 | |
| 573 | local reponame="$1" |
| 574 | local dir="$2" |
| 575 | echo "clone $CLIENTCERTGITSERVER/$reponame to $dir" |
| 576 | git clone "$CLIENTCERTGITSERVER/$reponame" "$dir" 2>&1 | tee clone_client_cert.log |
| 577 | |
| 578 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 579 | return 1 |
| 580 | fi |
| 581 | |
| 582 | cd "$dir" |
| 583 | mv ../clone_client_cert.log . |
| 584 | |
| 585 | git config credential.helper lfstest |
| 586 | } |
| 587 | |
| 588 | # setup_remote_repo_with_file creates a remote repo, clones it locally, commits |
| 589 | # a file tracked by LFS, and pushes it to the remote: |
| 590 | # |
| 591 | # setup_remote_repo_with_file "reponame" "filename" |
| 592 | setup_remote_repo_with_file() { |
| 593 | local reponame="$1" |
| 594 | local filename="$2" |
| 595 | local dirname="$(dirname "$filename")" |
| 596 | |
| 597 | setup_remote_repo "$reponame" |
| 598 | clone_repo "$reponame" "clone_$reponame" |
| 599 | |
| 600 | mkdir -p "$dirname" |
| 601 | |
| 602 | git lfs track "$filename" |
| 603 | echo "$filename" > "$filename" |
| 604 | git add .gitattributes $filename |
| 605 | git commit -m "add $filename" | tee commit.log |
| 606 | |
| 607 | grep "main (root-commit)" commit.log |
| 608 | grep "2 files changed" commit.log |
| 609 | grep "create mode 100644 $filename" commit.log |
| 610 | grep "create mode 100644 .gitattributes" commit.log |
| 611 | |
| 612 | git push origin main 2>&1 | tee push.log |
| 613 | grep "main -> main" push.log |
| 614 | } |
| 615 | |
| 616 | # substring_position returns the position of a substring in a 1-indexed search |
| 617 | # space. |
| 618 | # |
| 619 | # [ "$(substring_position "foo bar baz" "baz")" -eq "9" ] |
| 620 | substring_position() { |
| 621 | local str="$1" |
| 622 | local substr="$2" |
| 623 | |
| 624 | # 1) Print the string... |
| 625 | # 2) Remove the substring and everything after it |
| 626 | # 3) Count the number of characters (bytes) left, i.e., the offset of the |
| 627 | # string we were looking for. |
| 628 | |
| 629 | echo "$str" \ |
| 630 | | sed "s/$substr.*$//" \ |
| 631 | | wc -c |
| 632 | } |
| 633 | |
| 634 | # repo_endpoint returns the LFS endpoint for a given server and repository. |
| 635 | # |
| 636 | # [ "$GITSERVER/example/repo.git/info/lfs" = "$(repo_endpoint $GITSERVER example-repo)" ] |
| 637 | repo_endpoint() { |
| 638 | local server="$1" |
| 639 | local repo="$2" |
| 640 | |
| 641 | echo "$server/$repo.git/info/lfs" |
| 642 | } |
| 643 | |
| 644 | # write_creds_file writes credentials to a file iff it doesn't exist. |
| 645 | write_creds_file() { |
| 646 | local creds="$1" |
| 647 | local file="$2" |
| 648 | |
| 649 | if [ ! -f "$file" ] |
| 650 | then |
| 651 | printf "%s" "$creds" > "$file" |
| 652 | fi |
| 653 | } |
| 654 | |
| 655 | setup_creds() { |
| 656 | mkdir -p "$CREDSDIR" |
| 657 | write_creds_file ":user:pass" "$CREDSDIR/127.0.0.1" |
| 658 | } |
| 659 | |
| 660 | # setup initializes the clean, isolated environment for integration tests. |
| 661 | setup() { |
| 662 | cd "$ROOTDIR" |
| 663 | |
| 664 | if [ ! -d "$REMOTEDIR" ]; then |
| 665 | mkdir "$REMOTEDIR" |
| 666 | fi |
| 667 | |
| 668 | echo "# Git LFS: ${LFS_BIN:-$(command -v git-lfs)}" |
| 669 | git lfs version | sed -e 's/^/# /g' |
| 670 | git version | sed -e 's/^/# /g' |
| 671 | |
| 672 | LFSTEST_URL="$LFS_URL_FILE" \ |
| 673 | LFSTEST_SSL_URL="$LFS_SSL_URL_FILE" \ |
| 674 | LFSTEST_CLIENT_CERT_URL="$LFS_CLIENT_CERT_URL_FILE" \ |
| 675 | LFSTEST_DIR="$REMOTEDIR" \ |
| 676 | LFSTEST_CERT="$LFS_CERT_FILE" \ |
| 677 | LFSTEST_CLIENT_CERT="$LFS_CLIENT_CERT_FILE" \ |
| 678 | LFSTEST_CLIENT_KEY="$LFS_CLIENT_KEY_FILE" \ |
| 679 | LFSTEST_CLIENT_KEY_ENCRYPTED="$LFS_CLIENT_KEY_FILE_ENCRYPTED" \ |
| 680 | lfstest-count-tests increment |
| 681 | |
| 682 | wait_for_file "$LFS_URL_FILE" |
| 683 | wait_for_file "$LFS_SSL_URL_FILE" |
| 684 | wait_for_file "$LFS_CLIENT_CERT_URL_FILE" |
| 685 | wait_for_file "$LFS_CERT_FILE" |
| 686 | wait_for_file "$LFS_CLIENT_CERT_FILE" |
| 687 | wait_for_file "$LFS_CLIENT_KEY_FILE" |
| 688 | wait_for_file "$LFS_CLIENT_KEY_FILE_ENCRYPTED" |
| 689 | |
| 690 | LFS_CLIENT_CERT_URL=`cat $LFS_CLIENT_CERT_URL_FILE` |
| 691 | |
| 692 | # Set up the initial git config and osx keychain if applicable |
| 693 | HOME="$TESTHOME" |
| 694 | if [ ! -d "$HOME" ]; then |
| 695 | mkdir "$HOME" |
| 696 | fi |
| 697 | |
| 698 | # do not let Git use a different configuration file |
| 699 | unset GIT_CONFIG |
| 700 | unset XDG_CONFIG_HOME |
| 701 | |
| 702 | if [ ! -f $HOME/.gitconfig ]; then |
| 703 | git lfs install --skip-repo |
| 704 | git config --global credential.usehttppath true |
| 705 | git config --global credential.helper lfstest |
| 706 | git config --global user.name "Git LFS Tests" |
| 707 | git config --global user.email "[email protected]" |
| 708 | git config --global http.sslcainfo "$LFS_CERT_FILE" |
| 709 | git config --global init.defaultBranch main |
| 710 | fi | sed -e 's/^/# /g' |
| 711 | |
| 712 | # setup the git credential password storage |
| 713 | setup_creds |
| 714 | |
| 715 | echo "#" |
| 716 | echo "# HOME: $HOME" |
| 717 | echo "# TMP: $TMPDIR" |
| 718 | echo "# CREDS: $CREDSDIR" |
| 719 | echo "# lfstest-gitserver:" |
| 720 | echo "# LFSTEST_URL=$LFS_URL_FILE" |
| 721 | echo "# LFSTEST_SSL_URL=$LFS_SSL_URL_FILE" |
| 722 | echo "# LFSTEST_CLIENT_CERT_URL=$LFS_CLIENT_CERT_URL_FILE ($LFS_CLIENT_CERT_URL)" |
| 723 | echo "# LFSTEST_CERT=$LFS_CERT_FILE" |
| 724 | echo "# LFSTEST_CLIENT_CERT=$LFS_CLIENT_CERT_FILE" |
| 725 | echo "# LFSTEST_CLIENT_KEY=$LFS_CLIENT_KEY_FILE" |
| 726 | echo "# LFSTEST_CLIENT_KEY_ENCRYPTED=$LFS_CLIENT_KEY_FILE_ENCRYPTED" |
| 727 | echo "# LFSTEST_DIR=$REMOTEDIR" |
| 728 | } |
| 729 | |
| 730 | # shutdown cleans the $TRASHDIR and shuts the test Git server down. |
| 731 | shutdown() { |
| 732 | # every t/t-*.sh file should cleanup its trashdir |
| 733 | [ -z "$KEEPTRASH" ] && rm -rf "$TRASHDIR" |
| 734 | |
| 735 | LFSTEST_DIR="$REMOTEDIR" \ |
| 736 | LFS_URL_FILE="$LFS_URL_FILE" \ |
| 737 | lfstest-count-tests decrement |
| 738 | |
| 739 | # delete entire lfs test root if we created it (double check pattern) |
| 740 | if [ -z "$KEEPTRASH" ] && [ "$RM_GIT_LFS_TEST_DIR" = "yes" ] && [[ $GIT_LFS_TEST_DIR == *"$TEMPDIR_PREFIX"* ]]; then |
| 741 | rm -rf "$GIT_LFS_TEST_DIR" |
| 742 | fi |
| 743 | } |
| 744 | |
| 745 | tap_show_plan() { |
| 746 | local tests="$1" |
| 747 | |
| 748 | printf "1..%i\n" "$tests" |
| 749 | } |
| 750 | |
| 751 | skip_if_root_or_admin() { |
| 752 | local test_description="$1" |
| 753 | |
| 754 | if [ "$IS_WINDOWS" -eq 1 ]; then |
| 755 | # The sfc.exe (System File Checker) command should be available on all |
| 756 | # modern Windows systems, and when run without arguments, returns help |
| 757 | # text, but only when the user has Administrator privileges. By checking |
| 758 | # the help text, if any, for the /SCANNOW (i.e., "scan now") option |
| 759 | # common to all versions of the command, we can determine if the |
| 760 | # current user has Administrator privileges. |
| 761 | # |
| 762 | # Adapted from: https://stackoverflow.com/a/58846650 |
| 763 | # https://stackoverflow.com/a/21295806 |
| 764 | SFC=$(sfc | tr -d '\0' | grep "SCANNOW") |
| 765 | if [ -n "$SFC" ]; then |
| 766 | printf "skip: '%s' test requires non-administrator privileges\n" \ |
| 767 | "$test_description" |
| 768 | exit 0 |
| 769 | fi |
| 770 | elif [ "$EUID" -eq 0 ]; then |
| 771 | printf "skip: '%s' test requires non-root user\n" "$test_description" |
| 772 | exit 0 |
| 773 | fi |
| 774 | } |
| 775 | |
| 776 | ensure_git_version_isnt() { |
| 777 | local expectedComparison=$1 |
| 778 | local version=$2 |
| 779 | |
| 780 | local gitVersion=$(git version | cut -d" " -f3) |
| 781 | |
| 782 | set +e |
| 783 | compare_version $gitVersion $version |
| 784 | result=$? |
| 785 | set -e |
| 786 | |
| 787 | if [[ $result == $expectedComparison ]]; then |
| 788 | echo "skip: $0 (git version $(comparison_to_operator $expectedComparison) $version)" |
| 789 | exit |
| 790 | fi |
| 791 | } |
| 792 | |
| 793 | VERSION_EQUAL=0 |
| 794 | VERSION_HIGHER=1 |
| 795 | VERSION_LOWER=2 |
| 796 | |
| 797 | # Compare $1 and $2 and return VERSION_EQUAL / VERSION_LOWER / VERSION_HIGHER |
| 798 | compare_version() { |
| 799 | if [[ $1 == $2 ]] |
| 800 | then |
| 801 | return $VERSION_EQUAL |
| 802 | fi |
| 803 | local IFS=. |
| 804 | local i ver1=($1) ver2=($2) |
| 805 | # fill empty fields in ver1 with zeros |
| 806 | for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) |
| 807 | do |
| 808 | ver1[i]=0 |
| 809 | done |
| 810 | for ((i=0; i<${#ver1[@]}; i++)) |
| 811 | do |
| 812 | if [[ -z ${ver2[i]} ]] |
| 813 | then |
| 814 | # fill empty fields in ver2 with zeros |
| 815 | ver2[i]=0 |
| 816 | fi |
| 817 | if ((10#${ver1[i]} > 10#${ver2[i]})) |
| 818 | then |
| 819 | return $VERSION_HIGHER |
| 820 | fi |
| 821 | if ((10#${ver1[i]} < 10#${ver2[i]})) |
| 822 | then |
| 823 | return $VERSION_LOWER |
| 824 | fi |
| 825 | done |
| 826 | return $VERSION_EQUAL |
| 827 | } |
| 828 | |
| 829 | comparison_to_operator() { |
| 830 | local comparison=$1 |
| 831 | if [[ $1 == $VERSION_EQUAL ]]; then |
| 832 | echo "==" |
| 833 | elif [[ $1 == $VERSION_HIGHER ]]; then |
| 834 | echo ">" |
| 835 | elif [[ $1 == $VERSION_LOWER ]]; then |
| 836 | echo "<" |
| 837 | else |
| 838 | echo "???" |
| 839 | fi |
| 840 | } |
| 841 | |
| 842 | # Calculate the object ID from the string passed as the argument |
| 843 | calc_oid() { |
| 844 | printf "%s" "$1" | $SHASUM | cut -f 1 -d " " |
| 845 | } |
| 846 | |
| 847 | # Calculate the object ID from the file passed as the argument |
| 848 | calc_oid_file() { |
| 849 | $SHASUM "$1" | cut -f 1 -d " " |
| 850 | } |
| 851 | |
| 852 | # Get a date string with an offset |
| 853 | # Args: One or more date offsets of the form (regex) "[+-]\d+[dmyHM]" |
| 854 | # e.g. +1d = 1 day forward from today |
| 855 | # -5y = 5 years before today |
| 856 | # Example call: |
| 857 | # D=$(get_date +1y +1m -5H) |
| 858 | # returns date as string in RFC3339 format ccyy-mm-ddThh:MM:ssZ |
| 859 | # note returns in UTC time not local time hence Z and not +/- |
| 860 | get_date() { |
| 861 | # Wrapped because BSD (inc OSX) & GNU 'date' functions are different |
| 862 | # on Windows under Git Bash it's GNU |
| 863 | if date --version >/dev/null 2>&1 ; then # GNU |
| 864 | ARGS="" |
| 865 | for var in "$@" |
| 866 | do |
| 867 | # GNU offsets are more verbose |
| 868 | unit=${var: -1} |
| 869 | val=${var:0:${#var}-1} |
| 870 | case "$unit" in |
| 871 | d) unit="days" ;; |
| 872 | m) unit="months" ;; |
| 873 | y) unit="years" ;; |
| 874 | H) unit="hours" ;; |
| 875 | M) unit="minutes" ;; |
| 876 | esac |
| 877 | ARGS="$ARGS $val $unit" |
| 878 | done |
| 879 | date -d "$ARGS" -u +%Y-%m-%dT%TZ |
| 880 | else # BSD |
| 881 | ARGS="" |
| 882 | for var in "$@" |
| 883 | do |
| 884 | ARGS="$ARGS -v$var" |
| 885 | done |
| 886 | date $ARGS -u +%Y-%m-%dT%TZ |
| 887 | fi |
| 888 | } |
| 889 | |
| 890 | # escape any instance of '\' with '\\' on Windows |
| 891 | escape_path() { |
| 892 | local unescaped="$1" |
| 893 | if [ $IS_WINDOWS -eq 1 ]; then |
| 894 | printf '%s' "${unescaped//\\/\\\\}" |
| 895 | else |
| 896 | printf '%s' "$unescaped" |
| 897 | fi |
| 898 | } |
| 899 | |
| 900 | # As native_path but escape all backslash characters to "\\" |
| 901 | native_path_escaped() { |
| 902 | local unescaped=$(native_path "$1") |
| 903 | escape_path "$unescaped" |
| 904 | } |
| 905 | |
| 906 | # native_path_list_separator prints the operating system-specific path list |
| 907 | # separator. |
| 908 | native_path_list_separator() { |
| 909 | if [ "$IS_WINDOWS" -eq 1 ]; then |
| 910 | printf ";"; |
| 911 | else |
| 912 | printf ":"; |
| 913 | fi |
| 914 | } |
| 915 | |
| 916 | # canonical_path prints the native path name in a canonical form, as if |
| 917 | # realpath(3) were called on it. |
| 918 | canonical_path() { |
| 919 | printf "%s" "$(lfstest-realpath "$(native_path "$1")")" |
| 920 | } |
| 921 | |
| 922 | # canonical_path_escaped prints the native path name in a canonical form, as if |
| 923 | # realpath(3) were called on it, and then escapes it. |
| 924 | canonical_path_escaped() { |
| 925 | printf "%s" "$(escape_path "$(lfstest-realpath "$(native_path "$1")")")" |
| 926 | } |
| 927 | |
| 928 | cat_end() { |
| 929 | if [ $IS_WINDOWS -eq 1 ]; then |
| 930 | printf '^M$' |
| 931 | else |
| 932 | printf '$' |
| 933 | fi |
| 934 | } |
| 935 | |
| 936 | # Compare 2 lists which are newline-delimited in a string, ignoring ordering and blank lines |
| 937 | contains_same_elements() { |
| 938 | # Remove blank lines then sort |
| 939 | diff -u <(printf '%s' "$1" | grep -v '^$' | sort) <(printf '%s' "$2" | grep -v '^$' | sort) |
| 940 | } |
| 941 | |
| 942 | is_stdin_attached() { |
| 943 | test -t0 |
| 944 | echo $? |
| 945 | } |
| 946 | |
| 947 | has_test_dir() { |
| 948 | if [ -z "$GIT_LFS_TEST_DIR" ]; then |
| 949 | echo "No GIT_LFS_TEST_DIR. Skipping..." |
| 950 | exit 0 |
| 951 | fi |
| 952 | } |
| 953 | |
| 954 | has_native_symlinks() { |
| 955 | if [ -z "$NATIVE_SYMLINKS" ]; then |
| 956 | if [ "$IS_WINDOWS" -eq 1 ]; then |
| 957 | # On Windows, we need to enable native symlink support in Cygwin or MSYS2, |
| 958 | # without falling back to default Cygwin symlink emulation. If this mode |
| 959 | # is not available, we should skip our tests with symbolic links. |
| 960 | # |
| 961 | # https://cygwin.com/cygwin-ug-net/using.html#pathnames-symlinks |
| 962 | # https://www.msys2.org/docs/symlinks/ |
| 963 | # https://learn.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development |
| 964 | export CYGWIN="winsymlinks:nativestrict${CYGWIN:+ $CYGWIN}" |
| 965 | export MSYS="winsymlinks:nativestrict${MSYS:+ $MSYS}" |
| 966 | |
| 967 | touch testfile.tmp |
| 968 | ln -s testfile.tmp testlink.tmp |
| 969 | |
| 970 | if [ $(fsutil reparsepoint query testlink.tmp | grep -c "Tag value: Symbolic Link") -eq 0 ]; then |
| 971 | NATIVE_SYMLINKS=0 |
| 972 | else |
| 973 | NATIVE_SYMLINKS=1 |
| 974 | fi |
| 975 | |
| 976 | rm -f testfile.tmp testlink.tmp |
| 977 | else |
| 978 | NATIVE_SYMLINKS=1 |
| 979 | fi |
| 980 | fi |
| 981 | |
| 982 | if [ "$NATIVE_SYMLINKS" -ne 1 ]; then |
| 983 | return 1 |
| 984 | else |
| 985 | return 0 |
| 986 | fi |
| 987 | } |
| 988 | |
| 989 | skip_if_symlinks_unsupported() { |
| 990 | has_native_symlinks || exit 0 |
| 991 | } |
| 992 | |
| 993 | add_symlink() { |
| 994 | local src=$1 |
| 995 | local dest=$2 |
| 996 | |
| 997 | prefix=`git rev-parse --show-prefix` |
| 998 | hashsrc=`printf "$src" | git hash-object -w --stdin` |
| 999 | |
| 1000 | git update-index --add --cacheinfo 120000 "$hashsrc" "$prefix$dest" |
| 1001 | git checkout -- "$dest" |
| 1002 | } |
| 1003 | |
| 1004 | setup_case_inverter_extension() { |
| 1005 | export LFSTEST_EXT_LOG="$TRASHDIR/caseinverterextension.log" |
| 1006 | |
| 1007 | git config lfs.extension.caseinverter.clean \ |
| 1008 | "lfstest-caseinverterextension clean -- %f" |
| 1009 | git config lfs.extension.caseinverter.smudge \ |
| 1010 | "lfstest-caseinverterextension smudge -- %f" |
| 1011 | git config lfs.extension.caseinverter.priority 0 |
| 1012 | } |
| 1013 | |
| 1014 | case_inverter_extension_pointer() { |
| 1015 | local ext_oid_line="ext-0-caseinverter sha256:$1" |
| 1016 | local base_pointer="$(pointer "$2" "$3")" |
| 1017 | |
| 1018 | printf "%s" "$base_pointer" | sed "s/^oid /$ext_oid_line\noid /" |
| 1019 | } |
| 1020 | |
| 1021 | invert_case() { |
| 1022 | printf "%s" "$1" | tr "[:lower:][:upper:]" "[:upper:][:lower:]" |
| 1023 | } |
| 1024 | |
| 1025 | urlify() { |
| 1026 | if [ "$IS_WINDOWS" -eq 1 ] |
| 1027 | then |
| 1028 | local prefix="" path="$(canonical_path "$1")" |
| 1029 | if echo "$path" | grep -qsv "^/" |
| 1030 | then |
| 1031 | prefix="/" |
| 1032 | fi |
| 1033 | echo "$prefix$path" | sed -e 's,\\,/,g' -e 's,:,%3a,g' -e 's, ,%20,g' |
| 1034 | else |
| 1035 | echo "$1" |
| 1036 | fi |
| 1037 | } |
| 1038 | |
| 1039 | setup_pure_ssh() { |
| 1040 | export PATH="$ROOTDIR/t/scutiger/bin:$PATH" |
| 1041 | if ! command -v git-lfs-transfer >/dev/null 2>&1 |
| 1042 | then |
| 1043 | if [ -z "$CI" ] || [ -n "$TEST_SKIP_LFS_TRANSFER" ] |
| 1044 | then |
| 1045 | echo "No git-lfs-transfer. Skipping..." |
| 1046 | exit 0 |
| 1047 | else |
| 1048 | echo "No git-lfs-transfer. Failing.." |
| 1049 | exit 1 |
| 1050 | fi |
| 1051 | elif [ "$GIT_DEFAULT_HASH" = sha256 ] |
| 1052 | then |
| 1053 | # Scutiger's git-lfs-transfer uses libgit2, which doesn't yet do SHA-256 |
| 1054 | # repos. |
| 1055 | echo "Using SHA-256 repositories. Skipping..." |
| 1056 | exit 0 |
| 1057 | fi |
| 1058 | } |
| 1059 | |
| 1060 | ssh_remote() { |
| 1061 | local reponame="$1" |
| 1062 | local destination=$(urlify "$(canonical_path "$REMOTEDIR/$reponame.git")") |
| 1063 | # Prepend a slash iff it lacks one. Windows compatibiity. |
| 1064 | [ -z "${destination##/*}" ] || destination="/$destination" |
| 1065 | echo "ssh://[email protected]$destination" |
| 1066 | } |
| 1067 | |
| 1068 | # Create a pkt-line message from s, which is an argument string to printf(1). |
| 1069 | pktize() { |
| 1070 | local s="$1" |
| 1071 | local len=$(printf "$s" | wc -c) |
| 1072 | printf "%04x$s" $((len + 4)) |
| 1073 | } |
| 1074 | |
| 1075 | pktize_text() { |
| 1076 | local s="$1" |
| 1077 | pktize "$s"'\n' |
| 1078 | } |
| 1079 | |
| 1080 | pktize_delim() { |
| 1081 | printf '0001' |
| 1082 | } |
| 1083 | |
| 1084 | pktize_flush() { |
| 1085 | printf '0000' |
| 1086 | } |
| 1087 | |
| 1088 | # At present, we expect a fixed default value of 8 concurrent transfers, |
| 1089 | # which is less than ideal for HTTP-based transfers when many objects |
| 1090 | # are transferred by a single "git lfs filter-process" command. |
| 1091 | # |
| 1092 | # However, the "lfs.concurrentTransfers" setting also controls the number |
| 1093 | # of processes we spawn for SSH-based or custom transfers, and we want |
| 1094 | # to avoid spawning large numbers of separate processes. |
| 1095 | # |
| 1096 | # In the future, we may allow this default value to be scaled by the |
| 1097 | # number of CPUs, as returned by our lfstest-getnumcpu utility. |
| 1098 | setup_expected_concurrent_transfers() { |
| 1099 | expectedConcurrentTransfers=8 |
| 1100 | } |