698f3ffb189bbee2e17425cc05a8c4b19f5d69b2
[girocco.git] / jobd / gc.sh
blob698f3ffb189bbee2e17425cc05a8c4b19f5d69b2
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 umask 002
9 [ "$cfg_permission_control" != "Hooks" ] || umask 000
11 pidactive() {
12 if _result="$(kill -0 "$1" 2>&1)"; then
13 # process exists and we have permission to signal it
14 return 0
16 case "$_result" in *"not permitted"*)
17 # we do not have permission to signal the process
18 return 0
19 esac
20 # process does not exist
21 return 1
24 createlock() {
25 for _try in p n; do
26 if (set -C; > "$1.lock") 2>/dev/null; then
27 echo "$1.lock"
28 return 0
30 # delay and try again
31 [ "$_try" != "p" ] || sleep 1
32 done
33 # cannot create temp file
34 return 1
37 proj="$1"
38 cd "$cfg_reporoot/$proj.git"
40 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
41 datefmt='+%a, %d %b %Y %T %z'
43 if check_interval lastgc $cfg_min_gc_interval; then
44 progress "= [$proj] garbage check skip (last at $(config_get lastgc))"
45 exit 0
47 if [ -e .nogc ]; then
48 progress "x [$proj] garbage check disabled"
49 exit 0
52 # be compatibile with gc.pid file from newer Git releases
54 hn="$(hostname)"
55 lockf=gc.pid
56 active=
57 if [ "$(createlock "$lockf")" ]; then
58 # If $lockf is:
59 # 1) less than 12 hours old
60 # 2) contains two fields (pid hostname) NO trailing NL
61 # 3) the hostname is different OR the pid is still alive
62 # then we exit as another active process is holding the lock
63 if [ "$(find "$lockf" -mmin -720 -print 2>/dev/null)" ]; then
64 apid=
65 ahost=
66 read -r apid ahost ajunk < "$lockf" || :
67 if [ "$apid" ] && [ "$ahost" ]; then
68 if [ "$ahost" != "$hn" ] || pidactive "$apid"; then
69 active=1
73 else
74 echo >&2 "[$proj] unable to create gc.pid.lock file"
75 exit 1
77 if [ -n "$active" ]; then
78 rm "$lockf.lock"
79 echo >&2 "[$proj] gc already running on machine '$ahost' pid '$apid'"
80 exit 1
82 printf "%s %s" "$$" "$hn" > "$lockf.lock"
83 chmod 0664 "$lockf.lock"
84 mv -f "$lockf.lock" "$lockf"
86 progress "+ [$proj] garbage check (`date`)"
88 # safe pruning: we put all our objects in all forks, then we can
89 # safely get rid of extra ones; repacks in forks will get rid of
90 # the redundant ones again then
91 forkdir="$1"
92 if [ -d "../${forkdir##*/}" ]; then
93 # It is enough to copy objects just one level down and get_repo_list
94 # takes a regular expression (which is automatically prefixed with '^')
95 # so we can easily match forks exactly one level down from this project
96 get_repo_list "$forkdir/[^/]*:" |
97 while read fork; do
98 # Ignore forks that do not exist or are symbolic links
99 [ ! -L "$cfg_reporoot/$fork.git" -a -d "$cfg_reporoot/$fork.git" ] || \
100 continue
101 # Match objects in parent project
102 for d in objects/?? objects/pack; do
103 [ "$d" != "objects/??" ] || continue
104 mkdir -p "$cfg_reporoot/$fork.git/$d"
105 [ "$d" != "objects/pack" ] || \
106 [ "$(echo objects/pack/*)" != \
107 "objects/pack/*" ] || \
108 continue
109 ln -f "$d"/* "$cfg_reporoot/$fork.git/$d" || :
110 done
111 done
114 quiet=; [ -n "$show_progress" ] || quiet=-q
115 git pack-refs --all
116 git repack -a -d --window=50 --window-memory=1G --depth=50 -l $quiet
117 git prune
118 git update-server-info
119 # darcs:// mirrors have a xxx.log file that will grow endlessly
120 # if this is a mirror and the file exists, shorten it to 10000 lines
121 # also take this opportunity to optimize the darcs repo
122 if [ ! -e .nofetch ] && [ -n "$cfg_mirror" ]; then
123 url="$(config_get baseurl || :)"
124 case "$url" in darcs://*)
125 if [ -n "$cfg_mirror_darcs" ]; then
126 url="${url%/}"
127 basedarcs="$(basename "${url#darcs:/}")"
128 if [ -f "$basedarcs.log" ]; then
129 tail -n 10000 "$basedarcs.log" > "$basedarcs.log.$$"
130 mv -f "$basedarcs.log.$$" "$basedarcs.log"
132 if [ -d "$basedarcs.darcs" ]; then
134 cd "$basedarcs.darcs"
135 # Note that this does not optimize _darcs/inventories/ :(
136 darcs optimize
140 esac
142 config_set lastgc "$(date "$datefmt")"
143 rm "$lockf"
145 progress "- [$proj] garbage check (`date`)"