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
29 if (set -C; > "$1.lock") 2>/dev
/null
; then
34 [ "$_try" != "p" ] ||
sleep 1
36 # cannot create temp file
40 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
42 is_gfi_mirror ||
return 0
43 [ -s gfi-packs
] ||
return 0
44 while IFS
=': ' read -r _pack _junk
; do
45 if [ -s "$_pack" -a -s "${_pack%.pack}.idx" ]; then
46 git show-index
< "${_pack%.pack}.idx" | cut
-d ' ' -f 2
49 git pack-objects
$packopts --no-reuse-delta --delta-base-offset \
50 --non-empty --threads=1 $quiet objects
/pack
/packtmp | \
51 while read -r _newpack
; do
52 rm -f objects
/pack
/pack-
$_newpack.
*
53 ln -f objects
/pack
/packtmp-
$_newpack.pack objects
/pack
/pack-
$_newpack.pack
54 ln -f objects
/pack
/packtmp-
$_newpack.idx objects
/pack
/pack-
$_newpack.idx
55 rm -f objects
/pack
/packtmp-
$_newpack.
*
60 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
62 # Must have a matching .idx file and a non-empty packed-refs file
63 [ -s "${1%.pack}.idx" ] ||
return 1
64 [ -s "$2" ] ||
return 1
70 "ref: refs/"?
*|
"ref:refs/"?
*|
"refs/"?
*)
71 _headmatch
="${3#ref:}"
72 _headmatch
="${_headmatch# }"
73 _headmatchpat
="$(echo "$_headmatch" | sed -e 's/\([.$]\)/\\\1/g')"
74 _headsha
="$(grep -e "^
$octet20 $_headmatchpat\$
" < "$2" | \
76 case "$_headsha" in $octet20) :;; *)
84 rm -rf pack_is_complete_test
85 mkdir pack_is_complete_test
86 mkdir pack_is_complete_test
/refs
87 mkdir pack_is_complete_test
/objects
88 mkdir pack_is_complete_test
/objects
/pack
89 echo "$_headsha" > pack_is_complete_test
/HEAD
90 ln -s "$1" pack_is_complete_test
/objects
/pack
/
91 ln -s "${1%.pack}.idx" pack_is_complete_test
/objects
/pack
/
92 ln -s "$2" pack_is_complete_test
/packed-refs
93 _count
="$(git --git-dir=pack_is_complete_test rev-list --count --all 2>/dev/null || :)"
94 rm -rf pack_is_complete_test
95 [ -n "$_count" ] ||
return 1
96 [ "$_count" -gt 0 ] 2>/dev
/null ||
return 1
101 cd "$cfg_reporoot/$proj.git"
103 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
104 datefmt
='+%a, %d %b %Y %T %z'
106 if check_interval lastgc
$cfg_min_gc_interval; then
107 progress
"= [$proj] garbage check skip (last at $(config_get lastgc))"
110 if [ -e .nogc
]; then
111 progress
"x [$proj] garbage check disabled"
115 # Avoid unnecessary garbage collections:
116 # 1. If lastreceive is set and is older than lastgc
118 # 2. We are not a fork (! -s alternates) -OR- lastparentgc is older than lastgc
120 # If lastgc is NOT set or lastreceive is NOT set we MUST run gc
121 # If we are a fork and lastparentgc is NOT set we MUST run gc
123 gcstart
="$(date "$datefmt")"
126 [ -s objects
/info
/alternates
] && isfork
=1
128 [ -n "$isfork" ] && lastparentgcsecs
="$(config_get_date_seconds lastparentgc || :)"
130 if lastreceivesecs
="$(config_get_date_seconds lastreceive)" && \
131 lastgcsecs
="$(config_get_date_seconds lastgc)" && \
132 [ $lastreceivesecs -lt $lastgcsecs ]; then
133 # We've run gc since we last received, so maybe we can skip,
134 # check if not fork or fork and lastparentgc < lastgc
135 if [ -n "$isfork" ]; then
136 if [ -n "$lastparentgcsecs" ] && \
137 [ $lastparentgcsecs -lt $lastgcsecs ]; then
138 # We've run gc since our parent ran gc so we can skip
142 # We don't have any alternates (we're not a forK) so we can skip
147 # be compatibile with gc.pid file from newer Git releases
152 if [ "$(createlock "$lockf")" ]; then
154 # 1) less than 12 hours old
155 # 2) contains two fields (pid hostname) NO trailing NL
156 # 3) the hostname is different OR the pid is still alive
157 # then we exit as another active process is holding the lock
158 if [ "$(find "$lockf" -mmin -720 -print 2>/dev/null)" ]; then
161 read -r apid ahost ajunk
< "$lockf" ||
:
162 if [ "$apid" ] && [ "$ahost" ]; then
163 if [ "$ahost" != "$hn" ] || pidactive
"$apid"; then
169 echo >&2 "[$proj] unable to create gc.pid.lock file"
172 if [ -n "$active" ]; then
174 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
177 printf "%s %s" "$$" "$hn" > "$lockf.lock"
178 chmod 0664 "$lockf.lock"
179 mv -f "$lockf.lock" "$lockf"
181 if [ -n "$skipgc" ]; then
182 progress
"= [$proj] garbage check nothing to do (`date`)"
183 config_set lastgc
"$gcstart"
189 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
190 # set lastparentgc and then update gcstart to be at least 1 second later
191 config_set lastparentgc
"$gcstart"
194 if [ -z "$lastreceivesecs" ]; then
195 # set lastreceive and then update gcstart to be at least 1 second later
196 config_set lastreceive
"$gcstart"
199 if [ -n "$bumptime" ]; then
201 gcstart
="$(date "$datefmt")"
204 progress
"+ [$proj] garbage check (`date`)"
206 # Remove any stale pack remnants that are more than an hour old.
207 # Stale pack fragments are defined as any pack-<sha1>.ext where .ext is NOT
208 # .pack AND the corresponding .pack DOES NOT exist. A bunch of stale
209 # pack-<sha1>.idx files without their corresponding .pack files are worthless
210 # and just waste space. Normally there shouldn't be any remnants but actually
211 # this can happen when things are interrupted at just the wrong time.
212 # Note that the objects/pack directory is created by git init and should
214 find objects
/pack
-maxdepth 1 -type f
-mmin +60 -name "pack-$octet20.?*" | \
215 sed -e 's/^objects\/pack\/pack-//; s/\..*$//' |
sort -u | \
216 while read packsha
; do
217 [ ! -e "objects/pack/pack-$packsha.pack" ] ||
continue
218 rm -f "objects/pack/pack-$packsha".?
*
221 # safe pruning: we put all our objects in all forks, then we can
222 # safely get rid of extra ones; repacks in forks will get rid of
223 # the redundant ones again then
225 if [ -d "../${forkdir##*/}" ]; then
226 # It is enough to copy objects just one level down and get_repo_list
227 # takes a regular expression (which is automatically prefixed with '^')
228 # so we can easily match forks exactly one level down from this project
229 get_repo_list
"$forkdir/[^/]*:" |
231 # Ignore forks that do not exist or are symbolic links
232 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
234 # Or do not have a non-zero length alternates file
235 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
237 # Match objects in parent project
238 for d
in objects
/?? objects
/pack
; do
239 [ "$d" != "objects/??" ] ||
continue
240 mkdir
-p "$cfg_reporoot/$fork.git/$d"
241 [ "$d" != "objects/pack" ] || \
242 [ "$(echo objects/pack/*)" != \
243 "objects/pack/*" ] || \
245 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" ||
:
247 # Update the fork's lastparentgc date (must be current, not $gcstart)
248 GIT_DIR
="$cfg_reporoot/$fork.git" git config \
249 gitweb.lastparentgc
"$(date "$datefmt")"
253 quiet
=; [ -n "$show_progress" ] || quiet
=-q
258 rm -f objects
/pack
/pack-
*.bndl
259 git repack
$packopts -a -d -l $quiet
260 allpacks
="$(echo objects/pack/pack-$octet20.pack)"
261 curhead
="$(cat HEAD)"
262 eval "reposizek=$(( $(echo 0 $(du -k $allpacks 2>/dev/null | awk '{print $1}') | \
263 sed -e 's/ / + /g') ))"
265 git update-server-info
267 # darcs:// mirrors have a xxx.log file that will grow endlessly
268 # if this is a mirror and the file exists, shorten it to 10000 lines
269 # also take this opportunity to optimize the darcs repo
270 if [ ! -e .nofetch
] && [ -n "$cfg_mirror" ]; then
271 url
="$(config_get baseurl || :)"
272 case "$url" in darcs
://*)
273 if [ -n "$cfg_mirror_darcs" ]; then
275 basedarcs
="$(basename "${url#darcs:/}")"
276 if [ -f "$basedarcs.log" ]; then
277 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
278 mv -f "$basedarcs.log.$$" "$basedarcs.log"
280 if [ -d "$basedarcs.darcs" ]; then
282 cd "$basedarcs.darcs"
283 # Note that this does not optimize _darcs/inventories/ :(
291 # Create a matching .bndl header file for the all-in-one pack we just created
292 # but only if we're not a fork (otherwise the bundle would not be complete)
293 if [ ! -s objects
/info
/alternates
]; then
294 # There should only be one pack in $allpacks but if there was a
295 # simultaneous push...
296 # The one we just created will have a .idx and will NOT have a .keep
299 for pk
in $allpacks; do
300 [ -s "$pk" ] ||
continue
302 [ -s "$pkbase.idx" ] ||
continue
303 [ ! -e "$pkbase.keep" ] ||
continue
304 if pkhead
="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs
" "$curhead")"; then
309 if [ -n "$pkfound" -a -n "$pkhead" ]; then
311 echo "# v2 git bundle"
312 sed -ne "/^$octet20 refs\/[^ ]*\$/ p" < packed-refs
316 bndlsha
="${pkbase#objects/pack/pack-}"
317 bndlshatrailer
="${bndlsha#????????}"
318 bndlshaprefix
="${bndlsha%$bndlshatrailer}"
319 bndlname
="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
320 [ -d bundles
] || mkdir bundles
321 echo "$bndlsha" > "bundles/$bndlname"
325 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
326 config_set_raw girocco.reposizek
"${reposizek:-0}"
328 # We use $gcstart here to avoid a race where a push occurs during the gc itself
329 # and the next future gc could be incorrectly skipped if we used the current
330 # timestamp here instead
331 config_set lastgc
"$gcstart"
334 progress
"- [$proj] garbage check (`date`)"