gc.sh: combine loose git-svn fetch objects into a pack
[girocco.git] / jobd / gc.sh
blob6381804d9db75f99bbdb30da53267d9ebfc7f543
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
11 if [ $# -lt 1 ]; then
12 echo "Usage: gc.sh projname [extra-repack-args]" >&2
13 exit 1
16 # packing options
17 packopts="--depth=50 --window=50 --window-memory=${var_window_memory:-1g}"
18 quiet=; [ -n "$show_progress" ] || quiet=-q
20 umask 002
21 [ "$cfg_permission_control" != "Hooks" ] || umask 000
23 pidactive() {
24 if _result="$(kill -0 "$1" 2>&1)"; then
25 # process exists and we have permission to signal it
26 return 0
28 case "$_result" in *"not permitted"*)
29 # we do not have permission to signal the process
30 return 0
31 esac
32 # process does not exist
33 return 1
36 createlock() {
37 # A .lock file should only exist for much less than a second.
38 # If we see a stale lock file (> 1h old), remove it and then,
39 # just in case, wait 30 seconds for any process whose .lock
40 # we might have just removed (it's racy) to finish doing what
41 # should take much less than a second to do.
42 _stalelock="$(find "$1.lock" -maxdepth 1 -mmin +60 -print 2>/dev/null || :)"
43 if [ -n "$_stalelock" ]; then
44 rm -f "$_stalelock"
45 sleep 30
47 for _try in p p n; do
48 if (set -C; > "$1.lock") 2>/dev/null; then
49 echo "$1.lock"
50 return 0
52 # delay and try again
53 [ "$_try" != "p" ] || sleep 1
54 done
55 # cannot create lock file
56 return 1
59 # return true if there's more than one objects/pack-<sha>.pack file or
60 # ANY sha-1 files in objects
61 is_dirty() {
62 _packs=$(find objects/pack -type f -name "pack-$octet20.pack" -print | head -n 2 | wc -l)
63 if [ $_packs != 1 ] && [ $_packs != 0 ]; then
64 return 0
66 _objs=$(find objects/$octet -type f -name "$octet19" -print 2>/dev/null | head -n 1 | wc -l)
67 [ $_objs -ne 0 ]
70 # combine the input pack(s) into a new pack (or possibly packs if packSizeLimit set)
71 # input pack names are read from standard input one per line delimited by the first
72 # ':', ' ' or '\n' character on the line (which allows gfi-packs to be read directly)
73 # all arguments, if any, are passed to pack-objects as additional options
74 # returns non-zero on failure AND creates .gc_failed in that case
75 combine_packs() {
76 rm -f .gc_failed
77 find objects/pack -maxdepth 1 -type f -name '*.zap*' -print0 | xargs -0 rm -f
78 @basedir@/jobd/combine-packs.sh --replace "$@" $packopts --all-progress-implied $quiet --non-empty || {
79 >.gc_failed
80 return 1
82 return 0
85 # if the current directory is_gfi_mirror then repack all packs listed in gfi-packs
86 repack_gfi_packs() {
87 [ -f gfi-packs -a -s gfi-packs ] && is_gfi_mirror || return 0
88 [ -d objects/pack ] || { rm -f gfi-packs; return 0; }
89 combine_packs --ignore-missing --no-reuse-delta <gfi-packs
90 rm -f gfi-packs
91 return 0
94 # combine small packs into larger pack(s)
95 # we avoid any keep, bndl or bitmap packs
96 combine_small_packs() {
97 _lpo="--exclude-no-idx --exclude-keep --exclude-bitmap --exclude-bndl --quiet"
98 _lpo="$_lpo --object-limit $var_redelta_threshold objects/pack"
99 while
100 _cnt="$(list_packs --count $_lpo || :)"
101 test "${_cnt:-0}" -ge 2
103 _newp="$(list_packs $_lpo | combine_packs --names --no-reuse-delta)"
104 _newc="$(echo $(echo "$_newp" | wc -w))"
105 # be paranoid and exit the loop if we haven't reduced the number of packs
106 [ $_newc -lt $_cnt ] || break
107 done
108 return 0
111 # Unfortunately git-svn lacks the ability to store newly fetched revisions as a pack.
112 # However, the fetch code conveniently sets .svnpack just before it runs git-svn fetch
113 # so that it's easy to find all the objects that have been fetched by git-svn and
114 # combine them into a pack. The --no-reuse-delta option is meaningless here since
115 # everything to be packed is a loose object and therefore not a delta so deltification
116 # will always take place.
117 make_svn_pack() {
118 [ -f .svnpack ] && is_svn_mirror || return 0
119 rm -f .svnpackgc
120 mv -f .svnpack .svnpackgc
121 _newp="$(find objects/$octet -maxdepth 1 -type f -newer .svnpackgc -name "$octet19" -print 2>/dev/null |
122 awk -F / '{print $2 $3}' |
123 @basedir@/jobd/combine-packs.sh --objects --names $packopts --incremental --all-progress-implied $quiet --non-empty)" || {
124 mv -f .svnpackgc .svnpack
125 >.gc_failed
126 return 1
128 if [ -n "$_newp" ]; then
129 # remove the now-redundant loose objects -- this is always safe
130 # even during a concurrent push because a reprepare_packed_git
131 # will be triggered if an object that should be there is not
132 # found thereby finding it in the new pack instead
133 git prune-packed $quiet
135 rm -f .svnpackgc
138 # HEADSHA="$(pack_is_complete /full/path/to/some.pack /full/path/to/packed-refs "$(cat HEAD)")"
139 pack_is_complete() {
140 # Must have a matching .idx file and a non-empty packed-refs file
141 [ -s "${1%.pack}.idx" ] || return 1
142 [ -s "$2" ] || return 1
143 _headsha=
144 case "$3" in
145 $octet20)
146 _headsha="$3"
148 "ref: refs/"?*|"ref:refs/"?*|"refs/"?*)
149 _headmatch="${3#ref:}"
150 _headmatch="${_headmatch# }"
151 _headmatchpat="$(echo "$_headmatch" | sed -e 's/\([.$]\)/\\\1/g')"
152 _headsha="$(grep -e "^$octet20 $_headmatchpat\$" < "$2" | \
153 cut -d ' ' -f 1)"
154 case "$_headsha" in $octet20) :;; *)
155 return 1
156 esac
159 # bad HEAD
160 return 1
161 esac
162 rm -rf pack_is_complete_test
163 mkdir pack_is_complete_test
164 mkdir pack_is_complete_test/refs
165 mkdir pack_is_complete_test/objects
166 mkdir pack_is_complete_test/objects/pack
167 echo "$_headsha" > pack_is_complete_test/HEAD
168 ln -s "$1" pack_is_complete_test/objects/pack/
169 ln -s "${1%.pack}.idx" pack_is_complete_test/objects/pack/
170 ln -s "$2" pack_is_complete_test/packed-refs
171 _count="$(git --git-dir=pack_is_complete_test rev-list --count --all 2>/dev/null || :)"
172 rm -rf pack_is_complete_test
173 [ -n "$_count" ] || return 1
174 [ "$_count" -gt 0 ] 2>/dev/null || return 1
175 echo "$_headsha"
178 # On return a "$lockf" will have been created that must be removed when gc is done
179 lock_gc() {
180 # be compatibile with gc.pid file from newer Git releases
181 lockf=gc.pid
182 hn="$(hostname)"
183 active=
184 if [ "$(createlock "$lockf")" ]; then
185 # If $lockf is:
186 # 1) less than 12 hours old
187 # 2) contains two fields (pid hostname) NO trailing NL
188 # 3) the hostname is different OR the pid is still alive
189 # then we exit as another active process is holding the lock
190 if [ "$(find "$lockf" -maxdepth 1 -mmin -720 -print 2>/dev/null)" ]; then
191 apid=
192 ahost=
193 read -r apid ahost ajunk < "$lockf" || :
194 if [ "$apid" ] && [ "$ahost" ]; then
195 if [ "$ahost" != "$hn" ] || pidactive "$apid"; then
196 active=1
200 else
201 echo >&2 "[$proj] unable to create gc.pid.lock file"
202 exit 1
204 if [ -n "$active" ]; then
205 rm -f "$lockf.lock"
206 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
207 exit 1
209 printf "%s %s" "$$" "$hn" > "$lockf.lock"
210 chmod 0664 "$lockf.lock"
211 mv -f "$lockf.lock" "$lockf"
214 # Remove any crud that's been left behind by interrupted operations
215 # that did not clean up after themselves
216 remove_crud() {
217 # Remove any existing FETCH_HEAD
218 # There can only be a FETCH_HEAD if we've been fetching, not if we've been
219 # receiving pushes (those never create a FETCH_HEAD).
220 # And if we're fetching because we're a mirror, we know we're not fetching right
221 # now since jobd.pl never runs a project's fetch simultaneously with its gc.
222 # Therefore any existing FETCH_HEAD is junk. And it may be many megabytes if
223 # there were a lot of refs.
224 rm -f FETCH_HEAD
226 # Remove any stale pack remnants that are more than an hour old.
227 # Stale pack fragments are defined as any pack-<sha1>.ext where .ext is NOT
228 # .pack AND the corresponding .pack DOES NOT exist. A bunch of stale
229 # pack-<sha1>.idx files without their corresponding .pack files are worthless
230 # and just waste space. Normally there shouldn't be any remnants but actually
231 # this can happen when things are interrupted at just the wrong time.
232 # Note that the objects/pack directory is created by git init and should
233 # always exist.
234 find objects/pack -maxdepth 1 -type f -mmin +60 -name "pack-$octet20.?*" -print | \
235 sed -e 's/^objects\/pack\/pack-//; s/\..*$//' | LC_ALL=C sort -u | \
236 while read packsha; do
237 [ ! -e "objects/pack/pack-$packsha.pack" ] || continue
238 rm -f "objects/pack/pack-$packsha".?*
239 done
241 # Remove any stale pack .keep files that are more than 12 hours old.
242 # We don't do anything to create any permanent pack .keep files, so they must
243 # be remnants from some failed push or something. Removing the .keep will
244 # allow the pack to be properly repacked.
245 find objects/pack -maxdepth 1 -type f -mmin +720 -name "pack-$octet20.keep" -print0 | xargs -0 rm -f
247 # Remove any stale tmp_pack_*, tmp_idx_*, tmp_bitmap_*, packtmp-* or .tmp-*-pack files
248 # that are more than 12 hours old.
249 find objects/pack -maxdepth 1 -type f -mmin +720 \( \
250 -name "tmp_pack_?*" -o -name "tmp_idx_?*" -o -name "tmp_bitmap_?*" -o \
251 -name "packtmp-?*" -o -name ".tmp-?*-pack" \
252 \) -print0 | xargs -0 rm -f
254 # Remove any stale shallow_* files that are more than 12 hours old.
255 # These can be left behind by Git >= 1.8.4.2 and < 2.0.0 when a client
256 # requests a shallow clone.
257 find . -maxdepth 1 -type f -mmin +720 -name "shallow_?*" -print0 | xargs -0 rm -f
259 # Remove any stale *.temp files in the objects area that are more than 12 hours old.
260 # This can be stale sha1.temp, or stale *.pack.temp so we kill all stale *.temp.
261 find objects -type f -mmin +720 -name "*.temp" -print0 | xargs -0 rm -f
263 # Remove any stale *.lock files in the htmlcache area that might have been left
264 # behind after an abnormal exit during an attempt to update a cached file and
265 # are more than 1 hour old.
266 ! [ -d htmlcache ] || find htmlcache -type f -mmin +60 -name "*.lock" -print0 | xargs -0 rm -f
268 # Remove any stale git-svn temp files that are more than 12 hours old.
269 # The git-svn process creates temp files with random 10 character names
270 # in the root of $GIT_DIR. Unfortunately they do not have a recognizable
271 # prefix, so we just have to kill any files with a 10-character name. We
272 # do this only for git-svn mirrors. All characters are chosen from
273 # [A-Za-z0-9_] so we can at least check that and fortunately the only
274 # collision is 'FETCH_HEAD' but that shouldn't matter.
275 # There may also be temp files with a Git_ prefix as well.
276 if is_svn_mirror; then
277 _randchar='[A-Za-z0-9_]'
278 _randchar2="$_randchar$_randchar"
279 _randchar4="$_randchar2$_randchar2"
280 _randchar10="$_randchar4$_randchar4$_randchar2"
281 find . -maxdepth 1 -type f -mmin +720 -name "$_randchar10" -print0 | xargs -0 rm -f
282 find . -maxdepth 1 -type f -mmin +720 -name "Git_*" -print0 | xargs -0 rm -f
285 # Remove any stale fast_import_crash_<pid> files that are more than 3 days old.
286 if is_gfi_mirror; then
287 find . -maxdepth 1 -type f -mmin +4320 -name "fast_import_crash_?*" -print0 | xargs -0 rm -f
291 proj="${1%.git}"
292 shift
293 cd "$cfg_reporoot/$proj.git"
295 trap 'e=$?; rm -f .gc_in_progress; if [ $e != 0 ]; then echo "gc failed dir: $PWD" >&2; fi' EXIT
296 trap 'exit 130' INT
297 trap 'exit 143' TERM
299 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
300 datefmt='+%a, %d %b %Y %T %z'
302 if check_interval lastgc $cfg_min_gc_interval; then
303 progress "= [$proj] garbage check skip (last at $(config_get lastgc))"
304 exit 0
306 if [ -e .nogc ]; then
307 progress "x [$proj] garbage check disabled"
308 exit 0
311 # Avoid unnecessary garbage collections:
312 # 1. If lastreceive is set and is older than lastgc
313 # -AND-
314 # 2. We are not a fork (! -s alternates) -OR- lastparentgc is older than lastgc
316 # If lastgc is NOT set or lastreceive is NOT set we MUST run gc
317 # If we are a fork and lastparentgc is NOT set we MUST run gc
319 # If the repo is dirty after removing any crud we MUST run gc
321 gcstart="$(date "$datefmt")"
322 skipgc=
323 isfork=
324 [ -s objects/info/alternates ] && isfork=1
325 lastparentgcsecs=
326 [ -n "$isfork" ] && lastparentgcsecs="$(config_get_date_seconds lastparentgc || :)"
327 lastreceivesecs=
328 if lastreceivesecs="$(config_get_date_seconds lastreceive)" && \
329 lastgcsecs="$(config_get_date_seconds lastgc)" && \
330 [ $lastreceivesecs -lt $lastgcsecs ]; then
331 # We've run gc since we last received, so maybe we can skip,
332 # check if not fork or fork and lastparentgc < lastgc
333 if [ -n "$isfork" ]; then
334 if [ -n "$lastparentgcsecs" ] && \
335 [ $lastparentgcsecs -lt $lastgcsecs ]; then
336 # We've run gc since our parent ran gc so we can skip
337 skipgc=1
339 else
340 # We don't have any alternates (we're not a forK) so we can skip
341 skipgc=1
345 # Prevent any other simultaneous gc operations
346 lock_gc
348 # At this point, if .allowgc exists, it's now crud to be removed
349 rm -f .allowgc
351 # Always get rid of crud
352 remove_crud
354 # Run 'git svn gc' now for svn mirrors
355 if is_svn_mirror; then
356 git svn gc || :
359 # Skip the actual gc if .delaygc is set
360 if [ -e .delaygc ]; then
361 progress "x [$proj] garbage check delayed (except for crud removal)"
362 rm -f "$lockf"
363 exit 0
366 # Do not skip gc if the repo is dirty
367 if [ -n "$skipgc" ] && ! is_dirty; then
368 progress "= [$proj] garbage check nothing but crud removal to do (`date`)"
369 config_set lastgc "$gcstart"
370 rm -f "$lockf"
371 exit 0
374 bumptime=
375 if [ -n "$isfork" ] && [ -z "$lastparentgcsecs" ]; then
376 # set lastparentgc and then update gcstart to be at least 1 second later
377 config_set lastparentgc "$gcstart"
378 bumptime=1
380 if [ -z "$lastreceivesecs" ]; then
381 # set lastreceive and then update gcstart to be at least 1 second later
382 config_set lastreceive "$gcstart"
383 bumptime=1
385 if [ -n "$bumptime" ]; then
386 sleep 1
387 gcstart="$(date "$datefmt")"
390 progress "+ [$proj] garbage check (`date`)"
392 newdeltas=
393 if [ -f gfi-packs -a -s gfi-packs ] && is_gfi_mirror; then
394 if [ $(list_packs --exclude-no-idx --count objects/pack) -le \
395 $(list_packs --exclude-no-idx --count --quiet --only gfi-packs) ]; then
396 # Don't bother with repack_gfi_packs since everything's being repacked
397 newdeltas=-f
400 if [ -z "$newdeltas" ] && \
401 [ $(list_packs --exclude-no-idx --count-objects objects/pack) -le $var_redelta_threshold ]; then
402 # There aren't enough objects to worry about so just redelta to get the best pack
403 newdeltas=-f
405 if [ -z "$newdeltas" ]; then
406 # Since we're not going to recompute deltas overall, we need to do the "mini"
407 # maintenance and by doing it before we copy objects down to forks we reduce
408 # the amount that gets sprayed into the forks' objects directories
409 repack_gfi_packs
410 make_svn_pack
411 combine_small_packs
414 # safe pruning: we put all our objects in all forks, then we can
415 # safely get rid of extra ones; repacks in forks will get rid of
416 # the redundant ones again then; we carefully grab only loose
417 # objects and pack .idx and .pack files
418 forkdir="$proj"
419 if [ -d "../${forkdir##*/}" ]; then
420 # It is enough to copy objects just one level down and get_repo_list
421 # takes a regular expression (which is automatically prefixed with '^')
422 # so we can easily match forks exactly one level down from this project
423 get_repo_list "$forkdir/[^/]*:" |
424 while read fork; do
425 # Ignore forks that do not exist or are symbolic links
426 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
427 continue
428 # Or do not have a non-zero length alternates file
429 [ -s "$cfg_reporoot/$fork.git/objects/info/alternates" ] || \
430 continue
431 # Match objects in parent project
432 for d in objects/??; do
433 [ "$d" != "objects/??" ] || continue
434 mkdir -p "$cfg_reporoot/$fork.git/$d"
435 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
436 done
437 # Match packs in parent project
438 mkdir -p "$cfg_reporoot/$fork.git/objects/pack"
439 if [ "$(echo objects/pack/pack-*.idx)" != \
440 "objects/pack/pack-*.idx" ]; then
441 ln -f objects/pack/pack-*.pack "$cfg_reporoot/$fork.git/objects/pack" || :
442 ln -f objects/pack/pack-*.idx "$cfg_reporoot/$fork.git/objects/pack" || :
444 # Update the fork's lastparentgc date (must be current, not $gcstart)
445 GIT_DIR="$cfg_reporoot/$fork.git" git config \
446 gitweb.lastparentgc "$(date "$datefmt")"
447 done
450 git pack-refs --all
451 touch .gc_in_progress
452 rm -f .gc_failed bundles/*
453 rm -f objects/pack/pack-*.bndl
454 # We use the -A option with git repack so that unreachable objects can live
455 # on for a time as loose objects. This is particularly helpful if we just
456 # happen to be in the process of sending out a ref update for a ref that was
457 # force updated and the old ref value would have otherwise been removed by
458 # repack because it was now unreachable. Admittedly the window for gc to run
459 # and do that before we manage to send out the ref update is not large, but
460 # it would not be difficult to create such a situation. Unfortunately, when
461 # Git unpacks these unreachable objects it will give them the modification
462 # time of the *.pack file they came out of. This could be very, very old.
463 # If that happens, the subsequent git prune --expire some_time_ago will still
464 # remove the object(s) and our pending ref update will still lose out.
465 # To prevent this from happening and to get the behavior we want, we now
466 # touch the modification time of all pack-<sha>.pack files so that any
467 # loosened objects get a current time. Git does not provide any other
468 # mechanism to do this. We do not want to just touch all loose objects
469 # left after the repack because that would cause objects that were loosened
470 # previously to live on which we definitely do not want.
471 list_packs --exclude-no-idx objects/pack | xargs touch -c 2>/dev/null || :
472 # We wish to keep deltas from our last full pack so if we're not redeltaing
473 # then make sure the .pack associated with the .bitmap has a newer mod time
474 # (If there is no .bitmap then touch the pack with the most objects instead.)
475 if [ -z "$newdeltas" ]; then
476 bmpack="$(list_packs --exclude-no-bitmap --exclude-no-idx --max-matches 1 objects/pack)"
477 [ -n "$bmpack" ] || bmpack="$(list_packs --exclude-no-idx --max-matches 1 --object-limit -1 --include-boundary objects/pack)"
478 if [ -n "$bmpack" ] && [ -f "$bmpack" -a -s "$bmpack" ]; then
479 sleep 1
480 touch -c "$bmpack" 2>/dev/null || :
483 # The git repack command may issue a 'disabling bitmap' warning for some
484 # repositories. This is perfectly normal and should be suppressed unless
485 # show_progress is set. Unfortunately that means we have to grep -v the
486 # output. And furthermore, since it's a translated message, we have to
487 # force the language to english to be sure we do it.
488 repackcmd="git repack $packopts -A -d -l $quiet $newdeltas $@"
489 [ -n "$show_progress" ] || \
490 repackcmd="{ LC_ALL=C $repackcmd 2>&1 || touch .gc_failed; } | grep -v 'disabling bitmap' || :"
491 eval "$repackcmd"
492 [ ! -e .gc_failed ] || exit 1
493 allpacks="$(echo objects/pack/pack-$octet20.pack)"
494 curhead="$(cat HEAD)"
495 pkrf=
496 [ ! -e packed-refs ] || pkrf=packed-refs
497 eval "reposizek=$(( $(echo 0 $(du -k $pkrf $allpacks 2>/dev/null | awk '{print $1}') | \
498 sed -e 's/ / + /g') ))"
499 # The -A option to `git repack` may have caused some loose objects to pop
500 # out of their packs. We must make these objects group writable so that they
501 # can be freshened by other pushers. Technically we need only do this for
502 # push projects but to enable mirror projects to be more easily converted to
503 # push projects, we go ahead and do it for all projects.
504 { find objects/$octet -type f -name "$octet19" -print0 | xargs -0 chmod ug+w || :; } 2>/dev/null
505 # The git prune command does not take a -q or --quiet but started outputting
506 # 'Checking connectivity' progress messages in v1.7.9. However, we can
507 # suppress those by piping through cat as it only activates the progress
508 # messages when stderr is a tty. We only expire loose objects older than one
509 # day just in case there's some pending action (such as sending out a ref
510 # update) in progress that might want to examine them. This may leave us with
511 # loose objects. That's okay because at the next gc interval, we will always
512 # run gc if we see any loose objects regardless of whether or not we've seen
513 # any updates or we've received new linked objects from our parent. Note that
514 # in order to keep loose objects that just recently became unreferenced but
515 # have a very old modification date around we rely on some help from both the
516 # update.sh and hooks/pre-receive scripts. Furthermore, since Git v2.2.0
517 # (d3038d22 prune: keep objects reachable from recent objects) an unreachable
518 # object that would otherwise be pruned (because it's too old) will be kept
519 # alive by an unreachable object that refers to it that's not old enough to
520 # be pruned yet.
521 prunecmd='git prune --expire 1_day_ago'
522 [ -n "$show_progress" ] || \
523 prunecmd="{ $prunecmd 2>&1 || touch .gc_failed; } | cat"
524 eval "$prunecmd"
525 [ ! -e .gc_failed ] || exit 1
526 git update-server-info
528 # darcs:// mirrors have a xxx.log file that will grow endlessly
529 # if this is a mirror and the file exists, shorten it to 10000 lines
530 # also take this opportunity to optimize the darcs repo
531 if [ ! -e .nofetch ] && [ -n "$cfg_mirror" ]; then
532 url="$(config_get baseurl || :)"
533 case "$url" in darcs://*)
534 if [ -n "$cfg_mirror_darcs" ]; then
535 url="${url%/}"
536 basedarcs="$(basename "${url#darcs:/}")"
537 if [ -f "$basedarcs.log" ]; then
538 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
539 mv -f "$basedarcs.log.$$" "$basedarcs.log"
541 if [ -d "$basedarcs.darcs" ]; then
543 cd "$basedarcs.darcs"
544 # Note that this does not optimize _darcs/inventories/ :(
545 darcs optimize
549 esac
552 # Create a matching .bndl header file for the all-in-one pack we just created
553 # but only if we're not a fork (otherwise the bundle would not be complete)
554 # and we are running at least Git version 1.7.2 (pack_is_complete always fails otherwise)
555 if [ ! -s objects/info/alternates ] && [ -n "$var_have_git_172" ]; then
556 # There should only be one pack in $allpacks but if there was a
557 # simultaneous push...
558 # The one we just created will have a .idx and will NOT have a .keep
559 pkfound=
560 pkhead=
561 for pk in $allpacks; do
562 [ -s "$pk" ] || continue
563 pkbase="${pk%.pack}"
564 [ -s "$pkbase.idx" ] || continue
565 [ ! -e "$pkbase.keep" ] || continue
566 if pkhead="$(pack_is_complete "$PWD/$pk" "$PWD/packed-refs" "$curhead")"; then
567 pkfound="$pkbase"
568 break;
570 done
571 if [ -n "$pkfound" -a -n "$pkhead" ]; then
573 echo "# v2 git bundle"
574 sed -ne "/^$octet20 refs\/[^ $tab]*\$/ p" < packed-refs
575 echo "$pkhead HEAD"
576 echo ""
577 } > "$pkbase.bndl"
578 bndletag="$("$cfg_basedir/bin/rangecgi" --etag -m 1 "$pkbase.bndl" "$pkbase.pack" || :)"
579 bndlsha="$(printf '%s' "$bndletag" | git hash-object --stdin || :)"
580 if [ -n "$bndletag" ]; then
581 case "$bndlsha" in $octet20)
582 bndlshatrailer="${bndlsha#????????}"
583 bndlshaprefix="${bndlsha%$bndlshatrailer}"
584 bndlname="$(TZ=UTC date +%Y%m%d_%H%M%S)-${bndlshaprefix:-0}"
585 [ -d bundles ] || mkdir bundles
586 echo "${pkbase#objects/pack/}.bndl" > "bundles/$bndlname"
587 echo "${pkbase#objects/pack/}.pack" >> "bundles/$bndlname"
588 ln -s -f -n "$bndlname" bundles/latest
589 esac
594 # Record the size of this repo as the sum of its *.pack sizes as 1024-byte blocks
595 config_set_raw girocco.reposizek "${reposizek:-0}"
597 # We use $gcstart here to avoid a race where a push occurs during the gc itself
598 # and the next future gc could be incorrectly skipped if we used the current
599 # timestamp here instead
600 config_set lastgc "$gcstart"
601 rm -f "$lockf"
603 progress "- [$proj] garbage check (`date`)"