hooks/pre-receive: set .needsgc when many packs are present
[girocco.git] / hooks / pre-receive
blob7c66498651ce26d264738971a58679f10c8ed08c
1 #!/bin/sh
3 # Keep track of the last time we modified the object store
5 # Beware, we MAY be running in a chroot!
7 set -e
9 var_xargs_r=@var_xargs_r@
10 umask 002
12 if [ -x @perlbin@ ]; then
13 # We are NOT inside the chroot
14 basedir=@basedir@
15 list_packs() { command "$basedir/bin/list_packs" "$@"; }
18 # Some platforms' broken xargs runs the command always at least once even if
19 # there's no input unless given a special option. Automatically supply the
20 # option on those platforms by providing an xargs function.
21 xargs() { command xargs $var_xargs_r "$@"; }
23 git config gitweb.lastreceive "$(date '+%a, %d %b %Y %T %z')"
25 # Read the incoming refs and freshen old loose objects
26 # If we waited until post-receive a gc could have already nuked them
27 # We freshen the new ref in case it's being resurrected to protect it from gc
28 # We probably do not need to do it for new refs as Git tries to do that,
29 # but since we're already doing it for old refs (which Git does not do),
30 # it's almost no extra work for new refs, just in case. We also attempt
31 # to make all packs user and group writable so they can be touched later.
33 octet='[0-9a-f][0-9a-f]'
34 octet4="$octet$octet$octet$octet"
35 octet20="$octet4$octet4$octet4$octet4$octet4"
37 find objects/pack -maxdepth 1 -type f \! -perm -ug+w \
38 -name "pack-$octet20.pack" -print0 | \
39 xargs -0 chmod ug+w
40 } 2>/dev/null || :
42 # Trigger a mini-gc if there are at least 20 packs present.
43 # Our current pack that contains this push's data will have a .keep while
44 # this hook is running so we do not use --exclude-keep here under the
45 # assumption that by the time a mini-gc starts the .keep will have been
46 # removed. If not, that's okay too, it just means the current pack will
47 # not take part in the mini-gc and will have to wait for the next one.
48 # The mini-gc code contains the logic to sort out small packs vs. non-small
49 # packs and which should be combined in what order so we do not need to
50 # do any more complicated testing here.
51 if ! [ -e .needsgc ]; then
52 packs="$(list_packs --quiet --count --exclude-no-idx objects/pack || :)"
53 if [ -n "$packs" ] && [ "$packs" -ge 20 ]; then
54 >.needsgc
58 while read -r old new ref; do
59 oldp=
60 newp=
61 if [ "$old" != "0000000000000000000000000000000000000000" ]; then
62 # freshen mod time on recently unref'd loose objects
63 fn="${old#??}"
64 shard="${old%$fn}"
65 oldp="objects/$shard/$fn"
67 if [ "$new" != "0000000000000000000000000000000000000000" ]; then
68 # prevent imminent pruning of a ref being resurrected
69 fn="${new#??}"
70 shard="${new%$fn}"
71 newp="objects/$shard/$fn"
73 chmod ug+w $oldp $newp 2>/dev/null || :
74 touch -c $oldp $newp 2>/dev/null || :
75 done