gc.sh: remove stale htmlcache .lock files
[girocco.git] / toolbox / update-all-config.sh
bloba1ad00b79cb2c85ddb4285e64633f89e55c82ba7
1 #!/bin/sh
3 # Update all out-of-date config in all current projects and add missing bits
5 # If one or more project names are given, just update those instead
7 # update-all-config [--dry-run] [projname]...
9 set -e
11 . @basedir@/shlib.sh
13 force=
14 dryrun=
15 [ "$1" != "--force" -a "$1" != "-f" ] || { force=1; shift; }
16 [ "$1" != "--dry-run" -a "$1" != "-n" ] || { dryrun=1; shift; }
17 case "$1" in -*) echo "Invalid options: $1" >&2; exit 1;; esac
19 umask 002
20 dmode=02775
21 dperm='drwxrwsr-x'
22 drwxmode='ug+rwx,o+rx'
23 fmode=0664
24 fperm='-rw-rw-r--'
25 wall=
26 if [ -z "$cfg_owning_group" ]; then
27 if [ -z "$force" ]; then
28 echo "No owning_group set, refusing to run without --force" >&2
29 exit 1
31 dmode=02777
32 dperm='drwxrwsrwx'
33 fmode=0666
34 fperm='-rw-rw-rw-'
35 drwxmode='a+rwx'
36 wall=1
39 get_perm() {
40 ls -ldH "$1" | cut -d ' ' -f 1
43 get_grp() {
44 ls -ldH "$1" | awk '{print $4}'
47 wrote=
48 pmsg() {
49 [ -n "$hdr" ] || { hdr=1; [ -z "$wrote" ] || echo ""; echo "$proj:"; }
50 printf ' %s\n' "$*"
51 wrote=1
54 change_dperm() {
55 result=
56 oldperm="$(get_perm "$1")"
57 [ -n "$dryrun" ] || chmod $dmode "$1" 2>/dev/null || result=$?
58 pmsg "$1/:" "$oldperm" '->' "$dperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
61 change_dpermrwx() {
62 result=
63 oldperm="$(get_perm "$1")"
64 if [ -n "$wall" ]; then
65 newperm="drwxrwxrwx"
66 else
67 scratch="${oldperm#????????}"
68 newperm="drwxrwxr${scratch%?}x"
70 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod $drwxmode "$1" 2>/dev/null || result=$?
71 pmsg "$1/:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
74 change_dpermrx() {
75 result=
76 oldperm="$(get_perm "$1")"
77 scratch="${oldperm#??}"
78 newperm="dr${scratch%???????}xr"
79 scratch="${scratch#???}"
80 newperm="$newperm${scratch%????}xr"
81 scratch="${scratch#???}"
82 newperm="$newperm${scratch%?}x"
83 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a+rx "$1" 2>/dev/null || result=$?
84 pmsg "$1/:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
87 change_group() {
88 result=
89 oldgrp="$(get_grp "$1")"
90 [ -n "$dryrun" ] || chgrp "$cfg_owning_group" "$1" 2>/dev/null || result=$?
91 isdir=
92 ! [ -d "$1" ] || isdir='/'
93 pmsg "$1$isdir:" group "$oldgrp" '->' "$cfg_owning_group" ${result:+FAILED!} ${dryrun:+(dryrun)}
96 change_fperm() {
97 result=
98 oldperm="$(get_perm "$1")"
99 msg=
100 [ "$oldperm" = "$fperm" ] || { msg=1; [ -n "$dryrun" ]; } || chmod $fmode "$1" 2>/dev/null || result=$?
101 [ -z "$msg" ] || pmsg "$1:" "$oldperm" '->' "$fperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
104 change_fpermr() {
105 result=
106 oldperm="$(get_perm "$1")"
107 scratch="${oldperm#??}"
108 newperm="-r${scratch%??????}"
109 scratch="${scratch#???}"
110 newperm="${newperm}r${scratch%???}"
111 scratch="${scratch#???}"
112 newperm="${newperm}r$scratch"
113 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a+r "$1" 2>/dev/null || result=$?
114 pmsg "$1:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
117 change_fpermnox() {
118 result=
119 oldperm="$(get_perm "$1")"
120 newperm="${oldperm%???????}-"
121 scratch="${oldperm#????}"
122 newperm="${newperm}${scratch%????}-"
123 scratch="${scratch#???}"
124 newperm="${newperm}${scratch%?}-"
125 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a-x "$1" 2>/dev/null || result=$?
126 pmsg "$1:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
129 change_fpermx() {
130 result=
131 oldperm="$(get_perm "$1")"
132 newperm="${oldperm%???????}x"
133 scratch="${oldperm#????}"
134 newperm="${newperm}${scratch%????}x"
135 scratch="${scratch#???}"
136 newperm="${newperm}${scratch%?}x"
137 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a+x "$1" 2>/dev/null || result=$?
138 pmsg "$1:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
141 do_mkdir() {
142 result=
143 [ -n "$dryrun" ] || mkdir "$1" 2>/dev/null || [ -d "$1" ] || result=1
144 pmsg "$1/:" created ${result:+FAILED!} ${dryrun:+(dryrun)}
147 nl="$(printf '\n.')"
148 nl="${nl%?}"
149 readconfiglist() {
150 if [ -n "$dryrun" ] && ! [ -e config ]; then
151 configlist=
152 else
153 configlist="$(git config --list --file config)"
154 [ -z "$configlist" ] || configlist="$configlist$nl"
158 configitem() {
159 while read -r _item; do
160 case "$_item" in "$1"=*)
161 printf '%s' "${_item#*=}"
162 return 0
163 esac
164 done <<-EOT
165 $configlist
167 return 1
170 isbooltrue() {
171 case "$1" in 1|[Tt][Rr][Uu][Ee]|[Yy][Ee][Ss]|[Oo][Nn]) return 0; esac
172 return 1
175 isboolfalse() {
176 case "$1" in 0|[Ff][Aa][Ll][Ss][Ee]|[Nn][Oo]|[Oo][Ff][Ff]) return 0; esac
177 return 1
180 isbool() {
181 isbooltrue "$1" || isboolfalse "$1" || return 1
184 do_config() {
185 unset=
186 msg=
187 [ "$1" != "--unset" ] || { unset=1; shift; _x="$1"; shift; msg="$*"; set -- "$_x"; }
188 item="$1"; shift
189 result=
190 existsnot=
191 oldval="$(configitem "$item")" || existsnot=1
192 ccmd=unset
193 arg="$item"
194 [ -n "$existsnot" -o -z "$unset" ] || [ $(printf '%s' "$configlist" | grep "^${item%%.*}\.[^.][^.]*=" | wc -l) -ne 1 ] || { ccmd="remove-section"; arg="${item%%.*}"; }
195 [ -n "$dryrun" ] || [ -n "$unset" -a -n "$existsnot" ] || git config --file config ${unset:+--$ccmd} "$arg" "$@" 2>/dev/null || result=1
196 if [ -n "$existsnot" ]; then
197 [ -n "$unset" ] || pmsg "config $item:" created "\"$*\"" ${result:+FAILED!} ${dryrun:+(dryrun)}
198 else
199 if [ -n "$unset" ]; then
200 pmsg "config $item:" removed $msg "\"$oldval\"" ${result:+FAILED!} ${dryrun:+(dryrun)}
201 else
202 pmsg "config $item:" "\"$oldval\"" '->' "\"$*\"" ${result:+FAILED!} ${dryrun:+(dryrun)}
207 mkdirs='refs info hooks ctags htmlcache objects objects/info'
208 mkfiles='config info/lastactivity'
209 fixdpermsdirs='. refs info ctags htmlcache objects objects/info'
210 fixdpermsrwx='refs objects'
211 fixfpermsfiles='HEAD config description packed-refs README.html info/lastactivity'
212 fixfpermsfiles="$fixfpermsfiles info/alternates info/http-alternates info/packs"
213 fixfpermsdirs='ctags'
215 boolvars='gitweb.statusupdates'
216 falsevars='receive.denynonfastforwards'
217 false0vars='gc.auto receive.autogc'
218 truevars='receive.updateserverinfo repack.writebitmaps'
220 base="${cfg_reporoot%/}"
221 cmd='cut -d : -f 1 < "$cfg_chroot/etc/group" | grep -v ^_repo'
222 [ $# -eq 0 ] || cmd='printf "%s\n" "$@"'
223 eval "$cmd" | \
225 while read -r proj; do
226 proj="${proj#$base/}"
227 proj="${proj%.git}"
228 projdir="$base/$proj.git"
229 [ -d "$projdir" ] || { echo "$proj: does not exist -- skipping"; continue; }
230 is_git_dir "$projdir" || { echo "$proj: is not a .git directory -- skipping"; continue; }
231 ! [ -e "$projdir/.noconfig" ] || { echo "$proj: found .noconfig -- skipping"; continue; }
232 cd "$projdir"
233 bad=
234 hdr=
235 for mdir in $mkdirs; do
236 if ! [ -d "$mdir" ]; then
237 if [ -e "$mdir" ]; then
238 echo "$proj: bypassing project, exists but not directory: $mdir" >&2
239 bad=1
240 break
242 do_mkdir "$mdir"
244 done
245 [ -z "$bad" ] || continue
246 while read -r fixdir; do
247 [ -z "$fixdir" ] || change_dperm "$fixdir"
248 done <<-EOT
249 $(find $fixdpermsdirs -xdev -maxdepth 0 -type d -not -perm $dmode -print 2>/dev/null)
251 while read -r fixdir; do
252 [ -z "$fixdir" ] || change_dpermrwx "$fixdir"
253 done <<-EOT
254 $(find $fixdpermsrwx -xdev -type d -not -perm -$drwxmode -print 2>/dev/null)
256 while read -r fixdir; do
257 [ -z "$fixdir" ] || change_dpermrx "$fixdir"
258 done <<-EOT
259 $(find . -xdev -type d -not -perm -a+rx -print)
261 for mkfile in $mkfiles; do
262 if [ -e "$mkfile" ]; then
263 if ! [ -f "$mkfile" ]; then
264 echo "$proj: bypassing project, exists but not file: $mkfile" >&2
265 bad=1
266 break
268 else
269 result=
270 [ -n "$dryrun" ] || { >"$mkfile"; } 2>/dev/null || result=1
271 pmsg "$mkfile:" created ${result:+FAILED!} ${dryrun:+(dryrun)}
273 done
274 [ -z "$bad" ] || continue
275 [ -n "$dryrun" ] || change_fperm config
276 readconfiglist
277 grpshr="$(configitem core.sharedrepository || :)"
278 if [ -z "$grpshr" ] || isboolfalse "$grpshr"; then
279 do_config core.sharedrepository 1
280 elif ! isbooltrue "$grpshr"; then
281 pmsg "WARNING: odd core.sharedrepository value left unchanged: \"$grpshr\""
283 isbooltrue "$(configitem core.bare || :)" || pmsg "WARNING: core.bare is not true (left unchanged)"
284 for bvar in $boolvars; do
285 ! old="$(configitem "$bvar")" || isbool "$old" || do_config --unset "$bvar" "(not a boolean)"
286 done
287 for fvar in $falsevars; do
288 old="$(configitem "$fvar" || :)"
289 isboolfalse "$old" || do_config "$fvar" false
290 done
291 for fvar in $false0vars; do
292 old="$(configitem "$fvar" || :)"
293 isboolfalse "$old" || do_config "$fvar" 0
294 done
295 for tvar in $truevars; do
296 old="$(configitem "$tvar" || :)"
297 isbooltrue "$old" || do_config "$tvar" true
298 done
299 if [ -n "$cfg_owning_group" ]; then
300 while read -r fixgrp; do
301 [ -z "$fixgrp" ] || change_group "$fixgrp"
302 done <<-EOT
303 $(find . -xdev \( -type d -o -type f \) -not -group $cfg_owning_group -print)
306 for fixfile in $fixfpermsfiles; do
307 if [ -e "$fixfile" ]; then
308 if ! [ -f "$fixfile" ]; then
309 echo "$proj: bypassing project, exists but not file: $fixfile" >&2
310 bad=1
311 break
313 change_fperm "$fixfile"
315 done
316 [ -z "$bad" ] || continue
317 for fixfile in $(find $fixfpermsdirs -xdev -type f -not -perm $fmode -print 2>/dev/null); do
318 change_fperm "$fixfile"
319 done
320 while read -r fixfileread; do
321 [ -z "$fixfileread" ] || change_fpermr "$fixfileread"
322 done <<-EOT
323 $(find . -xdev -type f -not -perm -a+r -print)
325 while read -r fixfilenox; do
326 [ -z "$fixfilenox" ] || change_fpermnox "$fixfilenox"
327 done <<-EOT
328 $(find . -xdev -type d \( -path ./hooks -o -path ./mob/hooks \) -prune -o -type f -perm +a+x -print)
330 while read -r fixfilex; do
331 case "$fixfilex" in ""|*.sample) :;; *) change_fpermx "$fixfilex"; esac
332 done <<-EOT
333 $(! [ -d hooks ] || find hooks -xdev -type f -not -perm -a+x -print
334 ! [ -d mob/hooks ] || find mob/hooks -xdev -type f -not -perm -a+x -print)
336 bu="$(configitem gitweb.baseurl || :)"
337 [ -n "$bu" -o -e .nofetch ] || pmsg "WARNING: gitweb.baseurl is empty and .nofetch does not exist"
338 [ -z "$bu" -o ! -e .nofetch ] || pmsg "WARNING: .nofetch exists but gitweb.baseurl is not empty"
339 done