shlib.sh: move octet20 pattern from gc.sh into shlib.sh
[girocco.git] / jobd / gc.sh
blob471b02fdfb8ba46f35efff997a26f66bc4d4d847
1 #!/bin/sh
3 . @basedir@/shlib.sh
5 set -e
6 trap 'if [ $? != 0 ]; then echo "gc failed dir: $PWD" >&2; fi; rm -f "$bang_log"' 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 for _try in p n; do
29 if (set -C; > "$1.lock") 2>/dev/null; then
30 echo "$1.lock"
31 return 0
33 # delay and try again
34 [ "$_try" != "p" ] || sleep 1
35 done
36 # cannot create temp file
37 return 1
40 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
41 repack_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
48 done < gfi-packs | \
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.*
56 done
57 rm -f gfi-packs
60 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
61 pack_is_complete() {
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
65 _headsha=
66 case "$3" in
67 $octet20)
68 _headsha="$3"
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" | \
75 cut -d ' ' -f 1)"
76 case "$_headsha" in $octet20) :;; *)
77 return 1
78 esac
81 # bad HEAD
82 return 1
83 esac
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
97 echo "$_headsha"
100 proj="$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))"
108 exit 0
110 if [ -e .nogc ]; then
111 progress "x [$proj] garbage check disabled"
112 exit 0
115 # Avoid unnecessary garbage collections:
116 # 1. If lastreceive is set and is older than lastgc
117 # -AND-
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")"
124 skipgc=
125 isfork=
126 [ -s objects/info/alternates ] && isfork=1
127 lastparentgcsecs=
128 [ -n "$isfork" ] && lastparentgcsecs="$(config_get_date_seconds lastparentgc || :)"
129 lastreceivesecs=
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
139 skipgc=1
141 else
142 # We don't have any alternates (we're not a forK) so we can skip
143 skipgc=1
147 # be compatibile with gc.pid file from newer Git releases
149 hn="$(hostname)"
150 lockf=gc.pid
151 active=
152 if [ "$(createlock "$lockf")" ]; then
153 # If $lockf is:
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
159 apid=
160 ahost=
161 read -r apid ahost ajunk < "$lockf" || :
162 if [ "$apid" ] && [ "$ahost" ]; then
163 if [ "$ahost" != "$hn" ] || pidactive "$apid"; then
164 active=1
168 else
169 echo >&2 "[$proj] unable to create gc.pid.lock file"
170 exit 1
172 if [ -n "$active" ]; then
173 rm "$lockf.lock"
174 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
175 exit 1
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"
184 rm "$lockf"
185 exit 0
188 bumptime=
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"
192 bumptime=1
194 if [ -z "$lastreceivesecs" ]; then
195 # set lastreceive and then update gcstart to be at least 1 second later
196 config_set lastreceive "$gcstart"
197 bumptime=1
199 if [ -n "$bumptime" ]; then
200 sleep 1
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
213 # always exist.
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".?*
219 done
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
224 forkdir="$1"
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/[^/]*:" |
230 while read fork; do
231 # Ignore forks that do not exist or are symbolic links
232 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
233 continue
234 # Or do not have a non-zero length alternates file
235 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
236 continue
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/*" ] || \
244 continue
245 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
246 done
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")"
250 done
253 quiet=; [ -n "$show_progress" ] || quiet=-q
255 git pack-refs --all
256 repack_gfi_packs
257 rm -f bundles/*
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') ))"
264 git prune
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
274 url="${url%/}"
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/ :(
284 darcs optimize
288 esac
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
297 pkfound=
298 pkhead=
299 for pk in $allpacks; do
300 [ -s "$pk" ] || continue
301 pkbase="${pk%.pack}"
302 [ -s "$pkbase.idx" ] || continue
303 [ ! -e "$pkbase.keep" ] || continue
304 if pkhead="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs" "$curhead")"; then
305 pkfound="$pkbase"
306 break;
308 done
309 if [ -n "$pkfound" -a -n "$pkhead" ]; then
311 echo "# v2 git bundle"
312 sed -ne "/^$octet20 refs\/[^ ]*\$/ p" < packed-refs
313 echo "$pkhead HEAD"
314 echo ""
315 } > "$pkbase.bndl"
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"
332 rm "$lockf"
334 progress "- [$proj] garbage check (`date`)"