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