| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | . "$(dirname "$0")/testlib.sh" |
| 4 | |
| 5 | ensure_git_version_isnt $VERSION_LOWER "2.5.0" |
| 6 | |
| 7 | begin_test "prune worktree" |
| 8 | ( |
| 9 | set -e |
| 10 | |
| 11 | reponame="prune_worktree" |
| 12 | setup_remote_repo "remote_$reponame" |
| 13 | |
| 14 | clone_repo "remote_$reponame" "$reponame" |
| 15 | |
| 16 | git lfs track "*.dat" 2>&1 | tee track.log |
| 17 | grep "Tracking \"\*.dat\"" track.log |
| 18 | |
| 19 | content_head="First checkout HEAD" |
| 20 | content_worktree1head="Worktree 1 head" |
| 21 | content_worktree1excluded="Worktree 1 excluded by filter" |
| 22 | content_worktree1indexed="Worktree 1 indexed" |
| 23 | content_worktree2head="Worktree 2 head" |
| 24 | content_worktree2excluded="Worktree 2 excluded by filter" |
| 25 | content_worktree2indexed="Worktree 2 indexed" |
| 26 | content_oldcommit1="Always pruned 1" |
| 27 | content_oldcommit2="Always pruned 2" |
| 28 | content_oldcommit3="Always pruned 3" |
| 29 | |
| 30 | oid_head=$(calc_oid "$content_head") |
| 31 | oid_worktree1head=$(calc_oid "$content_worktree1head") |
| 32 | oid_worktree1excluded=$(calc_oid "$content_worktree1excluded") |
| 33 | oid_worktree1indexed=$(calc_oid "$content_worktree1indexed") |
| 34 | oid_worktree2head=$(calc_oid "$content_worktree2head") |
| 35 | oid_worktree2excluded=$(calc_oid "$content_worktree2excluded") |
| 36 | oid_worktree2indexed=$(calc_oid "$content_worktree2indexed") |
| 37 | oid_oldcommit1=$(calc_oid "$content_oldcommit1"]) |
| 38 | oid_oldcommit2=$(calc_oid "$content_oldcommit2") |
| 39 | oid_oldcommit3=$(calc_oid "$content_oldcommit3") |
| 40 | |
| 41 | echo "[ |
| 42 | { |
| 43 | \"CommitDate\":\"$(get_date -40d)\", |
| 44 | \"Files\":[ |
| 45 | {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit1}, \"Data\":\"$content_oldcommit1\"}] |
| 46 | }, |
| 47 | { |
| 48 | \"CommitDate\":\"$(get_date -35d)\", |
| 49 | \"NewBranch\":\"branch1\", |
| 50 | \"Files\":[ |
| 51 | {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit2}, \"Data\":\"$content_oldcommit2\"}] |
| 52 | }, |
| 53 | { |
| 54 | \"CommitDate\":\"$(get_date -20d)\", |
| 55 | \"Files\":[ |
| 56 | {\"Filename\":\"file.dat\",\"Size\":${#content_worktree1head}, \"Data\":\"$content_worktree1head\"}, |
| 57 | {\"Filename\":\"foo/file.dat\",\"Size\":${#content_worktree1excluded}, \"Data\":\"$content_worktree1excluded\"}] |
| 58 | }, |
| 59 | { |
| 60 | \"CommitDate\":\"$(get_date -30d)\", |
| 61 | \"ParentBranches\":[\"main\"], |
| 62 | \"NewBranch\":\"branch2\", |
| 63 | \"Files\":[ |
| 64 | {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit3}, \"Data\":\"$content_oldcommit3\"}] |
| 65 | }, |
| 66 | { |
| 67 | \"CommitDate\":\"$(get_date -15d)\", |
| 68 | \"Files\":[ |
| 69 | {\"Filename\":\"file.dat\",\"Size\":${#content_worktree2head}, \"Data\":\"$content_worktree2head\"}, |
| 70 | {\"Filename\":\"foo/file.dat\",\"Size\":${#content_worktree2excluded}, \"Data\":\"$content_worktree2excluded\"}] |
| 71 | }, |
| 72 | { |
| 73 | \"CommitDate\":\"$(get_date -30d)\", |
| 74 | \"ParentBranches\":[\"main\"], |
| 75 | \"Files\":[ |
| 76 | {\"Filename\":\"file.dat\",\"Size\":${#content_head}, \"Data\":\"$content_head\"}] |
| 77 | } |
| 78 | ]" | lfstest-testutils addcommits |
| 79 | |
| 80 | # push everything so that's not a retention issue |
| 81 | git push origin main:main branch1:branch1 branch2:branch2 |
| 82 | |
| 83 | # don't keep any recent, just checkouts |
| 84 | git config lfs.fetchrecentrefsdays 0 |
| 85 | git config lfs.fetchrecentremoterefs true |
| 86 | git config lfs.fetchrecentcommitsdays 0 |
| 87 | |
| 88 | # We need to prevent MSYS from rewriting /foo into a Windows path. |
| 89 | MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo" |
| 90 | |
| 91 | # before worktree, everything except current checkout would be pruned |
| 92 | git lfs prune --dry-run 2>&1 | tee prune.log |
| 93 | grep "8 local objects, 1 retained, done." prune.log |
| 94 | grep "7 files would be pruned" prune.log |
| 95 | |
| 96 | # now add worktrees on the other branches |
| 97 | git worktree add "../w1_$reponame" "branch1" |
| 98 | git worktree add "../w2_$reponame" "branch2" |
| 99 | |
| 100 | # stage files in worktrees |
| 101 | cd "../w1_$reponame" |
| 102 | echo "$content_worktree1indexed" > indexed.dat |
| 103 | git lfs track "*.dat" |
| 104 | git add indexed.dat |
| 105 | |
| 106 | cd "../w2_$reponame" |
| 107 | echo "$content_worktree2indexed" > indexed.dat |
| 108 | git lfs track "*.dat" |
| 109 | git add indexed.dat |
| 110 | |
| 111 | cd "../$reponame" |
| 112 | |
| 113 | # now should retain all 3 heads except for paths excluded by filter plus the indexed files |
| 114 | git lfs prune --dry-run 2>&1 | tee prune.log |
| 115 | grep "10 local objects, 5 retained, done." prune.log |
| 116 | grep "5 files would be pruned" prune.log |
| 117 | |
| 118 | # also check that the same result is obtained when inside worktree rather than main |
| 119 | cd "../w1_$reponame" |
| 120 | git lfs prune --dry-run 2>&1 | tee prune.log |
| 121 | grep "10 local objects, 5 retained, done." prune.log |
| 122 | grep "5 files would be pruned" prune.log |
| 123 | |
| 124 | # now remove a worktree and prove that frees up the object staged in the |
| 125 | # worktree's index but leaves the non-excluded object in its HEAD commit |
| 126 | cd "../$reponame" |
| 127 | rm -rf "../w1_$reponame" |
| 128 | git lfs prune --dry-run 2>&1 | tee prune.log |
| 129 | grep "10 local objects, 4 retained, done." prune.log |
| 130 | grep "6 files would be pruned" prune.log |
| 131 | |
| 132 | # now ask Git to tidy the worktree metadata and prove that frees up the |
| 133 | # non-excluded object in the removed worktree's HEAD commit |
| 134 | git worktree prune |
| 135 | git lfs prune --dry-run 2>&1 | tee prune.log |
| 136 | grep "10 local objects, 3 retained, done." prune.log |
| 137 | grep "7 files would be pruned" prune.log |
| 138 | ) |
| 139 | end_test |
| 140 | |
| 141 | begin_test "prune worktree (bare main)" |
| 142 | ( |
| 143 | set -e |
| 144 | |
| 145 | reponame="prune_worktree_bare" |
| 146 | setup_remote_repo "remote_$reponame" |
| 147 | |
| 148 | clone_repo "remote_$reponame" "$reponame" |
| 149 | |
| 150 | git lfs track "*.dat" 2>&1 | tee track.log |
| 151 | grep "Tracking \"\*.dat\"" track.log |
| 152 | |
| 153 | content_head="First checkout HEAD" |
| 154 | content_worktree1head="Worktree 1 head" |
| 155 | content_worktree1excluded="Worktree 1 excluded by filter" |
| 156 | content_worktree1indexed="Worktree 1 indexed" |
| 157 | content_oldcommit1="Always pruned 1" |
| 158 | content_oldcommit2="Always pruned 2" |
| 159 | |
| 160 | oid_head=$(calc_oid "$content_head") |
| 161 | oid_worktree1head=$(calc_oid "$content_worktree1head") |
| 162 | oid_worktree1excluded=$(calc_oid "$content_worktree1excluded") |
| 163 | oid_worktree1indexed=$(calc_oid "$content_worktree1indexed") |
| 164 | oid_oldcommit1=$(calc_oid "$content_oldcommit1"]) |
| 165 | oid_oldcommit2=$(calc_oid "$content_oldcommit2") |
| 166 | |
| 167 | echo "[ |
| 168 | { |
| 169 | \"CommitDate\":\"$(get_date -40d)\", |
| 170 | \"Files\":[ |
| 171 | {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit1}, \"Data\":\"$content_oldcommit1\"}] |
| 172 | }, |
| 173 | { |
| 174 | \"CommitDate\":\"$(get_date -35d)\", |
| 175 | \"NewBranch\":\"branch1\", |
| 176 | \"Files\":[ |
| 177 | {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit2}, \"Data\":\"$content_oldcommit2\"}] |
| 178 | }, |
| 179 | { |
| 180 | \"CommitDate\":\"$(get_date -20d)\", |
| 181 | \"Files\":[ |
| 182 | {\"Filename\":\"file.dat\",\"Size\":${#content_worktree1head}, \"Data\":\"$content_worktree1head\"}, |
| 183 | {\"Filename\":\"foo/file.dat\",\"Size\":${#content_worktree1excluded}, \"Data\":\"$content_worktree1excluded\"}] |
| 184 | }, |
| 185 | { |
| 186 | \"CommitDate\":\"$(get_date -30d)\", |
| 187 | \"ParentBranches\":[\"main\"], |
| 188 | \"Files\":[ |
| 189 | {\"Filename\":\"file.dat\",\"Size\":${#content_head}, \"Data\":\"$content_head\"}] |
| 190 | } |
| 191 | ]" | lfstest-testutils addcommits |
| 192 | |
| 193 | git push origin main:main branch1:branch1 |
| 194 | |
| 195 | # checkout bare repo |
| 196 | cd .. |
| 197 | git clone --bare "$GITSERVER/remote_${reponame}" "${reponame}-bare" |
| 198 | cd "${reponame}-bare" |
| 199 | |
| 200 | # fetch all LFS objects |
| 201 | git lfs fetch --all |
| 202 | |
| 203 | # set retention configurations which should be ignored because there |
| 204 | # are no remote branches, so all objects are considered unpushed |
| 205 | git config lfs.fetchrecentrefsdays 0 |
| 206 | git config lfs.fetchrecentremoterefs true |
| 207 | git config lfs.fetchrecentcommitsdays 0 |
| 208 | |
| 209 | # We need to prevent MSYS from rewriting /foo into a Windows path. |
| 210 | MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo" |
| 211 | |
| 212 | # now add worktree on the branch |
| 213 | git worktree add "../w1_$reponame" "branch1" |
| 214 | |
| 215 | # stage files in worktree |
| 216 | cd "../w1_$reponame" |
| 217 | echo "$content_worktree1indexed" > indexed.dat |
| 218 | git lfs track "*.dat" |
| 219 | git add indexed.dat |
| 220 | |
| 221 | # should retain all objects because there are no remote branches |
| 222 | # in a bare repo, so all objects are considered unpushed |
| 223 | git lfs prune --dry-run 2>&1 | tee prune.log |
| 224 | grep "6 local objects, 6 retained, done." prune.log |
| 225 | [ "0" -eq "$(grep -c "files would be pruned" prune.log)" ] |
| 226 | ) |
| 227 | end_test |