3 # NOTE: additional options can be passed to git repack by specifying
4 # them after the project name, for example:
12 echo "Usage: gc.sh projname [extra-repack-args]" >&2
17 packopts
="--depth=50 --window=50 --window-memory=${var_window_memory:-1g}"
18 quiet
=; [ -n "$show_progress" ] || quiet
=-q
21 [ "$cfg_permission_control" != "Hooks" ] ||
umask 000
25 if _result
="$(kill -0 "$1" 2>&1)"; then
26 # process exists and we have permission to signal it
29 case "$_result" in *"not permitted"*)
30 # we do not have permission to signal the process
33 # process does not exist
38 # A .lock file should only exist for much less than a second.
39 # If we see a stale lock file (> 1h old), remove it and then,
40 # just in case, wait 30 seconds for any process whose .lock
41 # we might have just removed (it's racy) to finish doing what
42 # should take much less than a second to do.
43 _stalelock
="$(find "$1.lock
" -maxdepth 1 -mmin +60 -print 2>/dev/null || :)"
44 if [ -n "$_stalelock" ]; then
49 if (set -C; > "$1.lock") 2>/dev
/null
; then
54 [ "$_try" != "p" ] ||
sleep 1
56 # cannot create lock file
60 # The pre-receive script creates one ref log file per push but we want them to
61 # be coalesced into one ref log file per day. We are guaranteed that any files
62 # we find to coalesce are NOT currently being written to since they are always
63 # written first as temporary files and then moved into place. We attempt to
64 # transfer the most recent modification time to the coalesced log file which
65 # would step on its mod time if it were being written to directly, but if we
66 # find per-process ref log files then it must be a push project and the only
67 # thing that would write directly to the main per-day log file would be a
68 # mirror project so there's actually no conflict.
69 # Also, if the clock is wonky (or was futzed with) we may have both YYYYMMDD
70 # and YYYYMMDD.gz present in which case combine them into YYYYMMDD
72 [ -d reflogs
] ||
return 0
74 find reflogs
-maxdepth 1 -type f
-name "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" -print |
75 while read -r rname
; do
76 if [ -e "$rname.gz" ]; then
77 if [ -s "$rname" ]; then
78 # Presumably the .gz file must have been created before the non-gz
79 # file since it had to be uncompressed at some point therefore
80 # we need to append the non-gz contents to it but keep the non-gz
81 # contents timestamp so we rename to YYYYMMDD_ which will sort first
82 # and be picked up in the next step if we are interrupted in the middle.
83 # If a YYYYMMDD_ file already exists we append to it and transfer the
84 # timestamp. Finally we transfer the YYYYMMDD_ timestamp to the result
85 # and remove the YYYYMMDD_ temporary file leaving the result uncompressed.
86 if [ -e "${rname}_" ]; then
87 cat "$rname" >>"${rname}_"
88 touch -r "$rname" "${rname}_"
92 mv "$rname" "${rname}_"
94 gzip -d "$rname.gz" </dev
/null
95 [ -e "$rname" ] && ! [ -e "$rname.gz" ]
96 cat "${rname}_" >>"$rname"
97 touch -r "${rname}_" "$rname"
100 # Just remove the empty file to resolve the problem
105 find reflogs
-maxdepth 1 -type f
-name "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_*" -print | LC_ALL
=C
sort |
106 while read -r rname
; do
107 logname
="${rname%%_*}"
108 # If someone's been futzing with the date, the file we want to
109 # append to could already have been compressed, so we just uncompress
110 # it here. The previous block guarantees we do not have both a compressed
111 # and uncompressed version present at the same time.
112 if [ -e "$logname.gz" ]; then
113 gzip -d "$logname.gz" </dev
/null
114 [ -e "$logname" ] && ! [ -e "$logname.gz" ]
116 cat "$rname" >>"$logname"
117 touch -r "$rname" "$logname"
119 if [ -e "$rname" ]; then
121 echo "! [$proj] failed to remove $rname" >&2
122 exit 1 # will only exit subshell created by "|"
128 # Remove any files in reflogs that are older than $cfg_reflogs_lifetime days
130 [ -d reflogs
] ||
return 0
131 exp
="$(( ${cfg_reflogs_lifetime:-1} * 1440 ))"
132 [ $exp -gt 0 ] || exp
=1440
133 [ $exp -le 43200 ] || exp
=43200
134 find reflogs
-maxdepth 1 -type f
-mmin "+$exp" -print0 |
xargs -0 rm -f
137 # Compact any reflogs that are not today's UTC date unless a .gz version exists
139 [ -d reflogs
] ||
return 0
140 _td
="reflogs/$(TZ=UTC date '+%Y%m%d')"
141 find reflogs
-maxdepth 1 -type f
-name "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" -print |
142 while read -r rname
; do
143 [ "$rname" != "$_td" ] ||
continue
144 ! [ -e "$rname.gz" ] ||
continue
145 gzip -9 "$rname" </dev
/null
149 # return true if there's more than one objects/pack-<sha>.pack file or
150 # ANY sha-1 files in objects
152 _packs
=$
(find objects
/pack
-type f
-name "pack-$octet20.pack" -print |
head -n 2 | LC_ALL
=C
wc -l)
153 if [ $_packs != 1 ] && [ $_packs != 0 ]; then
156 _objs
=$
(find objects
/$octet -type f
-name "$octet19" -print 2>/dev
/null |
head -n 1 | LC_ALL
=C
wc -l)
160 # make sure combine-packs uses the correct Git executable
161 run_combine_packs
() {
162 PATH
="$var_git_exec_path:$cfg_basedir/bin:$PATH" @basedir@
/jobd
/combine-packs.sh
"$@"
165 # combine the input pack(s) into a new pack (or possibly packs if packSizeLimit set)
166 # input pack names are read from standard input one per line delimited by the first
167 # ':', ' ' or '\n' character on the line (which allows gfi-packs to be read directly)
168 # all arguments, if any, are passed to pack-objects as additional options
169 # returns non-zero on failure AND creates .gc_failed in that case
172 find objects
/pack
-maxdepth 1 -type f
-name '*.zap*' -print0 |
xargs -0 rm -f
173 run_combine_packs
--replace "$@" $packopts --all-progress-implied $quiet --non-empty ||
{
180 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
182 [ -n "$gfi_mirror" ] ||
return 0
183 [ -d objects
/pack
] ||
{ rm -f gfi-packs
; return 0; }
184 progress
"~ [$proj] redeltifying poor quality git fast-import packs"
185 combine_packs
--ignore-missing --no-reuse-delta <gfi-packs
190 # combine small packs into larger pack(s)
191 # we avoid any keep, bndl or bitmap packs
192 # if the optional argument is non-empty even a single small pack will be redeltad
193 combine_small_packs
() {
196 if [ -n "$1" ] && [ -n "$noreusedeltaopt" ]; then
199 _lpo
="--exclude-no-idx --exclude-keep --exclude-bitmap --exclude-bndl --quiet"
200 _lpo
="$_lpo --object-limit $var_redelta_threshold objects/pack"
202 _cnt
="$(list_packs --count $_lpo || :)"
203 test "${_cnt:-0}" -ge $_minsmallpacks
205 [ -n "$_didprogress" ] ||
{
206 progress
"~ [$proj] combining small packs into a single larger pack"
209 _newp
="$(list_packs $_lpo | combine_packs --names $noreusedeltaopt)"
210 _newc
="$(echo $(echo "$_newp" | LC_ALL=C wc -w))"
211 # be paranoid and exit the loop if we haven't reduced the number of packs
212 [ $_newc -lt $_cnt ] ||
break
218 # Unfortunately git-svn lacks the ability to store newly fetched revisions as a pack.
219 # However, the fetch code conveniently sets .svnpack just before it runs git-svn fetch
220 # so that it's easy to find all the objects that have been fetched by git-svn and
221 # combine them into a pack. The --no-reuse-delta option is meaningless here since
222 # everything to be packed is a loose object and therefore not a delta so deltification
223 # will always take place.
225 [ -f .svnpack
] && [ -n "$svn_mirror" ] ||
return 0
227 mv -f .svnpack .svnpackgc
228 progress
"~ [$proj] combining loose git-svn objects into a pack"
229 _newp
="$(find objects/$octet -maxdepth 1 -type f -newer .svnpackgc -name "$octet19" -print 2>/dev/null |
230 LC_ALL=C awk -F / '{print $2 $3}' |
231 run_combine_packs --objects --names $packopts --incremental --all-progress-implied $quiet --non-empty)" ||
{
232 mv -f .svnpackgc .svnpack
236 if [ -n "$_newp" ]; then
237 # remove the now-redundant loose objects -- this is always safe
238 # even during a concurrent push because a reprepare_packed_git
239 # will be triggered if an object that should be there is not
240 # found thereby finding it in the new pack instead
241 git prune-packed
$quiet
246 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
248 # Must have a matching .idx file and a non-empty packed-refs file
249 [ -s "${1%.pack}.idx" ] ||
return 1
250 [ -s "$2" ] ||
return 1
256 "ref: refs/"?
*|
"ref:refs/"?
*|
"refs/"?
*)
257 _headmatch
="${3#ref:}"
258 _headmatch
="${_headmatch# }"
259 _headmatchpat
="$(echo "$_headmatch" | LC_ALL=C sed -e 's/\([.$]\)/\\\1/g')"
260 _headsha
="$(LC_ALL=C grep -e "^
$octet20 $_headmatchpat\$
" < "$2" | \
261 LC_ALL=C cut -d ' ' -f 1)"
262 case "$_headsha" in $octet20) :;; *)
270 rm -rf pack_is_complete_test
271 mkdir pack_is_complete_test
272 mkdir pack_is_complete_test
/refs
273 mkdir pack_is_complete_test
/objects
274 mkdir pack_is_complete_test
/objects
/pack
275 echo "$_headsha" > pack_is_complete_test
/HEAD
276 ln -s "$1" pack_is_complete_test
/objects
/pack
/
277 ln -s "${1%.pack}.idx" pack_is_complete_test
/objects
/pack
/
278 ln -s "$2" pack_is_complete_test
/packed-refs
279 _count
="$(git --git-dir=pack_is_complete_test rev-list --count --all 2>/dev/null || :)"
280 rm -rf pack_is_complete_test
281 [ -n "$_count" ] ||
return 1
282 [ "$_count" -gt 0 ] 2>/dev
/null ||
return 1
286 # On return a "$lockf" will have been created that must be removed when gc is done
288 # be compatibile with gc.pid file from newer Git releases
292 if [ "$(createlock "$lockf")" ]; then
294 # 1) less than 12 hours old
295 # 2) contains two fields (pid hostname) NO trailing NL
296 # 3) the hostname is different OR the pid is still alive
297 # then we exit as another active process is holding the lock
298 if [ "$(find "$lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then
301 read -r apid ahost ajunk
< "$lockf" ||
:
302 if [ "$apid" ] && [ "$ahost" ]; then
303 if [ "$ahost" != "$hn" ] || pidactive
"$apid"; then
309 echo >&2 "[$proj] unable to create gc.pid.lock file"
312 if [ -n "$active" ]; then
314 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
317 printf "%s %s" "$$" "$hn" > "$lockf.lock"
318 chmod 0664 "$lockf.lock"
319 mv -f "$lockf.lock" "$lockf"
322 # Create a repack subdirectory such that running repack in it will pack the
323 # same things that a pack in the normal directory would except that the pack
324 # is guaranteed to be generated in an optimized order by adding a suitable
325 # synthesized ref in the refs/tags namespace (yes, pack-objects.c really does
326 # behave differently depending on the contents of the refs/tags namespace).
327 # Before calling this, pack-refs --all MUST be performed or the wrong pack
328 # will end up being made.
329 # If a ref deletion is pushed after making the repack subdir but before the
330 # the actual repack, the discarded objects will be packed -- no big deal,
331 # they'll get discarded the next time gc runs.
332 # If a fast-forward ref update is pushed after making the repack subdir but
333 # before the actual repack, it will be picked up and the new objects packed
334 # (subject to the normal git repack race about picking such updates up).
335 # If a non-fast-forward ref update is pushed after making the repack subdir but
336 # before the actual repack, it will be picked up like a fast-forward update but
337 # the discarded objects will be included like a ref deletion (until the next
338 # scheduled gc takes place).
340 ! [ -d repack
] ||
rm -rf repack
341 ! [ -d repack
] ||
{ echo >&2 "[$proj] cannot remove repack subdirectory"; exit 1; }
343 [ -d info
] || mkdir info
344 ln -s ..
/config repack
/config
345 ln -s ..
/info repack
/info
346 ln -s ..
/objects repack
/objects
347 ln -s ..
/refs repack
/refs
348 sed 's, refs/, refs/!/,' < packed-refs
> repack
/packed-refs
349 optref
="$(git rev-list -n 1 --all 2>/dev/null || :)"
350 if [ -n "$optref" ]; then
351 echo "$optref refs/tags/!" >> repack
/packed-refs
352 echo "$optref" > repack
/HEAD
354 cat HEAD
> repack
/HEAD
358 # Remove any crud that's been left behind by interrupted operations
359 # that did not clean up after themselves
361 # Remove any existing FETCH_HEAD
362 # There can only be a FETCH_HEAD if we've been fetching, not if we've been
363 # receiving pushes (those never create a FETCH_HEAD).
364 # And if we're fetching because we're a mirror, we know we're not fetching right
365 # now since jobd.pl never runs a project's fetch simultaneously with its gc.
366 # Therefore any existing FETCH_HEAD is junk. And it may be many megabytes if
367 # there were a lot of refs.
370 # remove any existing pack_is_complete_test or repack subdirectories
371 # If either exists when this function is called it's crud
372 rm -rf pack_is_complete_test repack
374 # Remove any stale pack remnants that are more than an hour old.
375 # Stale pack fragments are defined as any pack-<sha1>.ext where .ext is NOT
376 # .pack AND the corresponding .pack DOES NOT exist. A bunch of stale
377 # pack-<sha1>.idx files without their corresponding .pack files are worthless
378 # and just waste space. Normally there shouldn't be any remnants but actually
379 # this can happen when things are interrupted at just the wrong time.
380 # Note that the objects/pack directory is created by git init and should
382 find objects
/pack
-maxdepth 1 -type f
-mmin +60 -name "pack-$octet20.?*" -print | \
383 LC_ALL
=C
sed -e 's/^objects\/pack\/pack-//; s/\..*$//' | LC_ALL
=C
sort -u | \
384 while read packsha
; do
385 [ ! -e "objects/pack/pack-$packsha.pack" ] ||
continue
386 rm -f "objects/pack/pack-$packsha".?
*
389 # Remove any stale tmp reflogs files that are more than one hour old.
390 # Since they are created only while the pre-receive hook is running and
391 # all it does is process a bunch of refs passed to it on standard input
392 # it's inconceivable that it would ever take as much as an hour to run.
393 if [ -d reflogs
]; then
394 find reflogs
-maxdepth 1 -type f
-mmin +60 -name "tmp_*" -print0 |
xargs -0 rm -f
397 # Remove any stale pack .keep files that are more than 12 hours old.
398 # We don't do anything to create any permanent pack .keep files, so they must
399 # be remnants from some failed push or something. Removing the .keep will
400 # allow the pack to be properly repacked.
401 find objects
/pack
-maxdepth 1 -type f
-mmin +720 -name "pack-$octet20.keep" -print0 |
xargs -0 rm -f
403 # Remove any stale tmp_pack_*, tmp_idx_*, tmp_bitmap_*, packtmp-* or .tmp-*-pack* files
404 # that are more than 12 hours old.
405 find objects
/pack
-maxdepth 1 -type f
-mmin +720 \
( \
406 -name "tmp_pack_?*" -o -name "tmp_idx_?*" -o -name "tmp_bitmap_?*" -o \
407 -name "packtmp-?*" -o -name ".tmp-?*-pack*" \
408 \
) -print0 |
xargs -0 rm -f
410 # Remove any stale shallow_* files that are more than 12 hours old.
411 # These can be left behind by Git >= 1.8.4.2 and < 2.0.0 when a client
412 # requests a shallow clone.
413 find .
-maxdepth 1 -type f
-mmin +720 -name "shallow_?*" -print0 |
xargs -0 rm -f
415 # Remove any stale *.temp files in the objects area that are more than 12 hours old.
416 # This can be stale sha1.temp, or stale *.pack.temp so we kill all stale *.temp.
417 find objects
-type f
-mmin +720 -name "*.temp" -print0 |
xargs -0 rm -f
419 # Remove any stale *.lock files in the htmlcache area that might have been left
420 # behind after an abnormal exit during an attempt to update a cached file and
421 # are more than 1 hour old.
422 ! [ -d htmlcache
] ||
find htmlcache
-type f
-mmin +60 -name "*.lock" -print0 |
xargs -0 rm -f
424 # Remove any stale git-svn temp files that are more than 12 hours old.
425 # The git-svn process creates temp files with random 10 character names
426 # in the root of $GIT_DIR. Unfortunately they do not have a recognizable
427 # prefix, so we just have to kill any files with a 10-character name. We
428 # do this only for git-svn mirrors. All characters are chosen from
429 # [A-Za-z0-9_] so we can at least check that and fortunately the only
430 # collision is 'FETCH_HEAD' but that shouldn't matter.
431 # There may also be temp files with a Git_ prefix as well.
432 if [ -n "$svn_mirror" ]; then
433 _randchar
='[A-Za-z0-9_]'
434 _randchar2
="$_randchar$_randchar"
435 _randchar4
="$_randchar2$_randchar2"
436 _randchar10
="$_randchar4$_randchar4$_randchar2"
437 find .
-maxdepth 1 -type f
-mmin +720 -name "$_randchar10" -print0 |
xargs -0 rm -f
438 find .
-maxdepth 1 -type f
-mmin +720 -name "Git_*" -print0 |
xargs -0 rm -f
441 # Remove any stale fast_import_crash_<pid> files that are more than 3 days old.
442 if [ -n "$gfi_mirror" ]; then
443 find .
-maxdepth 1 -type f
-mmin +4320 -name "fast_import_crash_?*" -print0 |
xargs -0 rm -f
448 ## Garbage Collection Types
450 ## There are two kinds of possible garbage collection (gc) operations:
452 ## 1. A normal, full gc
455 ## If the full garbage collection interval has expired (or gc has never been
456 ## run), then a normal, full gc will take place. Otherwise, a "mini" gc will
457 ## take place if the file .needsgc exists.
459 ## A "mini" gc is similar to "git gc --auto" in that it may not end up actually
460 ## doing anything unless the right conditions are present so it's not a burden
461 ## to run it often. If the file .needsgc exists, a "mini" gc will occur at
462 ## the next opportunity.
464 ## Note, however, that the .nogc file suppresses ALL gc activity (normal or mini).
469 cd "$cfg_reporoot/$proj.git"
470 [ -d objects
/pack
] ||
{ rm -f gfi-packs
; mkdir
-p objects
/pack
; }
471 mirror_url
="$(get_mirror_url)"
473 ! is_svn_mirror_url
"$mirror_url" || svn_mirror
=1
475 if [ -f gfi-packs
-a -s gfi-packs
] && is_gfi_mirror_url
"$mirror_url"; then
479 # If git config --bool --get girocco.redelta is explicitly false then automatic
480 # redelta when there are less than $var_redelta_threshold objects will be suppressed.
481 # On the other hand, if git config --get girocco.redelta is "always" then, on a full
482 # gc only, for the final repack, deltas will always be recomputed.
483 # This can be set on a per-project basis to avoid unusual pathological gc behavior.
484 # Setting this will hurt efficiency of the affected repository.
485 # Note that fast-import packs ALWAYS get new deltas regardless of this setting.
486 noreusedeltaopt
="--no-reuse-delta"
487 [ "$(git config --bool --get girocco.redelta 2>/dev/null || :)" != "false" ] || noreusedeltaopt
=
489 [ "$(git config --get girocco.redelta 2>/dev/null || :)" != "always" ] || alwaysredelta
=1
491 trap 'e=$?; rm -f .gc_in_progress; if [ $e != 0 ]; then echo "gc failed dir: $PWD" >&2; fi' EXIT
495 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
496 datefmt
='+%a, %d %b %Y %T %z'
499 if check_interval lastgc
$cfg_min_gc_interval; then
500 if [ -e .needsgc
]; then
503 progress
"= [$proj] garbage check skip (last at $(config_get lastgc))"
507 if [ -e .nogc
]; then
508 progress
"x [$proj] garbage check disabled"
512 if [ -n "$isminigc" ]; then
513 # Perform a "mini" gc
514 # Note that .delaygc is ignored here as that's only intended for full gc
516 rm -f .allowgc .needsgc
522 if [ -f .svnpack
] && [ -n "$svn_mirror" ]; then
524 progress
"+ [$proj] mini garbage check (`date`)"
527 if [ -z "$cfg_delay_gfi_redelta" ] && [ -n "$gfi_mirror" ]; then
528 # $Girocco::Config::delay_gfi_redelta is false, force redeltification now
529 if [ -z "$miniactive" ]; then
531 progress
"+ [$proj] mini garbage check (`date`)"
535 # If there aren't at least 10 non-keep, non-bitmap, non-bndl packs then
536 # don't actually process them yet
537 lpo
="--exclude-no-idx --exclude-keep --exclude-bitmap --exclude-bndl --quiet"
538 packcnt
="$(list_packs --count $lpo objects/pack || :)"
539 if [ "${packcnt:-0}" -ge 10 ]; then
540 if [ -z "$miniactive" ]; then
542 progress
"+ [$proj] mini garbage check (`date`)"
544 if [ -n "$gfi_mirror" ]; then
546 packcnt
="$(list_packs --count $lpo objects/pack || :)"
548 # if repack_gfi_packs dropped the pack count to < 10 don't combine
549 if [ "${packcnt:-0}" -ge 10 ]; then
551 packcnt
="$(list_packs --count $lpo objects/pack || :)"
553 # if we still have more than 10 packs trigger a full gc
554 if [ "${packcnt:-0}" -ge 10 ]; then
555 # We shouldn't be in a .delaygc state at this point, but if
556 # we are then nuke it because we really need a full gc now
558 git config
--unset gitweb.lastgc
560 progress
"- [$proj] mini garbage check triggering full gc too many packs (`date`)"
565 if [ -n "$miniactive" ]; then
566 git update-server-info
567 progress
"- [$proj] mini garbage check (`date`)"
569 progress
"= [$proj] mini garbage check nothing but crud removal to do (`date`)"
574 # Avoid unnecessary garbage collections:
575 # 1. If lastreceive is set and is older than lastgc
577 # 2. We are not a fork (! -s alternates) -OR- lastparentgc is older than lastgc
579 # If lastgc is NOT set or lastreceive is NOT set we MUST run gc
580 # If we are a fork and lastparentgc is NOT set we MUST run gc
582 # If the repo is dirty after removing any crud we MUST run gc
584 gcstart
="$(date "$datefmt")"
587 [ -s objects
/info
/alternates
] && isfork
=1
589 [ -n "$isfork" ] && lastparentgcsecs
="$(config_get_date_seconds lastparentgc || :)"
591 if lastreceivesecs
="$(config_get_date_seconds lastreceive)" && \
592 lastgcsecs
="$(config_get_date_seconds lastgc)" && \
593 [ $lastreceivesecs -lt $lastgcsecs ]; then
594 # We've run gc since we last received, so maybe we can skip,
595 # check if not fork or fork and lastparentgc < lastgc
596 if [ -n "$isfork" ]; then
597 if [ -n "$lastparentgcsecs" ] && \
598 [ $lastparentgcsecs -lt $lastgcsecs ]; then
599 # We've run gc since our parent ran gc so we can skip
603 # We don't have any alternates (we're not a forK) so we can skip
608 # Prevent any other simultaneous gc operations
611 # At this point, if .allowgc exists, it's now crud to be removed
614 # Ideally we would do this in post-receive, but that would mean duplicating the
615 # logic so it's available in the chroot jail and that's highly undesirable
616 # Instead, since the first gc will be triggered immediately following the first
617 # push, we do the check here as it's quick and harmless if HEAD is already valid
618 check_and_set_head ||
:
620 # Always get rid of crud
623 # Always perform reflogs maintenance
628 # Run 'git svn gc' now for svn mirrors
629 if [ -n "$svn_mirror" ]; then
633 # Skip the actual gc if .delaygc is set
634 if [ -e .delaygc
]; then
635 progress
"x [$proj] garbage check delayed (except for crud removal)"
640 # Do not skip gc if the repo is dirty
641 if [ -n "$skipgc" ] && ! is_dirty
; then
642 progress
"= [$proj] garbage check nothing but crud removal to do (`date`)"
643 config_set lastgc
"$gcstart"
649 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
650 # set lastparentgc and then update gcstart to be at least 1 second later
651 config_set lastparentgc
"$gcstart"
654 if [ -z "$lastreceivesecs" ]; then
655 # set lastreceive and then update gcstart to be at least 1 second later
656 config_set lastreceive
"$gcstart"
659 if [ -n "$bumptime" ]; then
661 gcstart
="$(date "$datefmt")"
664 progress
"+ [$proj] garbage check (`date`)"
667 [ -z "$alwaysredelta" ] || newdeltas
=-f
668 if [ -z "$newdeltas" ] && [ -n "$gfi_mirror" ]; then
669 if [ $
(list_packs
--exclude-no-idx --count objects
/pack
) -le \
670 $
(list_packs
--exclude-no-idx --count --quiet --only gfi-packs
) ]; then
671 # Don't bother with repack_gfi_packs since everything's being repacked
675 if [ -z "$newdeltas" ] && [ -n "$noreusedeltaopt" ] && \
676 [ $
(list_packs
--exclude-no-idx --count-objects objects
/pack
) -le $var_redelta_threshold ]; then
677 # There aren't enough objects to worry about so just redelta to get the best pack
680 if [ -z "$newdeltas" ]; then
681 # Since we're not going to recompute deltas overall, we need to do the
682 # "mini" maintenance so that we can get more optimal deltas
683 [ -z "$noreusedeltaopt" ] || make_svn_pack
685 force_single_pack_redelta
=
686 [ -n "$gfi_mirror" ] ||
[ -n "$svn_mirror" ] || force_single_pack_redelta
=1
687 [ -z "$noreusedeltaopt" ] || combine_small_packs
$force_single_pack_redelta
691 ## Safe Pruning In Forks
693 ## We are about to perform garbage collection. We do NOT use the "git gc"
694 ## command directly as it does not provide enough control over the fine details
695 ## that we require. However, we DO maintain a "gc.pid" file during our garbage
696 ## collection so that a simultaneous "git gc" by an administrator will be
697 ## blocked (and similarly we refuse to start garbage collection if we cannot
698 ## create the "gc.pid" file). When we say "gc" in the below description we are
699 ## referring to our "gc.sh" script, NOT the "git gc" command.
701 ## If the project we are running garbage collection (gc) on has any forks we
702 ## must be careful not to remove any objects that while no longer referenced by
703 ## this project (the parent) are still referenced by one or more forks (the
704 ## children) otherwise the children will become corrupt and we can't abide
707 ## One way to accomplish this is to simply hard-link all currently existing
708 ## loose objects and packs in the parent into all the children that refer to the
709 ## parent (via a line in their objects/info/alternates file) before beginning
710 ## the gc operation and then relying on a subsequent gc in the child to clean up
711 ## any excess objects/packs. We used to use this strategy but it's very
712 ## inefficient because:
714 ## 1. The disk space used by the old pack(s)/object(s) will not be reclaimed
715 ## until all children (and their children, if any) run gc by which time
716 ## it's quite possible the topmost parent will have run gc again and
717 ## hard-linked yet another old pack down to its children (not to mention
720 ## 2. As we are now using the "-A" option with "git repack", any new objects
721 ## in the parent that are not referenced by children will continually get
722 ## exploded out of the hard-linked pack in the children whenever the
725 ## 3. To avoid suboptimal and/or unnecessarily many packs being hard-linked
726 ## into child forks, we must run the "mini" gc maintenance before we
727 ## perform the hard-linking into the children which provides yet another
728 ## source of inefficiency.
730 ## Since we are using the "-A" option to "git repack" (that was not always the
731 ## case) to guarantee we can access old ref values for long enough to send out
732 ## a meaningful mail.sh notification, we now have another, more efficient,
733 ## option available to prevent corruption of child forks that continue to refer
734 ## to objects that are no longer reachable from any ref in the parent.
736 ## The only things that need be copied (or hard-linked) into the child fork(s)
737 ## are those objects that have become unreachable from any ref in the parent.
738 ## They are the only things that could ever be removed by "git prune" and
739 ## therefore the only things we need to prevent the loss of in order to avoid
740 ## corruption of the child fork(s).
742 ## Therefore we now use the following strategy instead to avoid excessive disk
743 ## use and lots of unnecessary loose objects in child forks:
745 ## 1. Run "git repack -A -d -l" in the parent BEFORE doing anything about
748 ## 2. Hard-link all remaining existing loose objects in the parent into the
749 ## immediate child forks.
751 ## 3. Now run "git prune" in the parent.
753 ## With this new strategy we avoid the need to run any "mini" gc maintenance
754 ## before copying (or hard-linking) anything down to the child forks.
755 ## Furthermore, only when the parent performs a non-fast-forward update will
756 ## anything ever be transferred to the children leaving them unperturbed in the
757 ## vast majority of cases. Finally, even if the parent references objects the
758 ## children do not, those objects will no longer continually end up in the
759 ## children as unreachable loose objects after the children run gc.
764 touch .gc_in_progress
765 rm -f .gc_failed bundles
/*
766 rm -f objects
/pack
/pack-
*.bndl
767 # We use the -A option with git repack so that unreachable objects can live
768 # on for a time as loose objects. This is particularly helpful if we just
769 # happen to be in the process of sending out a ref update for a ref that was
770 # force updated and the old ref value would have otherwise been removed by
771 # repack because it was now unreachable. Admittedly the window for gc to run
772 # and do that before we manage to send out the ref update is not large, but
773 # it would not be difficult to create such a situation. Unfortunately, when
774 # Git unpacks these unreachable objects it will give them the modification
775 # time of the *.pack file they came out of. This could be very, very old.
776 # If that happens, the subsequent git prune --expire some_time_ago will still
777 # remove the object(s) and our pending ref update will still lose out.
778 # To prevent this from happening and to get the behavior we want, we now
779 # touch the modification time of all pack-<sha>.pack files so that any
780 # loosened objects get a current time. Git does not provide any other
781 # mechanism to do this. We do not want to just touch all loose objects
782 # left after the repack because that would cause objects that were loosened
783 # previously to live on which we definitely do not want.
784 list_packs
--exclude-no-idx objects
/pack |
xargs touch -c 2>/dev
/null ||
:
785 # We wish to keep deltas from our last full pack so if we're not redeltaing
786 # then make sure the .pack associated with the .bitmap has a newer mod time
787 # (If there is no .bitmap then touch the pack with the most objects instead.)
788 if [ -z "$newdeltas" ]; then
789 bmpack
="$(list_packs --exclude-no-bitmap --exclude-no-idx --max-matches 1 objects/pack)"
790 [ -n "$bmpack" ] || bmpack
="$(list_packs --exclude-no-idx --max-matches 1 --object-limit -1 --include-boundary objects/pack)"
791 if [ -n "$bmpack" ] && [ -f "$bmpack" -a -s "$bmpack" ]; then
793 touch -c "$bmpack" 2>/dev
/null ||
:
796 # The git repack command only supports bitmaps if all objects are being packed.
797 # While it is theoretically possible that a project with a non-empty alternates
798 # file ends up packing all objects (because it does not actually use any of the
799 # objects found in the alternates), it's very unlikely. And, in the unlikely
800 # event that did occur, clients would see a message about only using one bitmap
801 # because Git can only use one bitmap at a time and at least one of the
802 # alternates is bound to have a bitmap. Therefore if we see a non-empty
803 # alternates file, we disable writing bitmaps which avoids the warning and any
804 # possibility of a client warning as well.
806 [ -z "$var_have_git_172" ] ||
! [ -s objects
/info
/alternates
] || \
807 nobm
='-c repack.writebitmaps=false -c pack.writebitmaps=false'
808 progress
"~ [$proj] running full gc repack${nobm:+ (bitmaps disabled)}"
810 # We run git repack from the repack subdirectory so we can force optimized packs
811 # to be generated even for repositories that do not have any tagged commits
812 git
$nobm repack
$packopts -A -d -l $quiet $newdeltas $@
815 [ ! -e .gc_failed
] ||
exit 1
816 # These, if they exist, are now meaningless and need to be removed
817 rm -f gfi-packs .needsgc .svnpack .svnpackgc
818 allpacks
="$(echo objects/pack/pack-$octet20.pack)"
819 curhead
="$(cat HEAD)"
821 [ ! -e packed-refs
] || pkrf
=packed-refs
822 eval "reposizek=$(( $(echo 0 $(du -k $pkrf $allpacks 2>/dev/null | LC_ALL=C awk '{print $1}') | \
823 LC_ALL=C sed -e 's/ / + /g') ))"
824 git update-server-info
825 # The -A option to `git repack` may have caused some loose objects to pop
826 # out of their packs. We must make these objects group writable so that they
827 # can be freshened by other pushers. Technically we need only do this for
828 # push projects but to enable mirror projects to be more easily converted to
829 # push projects, we go ahead and do it for all projects.
830 { find objects
/$octet -type f
-name "$octet19" -print0 |
xargs -0 chmod ug
+w ||
:; } 2>/dev
/null
832 if has_forks
"$proj"; then
833 progress
"~ [$proj] hard-linking loose objects into immediate child forks"
834 # We have to update the lastparentgc time in the child forks even if they do not get any
835 # new "loose objects" because they need to run gc just in case the parent now has some
836 # objects that used to only be in the child so they can be removed from the child.
837 # For example, a "patch" might be developed first in a fork and then later accepted into
838 # the parent in which case the objects making up the patch in the child fork are now
839 # redundant (since they're now in the parent as well) and need to be removed from the
840 # child fork which can only happen if the child fork runs gc.
842 # It is enough to copy objects just one level down and get_repo_list
843 # takes a regular expression (which is automatically prefixed with '^')
844 # so we can easily match forks exactly one level down from this project
845 get_repo_list
"$forkdir/[^/]*:" |
847 # Ignore forks that do not exist or are symbolic links
848 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
850 # Or do not have a non-zero length alternates file
851 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
853 # Match objects in parent project
854 for d
in objects
/$octet; do
855 [ "$d" != "objects/$octet" ] ||
continue
856 mkdir
-p "$cfg_reporoot/$fork.git/$d"
857 find "$d" -maxdepth 1 -type f
-name "$octet19" -print0 |
858 xargs -0 "$var_sh_bin" -c 'ln -f "$@" '"'$cfg_reporoot/$fork.git/$d/'" sh ||
:
860 # Update the fork's lastparentgc date (must be current, not $gcstart)
861 git
--git-dir="$cfg_reporoot/$fork.git" config \
862 gitweb.lastparentgc
"$(date "$datefmt")"
866 # The git prune command does not take a -q or --quiet but started outputting
867 # 'Checking connectivity' progress messages in v1.7.9. However, we can
868 # suppress those by piping through cat as it only activates the progress
869 # messages when stderr is a tty. We only expire loose objects older than one
870 # day just in case there's some pending action (such as sending out a ref
871 # update) in progress that might want to examine them. This may leave us with
872 # loose objects. That's okay because at the next gc interval, we will always
873 # run gc if we see any loose objects regardless of whether or not we've seen
874 # any updates or we've received new linked objects from our parent. Note that
875 # in order to keep loose objects that just recently became unreferenced but
876 # have a very old modification date around we rely on some help from both the
877 # update.sh and hooks/pre-receive scripts. Furthermore, since Git v2.2.0
878 # (d3038d22 prune: keep objects reachable from recent objects) an unreachable
879 # object that would otherwise be pruned (because it's too old) will be kept
880 # alive by an unreachable object that refers to it that's not old enough to
882 prunecmd
='git prune --expire 1_day_ago'
883 [ -n "$show_progress" ] || \
884 prunecmd
="{ $prunecmd 2>&1 || touch .gc_failed; } | cat"
885 progress
"~ [$proj] pruning expired unreachable loose objects"
887 [ ! -e .gc_failed
] ||
exit 1
889 # darcs:// mirrors have a xxx.log file that will grow endlessly
890 # if this is a mirror and the file exists, shorten it to 10000 lines
891 # also take this opportunity to optimize the darcs repo
892 if [ ! -e .nofetch
] && [ -n "$cfg_mirror" ]; then
893 url
="$(config_get baseurl || :)"
894 case "$url" in darcs
://*)
895 if [ -n "$cfg_mirror_darcs" ]; then
897 basedarcs
="$(basename "${url#darcs:/}")"
898 if [ -f "$basedarcs.log" ]; then
899 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
900 mv -f "$basedarcs.log.$$" "$basedarcs.log"
902 if [ -d "$basedarcs.darcs" ]; then
904 cd "$basedarcs.darcs"
905 # without show_progress suppress non-error output
906 [ -n "$show_progress" ] ||
exec >/dev
/null
907 # Note that this does not optimize _darcs/inventories/ :(
915 # Create a matching .bndl header file for the all-in-one pack we just created
916 # but only if we're not a fork (otherwise the bundle would not be complete)
917 # and we are running at least Git version 1.7.2 (pack_is_complete always fails otherwise)
918 if [ ! -s objects
/info
/alternates
] && [ -n "$var_have_git_172" ]; then
919 # There should only be one pack in $allpacks but if there was a
920 # simultaneous push...
921 # The one we just created will have a .idx and will NOT have a .keep
922 progress
"~ [$proj] creating downloadble bundle header"
925 for pk
in $allpacks; do
926 [ -s "$pk" ] ||
continue
928 [ -s "$pkbase.idx" ] ||
continue
929 [ ! -e "$pkbase.keep" ] ||
continue
930 if pkhead
="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs
" "$curhead")"; then
935 if [ -n "$pkfound" -a -n "$pkhead" ]; then
937 echo "# v2 git bundle"
938 LC_ALL
=C
sed -ne "/^$octet20 refs\/[^ $tab]*\$/ p" < packed-refs
942 bndletag
="$("$cfg_basedir/bin
/rangecgi
" --etag -m 1 "$pkbase.bndl
" "$pkbase.pack
" || :)"
943 bndlsha
="$(printf '%s' "$bndletag" | git hash-object --stdin || :)"
944 if [ -n "$bndletag" ]; then
945 case "$bndlsha" in $octet20)
946 bndlshatrailer
="${bndlsha#????????}"
947 bndlshaprefix
="${bndlsha%$bndlshatrailer}"
948 bndlname
="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
949 [ -d bundles
] || mkdir bundles
950 echo "${pkbase#objects/pack/}.bndl" > "bundles/$bndlname"
951 echo "${pkbase#objects/pack/}.pack" >> "bundles/$bndlname"
952 ln -s -f -n "$bndlname" bundles
/latest
958 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
959 config_set_raw girocco.reposizek
"${reposizek:-0}"
961 # We use $gcstart here to avoid a race where a push occurs during the gc itself
962 # and the next future gc could be incorrectly skipped if we used the current
963 # timestamp here instead
964 config_set lastgc
"$gcstart"
967 progress
"- [$proj] garbage check (`date`)"