Imported Upstream version 2.4.2
[scm/test.git] / test / test-prune-worktree.sh
1 #!/usr/bin/env bash
2
3 . "test/testlib.sh"
4
5 ensure_git_version_isnt $VERSION_LOWER "2.5.0"
6
7 begin_test "prune worktree"
8 (
9
10   set -e
11
12   reponame="prune_worktree"
13   setup_remote_repo "remote_$reponame"
14
15   clone_repo "remote_$reponame" "$reponame"
16
17   git lfs track "*.dat" 2>&1 | tee track.log
18   grep "Tracking \"\*.dat\"" track.log
19
20   content_head="First checkout HEAD"
21   content_worktree1head="Worktree 1 head"
22   content_worktree2head="Worktree 2 head"
23   content_oldcommit1="Always pruned 1"
24   content_oldcommit2="Always pruned 2"
25   content_oldcommit3="Always pruned 3"
26
27   oid_head=$(calc_oid "$content_head")
28   oid_worktree1head=$(calc_oid "$content_worktree1head")
29   oid_worktree2head=$(calc_oid "$content_worktree2head")
30   oid_oldcommit1=$(calc_oid "$content_oldcommit1")
31   oid_oldcommit2=$(calc_oid "$content_oldcommit2")
32   oid_oldcommit3=$(calc_oid "$content_oldcommit3")
33
34   echo "[
35   {
36     \"CommitDate\":\"$(get_date -40d)\",
37     \"Files\":[
38       {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit1}, \"Data\":\"$content_oldcommit1\"}]
39   },
40   {
41     \"CommitDate\":\"$(get_date -35d)\",
42     \"NewBranch\":\"branch1\",
43     \"Files\":[
44       {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit2}, \"Data\":\"$content_oldcommit2\"}]
45   },
46   {
47     \"CommitDate\":\"$(get_date -20d)\",
48     \"Files\":[
49       {\"Filename\":\"file.dat\",\"Size\":${#content_worktree1head}, \"Data\":\"$content_worktree1head\"}]
50   },
51   {
52     \"CommitDate\":\"$(get_date -30d)\",
53     \"ParentBranches\":[\"master\"],
54     \"NewBranch\":\"branch2\",
55     \"Files\":[
56       {\"Filename\":\"file.dat\",\"Size\":${#content_oldcommit3}, \"Data\":\"$content_oldcommit3\"}]
57   },
58   {
59     \"CommitDate\":\"$(get_date -15d)\",
60     \"Files\":[
61       {\"Filename\":\"file.dat\",\"Size\":${#content_worktree2head}, \"Data\":\"$content_worktree2head\"}]
62   },
63   {
64     \"CommitDate\":\"$(get_date -30d)\",
65     \"ParentBranches\":[\"master\"],
66     \"Files\":[
67       {\"Filename\":\"file.dat\",\"Size\":${#content_head}, \"Data\":\"$content_head\"}]
68   }
69   ]" | lfstest-testutils addcommits
70
71   # push everything so that's not a retention issue
72   git push origin master:master branch1:branch1 branch2:branch2
73
74   # don't keep any recent, just checkouts
75   git config lfs.fetchrecentrefsdays 0
76   git config lfs.fetchrecentremoterefs true
77   git config lfs.fetchrecentcommitsdays 0
78
79   # before worktree, everything except current checkout would be pruned
80   git lfs prune --dry-run 2>&1 | tee prune.log
81   grep "prune: 6 local object(s), 1 retained, done" prune.log
82   grep "prune: 5 file(s) would be pruned" prune.log
83
84   # now add worktrees on the other branches
85   git worktree add "../w1_$reponame" "branch1"
86   git worktree add "../w2_$reponame" "branch2"
87   # now should retain all 3 heads
88   git lfs prune --dry-run 2>&1 | tee prune.log
89   grep "prune: 6 local object(s), 3 retained, done" prune.log
90   grep "prune: 3 file(s) would be pruned" prune.log
91
92   # also check that the same result is obtained when inside worktree rather than main
93   cd "../w1_$reponame"
94   git lfs prune --dry-run 2>&1 | tee prune.log
95   grep "prune: 6 local object(s), 3 retained, done" prune.log
96   grep "prune: 3 file(s) would be pruned" prune.log
97
98   # now remove a worktree & prove that frees up 1 head while keeping the other
99   cd "../$reponame"
100   rm -rf "../w1_$reponame"
101   git worktree prune # required to get git to tidy worktree metadata
102   git lfs prune --dry-run 2>&1 | tee prune.log
103   grep "prune: 6 local object(s), 2 retained, done" prune.log
104   grep "prune: 4 file(s) would be pruned" prune.log
105
106
107 )
108 end_test
109