gc.sh: also remove stale tmp_idx_* files
[girocco.git] / jobd / gc.sh
blob47b4071323d6af29e87884acb25b29546d114bc8
1 #!/bin/sh
3 . @basedir@/shlib.sh
5 set -e
6 trap 'if [ $? != 0 ]; then echo "gc failed dir: $PWD" >&2; fi' EXIT
8 # packing options
9 packopts='--window=50 --window-memory=1g --depth=50'
11 umask 002
12 [ "$cfg_permission_control" != "Hooks" ] || umask 000
14 pidactive() {
15 if _result="$(kill -0 "$1" 2>&1)"; then
16 # process exists and we have permission to signal it
17 return 0
19 case "$_result" in *"not permitted"*)
20 # we do not have permission to signal the process
21 return 0
22 esac
23 # process does not exist
24 return 1
27 createlock() {
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
35 rm -f "$_stalelock"
36 sleep 30
38 for _try in p p n; do
39 if (set -C; > "$1.lock") 2>/dev/null; then
40 echo "$1.lock"
41 return 0
43 # delay and try again
44 [ "$_try" != "p" ] || sleep 1
45 done
46 # cannot create lock file
47 return 1
50 # return true if there's more than one objects/pack-<sha>.pack file or
51 # ANY sha-1 files in objects
52 is_dirty() {
53 _packs=$(find objects/pack -type f -name "pack-$octet20.pack" -print | wc -l)
54 if [ $_packs != 1 ] && [ $_packs != 0 ]; then
55 return 0
57 _objs=$(find objects/$octet -type f -name "$octet19" -print 2>/dev/null | wc -l)
58 [ $_objs -ne 0 ]
61 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
62 repack_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
69 done < gfi-packs | \
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.*
77 done
78 rm -f gfi-packs
81 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
82 pack_is_complete() {
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
86 _headsha=
87 case "$3" in
88 $octet20)
89 _headsha="$3"
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" | \
96 cut -d ' ' -f 1)"
97 case "$_headsha" in $octet20) :;; *)
98 return 1
99 esac
102 # bad HEAD
103 return 1
104 esac
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
118 echo "$_headsha"
121 proj="$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))"
129 exit 0
131 if [ -e .nogc ]; then
132 progress "x [$proj] garbage check disabled"
133 exit 0
136 # Avoid unnecessary garbage collections:
137 # 1. If lastreceive is set and is older than lastgc
138 # -AND-
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")"
147 skipgc=
148 isfork=
149 [ -s objects/info/alternates ] && isfork=1
150 lastparentgcsecs=
151 [ -n "$isfork" ] && lastparentgcsecs="$(config_get_date_seconds lastparentgc || :)"
152 lastreceivesecs=
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
162 skipgc=1
164 else
165 # We don't have any alternates (we're not a forK) so we can skip
166 skipgc=1
170 # be compatibile with gc.pid file from newer Git releases
172 hn="$(hostname)"
173 lockf=gc.pid
174 active=
175 if [ "$(createlock "$lockf")" ]; then
176 # If $lockf is:
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
182 apid=
183 ahost=
184 read -r apid ahost ajunk < "$lockf" || :
185 if [ "$apid" ] && [ "$ahost" ]; then
186 if [ "$ahost" != "$hn" ] || pidactive "$apid"; then
187 active=1
191 else
192 echo >&2 "[$proj] unable to create gc.pid.lock file"
193 exit 1
195 if [ -n "$active" ]; then
196 rm -f "$lockf.lock"
197 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
198 exit 1
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
211 # always exist.
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".?*
217 done
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
225 rm -f "$packkeep"
226 done
228 # Remove any stale tmp_pack_* or tmp_idx_* 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
231 rm -f "$packtmp"
232 done
233 find objects/pack -maxdepth 1 -type f -mmin +720 -name "tmp_idx_?*" -print | \
234 while read packtmp; do
235 rm -f "$packtmp"
236 done
237 find objects/pack -maxdepth 1 -type f -mmin +720 -name "packtmp-?*" -print | \
238 while read packtmp; do
239 rm -f "$packtmp"
240 done
242 # Remove any stale git-svn temp files that are more than 12 hours old.
243 # The git-svn process creates temp files with random 10 character names
244 # in the root of $GIT_DIR. Unfortunately they do not have a recognizable
245 # prefix, so we just have to kill any files with a 10-character name. We
246 # do this only for git-svn mirrors. All characters are chosen from
247 # [A-Za-z0-9_] so we can at least check that and fortunately the only
248 # collision is 'FETCH_HEAD' but that shouldn't matter.
249 # There may also be temp files with a Git_ prefix as well.
250 if is_svn_mirror; then
251 _randchar='[A-Za-z0-9_]'
252 _randchar2="$_randchar$_randchar"
253 _randchar4="$_randchar2$_randchar2"
254 _randchar10="$_randchar4$_randchar4$_randchar2"
255 find . -maxdepth 1 -type f -mmin +720 -name "$_randchar10" -print | \
256 while read tmpcrud; do
257 rm -f "$tmpcrud"
258 done
259 find . -maxdepth 1 -type f -mmin +720 -name "Git_*" -print | \
260 while read tmpcrud; do
261 rm -f "$tmpcrud"
262 done
265 # Remove any stale fast_import_crash_<pid> files that are more than 3 days old.
266 if is_gfi_mirror; then
267 find . -maxdepth 1 -type f -mmin +4320 -name "fast_import_crash_?*" -print | \
268 while read fastcrash; do
269 rm -f "$fastcrash"
270 done
273 # Do not skip gc if the repo is dirty
274 if [ -n "$skipgc" ] && ! is_dirty; then
275 progress "= [$proj] garbage check nothing but crud removal to do (`date`)"
276 config_set lastgc "$gcstart"
277 rm -f "$lockf"
278 exit 0
281 bumptime=
282 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
283 # set lastparentgc and then update gcstart to be at least 1 second later
284 config_set lastparentgc "$gcstart"
285 bumptime=1
287 if [ -z "$lastreceivesecs" ]; then
288 # set lastreceive and then update gcstart to be at least 1 second later
289 config_set lastreceive "$gcstart"
290 bumptime=1
292 if [ -n "$bumptime" ]; then
293 sleep 1
294 gcstart="$(date "$datefmt")"
297 progress "+ [$proj] garbage check (`date`)"
299 # safe pruning: we put all our objects in all forks, then we can
300 # safely get rid of extra ones; repacks in forks will get rid of
301 # the redundant ones again then
302 forkdir="$1"
303 if [ -d "../${forkdir##*/}" ]; then
304 # It is enough to copy objects just one level down and get_repo_list
305 # takes a regular expression (which is automatically prefixed with '^')
306 # so we can easily match forks exactly one level down from this project
307 get_repo_list "$forkdir/[^/]*:" |
308 while read fork; do
309 # Ignore forks that do not exist or are symbolic links
310 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
311 continue
312 # Or do not have a non-zero length alternates file
313 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
314 continue
315 # Match objects in parent project
316 for d in objects/?? objects/pack; do
317 [ "$d" != "objects/??" ] || continue
318 mkdir -p "$cfg_reporoot/$fork.git/$d"
319 [ "$d" != "objects/pack" ] || \
320 [ "$(echo objects/pack/*)" != \
321 "objects/pack/*" ] || \
322 continue
323 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
324 done
325 # Update the fork's lastparentgc date (must be current, not $gcstart)
326 GIT_DIR="$cfg_reporoot/$fork.git" git config \
327 gitweb.lastparentgc "$(date "$datefmt")"
328 done
331 quiet=; [ -n "$show_progress" ] || quiet=-q
333 git pack-refs --all
334 repack_gfi_packs
335 rm -f bundles/*
336 rm -f objects/pack/pack-*.bndl
337 git repack $packopts -a -d -l $quiet
338 allpacks="$(echo objects/pack/pack-$octet20.pack)"
339 curhead="$(cat HEAD)"
340 pkrf=
341 [ ! -e packed-refs ] || pkrf=packed-refs
342 eval "reposizek=$(( $(echo 0 $(du -k $pkrf $allpacks 2>/dev/null | awk '{print $1}') | \
343 sed -e 's/ / + /g') ))"
344 git prune
345 git update-server-info
347 # darcs:// mirrors have a xxx.log file that will grow endlessly
348 # if this is a mirror and the file exists, shorten it to 10000 lines
349 # also take this opportunity to optimize the darcs repo
350 if [ ! -e .nofetch ] && [ -n "$cfg_mirror" ]; then
351 url="$(config_get baseurl || :)"
352 case "$url" in darcs://*)
353 if [ -n "$cfg_mirror_darcs" ]; then
354 url="${url%/}"
355 basedarcs="$(basename "${url#darcs:/}")"
356 if [ -f "$basedarcs.log" ]; then
357 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
358 mv -f "$basedarcs.log.$$" "$basedarcs.log"
360 if [ -d "$basedarcs.darcs" ]; then
362 cd "$basedarcs.darcs"
363 # Note that this does not optimize _darcs/inventories/ :(
364 darcs optimize
368 esac
371 # Create a matching .bndl header file for the all-in-one pack we just created
372 # but only if we're not a fork (otherwise the bundle would not be complete)
373 if [ ! -s objects/info/alternates ]; then
374 # There should only be one pack in $allpacks but if there was a
375 # simultaneous push...
376 # The one we just created will have a .idx and will NOT have a .keep
377 pkfound=
378 pkhead=
379 for pk in $allpacks; do
380 [ -s "$pk" ] || continue
381 pkbase="${pk%.pack}"
382 [ -s "$pkbase.idx" ] || continue
383 [ ! -e "$pkbase.keep" ] || continue
384 if pkhead="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs" "$curhead")"; then
385 pkfound="$pkbase"
386 break;
388 done
389 if [ -n "$pkfound" -a -n "$pkhead" ]; then
391 echo "# v2 git bundle"
392 sed -ne "/^$octet20 refs\/[^ ]*\$/ p" < packed-refs
393 echo "$pkhead HEAD"
394 echo ""
395 } > "$pkbase.bndl"
396 bndlsha="${pkbase#objects/pack/pack-}"
397 bndlshatrailer="${bndlsha#????????}"
398 bndlshaprefix="${bndlsha%$bndlshatrailer}"
399 bndlname="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
400 [ -d bundles ] || mkdir bundles
401 echo "$bndlsha" > "bundles/$bndlname"
405 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
406 config_set_raw girocco.reposizek "${reposizek:-0}"
408 # We use $gcstart here to avoid a race where a push occurs during the gc itself
409 # and the next future gc could be incorrectly skipped if we used the current
410 # timestamp here instead
411 config_set lastgc "$gcstart"
412 rm -f "$lockf"
414 progress "- [$proj] garbage check (`date`)"