3 test_description='basic git gc tests
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
12 git config gc.bigPackThreshold 2g
15 test_expect_success 'gc empty repository' '
19 test_expect_success 'gc does not leave behind pid file' '
21 test_path_is_missing .git/gc.pid
24 test_expect_success 'gc --gobbledegook' '
25 test_expect_code 129 git gc --nonsense 2>err &&
26 test_i18ngrep "[Uu]sage: git gc" err
29 test_expect_success 'gc -h with invalid configuration' '
34 echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
35 test_expect_code 129 git gc -h >usage 2>&1
37 test_i18ngrep "[Uu]sage" broken/usage
40 test_expect_success 'gc is not aborted due to a stale symref' '
44 test_commit initial &&
45 git clone . ../client &&
46 git branch -m develop &&
53 test_expect_success 'gc --keep-largest-pack' '
54 test_create_repo keep-pack &&
61 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
62 test_line_count = 1 pack-list &&
63 BASE_PACK=.git/objects/pack/pack-*.pack &&
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 &&
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.
87 # Our first gc will create a pack; our second will create a second pack
89 ls .git/objects/pack | sort >existing_packs &&
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
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)
112 test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
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
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 &&
134 test_config gc.autopacklimit 1 &&
135 test_config gc.autodetach true &&
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 &&
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 &&
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
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'.