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