| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | begin_test "install again" |
| 6 | ( |
| 7 | set -eo pipefail |
| 8 | |
| 9 | smudge="$(git config filter.lfs.smudge)" |
| 10 | clean="$(git config filter.lfs.clean)" |
| 11 | filter="$(git config filter.lfs.process)" |
| 12 | |
| 13 | [ "$smudge" = "git-lfs smudge -- %f" ] |
| 14 | [ "$clean" = "git-lfs clean -- %f" ] |
| 15 | [ "$filter" = "git-lfs filter-process" ] |
| 16 | |
| 17 | GIT_TRACE=1 git lfs install --skip-repo 2>&1 | tee install.log |
| 18 | |
| 19 | if grep -q "--replace-all" install.log; then |
| 20 | echo >&2 "fatal: unexpected git config --replace-all via 'git lfs install'" |
| 21 | exit 1 |
| 22 | fi |
| 23 | |
| 24 | [ "$smudge" = "$(git config filter.lfs.smudge)" ] |
| 25 | [ "$clean" = "$(git config filter.lfs.clean)" ] |
| 26 | [ "$filter" = "$(git config filter.lfs.process)" ] |
| 27 | ) |
| 28 | end_test |
| 29 | |
| 30 | begin_test "install with old (non-upgradeable) settings" |
| 31 | ( |
| 32 | set -e |
| 33 | |
| 34 | git config --global filter.lfs.smudge "git-lfs smudge --something %f" |
| 35 | git config --global filter.lfs.clean "git-lfs clean --something %f" |
| 36 | |
| 37 | git lfs install | tee install.log |
| 38 | [ "${PIPESTATUS[0]}" = 2 ] |
| 39 | |
| 40 | grep -E "(clean|smudge)\" attribute should be" install.log |
| 41 | [ 0 -eq "$(grep -c "(MISSING)" install.log)" ] |
| 42 | |
| 43 | [ "git-lfs smudge --something %f" = "$(git config --global filter.lfs.smudge)" ] |
| 44 | [ "git-lfs clean --something %f" = "$(git config --global filter.lfs.clean)" ] |
| 45 | |
| 46 | git lfs install --force |
| 47 | |
| 48 | [ "git-lfs smudge -- %f" = "$(git config --global filter.lfs.smudge)" ] |
| 49 | [ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ] |
| 50 | ) |
| 51 | end_test |
| 52 | |
| 53 | begin_test "install with upgradeable settings" |
| 54 | ( |
| 55 | set -e |
| 56 | |
| 57 | git config --global filter.lfs.smudge "git-lfs smudge %f" |
| 58 | git config --global filter.lfs.clean "git-lfs clean %f" |
| 59 | |
| 60 | # should not need force, should upgrade this old style |
| 61 | git lfs install |
| 62 | [ "git-lfs smudge -- %f" = "$(git config --global filter.lfs.smudge)" ] |
| 63 | [ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ] |
| 64 | [ "git-lfs filter-process" = "$(git config --global filter.lfs.process)" ] |
| 65 | ) |
| 66 | end_test |
| 67 | |
| 68 | begin_test "install updates repo hooks" |
| 69 | ( |
| 70 | set -e |
| 71 | |
| 72 | mkdir install-repo-hooks |
| 73 | cd install-repo-hooks |
| 74 | git init |
| 75 | |
| 76 | pre_push_hook="#!/bin/sh |
| 77 | command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; } |
| 78 | git lfs pre-push \"\$@\"" |
| 79 | |
| 80 | post_checkout_hook="#!/bin/sh |
| 81 | command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-checkout' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; } |
| 82 | git lfs post-checkout \"\$@\"" |
| 83 | |
| 84 | post_commit_hook="#!/bin/sh |
| 85 | command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; } |
| 86 | git lfs post-commit \"\$@\"" |
| 87 | |
| 88 | post_merge_hook="#!/bin/sh |
| 89 | command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-merge' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; } |
| 90 | git lfs post-merge \"\$@\"" |
| 91 | |
| 92 | [ "Updated Git hooks. |
| 93 | Git LFS initialized." = "$(git lfs install)" ] |
| 94 | [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ] |
| 95 | [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ] |
| 96 | [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ] |
| 97 | [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ] |
| 98 | |
| 99 | # replace old hook |
| 100 | # more-comprehensive hook update tests are in test-update.sh |
| 101 | echo "#!/bin/sh |
| 102 | git lfs push --stdin \$*" > .git/hooks/pre-push |
| 103 | [ "Updated Git hooks. |
| 104 | Git LFS initialized." = "$(git lfs install)" ] |
| 105 | [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ] |
| 106 | |
| 107 | # don't replace unexpected hook |
| 108 | expected="Hook already exists: pre-push |
| 109 | |
| 110 | test |
| 111 | |
| 112 | To resolve this, either: |
| 113 | 1: run \`git lfs update --manual\` for instructions on how to merge hooks. |
| 114 | 2: run \`git lfs update --force\` to overwrite your hook." |
| 115 | |
| 116 | echo "test" > .git/hooks/pre-push |
| 117 | echo "test" > .git/hooks/post-checkout |
| 118 | echo "test" > .git/hooks/post-commit |
| 119 | echo "test" > .git/hooks/post-merge |
| 120 | [ "test" = "$(cat .git/hooks/pre-push)" ] |
| 121 | [ "$expected" = "$(git lfs install 2>&1)" ] |
| 122 | [ "test" = "$(cat .git/hooks/pre-push)" ] |
| 123 | [ "test" = "$(cat .git/hooks/post-checkout)" ] |
| 124 | [ "test" = "$(cat .git/hooks/post-commit)" ] |
| 125 | [ "test" = "$(cat .git/hooks/post-merge)" ] |
| 126 | |
| 127 | # Make sure returns non-zero |
| 128 | set +e |
| 129 | git lfs install |
| 130 | if [ $? -eq 0 ] |
| 131 | then |
| 132 | exit 1 |
| 133 | fi |
| 134 | set -e |
| 135 | |
| 136 | # force replace unexpected hook |
| 137 | [ "Updated Git hooks. |
| 138 | Git LFS initialized." = "$(git lfs install --force)" ] |
| 139 | [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ] |
| 140 | [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ] |
| 141 | [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ] |
| 142 | [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ] |
| 143 | |
| 144 | has_test_dir || exit 0 |
| 145 | |
| 146 | echo "test with bare repository" |
| 147 | cd .. |
| 148 | git clone --mirror install-repo-hooks bare-install-repo-hooks |
| 149 | cd bare-install-repo-hooks |
| 150 | git lfs env |
| 151 | git lfs install |
| 152 | ls -al hooks |
| 153 | [ "$pre_push_hook" = "$(cat hooks/pre-push)" ] |
| 154 | ) |
| 155 | end_test |
| 156 | |
| 157 | begin_test "install outside repository directory" |
| 158 | ( |
| 159 | set -e |
| 160 | if [ -d "hooks" ]; then |
| 161 | ls -al |
| 162 | echo "hooks dir exists" |
| 163 | exit 1 |
| 164 | fi |
| 165 | |
| 166 | git lfs install > check.log 2>&1 |
| 167 | |
| 168 | if [ -d "hooks" ]; then |
| 169 | ls -al |
| 170 | echo "hooks dir exists" |
| 171 | exit 1 |
| 172 | fi |
| 173 | |
| 174 | cat check.log |
| 175 | |
| 176 | # doesn't print this because being in a git repo is not necessary for install |
| 177 | [ 0 -eq "$(grep -c "Not in a Git repository" check.log)" ] |
| 178 | [ 0 -eq "$(grep -c "Error" check.log)" ] |
| 179 | ) |
| 180 | end_test |
| 181 | |
| 182 | begin_test "install --skip-smudge" |
| 183 | ( |
| 184 | set -e |
| 185 | |
| 186 | mkdir install-skip-smudge-test |
| 187 | cd install-skip-smudge-test |
| 188 | |
| 189 | git lfs install |
| 190 | [ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ] |
| 191 | [ "git-lfs smudge -- %f" = "$(git config --global filter.lfs.smudge)" ] |
| 192 | [ "git-lfs filter-process" = "$(git config --global filter.lfs.process)" ] |
| 193 | |
| 194 | git lfs install --skip-smudge |
| 195 | [ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ] |
| 196 | [ "git-lfs smudge --skip -- %f" = "$(git config --global filter.lfs.smudge)" ] |
| 197 | [ "git-lfs filter-process --skip" = "$(git config --global filter.lfs.process)" ] |
| 198 | |
| 199 | git lfs install |
| 200 | [ "git-lfs clean -- %f" = "$(git config --global filter.lfs.clean)" ] |
| 201 | [ "git-lfs smudge -- %f" = "$(git config --global filter.lfs.smudge)" ] |
| 202 | [ "git-lfs filter-process" = "$(git config --global filter.lfs.process)" ] |
| 203 | |
| 204 | [ ! -e "lfs" ] |
| 205 | ) |
| 206 | end_test |
| 207 | |
| 208 | begin_test "install --local" |
| 209 | ( |
| 210 | set -e |
| 211 | |
| 212 | # old values that should be ignored by `install --local` |
| 213 | git config --global filter.lfs.smudge "global smudge" |
| 214 | git config --global filter.lfs.clean "global clean" |
| 215 | git config --global filter.lfs.process "global filter" |
| 216 | |
| 217 | mkdir install-local-repo |
| 218 | cd install-local-repo |
| 219 | git init |
| 220 | git lfs install --local |
| 221 | |
| 222 | # local configs are correct |
| 223 | [ "git-lfs smudge -- %f" = "$(git config filter.lfs.smudge)" ] |
| 224 | [ "git-lfs smudge -- %f" = "$(git config --local filter.lfs.smudge)" ] |
| 225 | [ "git-lfs clean -- %f" = "$(git config filter.lfs.clean)" ] |
| 226 | [ "git-lfs clean -- %f" = "$(git config --local filter.lfs.clean)" ] |
| 227 | [ "git-lfs filter-process" = "$(git config filter.lfs.process)" ] |
| 228 | [ "git-lfs filter-process" = "$(git config --local filter.lfs.process)" ] |
| 229 | |
| 230 | # global configs |
| 231 | [ "global smudge" = "$(git config --global filter.lfs.smudge)" ] |
| 232 | [ "global clean" = "$(git config --global filter.lfs.clean)" ] |
| 233 | [ "global filter" = "$(git config --global filter.lfs.process)" ] |
| 234 | ) |
| 235 | end_test |
| 236 | |
| 237 | begin_test "install --file" |
| 238 | ( |
| 239 | set -e |
| 240 | |
| 241 | # old values that should be ignored by `install --local` |
| 242 | git config --global filter.lfs.smudge "global smudge" |
| 243 | git config --global filter.lfs.clean "global clean" |
| 244 | git config --global filter.lfs.process "global filter" |
| 245 | |
| 246 | mkdir install-file-repo |
| 247 | cd install-file-repo |
| 248 | git init |
| 249 | git lfs install --file=test-file |
| 250 | |
| 251 | # local configs are correct |
| 252 | [ "git-lfs smudge -- %f" = "$(git config --file test-file filter.lfs.smudge)" ] |
| 253 | [ "git-lfs clean -- %f" = "$(git config --file test-file filter.lfs.clean)" ] |
| 254 | [ "git-lfs filter-process" = "$(git config --file test-file filter.lfs.process)" ] |
| 255 | |
| 256 | # global configs |
| 257 | [ "global smudge" = "$(git config --global filter.lfs.smudge)" ] |
| 258 | [ "global clean" = "$(git config --global filter.lfs.clean)" ] |
| 259 | [ "global filter" = "$(git config --global filter.lfs.process)" ] |
| 260 | ) |
| 261 | end_test |
| 262 | |
| 263 | begin_test "install --local with failed permissions" |
| 264 | ( |
| 265 | set -e |
| 266 | |
| 267 | # Windows lacks POSIX permissions. |
| 268 | [ "$IS_WINDOWS" -eq 1 ] && exit 0 |
| 269 | |
| 270 | # Root is exempt from permissions. |
| 271 | [ "$(id -u)" -eq 0 ] && exit 0 |
| 272 | |
| 273 | mkdir install-local-repo-perms |
| 274 | cd install-local-repo-perms |
| 275 | git init |
| 276 | |
| 277 | # Make it impossible to write a new .git/config file so we can't write config |
| 278 | # options. |
| 279 | chmod 500 .git |
| 280 | |
| 281 | res=0 |
| 282 | git lfs install --local >out.log || res=$? |
| 283 | |
| 284 | # Cleanup fails without this. |
| 285 | chmod 700 .git |
| 286 | |
| 287 | cat out.log |
| 288 | grep -E "error running.*git.*config" out.log |
| 289 | [ "$res" -eq 2 ] |
| 290 | ) |
| 291 | end_test |
| 292 | |
| 293 | begin_test "install --local outside repository" |
| 294 | ( |
| 295 | set -e |
| 296 | |
| 297 | # If run inside the git-lfs source dir this will update its .git/config & cause issues |
| 298 | if [ "$GIT_LFS_TEST_DIR" == "" ]; then |
| 299 | echo "Skipping install --local because GIT_LFS_TEST_DIR is not set" |
| 300 | exit 0 |
| 301 | fi |
| 302 | |
| 303 | has_test_dir || exit 0 |
| 304 | |
| 305 | set +e |
| 306 | git lfs install --local >out.log |
| 307 | res=$? |
| 308 | set -e |
| 309 | |
| 310 | [ "Not in a Git repository." = "$(cat out.log)" ] |
| 311 | [ "0" != "$res" ] |
| 312 | ) |
| 313 | end_test |
| 314 | |
| 315 | begin_test "install --local with conflicting scope" |
| 316 | ( |
| 317 | set -e |
| 318 | |
| 319 | reponame="$(basename "$0" ".sh")-scope-conflict" |
| 320 | mkdir "$reponame" |
| 321 | cd "$reponame" |
| 322 | git init |
| 323 | |
| 324 | set +e |
| 325 | git lfs install --local --system 2>err.log |
| 326 | res=$? |
| 327 | set -e |
| 328 | |
| 329 | [ "Only one of the --local, --system, --worktree, and --file options can be specified." = "$(cat err.log)" ] |
| 330 | [ "0" != "$res" ] |
| 331 | ) |
| 332 | end_test |
| 333 | |
| 334 | begin_test "install in directory without access to .git/lfs" |
| 335 | ( |
| 336 | set -e |
| 337 | mkdir not-a-repo |
| 338 | cd not-a-repo |
| 339 | mkdir .git |
| 340 | touch .git/lfs |
| 341 | touch lfs |
| 342 | |
| 343 | git config --global filter.lfs.clean whatevs |
| 344 | [ "whatevs" = "$(git config filter.lfs.clean)" ] |
| 345 | |
| 346 | git lfs install --force |
| 347 | |
| 348 | [ "git-lfs clean -- %f" = "$(git config filter.lfs.clean)" ] |
| 349 | ) |
| 350 | end_test |
| 351 | |
| 352 | begin_test "install in repo without changing hooks" |
| 353 | ( |
| 354 | set -e |
| 355 | git init non-lfs-repo |
| 356 | cd non-lfs-repo |
| 357 | |
| 358 | git lfs install --skip-repo |
| 359 | |
| 360 | # should not install hooks |
| 361 | [ ! -f .git/hooks/pre-push ] |
| 362 | [ ! -f .git/hooks/post-checkout ] |
| 363 | [ ! -f .git/hooks/post-merge ] |
| 364 | [ ! -f .git/hooks/post-commit ] |
| 365 | |
| 366 | # filters should still be installed |
| 367 | [ "git-lfs clean -- %f" = "$(git config filter.lfs.clean)" ] |
| 368 | [ "git-lfs smudge -- %f" = "$(git config filter.lfs.smudge)" ] |
| 369 | [ "git-lfs filter-process" = "$(git config filter.lfs.process)" ] |
| 370 | ) |
| 371 | end_test |
| 372 | |
| 373 | begin_test "can install when multiple global values registered" |
| 374 | ( |
| 375 | set -e |
| 376 | |
| 377 | git config --global filter.lfs.smudge "git-lfs smudge --something %f" |
| 378 | git config --global --add filter.lfs.smudge "git-lfs smudge --something-else %f" |
| 379 | |
| 380 | git lfs install --force |
| 381 | ) |
| 382 | end_test |