| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | begin_test "post-checkout" |
| 6 | ( |
| 7 | set -e |
| 8 | |
| 9 | reponame="$(basename "$0" ".sh")" |
| 10 | setup_remote_repo "$reponame" |
| 11 | |
| 12 | clone_repo "$reponame" "$reponame" |
| 13 | |
| 14 | git lfs track --lockable "*.dat" |
| 15 | git lfs track "*.big" # not lockable |
| 16 | git add .gitattributes |
| 17 | git commit -m "add git attributes" |
| 18 | |
| 19 | echo "[ |
| 20 | { |
| 21 | \"CommitDate\":\"$(get_date -10d)\", |
| 22 | \"Files\":[ |
| 23 | {\"Filename\":\"file1.dat\",\"Data\":\"file 1 creation\"}, |
| 24 | {\"Filename\":\"file2.dat\",\"Data\":\"file 2 creation\"}] |
| 25 | }, |
| 26 | { |
| 27 | \"CommitDate\":\"$(get_date -7d)\", |
| 28 | \"Files\":[ |
| 29 | {\"Filename\":\"file1.dat\",\"Data\":\"file 1 updated commit 2\"}, |
| 30 | {\"Filename\":\"file3.big\",\"Data\":\"file 3 creation\"}, |
| 31 | {\"Filename\":\"file4.big\",\"Data\":\"file 4 creation\"}], |
| 32 | \"Tags\":[\"atag\"] |
| 33 | }, |
| 34 | { |
| 35 | \"CommitDate\":\"$(get_date -5d)\", |
| 36 | \"Files\":[ |
| 37 | {\"Filename\":\"file2.dat\",\"Data\":\"file 2 updated commit 3\"}] |
| 38 | }, |
| 39 | { |
| 40 | \"CommitDate\":\"$(get_date -3d)\", |
| 41 | \"NewBranch\":\"branch2\", |
| 42 | \"Files\":[ |
| 43 | {\"Filename\":\"file5.dat\",\"Data\":\"file 5 creation in branch2\"}, |
| 44 | {\"Filename\":\"file6.big\",\"Data\":\"file 6 creation in branch2\"}] |
| 45 | }, |
| 46 | { |
| 47 | \"CommitDate\":\"$(get_date -1d)\", |
| 48 | \"Files\":[ |
| 49 | {\"Filename\":\"file2.dat\",\"Data\":\"file 2 updated in branch2\"}, |
| 50 | {\"Filename\":\"file3.big\",\"Data\":\"file 3 updated in branch2\"}] |
| 51 | } |
| 52 | ]" | GIT_LFS_SET_LOCKABLE_READONLY=0 lfstest-testutils addcommits |
| 53 | |
| 54 | # skipped setting read-only above to make bulk load simpler (no read-only issues) |
| 55 | |
| 56 | git push -u origin main branch2 |
| 57 | |
| 58 | # re-clone the repo so we start fresh |
| 59 | cd .. |
| 60 | rm -rf "$reponame" |
| 61 | clone_repo "$reponame" "$reponame" |
| 62 | |
| 63 | # this will be main |
| 64 | |
| 65 | touch untracked.dat |
| 66 | |
| 67 | [ "$(cat file1.dat)" == "file 1 updated commit 2" ] |
| 68 | [ "$(cat file2.dat)" == "file 2 updated commit 3" ] |
| 69 | [ "$(cat file3.big)" == "file 3 creation" ] |
| 70 | [ "$(cat file4.big)" == "file 4 creation" ] |
| 71 | [ ! -e file5.dat ] |
| 72 | [ ! -e file6.big ] |
| 73 | # without the post-checkout hook, any changed files would now be writeable |
| 74 | refute_file_writeable file1.dat |
| 75 | refute_file_writeable file2.dat |
| 76 | assert_file_writeable untracked.dat |
| 77 | assert_file_writeable file3.big |
| 78 | assert_file_writeable file4.big |
| 79 | |
| 80 | # checkout branch |
| 81 | git checkout branch2 |
| 82 | [ -e file5.dat ] |
| 83 | [ -e file6.big ] |
| 84 | refute_file_writeable file1.dat |
| 85 | refute_file_writeable file2.dat |
| 86 | refute_file_writeable file5.dat |
| 87 | assert_file_writeable untracked.dat |
| 88 | assert_file_writeable file3.big |
| 89 | assert_file_writeable file4.big |
| 90 | assert_file_writeable file6.big |
| 91 | |
| 92 | # Confirm that contents of existing files were updated even though were read-only |
| 93 | [ "$(cat file2.dat)" == "file 2 updated in branch2" ] |
| 94 | [ "$(cat file3.big)" == "file 3 updated in branch2" ] |
| 95 | |
| 96 | |
| 97 | # restore files inside a branch (causes full scan since no diff) |
| 98 | rm -f *.dat |
| 99 | [ ! -e file1.dat ] |
| 100 | [ ! -e file2.dat ] |
| 101 | [ ! -e file5.dat ] |
| 102 | git checkout file1.dat file2.dat file5.dat |
| 103 | [ "$(cat file1.dat)" == "file 1 updated commit 2" ] |
| 104 | [ "$(cat file2.dat)" == "file 2 updated in branch2" ] |
| 105 | [ "$(cat file5.dat)" == "file 5 creation in branch2" ] |
| 106 | refute_file_writeable file1.dat |
| 107 | refute_file_writeable file2.dat |
| 108 | refute_file_writeable file5.dat |
| 109 | |
| 110 | # now lock files, then remove & restore |
| 111 | git lfs lock file1.dat |
| 112 | git lfs lock file2.dat |
| 113 | assert_file_writeable file1.dat |
| 114 | assert_file_writeable file2.dat |
| 115 | rm -f *.dat |
| 116 | git checkout file1.dat file2.dat file5.dat |
| 117 | assert_file_writeable file1.dat |
| 118 | assert_file_writeable file2.dat |
| 119 | refute_file_writeable file5.dat |
| 120 | |
| 121 | ) |
| 122 | end_test |
| 123 | |
| 124 | begin_test "post-checkout with subdirectories" |
| 125 | ( |
| 126 | set -e |
| 127 | |
| 128 | reponame="post-checkout-subdirectories" |
| 129 | setup_remote_repo "$reponame" |
| 130 | |
| 131 | clone_repo "$reponame" "$reponame" |
| 132 | |
| 133 | git lfs track --lockable "bin/*.dat" |
| 134 | git lfs track "*.big" # not lockable |
| 135 | git add .gitattributes |
| 136 | git commit -m "add git attributes" |
| 137 | |
| 138 | echo "[ |
| 139 | { |
| 140 | \"CommitDate\":\"$(get_date -10d)\", |
| 141 | \"Files\":[ |
| 142 | {\"Filename\":\"bin/file1.dat\",\"Data\":\"file 1 creation\"}, |
| 143 | {\"Filename\":\"bin/file2.dat\",\"Data\":\"file 2 creation\"}] |
| 144 | }, |
| 145 | { |
| 146 | \"CommitDate\":\"$(get_date -7d)\", |
| 147 | \"Files\":[ |
| 148 | {\"Filename\":\"bin/file1.dat\",\"Data\":\"file 1 updated commit 2\"}, |
| 149 | {\"Filename\":\"file3.big\",\"Data\":\"file 3 creation\"}, |
| 150 | {\"Filename\":\"file4.big\",\"Data\":\"file 4 creation\"}], |
| 151 | \"Tags\":[\"atag\"] |
| 152 | }, |
| 153 | { |
| 154 | \"CommitDate\":\"$(get_date -5d)\", |
| 155 | \"Files\":[ |
| 156 | {\"Filename\":\"bin/file2.dat\",\"Data\":\"file 2 updated commit 3\"}] |
| 157 | }, |
| 158 | { |
| 159 | \"CommitDate\":\"$(get_date -3d)\", |
| 160 | \"NewBranch\":\"branch2\", |
| 161 | \"Files\":[ |
| 162 | {\"Filename\":\"bin/file5.dat\",\"Data\":\"file 5 creation in branch2\"}, |
| 163 | {\"Filename\":\"file6.big\",\"Data\":\"file 6 creation in branch2\"}] |
| 164 | }, |
| 165 | { |
| 166 | \"CommitDate\":\"$(get_date -1d)\", |
| 167 | \"Files\":[ |
| 168 | {\"Filename\":\"bin/file2.dat\",\"Data\":\"file 2 updated in branch2\"}, |
| 169 | {\"Filename\":\"file3.big\",\"Data\":\"file 3 updated in branch2\"}] |
| 170 | } |
| 171 | ]" | GIT_LFS_SET_LOCKABLE_READONLY=0 lfstest-testutils addcommits |
| 172 | |
| 173 | # skipped setting read-only above to make bulk load simpler (no read-only issues) |
| 174 | |
| 175 | git push -u origin main branch2 |
| 176 | |
| 177 | # re-clone the repo so we start fresh |
| 178 | cd .. |
| 179 | rm -rf "$reponame" |
| 180 | clone_repo "$reponame" "$reponame" |
| 181 | |
| 182 | # this will be main |
| 183 | |
| 184 | [ "$(cat bin/file1.dat)" == "file 1 updated commit 2" ] |
| 185 | [ "$(cat bin/file2.dat)" == "file 2 updated commit 3" ] |
| 186 | [ "$(cat file3.big)" == "file 3 creation" ] |
| 187 | [ "$(cat file4.big)" == "file 4 creation" ] |
| 188 | [ ! -e bin/file5.dat ] |
| 189 | [ ! -e file6.big ] |
| 190 | # without the post-checkout hook, any changed files would now be writeable |
| 191 | refute_file_writeable bin/file1.dat |
| 192 | refute_file_writeable bin/file2.dat |
| 193 | assert_file_writeable file3.big |
| 194 | assert_file_writeable file4.big |
| 195 | |
| 196 | # checkout branch |
| 197 | git checkout branch2 |
| 198 | [ -e bin/file5.dat ] |
| 199 | [ -e file6.big ] |
| 200 | refute_file_writeable bin/file1.dat |
| 201 | refute_file_writeable bin/file2.dat |
| 202 | refute_file_writeable bin/file5.dat |
| 203 | assert_file_writeable file3.big |
| 204 | assert_file_writeable file4.big |
| 205 | assert_file_writeable file6.big |
| 206 | |
| 207 | # Confirm that contents of existing files were updated even though were read-only |
| 208 | [ "$(cat bin/file2.dat)" == "file 2 updated in branch2" ] |
| 209 | [ "$(cat file3.big)" == "file 3 updated in branch2" ] |
| 210 | ) |
| 211 | end_test |
| 212 | |
| 213 | begin_test "post-checkout with an invalid attribute pattern" |
| 214 | ( |
| 215 | set -e |
| 216 | |
| 217 | reponame="post-checkout-invalid-attribute-pattern" |
| 218 | mkdir "$reponame" |
| 219 | cd "$reponame" |
| 220 | git init |
| 221 | |
| 222 | # Test with a ".gitattributes" file which contains a pattern with |
| 223 | # an incomplete POSIX named character class ("[[:space]]"). |
| 224 | # See https://github.com/git-lfs/git-lfs/issues/6120. |
| 225 | invalid_pattern="file[[:space:]]with[[:space]]incomplete_class" |
| 226 | echo "$invalid_pattern lockable" >.gitattributes |
| 227 | |
| 228 | GIT_TRACE=1 git lfs post-checkout \ |
| 229 | 0000000000000000000000000000000000000000 \ |
| 230 | 0000000000000000000000000000000000000000 1 2>&1 | tee post-checkout.log |
| 231 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 232 | |
| 233 | grep "Error parsing attributes from .*$(escape_path "$PATH_SEPARATOR")\.gitattributes: invalid attribute pattern" post-checkout.log |
| 234 | grep -F "invalid attribute pattern \"$invalid_pattern\": unclosed character class" post-checkout.log |
| 235 | [ 0 -eq "$(grep -c "panic:" post-checkout.log)" ] |
| 236 | |
| 237 | # Test with a ".gitattributes" file which contains a pattern with |
| 238 | # an invalid POSIX named character class. |
| 239 | invalid_pattern="file[[:space:]]with[[:invalid:]]class" |
| 240 | echo "$invalid_pattern lockable" >.gitattributes |
| 241 | |
| 242 | GIT_TRACE=1 git lfs post-checkout \ |
| 243 | 0000000000000000000000000000000000000000 \ |
| 244 | 0000000000000000000000000000000000000000 1 2>&1 | tee post-checkout.log |
| 245 | [ 0 -eq "${PIPESTATUS[0]}" ] |
| 246 | |
| 247 | grep "Error parsing attributes from .*$(escape_path "$PATH_SEPARATOR")\.gitattributes: invalid attribute pattern" post-checkout.log |
| 248 | grep -F "invalid attribute pattern \"$invalid_pattern\": wildmatch: unknown class" post-checkout.log |
| 249 | [ 0 -eq "$(grep -c "panic:" post-checkout.log)" ] |
| 250 | ) |
| 251 | end_test |