mirroring: reduce bloat caused by mirrors using git fast-import
[girocco.git] / jobd / gc.sh
blob8adca3097f19a1165a2718246c8560940ad6c294
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 --threads=1 $quiet objects/pack/packtmp | \
50 while read -r _newpack; do
51 rm -f objects/pack/pack-$_newpack.*
52 ln objects/pack/packtmp-$_newpack.pack objects/pack/pack-$_newpack.pack
53 ln objects/pack/packtmp-$_newpack.idx objects/pack/pack-$_newpack.idx
54 rm -f objects/pack/packtmp-$_newpack.*
55 done
56 rm -f gfi-packs
59 proj="$1"
60 cd "$cfg_reporoot/$proj.git"
62 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
63 datefmt='+%a, %d %b %Y %T %z'
65 if check_interval lastgc $cfg_min_gc_interval; then
66 progress "= [$proj] garbage check skip (last at $(config_get lastgc))"
67 exit 0
69 if [ -e .nogc ]; then
70 progress "x [$proj] garbage check disabled"
71 exit 0
74 # be compatibile with gc.pid file from newer Git releases
76 hn="$(hostname)"
77 lockf=gc.pid
78 active=
79 if [ "$(createlock "$lockf")" ]; then
80 # If $lockf is:
81 # 1) less than 12 hours old
82 # 2) contains two fields (pid hostname) NO trailing NL
83 # 3) the hostname is different OR the pid is still alive
84 # then we exit as another active process is holding the lock
85 if [ "$(find "$lockf" -mmin -720 -print 2>/dev/null)" ]; then
86 apid=
87 ahost=
88 read -r apid ahost ajunk < "$lockf" || :
89 if [ "$apid" ] && [ "$ahost" ]; then
90 if [ "$ahost" != "$hn" ] || pidactive "$apid"; then
91 active=1
95 else
96 echo >&2 "[$proj] unable to create gc.pid.lock file"
97 exit 1
99 if [ -n "$active" ]; then
100 rm "$lockf.lock"
101 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
102 exit 1
104 printf "%s %s" "$$" "$hn" > "$lockf.lock"
105 chmod 0664 "$lockf.lock"
106 mv -f "$lockf.lock" "$lockf"
108 progress "+ [$proj] garbage check (`date`)"
110 # safe pruning: we put all our objects in all forks, then we can
111 # safely get rid of extra ones; repacks in forks will get rid of
112 # the redundant ones again then
113 forkdir="$1"
114 if [ -d "../${forkdir##*/}" ]; then
115 # It is enough to copy objects just one level down and get_repo_list
116 # takes a regular expression (which is automatically prefixed with '^')
117 # so we can easily match forks exactly one level down from this project
118 get_repo_list "$forkdir/[^/]*:" |
119 while read fork; do
120 # Ignore forks that do not exist or are symbolic links
121 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
122 continue
123 # Match objects in parent project
124 for d in objects/?? objects/pack; do
125 [ "$d" != "objects/??" ] || continue
126 mkdir -p "$cfg_reporoot/$fork.git/$d"
127 [ "$d" != "objects/pack" ] || \
128 [ "$(echo objects/pack/*)" != \
129 "objects/pack/*" ] || \
130 continue
131 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
132 done
133 done
136 quiet=; [ -n "$show_progress" ] || quiet=-q
137 git pack-refs --all
138 repack_gfi_packs
139 git repack $packopts -a -d -l $quiet
140 git prune
141 git update-server-info
142 # darcs:// mirrors have a xxx.log file that will grow endlessly
143 # if this is a mirror and the file exists, shorten it to 10000 lines
144 # also take this opportunity to optimize the darcs repo
145 if [ ! -e .nofetch ] && [ -n "$cfg_mirror" ]; then
146 url="$(config_get baseurl || :)"
147 case "$url" in darcs://*)
148 if [ -n "$cfg_mirror_darcs" ]; then
149 url="${url%/}"
150 basedarcs="$(basename "${url#darcs:/}")"
151 if [ -f "$basedarcs.log" ]; then
152 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
153 mv -f "$basedarcs.log.$$" "$basedarcs.log"
155 if [ -d "$basedarcs.darcs" ]; then
157 cd "$basedarcs.darcs"
158 # Note that this does not optimize _darcs/inventories/ :(
159 darcs optimize
163 esac
165 config_set lastgc "$(date "$datefmt")"
166 rm "$lockf"
168 progress "- [$proj] garbage check (`date`)"