toolbox/update-all-config.pl: quiet means quiet
[girocco.git] / toolbox / update-all-config.pl
blob625ffb5d3922ad1b2551554d9b7cd1253b1b7e06
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 BEGIN {
16 eval 'require Pod::Text::Termcap; 1;' and
17 @Pod::Usage::ISA = (qw( Pod::Text::Termcap ));
18 defined($ENV{PERLDOC}) && $ENV{PERLDOC} ne "" or
19 $ENV{PERLDOC} = "-oterm -oman";
21 use lib "__BASEDIR__";
22 use Girocco::Config;
23 use Girocco::Util;
24 use Girocco::CLIUtil;
25 use Girocco::Project;
27 my $shbin;
28 BEGIN {
29 $shbin = $Girocco::Config::posix_sh_bin;
30 defined($shbin) && $shbin ne "" or $shbin = "/bin/sh";
33 exit(&main(@ARGV)||0);
35 my ($dryrun, $force, $quiet);
37 sub die_usage {
38 pod2usage(-exitval => 2);
41 sub do_help {
42 pod2usage(-verbose => 2, -exitval => 0);
45 sub do_version {
46 print basename($0), " version ", $VERSION, "\n";
47 exit 0;
50 my ($dmode, $dperm, $drwxmode, $fmode, $fmodeoct, $fperm, $wall);
51 BEGIN {
52 $dmode=02775;
53 $dperm='drwxrwsr-x';
54 $drwxmode='ug+rwx,o+rx';
55 $fmode=0664;
56 $fmodeoct='0664';
57 $fperm='-rw-rw-r--';
58 $wall=0;
61 my $owning_group_id;
62 my $htmlcache_owning_group_id;
63 my $ctags_owning_group_id;
64 my $progress;
66 sub pwarn { $progress->warn(@_) }
68 sub main {
69 local *ARGV = \@_;
70 my ($help, $version);
72 umask 002;
73 close(DATA) if fileno(DATA);
74 Getopt::Long::Configure('bundling');
75 GetOptions(
76 'help|h' => sub {do_help},
77 'version|V' => sub {do_version},
78 'dry-run|n' => \$dryrun,
79 'quiet|q' => \$quiet,
80 'force|f' => \$force,
81 ) or die_usage;
82 $dryrun and $quiet = 0;
84 -f jailed_file("/etc/group") or
85 die "Girocco group file not found: " . jailed_file("/etc/group") . "\n";
87 if (!defined($Girocco::Config::owning_group) || $Girocco::Config::owning_group eq "") {
88 die "\$Girocco::Config::owning_group unset, refusing to run without --force\n" unless $force;
89 $dmode=02777;
90 $dperm='drwxrwsrwx';
91 $drwxmode='a+rwx';
92 $fmode=0666;
93 $fmodeoct='0666';
94 $fperm='-rw-rw-rw-';
95 $wall=1;
96 warn "Mode 666 in effect\n" unless $quiet;
97 } elsif (($owning_group_id = scalar(getgrnam($Girocco::Config::owning_group))) !~ /^\d+$/) {
98 die "\$Girocco::Config::owning_group invalid ($Girocco::Config::owning_group), refusing to run\n";
100 if (defined($owning_group_id) && $Girocco::Config::htmlcache_owning_group) {
101 die "\$Girocco::Config::htmlcache_owning_group invalid ($Girocco::Config::htmlcache_owning_group), refusing to run\n"
102 unless ($htmlcache_owning_group_id = scalar(getgrnam($Girocco::Config::htmlcache_owning_group))) =~ /^\d+$/;
104 if (defined($owning_group_id) && $Girocco::Config::ctags_owning_group) {
105 die "\$Girocco::Config::ctags_owning_group invalid ($Girocco::Config::ctags_owning_group), refusing to run\n"
106 unless ($ctags_owning_group_id = scalar(getgrnam($Girocco::Config::ctags_owning_group))) =~ /^\d+$/;
109 my @allprojs = Girocco::Project::get_full_list;
110 my @projects = ();
112 my $root = $Girocco::Config::reporoot;
113 $root or die "\$Girocco::Config::reporoot is invalid\n";
114 $root =~ s,/+$,,;
115 $root ne "" or $root = "/";
116 $root = realpath($root);
117 $root = $1 if $root =~ m|^(/.+)$|;
118 if (@ARGV) {
119 my %projnames = map {($_ => 1)} @allprojs;
120 foreach (@ARGV) {
121 s,/+$,,;
122 $_ or $_ = "/";
123 -d $_ and $_ = realpath($_);
124 $_ = $1 if $_ =~ m|^(.+)$|;
125 s,^\Q$root\E/,,;
126 s,\.git$,,;
127 if (!exists($projnames{$_})) {
128 warn "$_: unknown to Girocco (not in etc/group)\n"
129 unless $quiet;
130 next;
132 push(@projects, $_);
134 } else {
135 @projects = sort {lc($a) cmp lc($b) || $a cmp $b} @allprojs;
138 nice_me(18);
139 my $bad = 0;
140 $progress = Girocco::CLIUtil::Progress->new(scalar(@projects),
141 "Updating config files");
142 foreach (@projects) {
143 my $projdir = "$root/$_.git";
144 if (! -d $projdir) {
145 pwarn "$_: does not exist -- skipping\n" unless $quiet;
146 next;
148 if (!is_git_dir($projdir)) {
149 pwarn "$_: is not a .git directory -- skipping\n" unless $quiet;
150 next;
152 if (-e "$projdir/.noconfig") {
153 pwarn "$_: found .noconfig -- skipping\n" unless $quiet;
154 next;
156 if (!chdir($projdir)) {
157 pwarn "$_: chdir to project directory failed: $!\n" unless $quiet;
158 next;
160 process_one_project($_) or $bad = 1;
161 } continue {$progress->update}
162 $progress = undef;
164 return $bad ? 1 : 0;
167 my (@mkdirs, @mkfiles);
168 my (@fixdpermsdirs, @fixdpermsrwx, @fixfpermsfiles, @fixfpermsdirs);
169 BEGIN {
170 @mkdirs = qw(refs info hooks ctags htmlcache bundles reflogs objects objects/info);
171 @mkfiles = qw(config info/lastactivity);
172 @fixdpermsdirs = qw(. refs info ctags htmlcache bundles reflogs objects objects/info);
173 @fixdpermsrwx = qw(refs objects);
174 @fixfpermsfiles = qw(HEAD config description packed-refs README.html info/lastactivity
175 info/alternates info/http-alternates info/packs);
176 @fixfpermsdirs = qw(ctags);
179 my (@boolvars, @falsevars, @false0vars, @truevars);
180 BEGIN {
181 @boolvars = qw(gitweb.statusupdates);
182 @falsevars = qw(core.ignorecase receive.denynonfastforwards);
183 @false0vars = qw(receive.autogc);
184 @truevars = qw(receive.fsckobjects receive.updateserverinfo repack.writebitmaps transfer.fsckobjects);
187 my $hdr;
189 sub defval($$) {
190 return defined($_[0]) ? $_[0] : $_[1];
193 sub openfind_ {
194 my $noe = shift;
195 my $duperr;
196 if ($noe) {{
197 open $duperr, '>&2' or last;
198 my $errfd = POSIX::open(File::Spec->devnull, &POSIX::O_RDWR);
199 defined($errfd) or close($duperr), $duperr = undef, last;
200 POSIX::dup2($errfd, 2) or close($duperr), $duperr = undef;
201 POSIX::close($errfd);
203 my $fd;
204 my $ans = open $fd, '-|', "find", @_;
205 if ($noe && defined($duperr) && defined(fileno($duperr))) {
206 POSIX::dup2(fileno($duperr), 2);
207 close($duperr);
209 $ans or die "\nfind failed: $!\n";
210 return $fd;
213 sub openfind { return openfind_(0, @_); }
214 sub openfindne { return openfind_(1, @_); }
216 sub all_remotes
218 my $config = shift;
219 return map({
220 my ($i,$r) = (index($_,"."),rindex($_,"."));
221 substr($_,$i+1,$r-$i-1);
222 } grep(/^remote\.[^.].*\.url$/i, keys(%$config)));
225 sub has_default_fetch_spec
227 my $config = shift;
228 my $default = $config->{'remotes.default'};
229 my @remotes = defined($default) ? split(' ', $default) : all_remotes($config);
230 foreach (@remotes) {
231 defval($config->{"remote.$_.url"},"") ne "" or next;
232 !defined($default) && git_bool($config->{"remote.$_.skipdefaultupdate"}) and next;
233 defval($config->{"remote.$_.fetch"},"") ne "" and return 1;
235 return 0;
238 sub is_native_git_mirror_url
240 my $bu = shift;
241 defined($bu) && $bu ne "" or return 0;
242 # All current or former natively supported by Git URLs return true:
243 # 1. rsync: (removed in 2.8.0, also recognize rsync+ and rsync::)
244 # 2. ftp:/ftps: (strongly discouraged)
245 # 3. git:
246 # 4. http:/https: (smart and non-smart)
247 # 5. ssh:
248 # 6. scp-like ssh syntax [user@]host:[^:/]
249 return $bu =~ /^(?:
250 rsync[:+] |
251 ftps?: |
252 git: |
253 https?: |
254 ssh: |
255 (?:[^\s:\@]+\@)?[^\s:\@+]+:(?!\/\/)[^\s:\\]
256 )/xi;
259 sub process_one_project
261 my ($proj) = @_;
262 my $bad = 0;
263 my $reallybad = 0;
264 $hdr = 0;
265 do {
266 if (! -d $_) {
267 if (-e $_) {
268 pwarn "$proj: bypassing project, exists but not directory: $_\n" unless $quiet;
269 $reallybad = $bad = 1;
270 last;
271 } else {
272 my $grpid = $owning_group_id;
273 $grpid = $htmlcache_owning_group_id
274 if $htmlcache_owning_group_id && $_ eq "htmlcache";
275 $grpid = $ctags_owning_group_id
276 if $ctags_owning_group_id && $_ eq "ctags";
277 do_mkdir($proj, $_, $grpid) or $bad = 1, last;
280 } foreach (@mkdirs);
281 return 0 if $reallybad;
283 -d $_ && check_dperm($proj, $_) or $bad = 1 foreach (@fixdpermsdirs);
284 my $fp = openfindne(@fixdpermsrwx, qw(-xdev -type d ( ! -path objects/?? -o -prune ) ! -perm), "-$drwxmode", "-print");
285 while (<$fp>) {
286 chomp;
287 change_dpermrwx($proj, $_) or $bad = 1;
289 close($fp) or $bad = 1;
290 $fp = openfind(qw(. -xdev -type d ( ! -path ./objects/?? -o -prune ) ! -perm -a+rx -print));
291 while (<$fp>) {
292 chomp;
293 change_dpermrx($proj, $_) or $bad = 1;
295 close($fp) or $bad = 1;
297 do {
298 if (-e $_) {
299 if (! -f $_) {
300 pwarn "$proj: bypassing project, exists but not file: $_\n" unless $quiet;
301 $reallybad = $bad = 1;
302 last;
304 } else {
305 my $result = "(dryrun)";
306 if (!$dryrun) {
307 $result = "";
308 my $tf;
309 open($tf, '>', $_) && close ($tf) or $result = "FAILED", $bad = 1;
311 pmsg($proj, "$_: created", $result) unless $quiet;
313 } foreach(@mkfiles);
314 return 0 if $reallybad;
316 $dryrun || check_fperm($proj, "config") or $bad = 1;
317 my $config = read_config_file_hash("config", !$quiet);
318 if (!defined($config)) {
319 pwarn "$proj: could not read config file -- skipping\n" unless $quiet;
320 return 0;
323 my $do_config = sub {
324 my ($item, $val) = @_;
325 my $oldval = defval($config->{$item},"");
326 my $result = "(dryrun)";
327 if (!$dryrun) {
328 $result = "";
329 system($Girocco::Config::git_bin, "config", "--file", "config", "--replace-all", $item, $val) == 0 or
330 $result = "FAILED", $bad = 1;
332 if (!exists($config->{$item})) {
333 pmsg($proj, "config $item: created \"$val\"", $result) unless $quiet;
334 } else {
335 pmsg($proj, "config $item: \"$oldval\" -> \"$val\"", $result) unless $quiet;
338 my $do_config_unset = sub {
339 my ($item, $msg) = @_;
340 defined($msg) or $msg = "";
341 $msg eq "" or $msg = " " . $msg;
342 my $oldval = defval($config->{$item},"");
343 my $result = "(dryrun)";
344 if (!$dryrun) {
345 $result = "";
346 system($Girocco::Config::git_bin, "config", "--file", "config", "--unset-all", $item) == 0 or
347 $result = "FAILED", $bad = 1;
349 pmsg($proj, "config $item: removed$msg \"$oldval\"", $result) unless $quiet;
352 my $repovers = $config->{'core.repositoryformatversion'};
353 if (!defined($repovers)) {
354 $repovers = "";
355 } elsif ($repovers =~ /^[2345]$/) {
356 pmsg($proj, "WARNING: unknown core.repositoryformatversion value left unchanged: \"$repovers\"")
357 unless $quiet;
358 } elsif ($repovers !~ /^[01]$/) {
359 pmsg($proj, "WARNING: replacing invalid core.repositoryformatversion value: \"$repovers\"")
360 unless $quiet;
361 $repovers = "";
363 &$do_config('core.repositoryformatversion', 0) if $repovers eq "";
364 my $hookspath = $Girocco::Config::reporoot . "/_global/hooks";
365 my $cfghooks = defval($config->{'core.hookspath'},"");
366 if ($cfghooks ne $hookspath) {
367 my $updatehookspath = 1;
368 $hookspath = $Girocco::Config::reporoot . "/$proj.git/hooks" if $Girocco::Config::localhooks;
369 if ($cfghooks =~ m{^/[^/]} && -d $cfghooks && -d "hooks") {
370 # tolerate this situation provided the realpath of $cfghooks
371 # matches the realpath of the hooks subdirectory and the hooks
372 # subdirectory exists; actually making sure the correct symlinks
373 # are present remains up to update-all-hooks not us
374 if (realpath($cfghooks) eq realpath("hooks")) {
375 # we do, however, insist that it be stored exactly
376 # as $reporoot/<project_name>.git/hooks in this case because
377 # that's the only guaranteed version that works in the chroot
378 $hookspath = $Girocco::Config::reporoot . "/$proj.git/hooks";
379 $cfghooks eq $hookspath and $updatehookspath = 0;
382 &$do_config('core.hookspath', $hookspath) if $updatehookspath;
384 my $cmplvl = defval($config->{'core.compression'},"");
385 if ($cmplvl !~ /^-?\d+$/ || $cmplvl < -1 || $cmplvl > 9 || "" . (0 + $cmplvl) ne "" . $cmplvl) {
386 pmsg($proj, "WARNING: replacing invalid core.compression value: \"$cmplvl\"")
387 unless $cmplvl eq "" || $quiet;
388 $cmplvl = "";
389 } elsif ($cmplvl != 5) {
390 pmsg($proj, "WARNING: suboptimal core.compression value left unchanged: \"$cmplvl\"") unless $quiet;
392 $cmplvl ne "" or &$do_config('core.compression', 5);
393 my $grpshr = defval($config->{'core.sharedrepository'},"");
394 if ($grpshr eq "" || (valid_bool($grpshr) && !git_bool($grpshr))) {
395 &$do_config('core.sharedrepository', 1);
396 } elsif (!(valid_bool($grpshr) && git_bool($grpshr))) {
397 pmsg($proj, "WARNING: odd core.sharedrepository value left unchanged: \"$grpshr\"") unless $quiet;
399 if (git_bool($config->{'core.bare'})) {
400 my $setlaru = 1;
401 my $laru = $config->{'core.logallrefupdates'};
402 if (defined($laru)) {
403 if (valid_bool($laru)) {
404 $setlaru = 0;
405 if (git_bool($laru)) {
406 pmsg($proj, "WARNING: core.logallrefupdates is true (left unchanged)")
407 unless $quiet || -d "worktrees";
409 } else {
410 pmsg($proj, "WARNING: replacing non-boolean core.logallrefupdates value") unless $quiet;
413 !$setlaru or &$do_config('core.logallrefupdates', 'false');
414 } else {
415 pmsg($proj, "WARNING: core.bare is not true (left unchanged)") unless $quiet;
417 my $precious = defval($config->{'extensions.preciousobjects'},"");
418 valid_bool($precious) && git_bool($precious) or &$do_config('extensions.preciousobjects', 'true');
419 defval($config->{'transfer.unpacklimit'},"") eq "1" or &$do_config('transfer.unpacklimit', 1);
420 lc(defval($config->{'receive.denydeletecurrent'},"")) eq "warn" or &$do_config('receive.denydeletecurrent', 'warn');
421 do {
422 !exists($config->{$_}) || valid_bool(defval($config->{$_},"")) or &$do_config_unset($_, "(not a boolean)");
423 } foreach (@boolvars);
424 do {
425 (valid_bool(defval($config->{$_},"")) && !git_bool($config->{$_})) or &$do_config($_, "false");
426 } foreach (@falsevars);
427 do {
428 (valid_bool(defval($config->{$_},"")) && !git_bool($config->{$_})) or &$do_config($_, 0);
429 } foreach (@false0vars);
430 do {
431 (valid_bool(defval($config->{$_},"")) && git_bool($config->{$_})) or &$do_config($_, "true");
432 } foreach (@truevars);
434 if (defined($Girocco::Config::owning_group) && $Girocco::Config::owning_group ne "") {
435 $fp = openfind(qw(. -xdev ( -type d -o -type f ) ! -group), $Girocco::Config::owning_group, "-print");
436 while (<$fp>) {
437 chomp;
438 my $grpid = $owning_group_id;
439 $grpid = $htmlcache_owning_group_id if $htmlcache_owning_group_id && m{^\./htmlcache(?:/|$)}i;
440 $grpid = $ctags_owning_group_id if $ctags_owning_group_id && m{^\./ctags(?:/|$)}i;
441 change_group($proj, $_, $grpid) or $bad = 1;
443 close($fp) or $bad = 1;
445 foreach (@fixfpermsfiles) {
446 if (-e $_) {
447 if (! -f $_) {
448 pwarn "$proj: bypassing project, exists but not file: $_\n" unless $quiet;
449 $reallybad = $bad = 1;
450 last;
452 check_fperm($proj, $_) or $bad = 1;
455 return 0 if $reallybad;
457 $fp = openfindne(@fixfpermsdirs, qw(-xdev -type f ! -perm), $fmodeoct, "-print");
458 while (<$fp>) {
459 chomp;
460 check_fperm($proj, $_) or $bad = 1;
462 close($fp) or $bad = 1;
463 $fp = openfind(qw(. -xdev -type f ! -perm -a+r -print));
464 while (<$fp>) {
465 chomp;
466 check_fpermr($proj, $_) or $bad = 1;
468 close($fp) or $bad = 1;
469 $fp = openfind(qw(. -xdev -type d ( -path ./hooks -o -path ./mob/hooks ) -prune -o -type f ( -perm -u=x -o -perm -g=x -o -perm -o=x ) -print));
470 while (<$fp>) {
471 chomp;
472 check_fpermnox($proj, $_) or $bad = 1;
474 close($fp) or $bad = 1;
476 my $bu = defval($config->{'gitweb.baseurl'},"");
477 if (-e ".nofetch") {
478 $bu eq "" or pmsg($proj, "WARNING: .nofetch exists but gitweb.baseurl is not empty ($bu)") unless $quiet;
479 } else {
480 if ($bu eq "") {
481 if (has_default_fetch_spec($config)) {
482 pmsg($proj, "WARNING: gitweb.baseurl is empty and .nofetch does not exist but fetch spec does") unless $quiet;
483 } else {
484 pmsg($proj, "WARNING: gitweb.baseurl is empty and .nofetch does not exist") unless $quiet;
486 } elsif (is_native_git_mirror_url($bu) && !has_default_fetch_spec($config)) {
487 pmsg($proj, "WARNING: gitweb.baseurl is not empty but fetch spec is") unless $quiet;
491 return !$bad;
494 sub do_mkdir
496 my ($proj, $subdir, $grpid) = @_;
497 my $result = "";
498 if (!$dryrun) {
499 mkdir($subdir) && -d "$subdir" or $result = "FAILED";
500 if ($grpid && $grpid != $owning_group_id) {
501 my @info = stat($subdir);
502 if (@info < 6 || $info[2] eq "" || $info[4] eq "" || $info[5] eq "") {
503 $result = "FAILED";
504 } elsif ($info[5] != $grpid) {
505 if (!chown($info[4], $grpid, $subdir)) {
506 $result = "FAILED";
507 pwarn "chgrp: ($proj) $subdir: $!\n" unless $quiet;
508 } elsif (!chmod($info[2] & 07777, $subdir)) {
509 $result = "FAILED";
510 pwarn "chmod: ($proj) $subdir: $!\n" unless $quiet;
514 } else {
515 $result = "(dryrun)";
517 pmsg($proj, "$subdir/: created", $result) unless $quiet;
518 return $result ne "FAILED";
521 sub check_dperm {
522 my ($proj, $subdir) = @_;
523 my $oldmode = (stat($subdir))[2];
524 if (!defined($oldmode) || $oldmode eq "") {
525 pwarn "chmod: ($proj) $subdir: No such file or directory\n" unless $quiet;
526 return 0;
528 my $newmode = ($oldmode & ~07777) | $dmode;
529 $newmode == $oldmode and return 1;
530 my $result = "";
531 if (!$dryrun) {
532 if (!chmod($newmode & 07777, $subdir)) {
533 $result = "FAILED";
534 pwarn "chmod: ($proj) $subdir: $!\n" unless $quiet;
536 } else {
537 $result = "(dryrun)";
539 pmsg($proj, "$subdir/:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result) unless $quiet;
540 return $result ne "FAILED";
543 sub change_dpermrwx {
544 my ($proj, $subdir) = @_;
545 my $oldmode = (stat($subdir))[2];
546 if (!defined($oldmode) || $oldmode eq "") {
547 pwarn "chmod: ($proj) $subdir: No such file or directory\n" unless $quiet;
548 return 0;
550 my $newmode = $oldmode | ($wall ? 0777 : 0775);
551 $newmode == $oldmode and return 1;
552 my $result = "";
553 if (!$dryrun) {
554 if (!chmod($newmode & 07777, $subdir)) {
555 $result = "FAILED";
556 pwarn "chmod: ($proj) $subdir: $!\n" unless $quiet;
558 } else {
559 $result = "(dryrun)";
561 pmsg($proj, "$subdir/:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result) unless $quiet;
562 return $result ne "FAILED";
565 sub change_dpermrx {
566 my ($proj, $subdir) = @_;
567 $subdir =~ s,^\./,,;
568 my $oldmode = (stat($subdir))[2];
569 if (!defined($oldmode) || $oldmode eq "") {
570 pwarn "chmod: ($proj) $subdir: No such file or directory\n" unless $quiet;
571 return 0;
573 my $newmode = $oldmode | 0555;
574 $newmode == $oldmode and return 1;
575 my $result = "";
576 if (!$dryrun) {
577 if (!chmod($newmode & 07777, $subdir)) {
578 $result = "FAILED";
579 pwarn "chmod: ($proj) $subdir: $!\n" unless $quiet;
581 } else {
582 $result = "(dryrun)";
584 pmsg($proj, "$subdir/:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result) unless $quiet;
585 return $result ne "FAILED";
588 sub check_fperm {
589 my ($proj, $file) = @_;
590 my $oldmode = (stat($file))[2];
591 if (!defined($oldmode) || $oldmode eq "") {
592 pwarn "chmod: ($proj) $file: No such file or directory\n" unless $quiet;
593 return 0;
595 my $newmode = ($oldmode & ~07777) | $fmode;
596 $newmode == $oldmode and return 1;
597 my $result = "";
598 if (!$dryrun) {
599 if (!chmod($newmode & 07777, $file)) {
600 $result = "FAILED";
601 pwarn "chmod: ($proj) $file: $!\n" unless $quiet;
603 } else {
604 $result = "(dryrun)";
606 pmsg($proj, "$file:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result) unless $quiet;
607 return $result ne "FAILED";
610 sub check_fpermr {
611 my ($proj, $file) = @_;
612 $file =~ s,^\./,,;
613 my $oldmode = (stat($file))[2];
614 if (!defined($oldmode) || $oldmode eq "") {
615 pwarn "chmod: ($proj) $file: No such file or directory\n" unless $quiet;
616 return 0;
618 my $newmode = $oldmode | 0444;
619 $newmode == $oldmode and return 1;
620 my $result = "";
621 if (!$dryrun) {
622 if (!chmod($newmode & 07777, $file)) {
623 $result = "FAILED";
624 pwarn "chmod: ($proj) $file: $!\n" unless $quiet;
626 } else {
627 $result = "(dryrun)";
629 pmsg($proj, "$file:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result) unless $quiet;
630 return $result ne "FAILED";
633 sub check_fpermnox {
634 my ($proj, $file) = @_;
635 $file =~ s,^\./,,;
636 my $oldmode = (stat($file))[2];
637 if (!defined($oldmode) || $oldmode eq "") {
638 pwarn "chmod: ($proj) $file: No such file or directory\n" unless $quiet;
639 return 0;
641 my $newmode = $oldmode & ~0111;
642 $newmode == $oldmode and return 1;
643 my $result = "";
644 if (!$dryrun) {
645 if (!chmod($newmode & 07777, $file)) {
646 $result = "FAILED";
647 pwarn "chmod: ($proj) $file: $!\n" unless $quiet;
649 } else {
650 $result = "(dryrun)";
652 pmsg($proj, "$file:", get_mode_perm($oldmode), '->', get_mode_perm($newmode), $result) unless $quiet;
653 return $result ne "FAILED";
656 sub change_group {
657 my ($proj, $item, $grpid) = @_;
658 $item =~ s,^\./,,;
659 my @info = stat($item);
660 if (@info < 6 || $info[2] eq "" || $info[4] eq "" || $info[5] eq "") {
661 pwarn "chgrp: ($proj) $item: No such file or directory\n" unless $quiet;
662 return 0;
664 $info[5] == $grpid and return 1;
665 my $result = "";
666 if (!$dryrun) {
667 if (!chown($info[4], $grpid, $item)) {
668 $result = "FAILED";
669 pwarn "chgrp: ($proj) $item: $!\n" unless $quiet;
670 } elsif (!chmod($info[2] & 07777, $item)) {
671 $result = "FAILED";
672 pwarn "chmod: ($proj) $item: $!\n" unless $quiet;
674 } else {
675 $result = "(dryrun)";
677 my $isdir = ((($info[2] >> 12) & 017) == 004) ? '/' : '';
678 pmsg($proj, "$item$isdir: group", get_grp_nam($info[5]), '->', get_grp_nam($grpid), $result) unless $quiet;
679 return $result ne "FAILED";
682 my $wrote; BEGIN {$wrote = ""}
683 sub pmsg {
684 my $proj = shift;
685 my $msg = join(" ", @_);
686 $msg =~ s/\s+$//;
687 my $prefix = "";
688 if (!$hdr) {
689 $prefix = $wrote . $proj . ":\n";
690 $hdr = 1;
692 $progress->emit($prefix . " " . join(' ', @_) . "\n");
693 $wrote = "\n";
696 my %ftypes;
697 BEGIN {%ftypes = (
698 000 => '?',
699 001 => 'p',
700 002 => 'c',
701 003 => '?',
702 004 => 'd',
703 005 => '?',
704 006 => 'b',
705 007 => '?',
706 010 => '-',
707 011 => '?',
708 012 => 'l',
709 013 => '?',
710 014 => 's',
711 015 => '?',
712 016 => 'w',
713 017 => '?'
715 my %fperms;
716 BEGIN {%fperms = (
717 0 => '---',
718 1 => '--x',
719 2 => '-w-',
720 3 => '-wx',
721 4 => 'r--',
722 5 => 'r-x',
723 6 => 'rw-',
724 7 => 'rwx'
727 sub get_mode_perm {
728 my $mode = $_[0];
729 my $str = $ftypes{($mode >> 12) & 017} .
730 $fperms{($mode >> 6) & 7} .
731 $fperms{($mode >> 3) & 7} .
732 $fperms{$mode & 7};
733 substr($str,3,1) = ($mode & 0100) ? 's' : 'S' if $mode & 04000;
734 substr($str,6,1) = ($mode & 0010) ? 's' : 'S' if $mode & 02000;
735 substr($str,9,1) = ($mode & 0001) ? 't' : 'T' if $mode & 01000;
736 return $str;
739 sub get_perm {
740 my $mode = (stat($_[0]))[2];
741 defined($mode) or return '??????????';
742 return get_mode_perm($mode);
745 sub get_grp_nam {
746 my $grpid = $_[0];
747 defined($grpid) or return '?';
748 my $grpnm = scalar(getgrgid($grpid));
749 return defined($grpnm) && $grpnm ne "" ? $grpnm : $grpid;
752 sub get_grp {
753 my $grp = (stat($_[0]))[5];
754 defined($grp) or return '?';
755 return get_grp_nam($grp);
758 __END__
760 =head1 NAME
762 update-all-config.pl - Update all projects' config settings
764 =head1 SYNOPSIS
766 update-all-config.pl [<options>] [<projname>]...
768 Options:
769 -h | --help detailed instructions
770 -V | --version show version
771 -n | --dry-run show what would be done but don't do it
772 -f | --force run without a Config.pm owning_group
773 -q | --quiet suppress change messages
775 <projname> if given, only operate on these projects
777 =head1 OPTIONS
779 =over 8
781 =item B<-h>, B<--help>
783 Print the full description of update-all-config.pl's options.
785 =item B<-V>, B<--version>
787 Print the version of update-all-config.pl.
789 =item B<-n>, B<--dry-run>
791 Do not actually make any changes, just show what would be done without
792 actually doing it.
794 =item B<-q>, B<--quiet>
796 Suppress the messages about what's actually being changed. This option
797 is ignored if B<--dry-run> is in effect.
799 The warnings about missing and unknown-to-Girocco projects are also
800 suppressed by this option.
802 =item B<-f>, B<--force>
804 Allow running without a $Girocco::Config::owning_group set. This is not
805 recommended as it results in world-writable items being used (instead of
806 just world-readable).
808 =item B<<projname>>
810 If no project names are specified then I<all> projects are processed.
812 If one or more project names are specified then only those projects are
813 processed. Specifying non-existent projects produces a warning for them,
814 but the rest of the projects specified will still be processed.
816 Each B<projname> may be either a full absolute path starting with
817 $Girocco::Config::reporoot or just the project name part with or without
818 a trailing C<.git>.
820 Any explicitly specified projects that do exist but are not known to
821 Girocco will be skipped (with a warning).
823 =back
825 =head1 DESCRIPTION
827 Inspect the C<config> files of Girocco projects (i.e. $GIT_DIR/config) and
828 look for anomalies and out-of-date settings.
830 Additionally check the existence and permissions on various files and
831 directories in the project.
833 If an explicity specified project is located under $Girocco::Config::reporoot
834 but is not actually known to Girocco (i.e. it's not in the etc/group file)
835 then it will be skipped.
837 By default, any anomalies or out-of-date settings will be corrected with a
838 message to that effect. However using B<--dry-run> will only show the
839 correction(s) which would be made without making them and B<--quiet> will make
840 the correction(s) without any messages.
842 Any projects that have a C<$GIT_DIR/.noconfig> file are always skipped (with a
843 message unless B<--quiet> is used).
845 =cut