gc.sh: allow extra arguments to be passed to git-repack
[girocco.git] / jobd / gc.sh
blob4a7e9f285f98d970700bf1e10c43df03fe4ce6c7
1 #!/bin/sh
3 # NOTE: additional options can be passed to git repack by specifying
4 # them after the project name, for example:
5 # gc.sh my-project -f
7 . @basedir@/shlib.sh
9 set -e
10 trap 'if [ $? != 0 ]; then echo "gc failed dir: $PWD" >&2; fi' EXIT
12 # packing options
13 packopts='--window=50 --window-memory=1g --depth=50'
15 umask 002
16 [ "$cfg_permission_control" != "Hooks" ] || umask 000
18 pidactive() {
19 if _result="$(kill -0 "$1" 2>&1)"; then
20 # process exists and we have permission to signal it
21 return 0
23 case "$_result" in *"not permitted"*)
24 # we do not have permission to signal the process
25 return 0
26 esac
27 # process does not exist
28 return 1
31 createlock() {
32 # A .lock file should only exist for much less than a second.
33 # If we see a stale lock file (> 1h old), remove it and then,
34 # just in case, wait 30 seconds for any process whose .lock
35 # we might have just removed (it's racy) to finish doing what
36 # should take much less than a second to do.
37 _stalelock="$(find "$1.lock" -maxdepth 1 -mmin +60 -print 2>/dev/null || :)"
38 if [ -n "$_stalelock" ]; then
39 rm -f "$_stalelock"
40 sleep 30
42 for _try in p p n; do
43 if (set -C; > "$1.lock") 2>/dev/null; then
44 echo "$1.lock"
45 return 0
47 # delay and try again
48 [ "$_try" != "p" ] || sleep 1
49 done
50 # cannot create lock file
51 return 1
54 # return true if there's more than one objects/pack-<sha>.pack file or
55 # ANY sha-1 files in objects
56 is_dirty() {
57 _packs=$(find objects/pack -type f -name "pack-$octet20.pack" -print | wc -l)
58 if [ $_packs != 1 ] && [ $_packs != 0 ]; then
59 return 0
61 _objs=$(find objects/$octet -type f -name "$octet19" -print 2>/dev/null | wc -l)
62 [ $_objs -ne 0 ]
65 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
66 repack_gfi_packs() {
67 is_gfi_mirror || return 0
68 [ -s gfi-packs ] || return 0
69 while IFS=': ' read -r _pack _junk; do
70 if [ -s "$_pack" -a -s "${_pack%.pack}.idx" ]; then
71 git show-index < "${_pack%.pack}.idx" | cut -d ' ' -f 2
73 done < gfi-packs | \
74 git pack-objects $packopts --no-reuse-delta --delta-base-offset \
75 --non-empty --threads=1 $quiet objects/pack/packtmp | \
76 while read -r _newpack; do
77 rm -f objects/pack/pack-$_newpack.*
78 ln -f objects/pack/packtmp-$_newpack.pack objects/pack/pack-$_newpack.pack
79 ln -f objects/pack/packtmp-$_newpack.idx objects/pack/pack-$_newpack.idx
80 rm -f objects/pack/packtmp-$_newpack.*
81 done
82 rm -f gfi-packs
85 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
86 pack_is_complete() {
87 # Must have a matching .idx file and a non-empty packed-refs file
88 [ -s "${1%.pack}.idx" ] || return 1
89 [ -s "$2" ] || return 1
90 _headsha=
91 case "$3" in
92 $octet20)
93 _headsha="$3"
95 "ref: refs/"?*|"ref:refs/"?*|"refs/"?*)
96 _headmatch="${3#ref:}"
97 _headmatch="${_headmatch# }"
98 _headmatchpat="$(echo "$_headmatch" | sed -e 's/\([.$]\)/\\\1/g')"
99 _headsha="$(grep -e "^$octet20 $_headmatchpat\$" < "$2" | \
100 cut -d ' ' -f 1)"
101 case "$_headsha" in $octet20) :;; *)
102 return 1
103 esac
106 # bad HEAD
107 return 1
108 esac
109 rm -rf pack_is_complete_test
110 mkdir pack_is_complete_test
111 mkdir pack_is_complete_test/refs
112 mkdir pack_is_complete_test/objects
113 mkdir pack_is_complete_test/objects/pack
114 echo "$_headsha" > pack_is_complete_test/HEAD
115 ln -s "$1" pack_is_complete_test/objects/pack/
116 ln -s "${1%.pack}.idx" pack_is_complete_test/objects/pack/
117 ln -s "$2" pack_is_complete_test/packed-refs
118 _count="$(git --git-dir=pack_is_complete_test rev-list --count --all 2>/dev/null || :)"
119 rm -rf pack_is_complete_test
120 [ -n "$_count" ] || return 1
121 [ "$_count" -gt 0 ] 2>/dev/null || return 1
122 echo "$_headsha"
125 proj="$1"
126 shift
127 cd "$cfg_reporoot/$proj.git"
129 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
130 datefmt='+%a, %d %b %Y %T %z'
132 if check_interval lastgc $cfg_min_gc_interval; then
133 progress "= [$proj] garbage check skip (last at $(config_get lastgc))"
134 exit 0
136 if [ -e .nogc ]; then
137 progress "x [$proj] garbage check disabled"
138 exit 0
141 # Avoid unnecessary garbage collections:
142 # 1. If lastreceive is set and is older than lastgc
143 # -AND-
144 # 2. We are not a fork (! -s alternates) -OR- lastparentgc is older than lastgc
146 # If lastgc is NOT set or lastreceive is NOT set we MUST run gc
147 # If we are a fork and lastparentgc is NOT set we MUST run gc
149 # If the repo is dirty after removing any crud we MUST run gc
151 gcstart="$(date "$datefmt")"
152 skipgc=
153 isfork=
154 [ -s objects/info/alternates ] && isfork=1
155 lastparentgcsecs=
156 [ -n "$isfork" ] && lastparentgcsecs="$(config_get_date_seconds lastparentgc || :)"
157 lastreceivesecs=
158 if lastreceivesecs="$(config_get_date_seconds lastreceive)" && \
159 lastgcsecs="$(config_get_date_seconds lastgc)" && \
160 [ $lastreceivesecs -lt $lastgcsecs ]; then
161 # We've run gc since we last received, so maybe we can skip,
162 # check if not fork or fork and lastparentgc < lastgc
163 if [ -n "$isfork" ]; then
164 if [ -n "$lastparentgcsecs" ] && \
165 [ $lastparentgcsecs -lt $lastgcsecs ]; then
166 # We've run gc since our parent ran gc so we can skip
167 skipgc=1
169 else
170 # We don't have any alternates (we're not a forK) so we can skip
171 skipgc=1
175 # be compatibile with gc.pid file from newer Git releases
177 hn="$(hostname)"
178 lockf=gc.pid
179 active=
180 if [ "$(createlock "$lockf")" ]; then
181 # If $lockf is:
182 # 1) less than 12 hours old
183 # 2) contains two fields (pid hostname) NO trailing NL
184 # 3) the hostname is different OR the pid is still alive
185 # then we exit as another active process is holding the lock
186 if [ "$(find "$lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then
187 apid=
188 ahost=
189 read -r apid ahost ajunk < "$lockf" || :
190 if [ "$apid" ] && [ "$ahost" ]; then
191 if [ "$ahost" != "$hn" ] || pidactive "$apid"; then
192 active=1
196 else
197 echo >&2 "[$proj] unable to create gc.pid.lock file"
198 exit 1
200 if [ -n "$active" ]; then
201 rm -f "$lockf.lock"
202 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
203 exit 1
205 printf "%s %s" "$$" "$hn" > "$lockf.lock"
206 chmod 0664 "$lockf.lock"
207 mv -f "$lockf.lock" "$lockf"
209 # Remove any stale pack remnants that are more than an hour old.
210 # Stale pack fragments are defined as any pack-<sha1>.ext where .ext is NOT
211 # .pack AND the corresponding .pack DOES NOT exist. A bunch of stale
212 # pack-<sha1>.idx files without their corresponding .pack files are worthless
213 # and just waste space. Normally there shouldn't be any remnants but actually
214 # this can happen when things are interrupted at just the wrong time.
215 # Note that the objects/pack directory is created by git init and should
216 # always exist.
217 find objects/pack -maxdepth 1 -type f -mmin +60 -name "pack-$octet20.?*" -print | \
218 sed -e 's/^objects\/pack\/pack-//; s/\..*$//' | LC_ALL=C sort -u | \
219 while read packsha; do
220 [ ! -e "objects/pack/pack-$packsha.pack" ] || continue
221 rm -f "objects/pack/pack-$packsha".?*
222 done
224 # Remove any stale pack .keep files that are more than 12 hours old.
225 # We don't do anything to create any permanent pack .keep files, so they must
226 # be remnants from some failed push or something. Removing the .keep will
227 # allow the pack to be properly repacked.
228 find objects/pack -maxdepth 1 -type f -mmin +720 -name "pack-$octet20.keep" -print | \
229 while read packkeep; do
230 rm -f "$packkeep"
231 done
233 # Remove any stale tmp_pack_* or tmp_idx_* or packtmp-* files that are more than 12 hours old.
234 find objects/pack -maxdepth 1 -type f -mmin +720 -name "tmp_pack_?*" -print | \
235 while read packtmp; do
236 rm -f "$packtmp"
237 done
238 find objects/pack -maxdepth 1 -type f -mmin +720 -name "tmp_idx_?*" -print | \
239 while read packtmp; do
240 rm -f "$packtmp"
241 done
242 find objects/pack -maxdepth 1 -type f -mmin +720 -name "packtmp-?*" -print | \
243 while read packtmp; do
244 rm -f "$packtmp"
245 done
247 # Remove any stale *.temp files in the objects area that are more than 12 hours old.
248 # This can be stale sha1.temp, or stale *.pack.temp so we kill all stale *.temp.
249 find objects -type f -mmin +720 -name "*.temp" -print | \
250 while read objtmp; do
251 rm -f "$objtmp"
252 done
254 # Remove any stale git-svn temp files that are more than 12 hours old.
255 # The git-svn process creates temp files with random 10 character names
256 # in the root of $GIT_DIR. Unfortunately they do not have a recognizable
257 # prefix, so we just have to kill any files with a 10-character name. We
258 # do this only for git-svn mirrors. All characters are chosen from
259 # [A-Za-z0-9_] so we can at least check that and fortunately the only
260 # collision is 'FETCH_HEAD' but that shouldn't matter.
261 # There may also be temp files with a Git_ prefix as well.
262 if is_svn_mirror; then
263 _randchar='[A-Za-z0-9_]'
264 _randchar2="$_randchar$_randchar"
265 _randchar4="$_randchar2$_randchar2"
266 _randchar10="$_randchar4$_randchar4$_randchar2"
267 find . -maxdepth 1 -type f -mmin +720 -name "$_randchar10" -print | \
268 while read tmpcrud; do
269 rm -f "$tmpcrud"
270 done
271 find . -maxdepth 1 -type f -mmin +720 -name "Git_*" -print | \
272 while read tmpcrud; do
273 rm -f "$tmpcrud"
274 done
277 # Remove any stale fast_import_crash_<pid> files that are more than 3 days old.
278 if is_gfi_mirror; then
279 find . -maxdepth 1 -type f -mmin +4320 -name "fast_import_crash_?*" -print | \
280 while read fastcrash; do
281 rm -f "$fastcrash"
282 done
285 # Do not skip gc if the repo is dirty
286 if [ -n "$skipgc" ] && ! is_dirty; then
287 progress "= [$proj] garbage check nothing but crud removal to do (`date`)"
288 config_set lastgc "$gcstart"
289 rm -f "$lockf"
290 exit 0
293 bumptime=
294 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
295 # set lastparentgc and then update gcstart to be at least 1 second later
296 config_set lastparentgc "$gcstart"
297 bumptime=1
299 if [ -z "$lastreceivesecs" ]; then
300 # set lastreceive and then update gcstart to be at least 1 second later
301 config_set lastreceive "$gcstart"
302 bumptime=1
304 if [ -n "$bumptime" ]; then
305 sleep 1
306 gcstart="$(date "$datefmt")"
309 progress "+ [$proj] garbage check (`date`)"
311 # safe pruning: we put all our objects in all forks, then we can
312 # safely get rid of extra ones; repacks in forks will get rid of
313 # the redundant ones again then
314 forkdir="$proj"
315 if [ -d "../${forkdir##*/}" ]; then
316 # It is enough to copy objects just one level down and get_repo_list
317 # takes a regular expression (which is automatically prefixed with '^')
318 # so we can easily match forks exactly one level down from this project
319 get_repo_list "$forkdir/[^/]*:" |
320 while read fork; do
321 # Ignore forks that do not exist or are symbolic links
322 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
323 continue
324 # Or do not have a non-zero length alternates file
325 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
326 continue
327 # Match objects in parent project
328 for d in objects/?? objects/pack; do
329 [ "$d" != "objects/??" ] || continue
330 mkdir -p "$cfg_reporoot/$fork.git/$d"
331 [ "$d" != "objects/pack" ] || \
332 [ "$(echo objects/pack/*)" != \
333 "objects/pack/*" ] || \
334 continue
335 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
336 done
337 # Update the fork's lastparentgc date (must be current, not $gcstart)
338 GIT_DIR="$cfg_reporoot/$fork.git" git config \
339 gitweb.lastparentgc "$(date "$datefmt")"
340 done
343 quiet=; [ -n "$show_progress" ] || quiet=-q
345 git pack-refs --all
346 repack_gfi_packs
347 rm -f bundles/*
348 rm -f objects/pack/pack-*.bndl
349 git repack $packopts -a -d -l $quiet "$@"
350 allpacks="$(echo objects/pack/pack-$octet20.pack)"
351 curhead="$(cat HEAD)"
352 pkrf=
353 [ ! -e packed-refs ] || pkrf=packed-refs
354 eval "reposizek=$(( $(echo 0 $(du -k $pkrf $allpacks 2>/dev/null | awk '{print $1}') | \
355 sed -e 's/ / + /g') ))"
356 git prune
357 git update-server-info
359 # darcs:// mirrors have a xxx.log file that will grow endlessly
360 # if this is a mirror and the file exists, shorten it to 10000 lines
361 # also take this opportunity to optimize the darcs repo
362 if [ ! -e .nofetch ] && [ -n "$cfg_mirror" ]; then
363 url="$(config_get baseurl || :)"
364 case "$url" in darcs://*)
365 if [ -n "$cfg_mirror_darcs" ]; then
366 url="${url%/}"
367 basedarcs="$(basename "${url#darcs:/}")"
368 if [ -f "$basedarcs.log" ]; then
369 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
370 mv -f "$basedarcs.log.$$" "$basedarcs.log"
372 if [ -d "$basedarcs.darcs" ]; then
374 cd "$basedarcs.darcs"
375 # Note that this does not optimize _darcs/inventories/ :(
376 darcs optimize
380 esac
383 # Create a matching .bndl header file for the all-in-one pack we just created
384 # but only if we're not a fork (otherwise the bundle would not be complete)
385 if [ ! -s objects/info/alternates ]; then
386 # There should only be one pack in $allpacks but if there was a
387 # simultaneous push...
388 # The one we just created will have a .idx and will NOT have a .keep
389 pkfound=
390 pkhead=
391 for pk in $allpacks; do
392 [ -s "$pk" ] || continue
393 pkbase="${pk%.pack}"
394 [ -s "$pkbase.idx" ] || continue
395 [ ! -e "$pkbase.keep" ] || continue
396 if pkhead="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs" "$curhead")"; then
397 pkfound="$pkbase"
398 break;
400 done
401 if [ -n "$pkfound" -a -n "$pkhead" ]; then
403 echo "# v2 git bundle"
404 sed -ne "/^$octet20 refs\/[^ ]*\$/ p" < packed-refs
405 echo "$pkhead HEAD"
406 echo ""
407 } > "$pkbase.bndl"
408 bndlsha="${pkbase#objects/pack/pack-}"
409 bndlshatrailer="${bndlsha#????????}"
410 bndlshaprefix="${bndlsha%$bndlshatrailer}"
411 bndlname="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
412 [ -d bundles ] || mkdir bundles
413 echo "$bndlsha" > "bundles/$bndlname"
417 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
418 config_set_raw girocco.reposizek "${reposizek:-0}"
420 # We use $gcstart here to avoid a race where a push occurs during the gc itself
421 # and the next future gc could be incorrectly skipped if we used the current
422 # timestamp here instead
423 config_set lastgc "$gcstart"
424 rm -f "$lockf"
426 progress "- [$proj] garbage check (`date`)"