File: t-prune-worktree.sh

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (227 lines) | stat: -rwxr-xr-x 7,940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env bash

. "$(dirname "$0")/testlib.sh"

ensure_git_version_isnt $VERSION_LOWER "2.5.0"

begin_test "prune worktree"
(
  set -e

  reponame="prune_worktree"
  setup_remote_repo "remote_$reponame"

  clone_repo "remote_$reponame" "$reponame"

  git lfs track "*.dat" 2>&1 | tee track.log
  grep "Tracking \"\*.dat\"" track.log

  content_head="First checkout HEAD"
  content_worktree1head="Worktree 1 head"
  content_worktree1excluded="Worktree 1 excluded by filter"
  content_worktree1indexed="Worktree 1 indexed"
  content_worktree2head="Worktree 2 head"
  content_worktree2excluded="Worktree 2 excluded by filter"
  content_worktree2indexed="Worktree 2 indexed"
  content_oldcommit1="Always pruned 1"
  content_oldcommit2="Always pruned 2"
  content_oldcommit3="Always pruned 3"

  oid_head=$(calc_oid "$content_head")
  oid_worktree1head=$(calc_oid "$content_worktree1head")
  oid_worktree1excluded=$(calc_oid "$content_worktree1excluded")
  oid_worktree1indexed=$(calc_oid "$content_worktree1indexed")
  oid_worktree2head=$(calc_oid "$content_worktree2head")
  oid_worktree2excluded=$(calc_oid "$content_worktree2excluded")
  oid_worktree2indexed=$(calc_oid "$content_worktree2indexed")
  oid_oldcommit1=$(calc_oid "$content_oldcommit1"])
  oid_oldcommit2=$(calc_oid "$content_oldcommit2")
  oid_oldcommit3=$(calc_oid "$content_oldcommit3")

  echo "[
  {
    \"CommitDate\":\"$(get_date -40d)\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit1}, \"Data\":\"$content_oldcommit1\"}]
  },
  {
    \"CommitDate\":\"$(get_date -35d)\",
    \"NewBranch\":\"branch1\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit2}, \"Data\":\"$content_oldcommit2\"}]
  },
  {
    \"CommitDate\":\"$(get_date -20d)\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_worktree1head}, \"Data\":\"$content_worktree1head\"},
      {\"Filename\":\"foo/file.dat\",\"Size\":${#content_worktree1excluded}, \"Data\":\"$content_worktree1excluded\"}]
  },
  {
    \"CommitDate\":\"$(get_date -30d)\",
    \"ParentBranches\":[\"main\"],
    \"NewBranch\":\"branch2\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit3}, \"Data\":\"$content_oldcommit3\"}]
  },
  {
    \"CommitDate\":\"$(get_date -15d)\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_worktree2head}, \"Data\":\"$content_worktree2head\"},
      {\"Filename\":\"foo/file.dat\",\"Size\":${#content_worktree2excluded}, \"Data\":\"$content_worktree2excluded\"}]
  },
  {
    \"CommitDate\":\"$(get_date -30d)\",
    \"ParentBranches\":[\"main\"],
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_head}, \"Data\":\"$content_head\"}]
  }
  ]" | lfstest-testutils addcommits

  # push everything so that's not a retention issue
  git push origin main:main branch1:branch1 branch2:branch2

  # don't keep any recent, just checkouts
  git config lfs.fetchrecentrefsdays 0
  git config lfs.fetchrecentremoterefs true
  git config lfs.fetchrecentcommitsdays 0

  # We need to prevent MSYS from rewriting /foo into a Windows path.
  MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo"

  # before worktree, everything except current checkout would be pruned
  git lfs prune --dry-run 2>&1 | tee prune.log
  grep "prune: 8 local objects, 1 retained, done." prune.log
  grep "prune: 7 files would be pruned" prune.log

  # now add worktrees on the other branches
  git worktree add "../w1_$reponame" "branch1"
  git worktree add "../w2_$reponame" "branch2"

  # stage files in worktrees
  cd "../w1_$reponame"
  echo "$content_worktree1indexed" > indexed.dat
  git lfs track "*.dat"
  git add indexed.dat

  cd "../w2_$reponame"
  echo "$content_worktree2indexed" > indexed.dat
  git lfs track "*.dat"
  git add indexed.dat

  cd "../$reponame"

  # now should retain all 3 heads except for paths excluded by filter plus the indexed files
  git lfs prune --dry-run 2>&1 | tee prune.log
  grep "prune: 10 local objects, 5 retained, done." prune.log
  grep "prune: 5 files would be pruned" prune.log

  # also check that the same result is obtained when inside worktree rather than main
  cd "../w1_$reponame"
  git lfs prune --dry-run 2>&1 | tee prune.log
  grep "prune: 10 local objects, 5 retained, done." prune.log
  grep "prune: 5 files would be pruned" prune.log

  # now remove a worktree and prove that frees up the object staged in the
  # worktree's index but leaves the non-excluded object in its HEAD commit
  cd "../$reponame"
  rm -rf "../w1_$reponame"
  git lfs prune --dry-run 2>&1 | tee prune.log
  grep "prune: 10 local objects, 4 retained, done." prune.log
  grep "prune: 6 files would be pruned" prune.log

  # now ask Git to tidy the worktree metadata and prove that frees up the
  # non-excluded object in the removed worktree's HEAD commit
  git worktree prune
  git lfs prune --dry-run 2>&1 | tee prune.log
  grep "prune: 10 local objects, 3 retained, done." prune.log
  grep "prune: 7 files would be pruned" prune.log
)
end_test

