6 trap 'if [ $? != 0 ]; then echo "gc failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
9 packopts
='--window=50 --window-memory=1g --depth=50'
12 [ "$cfg_permission_control" != "Hooks" ] ||
umask 000
15 if _result
="$(kill -0 "$1" 2>&1)"; then
16 # process exists and we have permission to signal it
19 case "$_result" in *"not permitted"*)
20 # we do not have permission to signal the process
23 # process does not exist
28 # A .lock file should only exist for much less than a second.
29 # If we see a stale lock file (> 1h old), remove it and then,
30 # just in case, wait 30 seconds for any process whose .lock
31 # we might have just removed (it's racy) to finish doing what
32 # should take much less than a second to do.
33 _stalelock
="$(find "$1.lock
" -maxdepth 1 -mmin +60 -print 2>/dev/null || :)"
34 if [ -n "$_stalelock" ]; then
39 if (set -C; > "$1.lock") 2>/dev
/null
; then
44 [ "$_try" != "p" ] ||
sleep 1
46 # cannot create lock file
50 # return true if there's more than one objects/pack-<sha>.pack file or
51 # ANY sha-1 files in objects
53 _packs
=$
(find objects
/pack
-type f
-name "pack-$octet20.pack" -print |
wc -l)
54 if [ $_packs != 1 ] && [ $_packs != 0 ]; then
57 _objs
=$
(find objects
/$octet -type f
-name "$octet19" -print 2>/dev
/null |
wc -l)
61 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
63 is_gfi_mirror ||
return 0
64 [ -s gfi-packs
] ||
return 0
65 while IFS
=': ' read -r _pack _junk
; do
66 if [ -s "$_pack" -a -s "${_pack%.pack}.idx" ]; then
67 git show-index
< "${_pack%.pack}.idx" | cut
-d ' ' -f 2
70 git pack-objects
$packopts --no-reuse-delta --delta-base-offset \
71 --non-empty --threads=1 $quiet objects
/pack
/packtmp | \
72 while read -r _newpack
; do
73 rm -f objects
/pack
/pack-
$_newpack.
*
74 ln -f objects
/pack
/packtmp-
$_newpack.pack objects
/pack
/pack-
$_newpack.pack
75 ln -f objects
/pack
/packtmp-
$_newpack.idx objects
/pack
/pack-
$_newpack.idx
76 rm -f objects
/pack
/packtmp-
$_newpack.
*
81 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
83 # Must have a matching .idx file and a non-empty packed-refs file
84 [ -s "${1%.pack}.idx" ] ||
return 1
85 [ -s "$2" ] ||
return 1
91 "ref: refs/"?
*|
"ref:refs/"?
*|
"refs/"?
*)
92 _headmatch
="${3#ref:}"
93 _headmatch
="${_headmatch# }"
94 _headmatchpat
="$(echo "$_headmatch" | sed -e 's/\([.$]\)/\\\1/g')"
95 _headsha
="$(grep -e "^
$octet20 $_headmatchpat\$
" < "$2" | \
97 case "$_headsha" in $octet20) :;; *)
105 rm -rf pack_is_complete_test
106 mkdir pack_is_complete_test
107 mkdir pack_is_complete_test
/refs
108 mkdir pack_is_complete_test
/objects
109 mkdir pack_is_complete_test
/objects
/pack
110 echo "$_headsha" > pack_is_complete_test
/HEAD
111 ln -s "$1" pack_is_complete_test
/objects
/pack
/
112 ln -s "${1%.pack}.idx" pack_is_complete_test
/objects
/pack
/
113 ln -s "$2" pack_is_complete_test
/packed-refs
114 _count
="$(git --git-dir=pack_is_complete_test rev-list --count --all 2>/dev/null || :)"
115 rm -rf pack_is_complete_test
116 [ -n "$_count" ] ||
return 1
117 [ "$_count" -gt 0 ] 2>/dev
/null ||
return 1
122 cd "$cfg_reporoot/$proj.git"
124 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
125 datefmt
='+%a, %d %b %Y %T %z'
127 if check_interval lastgc
$cfg_min_gc_interval; then
128 progress
"= [$proj] garbage check skip (last at $(config_get lastgc))"
131 if [ -e .nogc
]; then
132 progress
"x [$proj] garbage check disabled"
136 # Avoid unnecessary garbage collections:
137 # 1. If lastreceive is set and is older than lastgc
139 # 2. We are not a fork (! -s alternates) -OR- lastparentgc is older than lastgc
141 # If lastgc is NOT set or lastreceive is NOT set we MUST run gc
142 # If we are a fork and lastparentgc is NOT set we MUST run gc
144 # If the repo is dirty after removing any crud we MUST run gc
146 gcstart
="$(date "$datefmt")"
149 [ -s objects
/info
/alternates
] && isfork
=1
151 [ -n "$isfork" ] && lastparentgcsecs
="$(config_get_date_seconds lastparentgc || :)"
153 if lastreceivesecs
="$(config_get_date_seconds lastreceive)" && \
154 lastgcsecs
="$(config_get_date_seconds lastgc)" && \
155 [ $lastreceivesecs -lt $lastgcsecs ]; then
156 # We've run gc since we last received, so maybe we can skip,
157 # check if not fork or fork and lastparentgc < lastgc
158 if [ -n "$isfork" ]; then
159 if [ -n "$lastparentgcsecs" ] && \
160 [ $lastparentgcsecs -lt $lastgcsecs ]; then
161 # We've run gc since our parent ran gc so we can skip
165 # We don't have any alternates (we're not a forK) so we can skip
170 # be compatibile with gc.pid file from newer Git releases
175 if [ "$(createlock "$lockf")" ]; then
177 # 1) less than 12 hours old
178 # 2) contains two fields (pid hostname) NO trailing NL
179 # 3) the hostname is different OR the pid is still alive
180 # then we exit as another active process is holding the lock
181 if [ "$(find "$lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then
184 read -r apid ahost ajunk
< "$lockf" ||
:
185 if [ "$apid" ] && [ "$ahost" ]; then
186 if [ "$ahost" != "$hn" ] || pidactive
"$apid"; then
192 echo >&2 "[$proj] unable to create gc.pid.lock file"
195 if [ -n "$active" ]; then
197 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
200 printf "%s %s" "$$" "$hn" > "$lockf.lock"
201 chmod 0664 "$lockf.lock"
202 mv -f "$lockf.lock" "$lockf"
204 # Remove any stale pack remnants that are more than an hour old.
205 # Stale pack fragments are defined as any pack-<sha1>.ext where .ext is NOT
206 # .pack AND the corresponding .pack DOES NOT exist. A bunch of stale
207 # pack-<sha1>.idx files without their corresponding .pack files are worthless
208 # and just waste space. Normally there shouldn't be any remnants but actually
209 # this can happen when things are interrupted at just the wrong time.
210 # Note that the objects/pack directory is created by git init and should
212 find objects
/pack
-maxdepth 1 -type f
-mmin +60 -name "pack-$octet20.?*" -print | \
213 sed -e 's/^objects\/pack\/pack-//; s/\..*$//' | LC_ALL
=C
sort -u | \
214 while read packsha
; do
215 [ ! -e "objects/pack/pack-$packsha.pack" ] ||
continue
216 rm -f "objects/pack/pack-$packsha".?
*
219 # Remove any stale pack .keep files that are more than 12 hours old.
220 # We don't do anything to create any permanent pack .keep files, so they must
221 # be remnants from some failed push or something. Removing the .keep will
222 # allow the pack to be properly repacked.
223 find objects
/pack
-maxdepth 1 -type f
-mmin +720 -name "pack-$octet20.keep" -print | \
224 while read packkeep
; do
228 # Remove any stale tmp_pack_* or packtmp-* files that are more than 12 hours old.
229 find objects
/pack
-maxdepth 1 -type f
-mmin +720 -name "tmp_pack_?*" -print | \
230 while read packtmp
; do
233 find objects
/pack
-maxdepth 1 -type f
-mmin +720 -name "packtmp-?*" -print | \
234 while read packtmp
; do
238 # Remove any stale git-svn temp files that are more than 12 hours old.
239 # The git-svn process creates temp files with random 10 character names
240 # in the root of $GIT_DIR. Unfortunately they do not have a recognizable
241 # prefix, so we just have to kill any files with a 10-character name. We
242 # do this only for git-svn mirrors. All characters are chosen from
243 # [A-Za-z0-9_] so we can at least check that and fortunately the only
244 # collision is 'FETCH_HEAD' but that shouldn't matter.
245 # There may also be temp files with a Git_ prefix as well.
246 if is_svn_mirror
; then
247 _randchar
='[A-Za-z0-9_]'
248 _randchar2
="$_randchar$_randchar"
249 _randchar4
="$_randchar2$_randchar2"
250 _randchar10
="$_randchar4$_randchar4$_randchar2"
251 find .
-maxdepth 1 -type f
-mmin +720 -name "$_randchar10" -print | \
252 while read tmpcrud
; do
255 find .
-maxdepth 1 -type f
-mmin +720 -name "Git_*" -print | \
256 while read tmpcrud
; do
261 # Remove any stale fast_import_crash_<pid> files that are more than 3 days old.
262 if is_gfi_mirror
; then
263 find .
-maxdepth 1 -type f
-mmin +4320 -name "fast_import_crash_?*" -print | \
264 while read fastcrash
; do
269 # Do not skip gc if the repo is dirty
270 if [ -n "$skipgc" ] && ! is_dirty
; then
271 progress
"= [$proj] garbage check nothing but crud removal to do (`date`)"
272 config_set lastgc
"$gcstart"
278 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
279 # set lastparentgc and then update gcstart to be at least 1 second later
280 config_set lastparentgc
"$gcstart"
283 if [ -z "$lastreceivesecs" ]; then
284 # set lastreceive and then update gcstart to be at least 1 second later
285 config_set lastreceive
"$gcstart"
288 if [ -n "$bumptime" ]; then
290 gcstart
="$(date "$datefmt")"
293 progress
"+ [$proj] garbage check (`date`)"
295 # safe pruning: we put all our objects in all forks, then we can
296 # safely get rid of extra ones; repacks in forks will get rid of
297 # the redundant ones again then
299 if [ -d "../${forkdir##*/}" ]; then
300 # It is enough to copy objects just one level down and get_repo_list
301 # takes a regular expression (which is automatically prefixed with '^')
302 # so we can easily match forks exactly one level down from this project
303 get_repo_list
"$forkdir/[^/]*:" |
305 # Ignore forks that do not exist or are symbolic links
306 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
308 # Or do not have a non-zero length alternates file
309 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
311 # Match objects in parent project
312 for d
in objects
/?? objects
/pack
; do
313 [ "$d" != "objects/??" ] ||
continue
314 mkdir
-p "$cfg_reporoot/$fork.git/$d"
315 [ "$d" != "objects/pack" ] || \
316 [ "$(echo objects/pack/*)" != \
317 "objects/pack/*" ] || \
319 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" ||
:
321 # Update the fork's lastparentgc date (must be current, not $gcstart)
322 GIT_DIR
="$cfg_reporoot/$fork.git" git config \
323 gitweb.lastparentgc
"$(date "$datefmt")"
327 quiet
=; [ -n "$show_progress" ] || quiet
=-q
332 rm -f objects
/pack
/pack-
*.bndl
333 git repack
$packopts -a -d -l $quiet
334 allpacks
="$(echo objects/pack/pack-$octet20.pack)"
335 curhead
="$(cat HEAD)"
337 [ ! -e packed-refs
] || pkrf
=packed-refs
338 eval "reposizek=$(( $(echo 0 $(du -k $pkrf $allpacks 2>/dev/null | awk '{print $1}') | \
339 sed -e 's/ / + /g') ))"
341 git update-server-info
343 # darcs:// mirrors have a xxx.log file that will grow endlessly
344 # if this is a mirror and the file exists, shorten it to 10000 lines
345 # also take this opportunity to optimize the darcs repo
346 if [ ! -e .nofetch
] && [ -n "$cfg_mirror" ]; then
347 url
="$(config_get baseurl || :)"
348 case "$url" in darcs
://*)
349 if [ -n "$cfg_mirror_darcs" ]; then
351 basedarcs
="$(basename "${url#darcs:/}")"
352 if [ -f "$basedarcs.log" ]; then
353 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
354 mv -f "$basedarcs.log.$$" "$basedarcs.log"
356 if [ -d "$basedarcs.darcs" ]; then
358 cd "$basedarcs.darcs"
359 # Note that this does not optimize _darcs/inventories/ :(
367 # Create a matching .bndl header file for the all-in-one pack we just created
368 # but only if we're not a fork (otherwise the bundle would not be complete)
369 if [ ! -s objects
/info
/alternates
]; then
370 # There should only be one pack in $allpacks but if there was a
371 # simultaneous push...
372 # The one we just created will have a .idx and will NOT have a .keep
375 for pk
in $allpacks; do
376 [ -s "$pk" ] ||
continue
378 [ -s "$pkbase.idx" ] ||
continue
379 [ ! -e "$pkbase.keep" ] ||
continue
380 if pkhead
="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs
" "$curhead")"; then
385 if [ -n "$pkfound" -a -n "$pkhead" ]; then
387 echo "# v2 git bundle"
388 sed -ne "/^$octet20 refs\/[^ ]*\$/ p" < packed-refs
392 bndlsha
="${pkbase#objects/pack/pack-}"
393 bndlshatrailer
="${bndlsha#????????}"
394 bndlshaprefix
="${bndlsha%$bndlshatrailer}"
395 bndlname
="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
396 [ -d bundles
] || mkdir bundles
397 echo "$bndlsha" > "bundles/$bndlname"
401 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
402 config_set_raw girocco.reposizek
"${reposizek:-0}"
404 # We use $gcstart here to avoid a race where a push occurs during the gc itself
405 # and the next future gc could be incorrectly skipped if we used the current
406 # timestamp here instead
407 config_set lastgc
"$gcstart"
410 progress
"- [$proj] garbage check (`date`)"