| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | begin_test "lock with good ref" |
| 6 | ( |
| 7 | set -e |
| 8 | |
| 9 | reponame="lock-main-branch-required" |
| 10 | setup_remote_repo_with_file "$reponame" "a.dat" |
| 11 | clone_repo "$reponame" "$reponame" |
| 12 | |
| 13 | git lfs lock "a.dat" --json 2>&1 | tee lock.json |
| 14 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 15 | echo >&2 "fatal: expected \'git lfs lock \'a.dat\'\' to succeed" |
| 16 | exit 1 |
| 17 | fi |
| 18 | |
| 19 | id=$(assert_lock lock.json a.dat) |
| 20 | assert_server_lock "$reponame" "$id" "refs/heads/main" |
| 21 | ) |
| 22 | end_test |
| 23 | |
| 24 | begin_test "lock with good tracked ref" |
| 25 | ( |
| 26 | set -e |
| 27 | |
| 28 | reponame="lock-tracked-branch-required" |
| 29 | setup_remote_repo "$reponame" |
| 30 | clone_repo "$reponame" "$reponame" |
| 31 | |
| 32 | git lfs track "*.dat" |
| 33 | echo "a" > a.dat |
| 34 | git add .gitattributes a.dat |
| 35 | git commit -m "add a.dat" |
| 36 | |
| 37 | git config push.default upstream |
| 38 | git config branch.main.merge refs/heads/tracked |
| 39 | git config branch.main.remote origin |
| 40 | git push origin main |
| 41 | |
| 42 | git lfs lock "a.dat" --json 2>&1 | tee lock.json |
| 43 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 44 | echo >&2 "fatal: expected \'git lfs lock \'a.dat\'\' to succeed" |
| 45 | exit 1 |
| 46 | fi |
| 47 | |
| 48 | id=$(assert_lock lock.json a.dat) |
| 49 | assert_server_lock "$reponame" "$id" "refs/heads/tracked" |
| 50 | ) |
| 51 | end_test |
| 52 | |
| 53 | begin_test "lock with good ref (--remote overrides push default)" |
| 54 | ( |
| 55 | set -e |
| 56 | |
| 57 | reponame="lock-remote" |
| 58 | setup_remote_repo_with_file "$reponame" "a.dat" |
| 59 | clone_repo "$reponame" "$reponame" |
| 60 | |
| 61 | # Check invalid "remote.pushDefault" configuration causes error. |
| 62 | git remote add bad-remote "invalid-url" |
| 63 | git config remote.pushDefault bad-remote |
| 64 | |
| 65 | git lfs lock "a.dat" 2>&1 | tee lock.log |
| 66 | if [ "0" -eq "${PIPESTATUS[0]}" ]; then |
| 67 | echo >&2 "fatal: expected 'git lfs lock' to fail ..." |
| 68 | exit 1 |
| 69 | fi |
| 70 | |
| 71 | # Check --remote option overrides "remote.pushDefault" configuration. |
| 72 | git lfs lock --remote origin --json "a.dat" | tee lock.log |
| 73 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 74 | echo >&2 "fatal: expected 'git lfs lock' to succeed ..." |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | id=$(assert_lock lock.log "a.dat") |
| 79 | assert_server_lock "$reponame" "$id" "refs/heads/main" |
| 80 | |
| 81 | # Remove "remote.pushDefault" configuration and unlock. |
| 82 | git config --unset remote.pushDefault |
| 83 | |
| 84 | git lfs unlock "a.dat" |
| 85 | refute_server_lock "$reponame" "$id" "refs/heads/main" |
| 86 | |
| 87 | # Check invalid "branch.<name>.pushRemote" configuration causes error. |
| 88 | git config branch.main.pushRemote bad-remote |
| 89 | |
| 90 | git lfs lock "a.dat" 2>&1 | tee lock.log |
| 91 | if [ "0" -eq "${PIPESTATUS[0]}" ]; then |
| 92 | echo >&2 "fatal: expected 'git lfs lock' to fail ..." |
| 93 | exit 1 |
| 94 | fi |
| 95 | |
| 96 | # Check --remote option overrides "branch.<name>.pushRemote" configuration. |
| 97 | git lfs lock --remote origin --json "a.dat" | tee lock.log |
| 98 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 99 | echo >&2 "fatal: expected 'git lfs lock' to succeed ..." |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | id=$(assert_lock lock.log "a.dat") |
| 104 | assert_server_lock "$reponame" "$id" "refs/heads/main" |
| 105 | ) |
| 106 | end_test |
| 107 | |
| 108 | begin_test "lock with bad ref" |
| 109 | ( |
| 110 | set -e |
| 111 | |
| 112 | reponame="lock-other-branch-required" |
| 113 | setup_remote_repo "$reponame" |
| 114 | clone_repo "$reponame" "$reponame" |
| 115 | |
| 116 | git lfs track "*.dat" |
| 117 | echo "a" > a.dat |
| 118 | git add .gitattributes a.dat |
| 119 | git commit -m "add a.dat" |
| 120 | git push origin main:other |
| 121 | |
| 122 | GIT_CURL_VERBOSE=1 git lfs lock "a.dat" 2>&1 | tee lock.json |
| 123 | if [ "0" -eq "${PIPESTATUS[0]}" ]; then |
| 124 | echo >&2 "fatal: expected \'git lfs lock \'a.dat\'\' to fail" |
| 125 | exit 1 |
| 126 | fi |
| 127 | |
| 128 | grep 'Locking a.dat failed: Expected ref "refs/heads/other", got "refs/heads/main"' lock.json |
| 129 | ) |
| 130 | end_test |
| 131 | |
| 132 | begin_test "lock multiple files" |
| 133 | ( |
| 134 | set -e |
| 135 | |
| 136 | reponame="lock-multiple-files" |
| 137 | setup_remote_repo "$reponame" |
| 138 | clone_repo "$reponame" "$reponame" |
| 139 | |
| 140 | git lfs track "*.dat" |
| 141 | echo "a" > a.dat |
| 142 | echo "b" > b.dat |
| 143 | git add .gitattributes a.dat b.dat |
| 144 | git commit -m "add dat files" |
| 145 | git push origin main:other |
| 146 | |
| 147 | GIT_TRACE=0 git lfs lock *.dat >log 2>errlog |
| 148 | [ $(grep -c "Locked [ab].dat" log) -eq 2 ] |
| 149 | grep -v CREDS errlog && exit 1 |
| 150 | grep "Usage:" errlog && exit 1 |
| 151 | true |
| 152 | ) |
| 153 | end_test |
| 154 | |
| 155 | begin_test "lock multiple files (JSON)" |
| 156 | ( |
| 157 | set -e |
| 158 | |
| 159 | reponame="lock-multiple-files-json" |
| 160 | setup_remote_repo "$reponame" |
| 161 | clone_repo "$reponame" "$reponame" |
| 162 | |
| 163 | git lfs track "*.dat" |
| 164 | echo "a" > a.dat |
| 165 | echo "b" > b.dat |
| 166 | git add .gitattributes a.dat b.dat |
| 167 | git commit -m "add dat files" |
| 168 | git push origin main:other |
| 169 | |
| 170 | git lfs lock --json *.dat | tee lock.json |
| 171 | grep -E '\[\{"id":"[^"]+","path":"a\.dat","owner":\{"name":"Git LFS Tests"\},"locked_at":"[^"]+"\},\{"id":"[^"]+","path":"b\.dat","owner":\{"name":"Git LFS Tests"\},"locked_at":"[^"]+"\}\]' lock.json |
| 172 | ) |
| 173 | end_test |
| 174 | |
| 175 | begin_test "lock absolute path" |
| 176 | ( |
| 177 | set -e |
| 178 | |
| 179 | reponame="lock-absolute-path" |
| 180 | setup_remote_repo "$reponame" |
| 181 | clone_repo "$reponame" "$reponame" |
| 182 | |
| 183 | git lfs track "*.dat" |
| 184 | echo "a" > a.dat |
| 185 | echo "b" > b.dat |
| 186 | git add .gitattributes a.dat b.dat |
| 187 | git commit -m "add dat files" |
| 188 | git push origin main:other |
| 189 | |
| 190 | git lfs lock --json "$(pwd)/a.dat" | tee lock.json |
| 191 | id=$(assert_lock lock.json a.dat) |
| 192 | assert_server_lock "$reponame" "$id" |
| 193 | ) |
| 194 | end_test |
| 195 | |
| 196 | begin_test "create lock with server using client cert" |
| 197 | ( |
| 198 | set -e |
| 199 | |
| 200 | git config --global "http.$LFS_CLIENT_CERT_URL/.sslCert" "$LFS_CLIENT_CERT_FILE" |
| 201 | git config --global "http.$LFS_CLIENT_CERT_URL/.sslKey" "$LFS_CLIENT_KEY_FILE" |
| 202 | |
| 203 | reponame="lock_create_client_cert" |
| 204 | setup_remote_repo_with_file "$reponame" "cc.dat" |
| 205 | |
| 206 | git config lfs.url "$CLIENTCERTGITSERVER/$reponame.git/info/lfs" |
| 207 | git lfs lock --json "cc.dat" | tee lock.json |
| 208 | id=$(assert_lock lock.json cc.dat) |
| 209 | assert_server_lock "$reponame" "$id" |
| 210 | ) |
| 211 | end_test |
| 212 | |
| 213 | begin_test "creating a lock (with output)" |
| 214 | ( |
| 215 | set -e |
| 216 | |
| 217 | reponame="lock_create_simple_output" |
| 218 | setup_remote_repo_with_file "$reponame" "a_output.dat" |
| 219 | |
| 220 | git lfs lock "a_output.dat" | tee lock.log |
| 221 | grep "Locked a_output.dat" lock.log |
| 222 | id=$(grep -oh "\((.*)\)" lock.log | tr -d \(\)) |
| 223 | assert_server_lock "$reponame" "$id" |
| 224 | ) |
| 225 | end_test |
| 226 | |
| 227 | begin_test "locking a file that doesn't exist" |
| 228 | ( |
| 229 | set -e |
| 230 | |
| 231 | reponame="lock_create_nonexistent" |
| 232 | setup_remote_repo_with_file "$reponame" "a_output.dat" |
| 233 | |
| 234 | git lfs lock "b_output.dat" | tee lock.log |
| 235 | grep "Locked b_output.dat" lock.log |
| 236 | id=$(grep -oh "\((.*)\)" lock.log | tr -d \(\)) |
| 237 | assert_server_lock "$reponame" "$id" |
| 238 | ) |
| 239 | end_test |
| 240 | |
| 241 | begin_test "locking a previously locked file" |
| 242 | ( |
| 243 | set -e |
| 244 | |
| 245 | reponame="lock_create_previously_created" |
| 246 | setup_remote_repo_with_file "$reponame" "b.dat" |
| 247 | |
| 248 | git lfs lock --json "b.dat" | tee lock.json |
| 249 | id=$(assert_lock lock.json b.dat) |
| 250 | assert_server_lock "$reponame" "$id" |
| 251 | |
| 252 | grep "lock already created" <(git lfs lock "b.dat" 2>&1) |
| 253 | ) |
| 254 | end_test |
| 255 | |
| 256 | begin_test "locking a directory" |
| 257 | ( |
| 258 | set -e |
| 259 | |
| 260 | reponame="locking_directories" |
| 261 | setup_remote_repo "remote_$reponame" |
| 262 | clone_repo "remote_$reponame" "clone_$reponame" |
| 263 | |
| 264 | git lfs track "*.dat" |
| 265 | mkdir dir |
| 266 | echo "a" > dir/a.dat |
| 267 | |
| 268 | git add dir/a.dat .gitattributes |
| 269 | |
| 270 | git commit -m "add dir/a.dat" | tee commit.log |
| 271 | grep "main (root-commit)" commit.log |
| 272 | grep "2 files changed" commit.log |
| 273 | grep "create mode 100644 dir/a.dat" commit.log |
| 274 | grep "create mode 100644 .gitattributes" commit.log |
| 275 | |
| 276 | git push origin main 2>&1 | tee push.log |
| 277 | grep "main -> main" push.log |
| 278 | |
| 279 | git lfs lock ./dir/ 2>&1 | tee lock.log |
| 280 | grep "cannot lock directory" lock.log |
| 281 | ) |
| 282 | end_test |
| 283 | |
| 284 | begin_test "locking a nested file" |
| 285 | ( |
| 286 | set -e |
| 287 | |
| 288 | reponame="locking-nested-file" |
| 289 | setup_remote_repo "$reponame" |
| 290 | clone_repo "$reponame" "$reponame" |
| 291 | |
| 292 | git lfs track "*.dat" --lockable |
| 293 | git add .gitattributes |
| 294 | git commit -m "initial commit" |
| 295 | |
| 296 | mkdir -p foo/bar/baz |
| 297 | |
| 298 | contents="contents" |
| 299 | contents_oid="$(calc_oid "$contents")" |
| 300 | |
| 301 | printf "%s" "$contents" > foo/bar/baz/a.dat |
| 302 | git add foo/bar/baz/a.dat |
| 303 | git commit -m "add a.dat" |
| 304 | |
| 305 | git push origin main |
| 306 | |
| 307 | assert_server_object "$reponame" "$contents_oid" |
| 308 | |
| 309 | git lfs lock foo/bar/baz/a.dat 2>&1 | tee lock.log |
| 310 | grep "Locked foo/bar/baz/a.dat" lock.log |
| 311 | |
| 312 | git lfs locks 2>&1 | tee locks.log |
| 313 | grep "foo/bar/baz/a.dat" locks.log |
| 314 | ) |
| 315 | end_test |
| 316 | |
| 317 | begin_test "creating a lock (within subdirectory)" |
| 318 | ( |
| 319 | set -e |
| 320 | |
| 321 | reponame="lock_create_within_subdirectory" |
| 322 | setup_remote_repo_with_file "$reponame" "sub/a.dat" |
| 323 | |
| 324 | cd sub |
| 325 | |
| 326 | git lfs lock --json "a.dat" | tee lock.json |
| 327 | if [ "0" -ne "${PIPESTATUS[0]}" ]; then |
| 328 | echo >&2 "fatal: expected \'git lfs lock \'a.dat\'\' to succeed" |
| 329 | exit 1 |
| 330 | fi |
| 331 | |
| 332 | id=$(assert_lock lock.json sub/a.dat) |
| 333 | assert_server_lock "$reponame" "$id" |
| 334 | ) |
| 335 | end_test |
| 336 | |
| 337 | begin_test "creating a lock (symlinked working directory)" |
| 338 | ( |
| 339 | set -eo pipefail |
| 340 | |
| 341 | if [[ $(uname) == *"MINGW"* ]]; then |
| 342 | echo >&2 "info: skipped on Windows ..." |
| 343 | exit 0 |
| 344 | fi |
| 345 | |
| 346 | reponame="lock-in-symlinked-working-directory" |
| 347 | setup_remote_repo "$reponame" |
| 348 | clone_repo "$reponame" "$reponame" |
| 349 | |
| 350 | git lfs track -l "*.dat" |
| 351 | mkdir -p folder1 folder2 |
| 352 | printf "hello" > folder2/a.dat |
| 353 | add_symlink "../folder2" "folder1/folder2" |
| 354 | |
| 355 | git add --all . |
| 356 | git commit -m "initial commit" |
| 357 | git push origin main |
| 358 | |
| 359 | pushd "$TRASHDIR" > /dev/null |
| 360 | ln -s "$reponame" "$reponame-symlink" |
| 361 | cd "$reponame-symlink" |
| 362 | |
| 363 | git lfs lock --json folder1/folder2/a.dat 2>&1 | tee lock.json |
| 364 | |
| 365 | id="$(assert_lock lock.json folder1/folder2/a.dat)" |
| 366 | assert_server_lock "$reponame" "$id" main |
| 367 | popd > /dev/null |
| 368 | ) |
| 369 | end_test |
| 370 | |
| 371 | begin_test "lock with .gitignore" |
| 372 | ( |
| 373 | set -e |
| 374 | |
| 375 | reponame="lock-with-gitignore" |
| 376 | setup_remote_repo_with_file "$reponame" "a.txt" |
| 377 | clone_repo "$reponame" "$reponame" |
| 378 | |
| 379 | echo "*.txt filter=lfs diff=lfs merge=lfs -text lockable" > .gitattributes |
| 380 | |
| 381 | git add .gitattributes |
| 382 | git commit -m ".gitattributes: mark 'a.txt' as lockable" |
| 383 | |
| 384 | rm -f a.txt && git checkout a.txt |
| 385 | refute_file_writeable a.txt |
| 386 | |
| 387 | echo "*.txt" > .gitignore |
| 388 | git add .gitignore |
| 389 | git commit -m ".gitignore: ignore 'a.txt'" |
| 390 | rm -f a.txt && git checkout a.txt |
| 391 | refute_file_writeable a.txt |
| 392 | ) |
| 393 | end_test |
| 394 | |
| 395 | |
| 396 | begin_test "lock with .gitignore and lfs.lockignoredfiles" |
| 397 | ( |
| 398 | set -e |
| 399 | |
| 400 | reponame="lock-with-gitignore-and-ignoredfiles" |
| 401 | setup_remote_repo_with_file "$reponame" "a.txt" |
| 402 | clone_repo "$reponame" "$reponame" |
| 403 | |
| 404 | git config lfs.lockignoredfiles true |
| 405 | echo "*.txt filter=lfs diff=lfs merge=lfs -text lockable" > .gitattributes |
| 406 | |
| 407 | git add .gitattributes |
| 408 | git commit -m ".gitattributes: mark 'a.txt' as lockable" |
| 409 | |
| 410 | rm -f a.txt && git checkout a.txt |
| 411 | refute_file_writeable a.txt |
| 412 | |
| 413 | echo "*.txt" > .gitignore |
| 414 | git add .gitignore |
| 415 | git commit -m ".gitignore: ignore 'a.txt'" |
| 416 | rm -f a.txt && git checkout a.txt |
| 417 | refute_file_writeable a.txt |
| 418 | ) |
| 419 | end_test |
| 420 | |
| 421 | begin_test "lock with git-lfs-transfer" |
| 422 | ( |
| 423 | set -e |
| 424 | |
| 425 | setup_pure_ssh |
| 426 | |
| 427 | reponame="lock-with-git-lfs-transfer" |
| 428 | setup_remote_repo_with_file "$reponame" "f.dat" |
| 429 | clone_repo "$reponame" "$reponame" |
| 430 | |
| 431 | sshurl=$(ssh_remote "$reponame") |
| 432 | git config lfs.url "$sshurl" |
| 433 | |
| 434 | GIT_TRACE_PACKET=1 git lfs lock --json "f.dat" | tee lock.log |
| 435 | |
| 436 | id=$(assert_lock lock.log f.dat) |
| 437 | assert_server_lock_ssh "$reponame" "$id" "refs/heads/main" |
| 438 | ) |
| 439 | end_test |