| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | begin_test "smudge" |
| 6 | ( |
| 7 | set -e |
| 8 | |
| 9 | reponame="$(basename "$0" ".sh")" |
| 10 | setup_remote_repo "$reponame" |
| 11 | clone_repo "$reponame" repo |
| 12 | |
| 13 | git lfs track "*.dat" |
| 14 | echo "smudge a" > a.dat |
| 15 | git add .gitattributes a.dat |
| 16 | git commit -m "add a.dat" |
| 17 | |
| 18 | # smudge works even though it hasn't been pushed, by reading from .git/lfs/objects |
| 19 | output="$(pointer fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254 9 | git lfs smudge)" |
| 20 | [ "smudge a" = "$output" ] |
| 21 | |
| 22 | git push origin main |
| 23 | |
| 24 | # download it from the git lfs server |
| 25 | rm -rf .git/lfs/objects |
| 26 | output="$(pointer fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254 9 | git lfs smudge)" |
| 27 | [ "smudge a" = "$output" ] |
| 28 | ) |
| 29 | end_test |
| 30 | |
| 31 | begin_test "smudge with temp file" |
| 32 | ( |
| 33 | set -e |
| 34 | |
| 35 | cd repo |
| 36 | |
| 37 | rm -rf .git/lfs/objects |
| 38 | mkdir -p .git/lfs/tmp/objects |
| 39 | touch .git/lfs/tmp/objects/fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254-1 |
| 40 | pointer fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254 9 | GIT_TRACE=5 git lfs smudge | tee smudge.log |
| 41 | [ "smudge a" = "$(cat smudge.log)" ] || { |
| 42 | rm -rf .git/lfs/tmp |
| 43 | git lfs logs last |
| 44 | exit 1 |
| 45 | } |
| 46 | ) |
| 47 | end_test |
| 48 | |
| 49 | begin_test "smudge with invalid pointer" |
| 50 | ( |
| 51 | set -e |
| 52 | |
| 53 | cd repo |
| 54 | [ "wat" = "$(echo "wat" | git lfs smudge)" ] |
| 55 | [ "not a git-lfs file" = "$(echo "not a git-lfs file" | git lfs smudge)" ] |
| 56 | [ "version " = "$(echo "version " | git lfs smudge)" ] |
| 57 | |
| 58 | # Test with input data larger than the internal memory buffer used |
| 59 | # to spool data between streams, so a temporary file is used as well. |
| 60 | # See https://github.com/git-lfs/git-lfs/pull/1922, |
| 61 | # https://github.com/git-lfs/git-lfs/pull/1932, |
| 62 | # and https://github.com/git-lfs/git-lfs/pull/5617. |
| 63 | max_spool_mem_buffer_size="$(lfstest-getlimit --max-spool-mem-buffer-size)" |
| 64 | spool="$(lfstest-genrandom --base64 $((max_spool_mem_buffer_size * 2)))" |
| 65 | [ "$spool" = "$(printf "%s" "$spool" | git lfs smudge)" ] |
| 66 | ) |
| 67 | end_test |
| 68 | |
| 69 | begin_test "smudge with pointer extension" |
| 70 | ( |
| 71 | set -e |
| 72 | |
| 73 | reponame="smudge-pointer-extension" |
| 74 | git init "$reponame" |
| 75 | cd "$reponame" |
| 76 | |
| 77 | setup_case_inverter_extension |
| 78 | |
| 79 | git lfs track "*.dat" |
| 80 | |
| 81 | contents="$(printf "%s\n%s" "abc" "def")" |
| 82 | contents_oid="$(calc_oid "$contents")" |
| 83 | inverted_contents_oid="$(calc_oid "$(invert_case "$contents")")" |
| 84 | mkdir dir1 |
| 85 | printf "%s" "$contents" >dir1/abc.dat |
| 86 | git add .gitattributes dir1 |
| 87 | |
| 88 | pointer="$(case_inverter_extension_pointer "$contents_oid" "$inverted_contents_oid" 7)" |
| 89 | |
| 90 | assert_local_object "$inverted_contents_oid" 7 |
| 91 | |
| 92 | # smudge works even though it hasn't been pushed, by reading from .git/lfs/objects |
| 93 | [ "$contents" = "$(echo "$pointer" | git lfs smudge -- "dir1/abc.dat")" ] |
| 94 | grep "smudge: dir1/abc.dat" "$LFSTEST_EXT_LOG" |
| 95 | ) |
| 96 | end_test |
| 97 | |
| 98 | begin_test "smudge include/exclude" |
| 99 | ( |
| 100 | set -e |
| 101 | |
| 102 | reponame="$(basename "$0" ".sh")-includeexclude" |
| 103 | setup_remote_repo "$reponame" |
| 104 | clone_repo "$reponame" includeexclude |
| 105 | |
| 106 | git lfs track "*.dat" |
| 107 | echo "smudge a" > a.dat |
| 108 | git add .gitattributes a.dat |
| 109 | git commit -m "add a.dat" |
| 110 | |
| 111 | pointer="$(pointer fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254 9)" |
| 112 | |
| 113 | # smudge works even though it hasn't been pushed, by reading from .git/lfs/objects |
| 114 | [ "smudge a" = "$(echo "$pointer" | git lfs smudge)" ] |
| 115 | |
| 116 | git push origin main |
| 117 | |
| 118 | # this WOULD download except we're going to prevent it with include/exclude |
| 119 | rm -rf .git/lfs/objects |
| 120 | git config "lfs.fetchexclude" "a*" |
| 121 | |
| 122 | [ "$pointer" = "$(echo "$pointer" | git lfs smudge a.dat)" ] |
| 123 | |
| 124 | mkdir -p foo/bar |
| 125 | echo "smudge a" > foo/a.dat |
| 126 | echo "smudge a" > foo/bar/a.dat |
| 127 | git add foo |
| 128 | git commit -m 'add foo' |
| 129 | |
| 130 | git push origin main |
| 131 | |
| 132 | # The Git LFS objects for a.dat and foo/bar/a.dat would both download except |
| 133 | # we're going to prevent them from doing so with include/exclude. |
| 134 | rm -rf .git/lfs/objects |
| 135 | # We also need to prevent MSYS from rewriting /foo into a Windows path. |
| 136 | MSYS_NO_PATHCONV=1 git config "lfs.fetchinclude" "/foo" |
| 137 | MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo/bar" |
| 138 | |
| 139 | [ "$pointer" = "$(echo "$pointer" | git lfs smudge a.dat)" ] |
| 140 | [ "smudge a" = "$(echo "$pointer" | git lfs smudge foo/a.dat)" ] |
| 141 | [ "$pointer" = "$(echo "$pointer" | git lfs smudge foo/bar/a.dat)" ] |
| 142 | ) |
| 143 | end_test |
| 144 | |
| 145 | begin_test "smudge with skip" |
| 146 | ( |
| 147 | set -e |
| 148 | |
| 149 | reponame="$(basename "$0" ".sh")-skip" |
| 150 | setup_remote_repo "$reponame" |
| 151 | clone_repo "$reponame" "skip" |
| 152 | |
| 153 | git lfs track "*.dat" |
| 154 | echo "smudge a" > a.dat |
| 155 | git add .gitattributes a.dat |
| 156 | git commit -m "add a.dat" |
| 157 | |
| 158 | pointer="$(pointer fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254 9)" |
| 159 | [ "smudge a" = "$(echo "$pointer" | git lfs smudge)" ] |
| 160 | |
| 161 | git push origin main |
| 162 | |
| 163 | # Must clear the cache because smudge will use |
| 164 | # cached objects even with --skip/GIT_LFS_SKIP_SMUDGE |
| 165 | # (--skip applies to whether or not it downloads). |
| 166 | rm -rf .git/lfs/objects |
| 167 | |
| 168 | [ "$pointer" = "$(echo "$pointer" | GIT_LFS_SKIP_SMUDGE=1 git lfs smudge)" ] |
| 169 | |
| 170 | echo "test clone with env" |
| 171 | export GIT_LFS_SKIP_SMUDGE=1 |
| 172 | env | grep LFS_SKIP |
| 173 | clone_repo "$reponame" "skip-clone-env" |
| 174 | [ "$pointer" = "$(cat a.dat)" ] |
| 175 | |
| 176 | git lfs pull |
| 177 | [ "smudge a" = "$(cat a.dat)" ] |
| 178 | |
| 179 | echo "test clone without env" |
| 180 | unset GIT_LFS_SKIP_SMUDGE |
| 181 | clone_repo "$reponame" "no-skip" |
| 182 | [ "smudge a" = "$(cat a.dat)" ] |
| 183 | |
| 184 | echo "test clone with init --skip-smudge" |
| 185 | git lfs install --skip-smudge |
| 186 | clone_repo "$reponame" "skip-clone-init" |
| 187 | [ "$pointer" = "$(cat a.dat)" ] |
| 188 | |
| 189 | git lfs install --force |
| 190 | ) |
| 191 | end_test |
| 192 | |
| 193 | begin_test "smudge clone with include/exclude" |
| 194 | ( |
| 195 | set -e |
| 196 | |
| 197 | reponame="smudge_include_exclude" |
| 198 | setup_remote_repo "$reponame" |
| 199 | |
| 200 | clone_repo "$reponame" "repo_$reponame" |
| 201 | |
| 202 | git lfs track "*.dat" 2>&1 | tee track.log |
| 203 | grep "Tracking \"\*.dat\"" track.log |
| 204 | |
| 205 | contents="a" |
| 206 | contents_oid=$(calc_oid "$contents") |
| 207 | |
| 208 | printf "%s" "$contents" > a.dat |
| 209 | git add a.dat |
| 210 | git add .gitattributes |
| 211 | git commit -m "add a.dat" 2>&1 | tee commit.log |
| 212 | grep "main (root-commit)" commit.log |
| 213 | grep "2 files changed" commit.log |
| 214 | grep "create mode 100644 a.dat" commit.log |
| 215 | grep "create mode 100644 .gitattributes" commit.log |
| 216 | |
| 217 | [ "a" = "$(cat a.dat)" ] |
| 218 | |
| 219 | assert_local_object "$contents_oid" 1 |
| 220 | |
| 221 | git push origin main 2>&1 | tee push.log |
| 222 | grep "Uploading LFS objects: 100% (1/1), 1 B" push.log |
| 223 | grep "main -> main" push.log |
| 224 | |
| 225 | assert_server_object "$reponame" "$contents_oid" |
| 226 | |
| 227 | clone="$TRASHDIR/clone_$reponame" |
| 228 | git -c lfs.fetchexclude="a*" clone "$GITSERVER/$reponame" "$clone" |
| 229 | pushd "$clone" |
| 230 | # Should have succeeded but not downloaded |
| 231 | refute_local_object "$contents_oid" |
| 232 | popd |
| 233 | rm -rf "$clone" |
| 234 | |
| 235 | contents2="b" |
| 236 | contents2_oid=$(calc_oid "$contents2") |
| 237 | contents3="c" |
| 238 | contents3_oid=$(calc_oid "$contents3") |
| 239 | |
| 240 | mkdir -p foo/bar |
| 241 | printf "%s" "$contents2" > foo/b.dat |
| 242 | printf "%s" "$contents3" > foo/bar/c.dat |
| 243 | git add foo |
| 244 | git commit -m 'add foo' |
| 245 | |
| 246 | assert_local_object "$contents2_oid" 1 |
| 247 | assert_local_object "$contents3_oid" 1 |
| 248 | |
| 249 | git push origin main |
| 250 | |
| 251 | assert_server_object "$reponame" "$contents2_oid" |
| 252 | assert_server_object "$reponame" "$contents3_oid" |
| 253 | |
| 254 | # The Git LFS objects for a.dat and foo/bar/a.dat would both download except |
| 255 | # we're going to prevent them from doing so with include/exclude. |
| 256 | # We also need to prevent MSYS from rewriting /foo into a Windows path. |
| 257 | MSYS_NO_PATHCONV=1 git config --global "lfs.fetchinclude" "/foo" |
| 258 | MSYS_NO_PATHCONV=1 git config --global "lfs.fetchexclude" "/foo/bar" |
| 259 | git clone "$GITSERVER/$reponame" "$clone" |
| 260 | pushd "$clone" |
| 261 | refute_local_object "$contents_oid" |
| 262 | assert_local_object "$contents2_oid" 1 |
| 263 | refute_local_object "$contents3_oid" |
| 264 | popd |
| 265 | ) |
| 266 | end_test |
| 267 | |
| 268 | begin_test "smudge skip download failure" |
| 269 | ( |
| 270 | set -e |
| 271 | |
| 272 | reponame="$(basename "$0" ".sh")-skipdownloadfail" |
| 273 | setup_remote_repo "$reponame" |
| 274 | clone_repo "$reponame" skipdownloadfail |
| 275 | |
| 276 | git lfs track "*.dat" |
| 277 | echo "smudge a" > a.dat |
| 278 | git add .gitattributes a.dat |
| 279 | git commit -m "add a.dat" |
| 280 | |
| 281 | pointer="$(pointer fcf5015df7a9089a7aa7fe74139d4b8f7d62e52d5a34f9a87aeffc8e8c668254 9)" |
| 282 | |
| 283 | # smudge works even though it hasn't been pushed, by reading from .git/lfs/objects |
| 284 | [ "smudge a" = "$(echo "$pointer" | git lfs smudge)" ] |
| 285 | |
| 286 | git push origin main |
| 287 | |
| 288 | # make it try to download but we're going to make it fail |
| 289 | rm -rf .git/lfs/objects |
| 290 | git remote set-url origin httpnope://nope.com/nope |
| 291 | |
| 292 | # this should fail |
| 293 | set +e |
| 294 | echo "$pointer" | git lfs smudge a.dat; test ${PIPESTATUS[1]} -ne 0 |
| 295 | set -e |
| 296 | |
| 297 | git config lfs.skipdownloaderrors true |
| 298 | echo "$pointer" | git lfs smudge a.dat |
| 299 | |
| 300 | # check content too |
| 301 | [ "$pointer" = "$(echo "$pointer" | git lfs smudge a.dat)" ] |
| 302 | |
| 303 | # now try env var |
| 304 | git config --unset lfs.skipdownloaderrors |
| 305 | |
| 306 | echo "$pointer" | GIT_LFS_SKIP_DOWNLOAD_ERRORS=1 git lfs smudge a.dat |
| 307 | |
| 308 | ) |
| 309 | end_test |
| 310 | |
| 311 | begin_test "smudge no ref, non-origin" |
| 312 | ( |
| 313 | set -e |
| 314 | |
| 315 | reponame="$(basename "$0" ".sh")-no-ref-non-origin" |
| 316 | setup_remote_repo "$reponame" |
| 317 | clone_repo "$reponame" "$reponame-1" |
| 318 | |
| 319 | git lfs track "*.dat" |
| 320 | echo "smudge a" > a.dat |
| 321 | git add .gitattributes a.dat |
| 322 | git commit -m "add a.dat" |
| 323 | |
| 324 | git push origin main |
| 325 | main=$(git rev-parse main) |
| 326 | |
| 327 | cd .. |
| 328 | git init "$reponame" |
| 329 | cd "$reponame" |
| 330 | |
| 331 | # We intentionally pick a name that is not origin to exercise the remote |
| 332 | # selection code path. Since there is only one remote, we should use it |
| 333 | # regardless of its name |
| 334 | git config remote.random.url "$GITSERVER/$reponame" |
| 335 | git fetch "$GITSERVER/$reponame" |
| 336 | |
| 337 | git checkout "$main" |
| 338 | [ "smudge a" = "$(cat a.dat)" ] |
| 339 | ) |
| 340 | end_test |