Sync with 2.18.2
[git] / t / t6500-gc.sh
1 #!/bin/sh
2
3 test_description='basic git gc tests
4 '
5
6 . ./test-lib.sh
7
8 test_expect_success 'setup' '
9         # do not let the amount of physical memory affects gc
10         # behavior, make sure we always pack everything to one pack by
11         # default
12         git config gc.bigPackThreshold 2g
13 '
14
15 test_expect_success 'gc empty repository' '
16         git gc
17 '
18
19 test_expect_success 'gc does not leave behind pid file' '
20         git gc &&
21         test_path_is_missing .git/gc.pid
22 '
23
24 test_expect_success 'gc --gobbledegook' '
25         test_expect_code 129 git gc --nonsense 2>err &&
26         test_i18ngrep "[Uu]sage: git gc" err
27 '
28
29 test_expect_success 'gc -h with invalid configuration' '
30         mkdir broken &&
31         (
32                 cd broken &&
33                 git init &&
34                 echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
35                 test_expect_code 129 git gc -h >usage 2>&1
36         ) &&
37         test_i18ngrep "[Uu]sage" broken/usage
38 '
39
40 test_expect_success 'gc is not aborted due to a stale symref' '
41         git init remote &&
42         (
43                 cd remote &&
44                 test_commit initial &&
45                 git clone . ../client &&
46                 git branch -m develop &&
47                 cd ../client &&
48                 git fetch --prune &&
49                 git gc
50         )
51 '
52
53 test_expect_success 'gc --keep-largest-pack' '
54         test_create_repo keep-pack &&
55         (
56                 cd keep-pack &&
57                 test_commit one &&
58                 test_commit two &&
59                 test_commit three &&
60                 git gc &&
61                 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
62                 test_line_count = 1 pack-list &&
63                 BASE_PACK=.git/objects/pack/pack-*.pack &&
64                 test_commit four &&
65                 git repack -d &&
66                 test_commit five &&
67                 git repack -d &&
68                 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
69                 test_line_count = 3 pack-list &&
70                 git gc --keep-largest-pack &&
71                 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
72                 test_line_count = 2 pack-list &&
73                 test_path_is_file $BASE_PACK &&
74                 git fsck
75         )
76 '
77
78 test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
79         test_config gc.auto 3 &&
80         test_config gc.autodetach false &&
81         test_config pack.writebitmaps true &&
82         # We need to create two object whose sha1s start with 17
83         # since this is what git gc counts.  As it happens, these
84         # two blobs will do so.
85         test_commit 263 &&
86         test_commit 410 &&
87         # Our first gc will create a pack; our second will create a second pack
88         git gc --auto &&
89         ls .git/objects/pack | sort >existing_packs &&
90         test_commit 523 &&
91         test_commit 790 &&
92
93         git gc --auto 2>err &&
94         test_i18ngrep ! "^warning:" err &&
95         ls .git/objects/pack/ | sort >post_packs &&
96         comm -1 -3 existing_packs post_packs >new &&
97         comm -2 -3 existing_packs post_packs >del &&
98         test_line_count = 0 del && # No packs are deleted
99         test_line_count = 2 new # There is one new pack and its .idx
100 '
101
102 run_and_wait_for_auto_gc () {
103         # We read stdout from gc for the side effect of waiting until the
104         # background gc process exits, closing its fd 9.  Furthermore, the
105         # variable assignment from a command substitution preserves the
106         # exit status of the main gc process.
107         # Note: this fd trickery doesn't work on Windows, but there is no
108         # need to, because on Win the auto gc always runs in the foreground.
109         doesnt_matter=$(git gc --auto 9>&1)
110 }
111
112 test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
113         test_commit foo &&
114         test_commit bar &&
115         git repack &&
116         test_config gc.autopacklimit 1 &&
117         test_config gc.autodetach true &&
118         echo fleem >.git/gc.log &&
119         test_must_fail git gc --auto 2>err &&
120         test_i18ngrep "^error:" err &&
121         test_config gc.logexpiry 5.days &&
122         test-tool chmtime =-345600 .git/gc.log &&
123         test_must_fail git gc --auto &&
124         test_config gc.logexpiry 2.days &&
125         run_and_wait_for_auto_gc &&
126         ls .git/objects/pack/pack-*.pack >packs &&
127         test_line_count = 1 packs
128 '
129
130 test_expect_success 'background auto gc respects lock for all operations' '
131         # make sure we run a background auto-gc
132         test_commit make-pack &&
133         git repack &&
134         test_config gc.autopacklimit 1 &&
135         test_config gc.autodetach true &&
136
137         # create a ref whose loose presence we can use to detect a pack-refs run
138         git update-ref refs/heads/should-be-loose HEAD &&
139         test_path_is_file .git/refs/heads/should-be-loose &&
140
141         # now fake a concurrent gc that holds the lock; we can use our
142         # shell pid so that it looks valid.
143         hostname=$(hostname || echo unknown) &&
144         printf "$$ %s" "$hostname" >.git/gc.pid &&
145
146         # our gc should exit zero without doing anything
147         run_and_wait_for_auto_gc &&
148         test_path_is_file .git/refs/heads/should-be-loose
149 '
150
151 # DO NOT leave a detached auto gc process running near the end of the
152 # test script: it can run long enough in the background to racily
153 # interfere with the cleanup in 'test_done'.
154
155 test_done