scripts: purge use of test '-a' and '-o' ops and clean up
[girocco.git] / toolbox / update-all-config.sh
blob0c283ede77c18925b08d63f7772422e35812e53e
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" ] && [ "$1" != "-f" ] || { force=1; shift; }
16 [ "$1" != "--dry-run" ] && [ "$1" != "-n" ] || { dryrun=1; shift; }
17 case "$1" in -*) echo "Invalid options: $1" >&2; exit 1;; esac
19 if is_root; then
20 printf '%s\n' "$(basename "$0"): refusing to run as root -- bad things would happen"
21 exit 1
24 umask 002
25 dmode=02775
26 dperm='drwxrwsr-x'
27 drwxmode='ug+rwx,o+rx'
28 fmode=0664
29 fperm='-rw-rw-r--'
30 wall=
31 if [ -z "$cfg_owning_group" ]; then
32 if [ -z "$force" ]; then
33 echo "No owning_group set, refusing to run without --force" >&2
34 exit 1
36 dmode=02777
37 dperm='drwxrwsrwx'
38 fmode=0666
39 fperm='-rw-rw-rw-'
40 drwxmode='a+rwx'
41 wall=1
44 get_perm() {
45 ls -ldH "$1" | cut -d ' ' -f 1
48 get_grp() {
49 ls -ldH "$1" | awk '{print $4}'
52 wrote=
53 pmsg() {
54 [ -n "$hdr" ] || { hdr=1; [ -z "$wrote" ] || echo ""; echo "$proj:"; }
55 printf ' %s\n' "$*"
56 wrote=1
59 change_dperm() {
60 result=
61 oldperm="$(get_perm "$1")"
62 [ -n "$dryrun" ] || chmod $dmode "$1" 2>/dev/null || result=$?
63 pmsg "$1/:" "$oldperm" '->' "$dperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
66 change_dpermrwx() {
67 result=
68 oldperm="$(get_perm "$1")"
69 if [ -n "$wall" ]; then
70 newperm="drwxrwxrwx"
71 else
72 scratch="${oldperm#????????}"
73 newperm="drwxrwxr${scratch%?}x"
75 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod $drwxmode "$1" 2>/dev/null || result=$?
76 pmsg "$1/:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
79 change_dpermrx() {
80 result=
81 oldperm="$(get_perm "$1")"
82 scratch="${oldperm#??}"
83 newperm="dr${scratch%???????}xr"
84 scratch="${scratch#???}"
85 newperm="$newperm${scratch%????}xr"
86 scratch="${scratch#???}"
87 newperm="$newperm${scratch%?}x"
88 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a+rx "$1" 2>/dev/null || result=$?
89 pmsg "$1/:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
92 change_group() {
93 result=
94 oldgrp="$(get_grp "$1")"
95 [ -n "$dryrun" ] || chgrp "$cfg_owning_group" "$1" 2>/dev/null || result=$?
96 isdir=
97 ! [ -d "$1" ] || isdir='/'
98 pmsg "$1$isdir:" group "$oldgrp" '->' "$cfg_owning_group" ${result:+FAILED!} ${dryrun:+(dryrun)}
101 change_fperm() {
102 result=
103 oldperm="$(get_perm "$1")"
104 msg=
105 [ "$oldperm" = "$fperm" ] || { msg=1; [ -n "$dryrun" ]; } || chmod $fmode "$1" 2>/dev/null || result=$?
106 [ -z "$msg" ] || pmsg "$1:" "$oldperm" '->' "$fperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
109 change_fpermr() {
110 result=
111 oldperm="$(get_perm "$1")"
112 scratch="${oldperm#??}"
113 newperm="-r${scratch%??????}"
114 scratch="${scratch#???}"
115 newperm="${newperm}r${scratch%???}"
116 scratch="${scratch#???}"
117 newperm="${newperm}r$scratch"
118 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a+r "$1" 2>/dev/null || result=$?
119 pmsg "$1:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
122 change_fpermnox() {
123 result=
124 oldperm="$(get_perm "$1")"
125 newperm="${oldperm%???????}-"
126 scratch="${oldperm#????}"
127 newperm="${newperm}${scratch%????}-"
128 scratch="${scratch#???}"
129 newperm="${newperm}${scratch%?}-"
130 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a-x "$1" 2>/dev/null || result=$?
131 pmsg "$1:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
134 change_fpermx() {
135 result=
136 oldperm="$(get_perm "$1")"
137 newperm="${oldperm%???????}x"
138 scratch="${oldperm#????}"
139 newperm="${newperm}${scratch%????}x"
140 scratch="${scratch#???}"
141 newperm="${newperm}${scratch%?}x"
142 [ -n "$dryrun" ] || [ "$oldperm" = "$newperm" ] || chmod a+x "$1" 2>/dev/null || result=$?
143 pmsg "$1:" "$oldperm" '->' "$newperm" ${result:+FAILED!} ${dryrun:+(dryrun)}
146 do_mkdir() {
147 result=
148 [ -n "$dryrun" ] || mkdir "$1" 2>/dev/null || [ -d "$1" ] || result=1
149 pmsg "$1/:" created ${result:+FAILED!} ${dryrun:+(dryrun)}
152 nl='
154 readconfiglist() {
155 if [ -n "$dryrun" ] && ! [ -e config ]; then
156 configlist=
157 else
158 configlist="$(git config --list --file config)"
159 [ -z "$configlist" ] || configlist="$configlist$nl"
163 configitem() {
164 while read -r _item; do
165 case "$_item" in "$1"=*)
166 printf '%s' "${_item#*=}"
167 return 0
168 esac
169 done <<-EOT
170 $configlist
172 return 1
175 isbooltrue() {
176 case "$1" in 1|[Tt][Rr][Uu][Ee]|[Yy][Ee][Ss]|[Oo][Nn]) return 0; esac
177 return 1
180 isboolfalse() {
181 case "$1" in 0|[Ff][Aa][Ll][Ss][Ee]|[Nn][Oo]|[Oo][Ff][Ff]) return 0; esac
182 return 1
185 isbool() {
186 isbooltrue "$1" || isboolfalse "$1" || return 1
189 do_config() {
190 unset=
191 msg=
192 [ "$1" != "--unset" ] || { unset=1; shift; _x="$1"; shift; msg="$*"; set -- "$_x"; }
193 item="$1"; shift
194 result=
195 existsnot=
196 oldval="$(configitem "$item")" || existsnot=1
197 ccmd=unset
198 arg="$item"
199 [ -n "$existsnot" ] || [ -z "$unset" ] || [ $(printf '%s' "$configlist" | grep "^${item%%.*}\.[^.][^.]*=" | wc -l) -ne 1 ] || { ccmd="remove-section"; arg="${item%%.*}"; }
200 [ -n "$dryrun" ] || { [ -n "$unset" ] && [ -n "$existsnot" ]; } || git config --file config ${unset:+--$ccmd} "$arg" "$@" 2>/dev/null || result=1
201 if [ -n "$existsnot" ]; then
202 [ -n "$unset" ] || pmsg "config $item:" created "\"$*\"" ${result:+FAILED!} ${dryrun:+(dryrun)}
203 else
204 if [ -n "$unset" ]; then
205 pmsg "config $item:" removed $msg "\"$oldval\"" ${result:+FAILED!} ${dryrun:+(dryrun)}
206 else
207 pmsg "config $item:" "\"$oldval\"" '->' "\"$*\"" ${result:+FAILED!} ${dryrun:+(dryrun)}
212 mkdirs='refs info hooks ctags htmlcache bundles reflogs objects objects/info'
213 mkfiles='config info/lastactivity'
214 fixdpermsdirs='. refs info ctags htmlcache bundles reflogs objects objects/info'
215 fixdpermsrwx='refs objects'
216 fixfpermsfiles='HEAD config description packed-refs README.html info/lastactivity'
217 fixfpermsfiles="$fixfpermsfiles info/alternates info/http-alternates info/packs"
218 fixfpermsdirs='ctags'
220 boolvars='gitweb.statusupdates'
221 falsevars='core.ignorecase receive.denynonfastforwards'
222 false0vars='gc.auto receive.autogc'
223 truevars='receive.updateserverinfo repack.writebitmaps transfer.fsckobjects'
225 base="${cfg_reporoot%/}"
226 cmd='cut -d : -f 1 <"$cfg_chroot/etc/group" | grep -v ^_repo'
227 [ $# -eq 0 ] || cmd='printf "%s\n" "$@"'
228 eval "$cmd" |
230 while read -r proj; do
231 proj="${proj#$base/}"
232 proj="${proj%.git}"
233 projdir="$base/$proj.git"
234 [ -d "$projdir" ] || { echo "$proj: does not exist -- skipping"; continue; }
235 is_git_dir "$projdir" || { echo "$proj: is not a .git directory -- skipping"; continue; }
236 ! [ -e "$projdir/.noconfig" ] || { echo "$proj: found .noconfig -- skipping"; continue; }
237 cd "$projdir"
238 bad=
239 hdr=
240 for mdir in $mkdirs; do
241 if ! [ -d "$mdir" ]; then
242 if [ -e "$mdir" ]; then
243 echo "$proj: bypassing project, exists but not directory: $mdir" >&2
244 bad=1
245 break
247 do_mkdir "$mdir"
249 done
250 [ -z "$bad" ] || continue
251 while read -r fixdir; do
252 [ -z "$fixdir" ] || change_dperm "$fixdir"
253 done <<-EOT
254 $(find $fixdpermsdirs -xdev -maxdepth 0 -type d ! -perm $dmode -print 2>/dev/null)
256 while read -r fixdir; do
257 [ -z "$fixdir" ] || change_dpermrwx "$fixdir"
258 done <<-EOT
259 $(find $fixdpermsrwx -xdev -type d ! -perm -$drwxmode -print 2>/dev/null)
261 while read -r fixdir; do
262 [ -z "$fixdir" ] || change_dpermrx "$fixdir"
263 done <<-EOT
264 $(find . -xdev -type d ! -perm -a+rx -print)
266 for mkfile in $mkfiles; do
267 if [ -e "$mkfile" ]; then
268 if ! [ -f "$mkfile" ]; then
269 echo "$proj: bypassing project, exists but not file: $mkfile" >&2
270 bad=1
271 break
273 else
274 result=
275 [ -n "$dryrun" ] || { >"$mkfile"; } 2>/dev/null || result=1
276 pmsg "$mkfile:" created ${result:+FAILED!} ${dryrun:+(dryrun)}
278 done
279 [ -z "$bad" ] || continue
280 [ -n "$dryrun" ] || change_fperm config
281 readconfiglist
282 cmplvl="$(configitem core.compression)" || :
283 case "$cmplvl" in
284 "5"|"")
286 "-1"|"0"|"1"|"2"|"3"|"4"|"6"|"7"|"8"|"9")
287 pmsg "WARNING: suboptimal core.compression value left unchanged: \"$cmplvl\""
290 pmsg "WARNING: replacing invalid core.compression value"
291 cmplvl=
293 esac
294 [ -n "$cmplvl" ] || do_config core.compression 5
295 grpshr="$(configitem core.sharedrepository)" || :
296 if [ -z "$grpshr" ] || isboolfalse "$grpshr"; then
297 do_config core.sharedrepository 1
298 elif ! isbooltrue "$grpshr"; then
299 pmsg "WARNING: odd core.sharedrepository value left unchanged: \"$grpshr\""
301 if isbooltrue "$(configitem core.bare || :)"; then
302 setlaru=1
303 if laru="$(configitem core.logallrefupdates)"; then
304 if isbool "$laru"; then
305 setlaru=
306 isboolfalse "$laru" ||
307 pmsg "WARNING: core.logallrefupdates is true (left unchanged)"
308 else
309 pmsg "WARNING: replacing non-boolean core.logallrefupdates value"
312 [ -z "$setlaru" ] || do_config core.logallrefupdates false
313 else
314 pmsg "WARNING: core.bare is not true (left unchanged)"
316 case "$(configitem receive.denydeletecurrent || :)" in [Ww][Aa][Rr][Nn]) :;; *)
317 do_config receive.denydeletecurrent warn
318 esac
319 for bvar in $boolvars; do
320 ! old="$(configitem "$bvar")" || isbool "$old" || do_config --unset "$bvar" "(not a boolean)"
321 done
322 for fvar in $falsevars; do
323 old="$(configitem "$fvar")" || :
324 isboolfalse "$old" || do_config "$fvar" false
325 done
326 for fvar in $false0vars; do
327 old="$(configitem "$fvar")" || :
328 isboolfalse "$old" || do_config "$fvar" 0
329 done
330 for tvar in $truevars; do
331 old="$(configitem "$tvar")" || :
332 isbooltrue "$old" || do_config "$tvar" true
333 done
334 if [ -n "$cfg_owning_group" ]; then
335 while read -r fixgrp; do
336 [ -z "$fixgrp" ] || change_group "$fixgrp"
337 done <<-EOT
338 $(find . -xdev \( -type d -o -type f \) ! -group $cfg_owning_group -print)
341 for fixfile in $fixfpermsfiles; do
342 if [ -e "$fixfile" ]; then
343 if ! [ -f "$fixfile" ]; then
344 echo "$proj: bypassing project, exists but not file: $fixfile" >&2
345 bad=1
346 break
348 change_fperm "$fixfile"
350 done
351 [ -z "$bad" ] || continue
352 for fixfile in $(find $fixfpermsdirs -xdev -type f ! -perm $fmode -print 2>/dev/null); do
353 change_fperm "$fixfile"
354 done
355 while read -r fixfileread; do
356 [ -z "$fixfileread" ] || change_fpermr "$fixfileread"
357 done <<-EOT
358 $(find . -xdev -type f ! -perm -a+r -print)
360 while read -r fixfilenox; do
361 [ -z "$fixfilenox" ] || change_fpermnox "$fixfilenox"
362 done <<-EOT
363 $(find . -xdev -type d \( -path ./hooks -o -path ./mob/hooks \) -prune -o -type f -perm +a+x -print)
365 while read -r fixfilex; do
366 #case "$fixfilex" in ""|*.sample) :;; *) change_fpermx "$fixfilex"; esac
368 done <<-EOT
369 $(! [ -d hooks ] || find hooks -xdev -type f ! -perm -a+x -print
370 ! [ -d mob/hooks ] || find mob/hooks -xdev -type f ! -perm -a+x -print)
372 bu="$(configitem gitweb.baseurl)" || :
373 [ -n "$bu" ] || [ -e .nofetch ] || pmsg "WARNING: gitweb.baseurl is empty and .nofetch does not exist"
374 [ -z "$bu" ] || ! [ -e .nofetch ] || pmsg "WARNING: .nofetch exists but gitweb.baseurl is not empty"
375 done