gc: allow gc --auto to run pre-auto-gc outside Girocco
[girocco/readme.git] / toolbox / update-all-config.pl
blobcc617520bf99d5ad9e6d53c8688a7760adff2841
1 #!/usr/bin/perl
3 # update-all-config.pl - Update all out-of-date config
5 use strict;
6 use warnings;
7 use vars qw($VERSION);
8 BEGIN {*VERSION = \'2.0'}
9 use File::Basename;
10 use File::Spec;
11 use Cwd qw(realpath);
12 use POSIX qw();
13 use Getopt::Long;
14 use Pod::Usage;
15 use lib "__BASEDIR__";
16 use Girocco::Config;
17 use Girocco::Util;
18 use Girocco::CLIUtil;
19 use Girocco::Project;
21 my $shbin;
22 BEGIN {
23 $shbin = $Girocco::Config::posix_sh_bin;
24 defined($shbin) && $shbin ne "" or $shbin = "/bin/sh";
27 exit(&main(@ARGV)||0);
29 my ($dryrun, $force, $quiet);
31 sub die_usage {
32 pod2usage(-exitval => 2);
35 sub do_help {
36 pod2usage(-verbose => 2, -exitval => 0);
39 sub do_version {
40 print basename($0), " version ", $VERSION, "\n";
41 exit 0;
44 my ($dmode, $dperm, $drwxmode, $fmode, $fmodeoct, $fperm, $wall);
45 BEGIN {
46 $dmode=02775;
47 $dperm='drwxrwsr-x';
48 $drwxmode='ug+rwx,o+rx';
49 $fmode=0664;
50 $fmodeoct='0664';
51 $fperm='-rw-rw-r--';
52 $wall=0;
55 my $owning_group_id;
56 my $htmlcache_owning_group_id;
57 my $ctags_owning_group_id;
59 sub main {
60 local *ARGV = \@_;
61 my ($help, $version);
63 umask 002;
64 close(DATA) if fileno(DATA);
65 Getopt::Long::Configure('bundling');
66 GetOptions(
67 'help|h' => sub {do_help},
68 'version|V' => sub {do_version},
69 'dry-run|n' => \$dryrun,
70 'quiet|q' => \$quiet,
71 'force|f' => \$force,
72 ) or die_usage;
73 $dryrun and $quiet = 0;
75 -f jailed_file("/etc/group") or
76 die "Girocco group file not found: " . jailed_file("/etc/group") . "\n";
78 if (!defined($Girocco::Config::owning_group) || $Girocco::Config::owning_group eq "") {
79 die "\$Girocco::Config::owning_group unset, refusing to run without --force\n" unless $force;
80 $dmode=02777;
81 $dperm='drwxrwsrwx';
82 $drwxmode='a+rwx';
83 $fmode=0666;
84 $fmodeoct='0666';
85 $fperm='-rw-rw-rw-';
86 $wall=1;
87 warn "Mode 666 in effect\n" unless $quiet;
88 } elsif (($owning_group_id = scalar(getgrnam($Girocco::Config::owning_group))) !~ /^\d+$/) {
89 die "\$Girocco::Config::owning_group invalid ($Girocco::Config::owning_group), refusing to run\n";
91 if (defined($owning_group_id) && $Girocco::Config::htmlcache_owning_group) {
92 die "\$Girocco::Config::htmlcache_owning_group invalid ($Girocco::Config::htmlcache_owning_group), refusing to run\n"
93 unless ($htmlcache_owning_group_id = scalar(getgrnam($Girocco::Config::htmlcache_owning_group))) =~ /^\d+$/;
95 if (defined($owning_group_id) && $Girocco::Config::ctags_owning_group) {
96 die "\$Girocco::Config::ctags_owning_group invalid ($Girocco::Config::ctags_owning_group), refusing to run\n"
97 unless ($ctags_owning_group_id = scalar(getgrnam($Girocco::Config::ctags_owning_group))) =~ /^\d+$/;
100 my @allprojs = Girocco::Project::get_full_list;
101 my @projects = ();
103 my $root = $Girocco::Config::reporoot;
104 $root or die "\$Girocco::Config::reporoot is invalid\n";
105 $root =~ s,/+$,,;
106 $root ne "" or $root = "/";
107 $root = realpath($root);
108 if (@ARGV) {
109 my %projnames = map {($_ => 1)} @allprojs;
110 foreach (@ARGV) {
111 s,/+$,,;
112 $_ or $_ = "/";
113 -d $_ and $_ = realpath($_);
114 s,^\Q$root\E/,,;
115 s,\.git$,,;
116 if (!exists($projnames{$_})) {
117 warn "$_: unknown to Girocco (not in etc/group)\n"
118 unless $quiet;
119 next;
121 push(@projects, $_);
123 } else {
124 @projects = sort {lc($a) cmp lc($b)} @allprojs;
127 nice_me(18);
128 my $bad = 0;
129 foreach (@projects) {
130 my $projdir = "$root/$_.git";
131 if (! -d $projdir) {
132 warn "$_: does not exist -- skipping\n" unless $quiet;
133 next;
135 if (!is_git_dir($projdir)) {
136 warn "$_: is not a .git directory -- skipping\n" unless $quiet;
137 next;
139 if (-e "$projdir/.noconfig") {
140 warn "$_: found .noconfig -- skipping\n" unless $quiet;
141 next;
143 if (!chdir($projdir)) {
144 warn "$_: chdir to project directory failed: $!\n" unless $quiet;
145 next;
147 process_one_project($_) or $bad = 1;
150 return $bad ? 1 : 0;
153 my (@mkdirs, @mkfiles);
154 my (@fixdpermsdirs, @fixdpermsrwx, @fixfpermsfiles, @fixfpermsdirs);
155 BEGIN {
156 @mkdirs = qw(refs info hooks ctags htmlcache bundles reflogs objects objects/info);
157 @mkfiles = qw(config info/lastactivity);
158 @fixdpermsdirs = qw(. refs info ctags htmlcache bundles reflogs objects objects/info);
159 @fixdpermsrwx = qw(refs objects);
160 @fixfpermsfiles = qw(HEAD config description packed-refs README.html info/lastactivity
161 info/alternates info/http-alternates info/packs);
162 @fixfpermsdirs = qw(ctags);
165 my (@boolvars, @falsevars, @false0vars, @truevars);
166 BEGIN {
167 @boolvars = qw(gitweb.statusupdates);
168 @falsevars = qw(core.ignorecase receive.denynonfastforwards);
169 @false0vars = qw(receive.autogc);
170 @truevars = qw(receive.updateserverinfo repack.writebitmaps transfer.fsckobjects);
173 my $hdr;
175 sub defval($$) {
176 return defined($_[0]) ? $_[0] : $_[1];
179 sub openfind_ {
180 my $noe = shift;
181 my $duperr;
182 if ($noe) {{
183 open $duperr, '>&2' or last;
184 my $errfd = POSIX::open(File::Spec->devnull, &POSIX::O_RDWR);
185 defined($errfd) or close($duperr), $duperr = undef, last;
186 POSIX::dup2($errfd, 2) or close($duperr), $duperr = undef;
187 POSIX::close($errfd);
189 my $fd;
190 my $ans = open $fd, '-|', "find", @_;
191 if ($noe && defined($duperr) && defined(fileno($duperr))) {
192 POSIX::dup2(fileno($duperr), 2);
193 close($duperr);
195 $ans or die "find failed: $!\n";
196 return $fd;
199 sub openfind { return openfind_(0, @_); }
200 sub openfindne { return openfind_(1, @_); }
202 sub all_remotes
204 my $config = shift;
205 return map({
206 my ($i,$r) = (index($_,"."),rindex($_,"."));
207 substr($_,$i+1,$r-$i-1);
208 } grep(/^remote\.[^.].*\.url$/i, keys(%$config)));
211 sub has_default_fetch_spec
213 my $config = shift;
214 my $default = $config->{'remotes.default'};
215 my @remotes = defined($default) ? split(' ', $default) : all_remotes($config);
216 foreach (@remotes) {
217 defval($config->{"remote.$_.url"},"") ne "" or next;
218 !defined($default) && git_bool($config->{"remote.$_.skipdefaultupdate"}) and next;
219 defval($config->{"remote.$_.fetch"},"") ne "" and return 1;
221 return 0;
224 sub is_native_git_mirror_url
226 my $bu = shift;
227 defined($bu) && $bu ne "" or return 0;
228 # All current or former natively supported by Git URLs return true:
229 # 1. rsync: (removed in 2.8.0, also recognize rsync+ and rsync::)
230 # 2. ftp:/ftps: (strongly discouraged)
231 # 3. git:
232 # 4. http:/https: (smart and non-smart)
233 # 5. ssh:
234 # 6. scp-like ssh syntax [user@]host:[^:/]
235 return $bu =~ /^(?:
236 rsync[:+] |
237 ftps?: |
238 git: |
239 https?: |
240 ssh: |
241 (?:[^\s:\@]+\@)?[^\s:\@+]+:(?!\/\/)[^\s:\\]
242 )/xi;
245 sub process_one_project
247 my ($proj) = @_;
248 my $bad = 0;
249 my $reallybad = 0;
250 $hdr = 0;
251 do {
252 if (! -d $_) {
253 if (-e $_) {
254 warn "$proj: bypassing project, exists but not directory: $_\n" unless $quiet;
255 $reallybad = $bad = 1;
256 last;
257 } else {
258 my $grpid = $owning_group_id;
259 $grpid = $htmlcache_owning_group_id
260 if $htmlcache_owning_group_id && $_ eq "htmlcache";
261 $grpid = $ctags_owning_group_id
262 if $ctags_owning_group_id && $_ eq "ctags";
263 do_mkdir($proj, $_, $grpid) or $bad = 1, last;
266 } foreach (@mkdirs);
267 return 0 if $reallybad;
269 -d $_ && check_dperm($proj, $_) or $bad = 1 foreach (@fixdpermsdirs);
270 my $fp = openfindne(@fixdpermsrwx, qw(-xdev -type d ( ! -path objects/?? -o -prune ) ! -perm), "-$drwxmode", "-print");
271 while (<$fp>) {
272 chomp;
273 change_dpermrwx($proj, $_) or $bad = 1;
275 close($fp) or $bad = 1;
276 $fp = openfind(qw(. -xdev -type d ( ! -path ./objects/?? -o -prune ) ! -perm -a+rx -print));
277 while (<$fp>) {
278 chomp;
279 change_dpermrx($proj, $_) or $bad = 1;
281 close($fp) or $bad = 1;
283 do {
284 if (-e $_) {
285 if (! -f $_) {
286 warn "$proj: bypassing project, exists but not file: $_\n" unless $quiet;
287 $reallybad = $bad = 1;
288 last;
290 } else {
291 my $result = "(dryrun)";
292 if (!$dryrun) {
293 $result = "";
294 my $tf;
295 open($tf, '>', $_) && close ($tf) or $result = "FAILED", $bad = 1;
297 pmsg($proj, "$_: created", $result) unless $quiet;
299 } foreach(@mkfiles);
300 return 0 if $reallybad;
302 $dryrun || check_fperm($proj, "config") or $bad = 1;
303 my $config = read_config_file_hash("config", !$quiet);
304 if (!defined($config)) {
305 warn "$proj: could not read config file -- skipping\n" unless $quiet;
306 return 0;
309 my $do_config = sub {
310 my ($item, $val) = @_;
311 my $oldval = defval($config->{$item},"");
312 my $result = "(dryrun)";
313 if (!$dryrun) {
314 $result = "";
315 system($Girocco::Config::git_bin, "config", "--file", "config", "--replace-all", $item, $val) == 0 or
316 $result = "FAILED", $bad = 1;
318 if (!exists($config->{$item})) {
319 pmsg($proj, "config $item: created \"$val\"", $result) unless $quiet;
320 } else {
321 pmsg($proj, "config $item: \"$oldval\" -> \"$val\"", $result) unless $quiet;
324 my $do_config_unset = sub {
325 my ($item, $msg) = @_;
326 defined($msg) or $msg = "";
327 $msg eq "" or $msg = " " . $msg;
328 my $oldval = defval($config->{$item},"");
329 my $result = "(dryrun)";
330 if (!$dryrun) {
331 $result = "";
332 system($Girocco::Config::git_bin, "config", "--file", "config", "--unset-all", $item) == 0 or
333 $result = "FAILED", $bad = 1;
335 pmsg($proj, "config $item: removed$msg \"$oldval\"", $result) unless $quiet;
338 my $repovers = $config->{'core.repositoryformatversion'};
339 if (!defined($repovers)) {
340 $repovers = "";
341 } elsif ($repovers =~ /^[2345]$/) {
342 pmsg($proj, "WARNING: unknown core.repositoryformatversion value left unchanged: \"$repovers\"");
343 } elsif ($repovers !~ /^[01]$/) {
344 pmsg($proj, "WARNING: replacing invalid core.repositoryformatversion value: \"$repovers\"") unless $quiet;
345 $repovers = "";
347 &$do_config('core.repositoryformatversion', 0) if $repovers eq "";
348 my $hookspath = $Girocco::Config::reporoot . "/_global/hooks";
349 my $cfghooks = defval($config->{'core.hookspath'},"");
350 if ($cfghooks ne $hookspath) {
351 my $updatehookspath = 1;
352 $hookspath = $Girocco::Config::reporoot . "/$proj.git/hooks" if $Girocco::Config::localhooks;
353 if ($cfghooks =~ m{^/[^/]} && -d $cfghooks && -d "hooks") {
354 # tolerate this situation provided the realpath of $cfghooks
355 # matches the realpath of the hooks subdirectory and the hooks
356 # subdirectory exists; actually making sure the correct symlinks
357 # are present remains up to update-all-hooks not us
358 if (realpath($cfghooks) eq realpath("hooks")) {
359 # we do, however, insist that it be stored exactly
360 # as $reporoot/<project_name>.git/hooks in this case because
361 # that's the only guaranteed version that works in the chroot
362 $hookspath = $Girocco::Config::reporoot . "/$proj.git/hooks";
363 $cfghooks eq $hookspath and $updatehookspath = 0;
366 &$do_config('core.hookspath', $hookspath) if $updatehookspath;
368 my $cmplvl = defval($config->{'core.compression'},"");
369 if ($cmplvl !~ /^-?\d+$/ || $cmplvl < -1 || $cmplvl > 9 || "" . (0 + $cmplvl) ne "" . $cmplvl) {
370 pmsg($proj, "WARNING: replacing invalid core.compression value: \"$cmplvl\"") unless $cmplvl eq "" || $quiet;
371 $cmplvl = "";
372 } elsif ($cmplvl != 5) {
373 pmsg($proj, "WARNING: suboptimal core.compression value left unchanged: \"$cmplvl\"") unless $quiet;
375 $cmplvl ne "" or &$do_config('core.compression', 5);
376 my $grpshr = defval($config->{'core.sharedrepository'},"");
377 if ($grpshr eq "" || (valid_bool($grpshr) && !git_bool($grpshr))) {
378 &$do_config('core.sharedrepository', 1);
379 } elsif (!(valid_bool($grpshr) && git_bool($grpshr))) {
380 pmsg($proj, "WARNING: odd core.sharedrepository value left unchanged: \"$grpshr\"");
382 if (git_bool($config->{'core.bare'})) {
383 my $setlaru = 1;
384 my $laru = $config->{'core.logallrefupdates'};
385 if (defined($laru)) {
386 if (valid_bool($laru)) {
387 $setlaru = 0;
388 if (git_bool($laru)) {
389 pmsg($proj, "WARNING: core.logallrefupdates is true (left unchanged)") unless $quiet;
391 } else {
392 pmsg($proj, "WARNING: replacing non-boolean core.logallrefupdates value") unless $quiet;
395 !$setlaru or &$do_config('core.logallrefupdates', 'false');
396 } else {
397 pmsg($proj, "WARNING: core.bare is not true (left unchanged)") unless $quiet;
399 my $precious = defval($config->{'extensions.preciousobjects'},"");
400 valid_bool($precious) && git_bool($precious) or &$do_config('extensions.preciousobjects', 'true');
401 defval($config->{'transfer.unpacklimit'},"") eq "1" or &$do_config('transfer.unpacklimit', 1);
402 lc(defval($config->{'receive.denydeletecurrent'},"")) eq "warn" or &$do_config('receive.denydeletecurrent', 'warn');
403 do {
404 !exists($config->{$_}) || valid_bool(defval($config->{$_},"")) or &$do_config_unset($_, "(not a boolean)");
405 } foreach (@boolvars);
406 do {
407 (valid_bool(defval($config->{$_},"")) && !git_bool($config->{$_})) or &$do_config($_, "false");
408 } foreach (@falsevars);
409 do {
410 (valid_bool(defval($config->{$_},"")) && !git_bool($config->{$_})) or &$do_config($_, 0);
411 } foreach (@false0vars);
412 do {
413 (valid_bool(defval($config->{$_},"")) && git_bool($config->{$_})) or &$do_config($_, "true");
414 } foreach (@truevars);
416 if (defined($Girocco::Config::owning_group) && $Girocco::Config::owning_group ne "") {
417 $fp = openfind(qw(. -xdev ( -type d -o -type f ) ! -group), $Girocco::Config::owning_group, "-print");
418 while (<$fp>) {
419 chomp;
420 my $grpid = $owning_group_id;
421 $grpid = $htmlcache_owning_group_id if $htmlcache_owning_group_id && m{^\./htmlcache(?:/|$)}i;
422 $grpid = $ctags_owning_group_id if $ctags_owning_group_id && m{^\./ctags(?:/|$)}i;
423 change_group($proj, $_, $grpid) or $bad = 1;
425 close($fp) or $bad = 1;
427 foreach (@fixfpermsfiles) {
428 if (-e $_) {
429 if (! -f $_) {
430 warn "$proj: bypassing project, exists but not file: $_\n" unless $quiet;
431 $reallybad = $bad = 1;
432 last;
434 check_fperm($proj, $_) or $bad = 1;
437 return 0 if $reallybad;
439 $fp = openfindne(@fixfpermsdirs, qw(-xdev -type f ! -perm), $fmodeoct, "-print");
440 while (<$fp>) {
441 chomp;
442 check_fperm($proj, $_) or $bad = 1;
444 close($fp) or $bad = 1;
445 $fp = openfind(qw(. -xdev -type f ! -perm -a+r -print));
446 while (<$fp>) {
447 chomp;
448 check_fpermr($proj, $_) or $bad = 1;
450 close($fp) or $bad = 1;
451 $fp = openfind(qw(. -xdev -type d ( -path ./hooks -o -path ./mob/hooks ) -prune -o -type f -perm +a+x -print));
452 while (<$fp>) {
453 chomp;
454 check_fpermnox($proj, $_) or $bad = 1;
456 close($fp) or $bad = 1;
458 my $bu = defval($config->{'gitweb.baseurl'},"");
459 if (-e ".nofetch") {
460 $bu eq "" or pmsg($proj, "WARNING: .nofetch exists but gitweb.baseurl is not empty ($bu)") unless $quiet;
461 } else {
462 if ($bu eq "") {
463 if (has_default_fetch_spec($config)) {
464 pmsg($proj, "WARNING: gitweb.baseurl is empty and .nofetch does not exist but fetch spec does") unless $quiet;
465 } else {
466 pmsg($proj, "WARNING: gitweb.baseurl is empty and .nofetch does not exist") unless $quiet;
468 } elsif (is_native_git_mirror_url($bu) && !has_default_fetch_spec($config)) {
469 pmsg($proj, "WARNING: gitweb.baseurl is not empty but fetch spec is") unless $quiet;
473 return !$bad;
476 sub do_mkdir
478 my ($proj, $subdir, $grpid) = @_;
479 my $result = "";
480 if (!$dryrun) {
481 mkdir($subdir) && -d "$subdir" or $result = "FAILED";
482 if ($grpid && $grpid != $owning_group_id) {
483 my @info = stat($subdir);
484 if (@info < 6 || $info[2] eq "" || $info[4] eq "" || $info[5] eq "") {
485 $result = "FAILED";
486 } elsif ($info[5] != $grpid) {
487 if (!chown($info[4], $grpid, $subdir)) {
488 $result = "FAILED";
489 warn "chgrp: ($proj) $subdir: $!\n" unless $quiet;
490 } elsif (!chmod($info[2] & 07777, $subdir)) {
491 $result = "FAILED";
492 warn "chmod: ($proj) $subdir: $!\n" unless $quiet;
496 } else {
497 $result = "(dryrun)";
499 pmsg($proj, "$subdir/: created", $result);
500 return $result ne "FAILED";
503 sub check_dperm {
504 my ($proj, $subdir) = @_;
505 my $oldmode = (stat($subdir))[2];
506 if (!defined($oldmode) || $oldmode eq "") {
507 warn "chmod: ($proj) $subdir: No such file or directory\n" unless $quiet;
508 return 0;
510 my $newmode = ($oldmode & ~07777) | $dmode;
511 $newmode == $oldmode and return 1;
512 my $result = "";
513 if (!$dryrun) {
514 if (!chmod($newmode & 07777, $subdir)) {
515 $result = "FAILED";
516 warn "chmod: ($proj) $subdir: $!\n" unless $quiet;
518 } else {
519 $result = "(dryrun)";
521 pmsg($proj, "$subdir/:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result);
522 return $result ne "FAILED";
525 sub change_dpermrwx {
526 my ($proj, $subdir) = @_;
527 my $oldmode = (stat($subdir))[2];
528 if (!defined($oldmode) || $oldmode eq "") {
529 warn "chmod: ($proj) $subdir: No such file or directory\n" unless $quiet;
530 return 0;
532 my $newmode = $oldmode | ($wall ? 0777 : 0775);
533 $newmode == $oldmode and return 1;
534 my $result = "";
535 if (!$dryrun) {
536 if (!chmod($newmode & 07777, $subdir)) {
537 $result = "FAILED";
538 warn "chmod: ($proj) $subdir: $!\n" unless $quiet;
540 } else {
541 $result = "(dryrun)";
543 pmsg($proj, "$subdir/:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result);
544 return $result ne "FAILED";
547 sub change_dpermrx {
548 my ($proj, $subdir) = @_;
549 $subdir =~ s,^\./,,;
550 my $oldmode = (stat($subdir))[2];
551 if (!defined($oldmode) || $oldmode eq "") {
552 warn "chmod: ($proj) $subdir: No such file or directory\n" unless $quiet;
553 return 0;
555 my $newmode = $oldmode | 0555;
556 $newmode == $oldmode and return 1;
557 my $result = "";
558 if (!$dryrun) {
559 if (!chmod($newmode & 07777, $subdir)) {
560 $result = "FAILED";
561 warn "chmod: ($proj) $subdir: $!\n" unless $quiet;
563 } else {
564 $result = "(dryrun)";
566 pmsg($proj, "$subdir/:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result);
567 return $result ne "FAILED";
570 sub check_fperm {
571 my ($proj, $file) = @_;
572 my $oldmode = (stat($file))[2];
573 if (!defined($oldmode) || $oldmode eq "") {
574 warn "chmod: ($proj) $file: No such file or directory\n" unless $quiet;
575 return 0;
577 my $newmode = ($oldmode & ~07777) | $fmode;
578 $newmode == $oldmode and return 1;
579 my $result = "";
580 if (!$dryrun) {
581 if (!chmod($newmode & 07777, $file)) {
582 $result = "FAILED";
583 warn "chmod: ($proj) $file: $!\n" unless $quiet;
585 } else {
586 $result = "(dryrun)";
588 pmsg($proj, "$file:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result);
589 return $result ne "FAILED";
592 sub check_fpermr {
593 my ($proj, $file) = @_;
594 $file =~ s,^\./,,;
595 my $oldmode = (stat($file))[2];
596 if (!defined($oldmode) || $oldmode eq "") {
597 warn "chmod: ($proj) $file: No such file or directory\n" unless $quiet;
598 return 0;
600 my $newmode = $oldmode | 0444;
601 $newmode == $oldmode and return 1;
602 my $result = "";
603 if (!$dryrun) {
604 if (!chmod($newmode & 07777, $file)) {
605 $result = "FAILED";
606 warn "chmod: ($proj) $file: $!\n" unless $quiet;
608 } else {
609 $result = "(dryrun)";
611 pmsg($proj, "$file:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result);
612 return $result ne "FAILED";
615 sub check_fpermnox {
616 my ($proj, $file) = @_;
617 $file =~ s,^\./,,;
618 my $oldmode = (stat($file))[2];
619 if (!defined($oldmode) || $oldmode eq "") {
620 warn "chmod: ($proj) $file: No such file or directory\n" unless $quiet;
621 return 0;
623 my $newmode = $oldmode & ~0111;
624 $newmode == $oldmode and return 1;
625 my $result = "";
626 if (!$dryrun) {
627 if (!chmod($newmode & 07777, $file)) {
628 $result = "FAILED";
629 warn "chmod: ($proj) $file: $!\n" unless $quiet;
631 } else {
632 $result = "(dryrun)";
634 pmsg($proj, "$file:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result);
635 return $result ne "FAILED";
638 sub change_group {
639 my ($proj, $item, $grpid) = @_;
640 $item =~ s,^\./,,;
641 my @info = stat($item);
642 if (@info < 6 || $info[2] eq "" || $info[4] eq "" || $info[5] eq "") {
643 warn "chgrp: ($proj) $item: No such file or directory\n" unless $quiet;
644 return 0;
646 $info[5] == $grpid and return 1;
647 my $result = "";
648 if (!$dryrun) {
649 if (!chown($info[4], $grpid, $item)) {
650 $result = "FAILED";
651 warn "chgrp: ($proj) $item: $!\n" unless $quiet;
652 } elsif (!chmod($info[2] & 07777, $item)) {
653 $result = "FAILED";
654 warn "chmod: ($proj) $item: $!\n" unless $quiet;
656 } else {
657 $result = "(dryrun)";
659 my $isdir = ((($info[2] >> 12) & 017) == 004) ? '/' : '';
660 pmsg($proj, "$item$isdir: group", get_grp_nam($info[5]), '->', get_grp_nam($grpid), $result);
661 return $result ne "FAILED";
664 my $wrote; BEGIN {$wrote = ""}
665 sub pmsg {
666 my $proj = shift;
667 my $msg = join(" ", @_);
668 $msg =~ s/\s+$//;
669 my $prefix = "";
670 if (!$hdr) {
671 $prefix = $wrote . $proj . ":\n";
672 $hdr = 1;
674 print $prefix, " ", join(' ', @_), "\n";
675 $wrote = "\n";
678 my %ftypes;
679 BEGIN {%ftypes = (
680 000 => '?',
681 001 => 'p',
682 002 => 'c',
683 003 => '?',
684 004 => 'd',
685 005 => '?',
686 006 => 'b',
687 007 => '?',
688 010 => '-',
689 011 => '?',
690 012 => 'l',
691 013 => '?',
692 014 => 's',
693 015 => '?',
694 016 => 'w',
695 017 => '?'
697 my %fperms;
698 BEGIN {%fperms = (
699 0 => '---',
700 1 => '--x',
701 2 => '-w-',
702 3 => '-wx',
703 4 => 'r--',
704 5 => 'r-x',
705 6 => 'rw-',
706 7 => 'rwx'
709 sub get_mode_perm {
710 my $mode = $_[0];
711 my $str = $ftypes{($mode >> 12) & 017} .
712 $fperms{($mode >> 6) & 7} .
713 $fperms{($mode >> 3) & 7} .
714 $fperms{$mode & 7};
715 substr($str,3,1) = ($mode & 0100) ? 's' : 'S' if $mode & 04000;
716 substr($str,6,1) = ($mode & 0010) ? 's' : 'S' if $mode & 02000;
717 substr($str,9,1) = ($mode & 0001) ? 't' : 'T' if $mode & 01000;
718 return $str;
721 sub get_perm {
722 my $mode = (stat($_[0]))[2];
723 defined($mode) or return '??????????';
724 return get_mode_perm($mode);
727 sub get_grp_nam {
728 my $grpid = $_[0];
729 defined($grpid) or return '?';
730 my $grpnm = scalar(getgrgid($grpid));
731 return defined($grpnm) && $grpnm ne "" ? $grpnm : $grpid;
734 sub get_grp {
735 my $grp = (stat($_[0]))[5];
736 defined($grp) or return '?';
737 return get_grp_nam($grp);
740 __END__
742 =head1 NAME
744 update-all-config.pl - Update all projects' config settings
746 =head1 SYNOPSIS
748 update-all-config.pl [<options>] [<projname>]...
750 Options:
751 -h | --help detailed instructions
752 -V | --version show version
753 -n | --dry-run show what would be done but don't do it
754 -f | --force run without a Config.pm owning_group
755 -q | --quiet suppress change messages
757 <projname> if given, only operate on these projects
759 =head1 OPTIONS
761 =over 8
763 =item B<-h>, B<--help>
765 Print the full description of update-all-config.pl's options.
767 =item B<-V>, B<--version>
769 Print the version of update-all-config.pl.
771 =item B<-n>, B<--dry-run>
773 Do not actually make any changes, just show what would be done without
774 actually doing it.
776 =item B<-q>, B<--quiet>
778 Suppress the messages about what's actually being changed. This option
779 is ignored if B<--dry-run> is in effect.
781 The warnings about missing and unknown-to-Girocco projects are also
782 suppressed by this option.
784 =item B<-f>, B<--force>
786 Allow running without a $Girocco::Config::owning_group set. This is not
787 recommended as it results in world-writable items being used (instead of
788 just world-readable).
790 =item B<<projname>>
792 If no project names are specified then I<all> projects are processed.
794 If one or more project names are specified then only those projects are
795 processed. Specifying non-existent projects produces a warning for them,
796 but the rest of the projects specified will still be processed.
798 Each B<projname> may be either a full absolute path starting with
799 $Girocco::Config::reporoot or just the project name part with or without
800 a trailing C<.git>.
802 Any explicitly specified projects that do exist but are not known to
803 Girocco will be skipped (with a warning).
805 =back
807 =head1 DESCRIPTION
809 Inspect the C<config> files of Girocco projects (i.e. $GIT_DIR/config) and
810 look for anomalies and out-of-date settings.
812 Additionally check the existence and permissions on various files and
813 directories in the project.
815 If an explicity specified project is located under $Girocco::Config::reporoot
816 but is not actually known to Girocco (i.e. it's not in the etc/group file)
817 then it will be skipped.
819 By default, any anomalies or out-of-date settings will be corrected with a
820 message to that effect. However using B<--dry-run> will only show the
821 correction(s) which would be made without making them and B<--quiet> will make
822 the correction(s) without any messages.
824 Any projects that have a C<$GIT_DIR/.noconfig> file are always skipped (with a
825 message unless B<--quiet> is used).
827 =cut