gc.sh: also remove stale *.temp files under objects/
[girocco.git] / jobd / gc.sh
blob281afe62084fc290afa7e51c07c81054de5e2200
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 *.temp files in the objects area that are more than 12 hours old.
243 # This can be stale sha1.temp, or stale *.pack.temp so we kill all stale *.temp.
244 find objects -type f -mmin +720 -name "*.temp" -print | \
245 while read objtmp; do
246 rm -f "$objtmp"
247 done
249 # Remove any stale git-svn temp files that are more than 12 hours old.
250 # The git-svn process creates temp files with random 10 character names
251 # in the root of $GIT_DIR. Unfortunately they do not have a recognizable
252 # prefix, so we just have to kill any files with a 10-character name. We
253 # do this only for git-svn mirrors. All characters are chosen from
254 # [A-Za-z0-9_] so we can at least check that and fortunately the only
255 # collision is 'FETCH_HEAD' but that shouldn't matter.
256 # There may also be temp files with a Git_ prefix as well.
257 if is_svn_mirror; then
258 _randchar='[A-Za-z0-9_]'
259 _randchar2="$_randchar$_randchar"
260 _randchar4="$_randchar2$_randchar2"
261 _randchar10="$_randchar4$_randchar4$_randchar2"
262 find . -maxdepth 1 -type f -mmin +720 -name "$_randchar10" -print | \
263 while read tmpcrud; do
264 rm -f "$tmpcrud"
265 done
266 find . -maxdepth 1 -type f -mmin +720 -name "Git_*" -print | \
267 while read tmpcrud; do
268 rm -f "$tmpcrud"
269 done
272 # Remove any stale fast_import_crash_<pid> files that are more than 3 days old.
273 if is_gfi_mirror; then
274 find . -maxdepth 1 -type f -mmin +4320 -name "fast_import_crash_?*" -print | \
275 while read fastcrash; do
276 rm -f "$fastcrash"
277 done
280 # Do not skip gc if the repo is dirty
281 if [ -n "$skipgc" ] && ! is_dirty; then
282 progress "= [$proj] garbage check nothing but crud removal to do (`date`)"
283 config_set lastgc "$gcstart"
284 rm -f "$lockf"
285 exit 0
288 bumptime=
289 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
290 # set lastparentgc and then update gcstart to be at least 1 second later
291 config_set lastparentgc "$gcstart"
292 bumptime=1
294 if [ -z "$lastreceivesecs" ]; then
295 # set lastreceive and then update gcstart to be at least 1 second later
296 config_set lastreceive "$gcstart"
297 bumptime=1
299 if [ -n "$bumptime" ]; then
300 sleep 1
301 gcstart="$(date "$datefmt")"
304 progress "+ [$proj] garbage check (`date`)"
306 # safe pruning: we put all our objects in all forks, then we can
307 # safely get rid of extra ones; repacks in forks will get rid of
308 # the redundant ones again then
309 forkdir="$1"
310 if [ -d "../${forkdir##*/}" ]; then
311 # It is enough to copy objects just one level down and get_repo_list
312 # takes a regular expression (which is automatically prefixed with '^')
313 # so we can easily match forks exactly one level down from this project
314 get_repo_list "$forkdir/[^/]*:" |
315 while read fork; do
316 # Ignore forks that do not exist or are symbolic links
317 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
318 continue
319 # Or do not have a non-zero length alternates file
320 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
321 continue
322 # Match objects in parent project
323 for d in objects/?? objects/pack; do
324 [ "$d" != "objects/??" ] || continue
325 mkdir -p "$cfg_reporoot/$fork.git/$d"
326 [ "$d" != "objects/pack" ] || \
327 [ "$(echo objects/pack/*)" != \
328 "objects/pack/*" ] || \
329 continue
330 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
331 done
332 # Update the fork's lastparentgc date (must be current, not $gcstart)
333 GIT_DIR="$cfg_reporoot/$fork.git" git config \
334 gitweb.lastparentgc "$(date "$datefmt")"
335 done
338 quiet=; [ -n "$show_progress" ] || quiet=-q
340 git pack-refs --all
341 repack_gfi_packs
342 rm -f bundles/*
343 rm -f objects/pack/pack-*.bndl
344 git repack $packopts -a -d -l $quiet
345 allpacks="$(echo objects/pack/pack-$octet20.pack)"
346 curhead="$(cat HEAD)"
347 pkrf=
348 [ ! -e packed-refs ] || pkrf=packed-refs
349 eval "reposizek=$(( $(echo 0 $(du -k $pkrf $allpacks 2>/dev/null | awk '{print $1}') | \
350 sed -e 's/ / + /g') ))"
351 git prune
352 git update-server-info
354 # darcs:// mirrors have a xxx.log file that will grow endlessly
355 # if this is a mirror and the file exists, shorten it to 10000 lines
356 # also take this opportunity to optimize the darcs repo
357 if [ ! -e .nofetch ] && [ -n "$cfg_mirror" ]; then
358 url="$(config_get baseurl || :)"
359 case "$url" in darcs://*)
360 if [ -n "$cfg_mirror_darcs" ]; then
361 url="${url%/}"
362 basedarcs="$(basename "${url#darcs:/}")"
363 if [ -f "$basedarcs.log" ]; then
364 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
365 mv -f "$basedarcs.log.$$" "$basedarcs.log"
367 if [ -d "$basedarcs.darcs" ]; then
369 cd "$basedarcs.darcs"
370 # Note that this does not optimize _darcs/inventories/ :(
371 darcs optimize
375 esac
378 # Create a matching .bndl header file for the all-in-one pack we just created
379 # but only if we're not a fork (otherwise the bundle would not be complete)
380 if [ ! -s objects/info/alternates ]; then
381 # There should only be one pack in $allpacks but if there was a
382 # simultaneous push...
383 # The one we just created will have a .idx and will NOT have a .keep
384 pkfound=
385 pkhead=
386 for pk in $allpacks; do
387 [ -s "$pk" ] || continue
388 pkbase="${pk%.pack}"
389 [ -s "$pkbase.idx" ] || continue
390 [ ! -e "$pkbase.keep" ] || continue
391 if pkhead="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs" "$curhead")"; then
392 pkfound="$pkbase"
393 break;
395 done
396 if [ -n "$pkfound" -a -n "$pkhead" ]; then
398 echo "# v2 git bundle"
399 sed -ne "/^$octet20 refs\/[^ ]*\$/ p" < packed-refs
400 echo "$pkhead HEAD"
401 echo ""
402 } > "$pkbase.bndl"
403 bndlsha="${pkbase#objects/pack/pack-}"
404 bndlshatrailer="${bndlsha#????????}"
405 bndlshaprefix="${bndlsha%$bndlshatrailer}"
406 bndlname="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
407 [ -d bundles ] || mkdir bundles
408 echo "$bndlsha" > "bundles/$bndlname"
412 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
413 config_set_raw girocco.reposizek "${reposizek:-0}"
415 # We use $gcstart here to avoid a race where a push occurs during the gc itself
416 # and the next future gc could be incorrectly skipped if we used the current
417 # timestamp here instead
418 config_set lastgc "$gcstart"
419 rm -f "$lockf"
421 progress "- [$proj] garbage check (`date`)"