begin_test "prune worktree (bare main)"
(
  set -e

  reponame="prune_worktree_bare"
  setup_remote_repo "remote_$reponame"

  clone_repo "remote_$reponame" "$reponame"

  git lfs track "*.dat" 2>&1 | tee track.log
  grep "Tracking \"\*.dat\"" track.log

  content_head="First checkout HEAD"
  content_worktree1head="Worktree 1 head"
  content_worktree1excluded="Worktree 1 excluded by filter"
  content_worktree1indexed="Worktree 1 indexed"
  content_oldcommit1="Always pruned 1"
  content_oldcommit2="Always pruned 2"

  oid_head=$(calc_oid "$content_head")
  oid_worktree1head=$(calc_oid "$content_worktree1head")
  oid_worktree1excluded=$(calc_oid "$content_worktree1excluded")
  oid_worktree1indexed=$(calc_oid "$content_worktree1indexed")
  oid_oldcommit1=$(calc_oid "$content_oldcommit1"])
  oid_oldcommit2=$(calc_oid "$content_oldcommit2")

  echo "[
  {
    \"CommitDate\":\"$(get_date -40d)\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit1}, \"Data\":\"$content_oldcommit1\"}]
  },
  {
    \"CommitDate\":\"$(get_date -35d)\",
    \"NewBranch\":\"branch1\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit2}, \"Data\":\"$content_oldcommit2\"}]
  },
  {
    \"CommitDate\":\"$(get_date -20d)\",
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_worktree1head}, \"Data\":\"$content_worktree1head\"},
      {\"Filename\":\"foo/file.dat\",\"Size\":${#content_worktree1excluded}, \"Data\":\"$content_worktree1excluded\"}]
  },
  {
    \"CommitDate\":\"$(get_date -30d)\",
    \"ParentBranches\":[\"main\"],
    \"Files\":[
      {\"Filename\":\"file.dat\",\"Size\":${#content_head}, \"Data\":\"$content_head\"}]
  }
  ]" | lfstest-testutils addcommits

  git push origin main:main branch1:branch1

  # checkout bare repo
  cd ..
  git clone --bare "$GITSERVER/remote_${reponame}" "${reponame}-bare"
  cd "${reponame}-bare"

  # fetch all LFS objects
  git lfs fetch --all

  # set retention configurations which should be ignored because there
  # are no remote branches, so all objects are considered unpushed
  git config lfs.fetchrecentrefsdays 0
  git config lfs.fetchrecentremoterefs true
  git config lfs.fetchrecentcommitsdays 0

  # We need to prevent MSYS from rewriting /foo into a Windows path.
  MSYS_NO_PATHCONV=1 git config "lfs.fetchexclude" "/foo"

  # now add worktree on the branch
  git worktree add "../w1_$reponame" "branch1"

  # stage files in worktree
  cd "../w1_$reponame"
  echo "$content_worktree1indexed" > indexed.dat
  git lfs track "*.dat"
  git add indexed.dat

  # should retain all objects because there are no remote branches
  # in a bare repo, so all objects are considered unpushed
  git lfs prune --dry-run 2>&1 | tee prune.log
  grep "prune: 6 local objects, 6 retained, done." prune.log
  [ "0" -eq "$(grep -c "files would be pruned" prune.log)" ]
)
end_test