]> git.donarmstrong.com Git - bin.git/blob - sa
stop using User in sa
[bin.git] / sa
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use Getopt::Long;
7 use Pod::Usage;
8
9 =head1 NAME
10
11 sa - sync all
12
13 =head1 SYNOPSIS
14
15 sa [options] [status|update|checkout] [directories to sync]
16
17  Options:
18   --quick, -Q don't update external projects
19   --verbose, -v be more verbose
20   --quiet, -q be quiet
21   --debug, -d debugging level (Default 0)
22   --help, -h display this help
23   --man, -m display manual
24
25 =head1 OPTIONS
26
27 =over
28
29 =item B<--debug, -d>
30
31 Debug verbosity. (Default 0)
32
33 =item B<--help, -h>
34
35 Display brief useage information.
36
37 =item B<--man, -m>
38
39 Display this manual.
40
41 =back
42
43 =head1 EXAMPLES
44
45 =cut
46
47
48
49 #use User;
50 use IO::File;
51 use vars qw($DEBUG $VERBOSE);
52
53 my %options = (quick           => 1,
54                quiet           => 1,
55                debug           => 0,
56                help            => 0,
57                man             => 0,
58                verbose         => 0,
59               );
60
61 GetOptions(\%options,'quick|Q+','quiet|q!','debug|d+','help|h|?','man|m','verbose|v+');
62
63 pod2usage() if $options{help};
64 pod2usage({verbose=>2}) if $options{man};
65
66 # parse configuration file
67
68 =head1 CONFIGURATION
69
70 Reads configuration information from Start by parsing /etc/sa.conf,
71 then the contents of ~/sa.d/ which match ^[\w\d][\w\d_.-]+$
72
73 The configuration file contains a list of svn repositories which
74 should be queried; each line can contain a tab, which indicates that
75 the command following the tab should be run after the svn directory is
76 updated.
77
78 The configuration files are read in the order given above.
79
80 =cut
81
82 sub parse_config_file {
83      my ($repos,$filename,$home) = @_;
84      return unless -e $filename and -r _;
85      my $fh = new IO::File $filename, 'r' or die "Unable to read configuration file $filename $!";
86      while (<$fh>) {
87           chomp;
88           next if /^#/;
89           my ($repo,$command) = split /\t/,$_,2;
90           $repo =~ s/^\~/$home/;
91           $$repos{$repo} = $command;
92      }
93 }
94
95 my $HOME=$ENV{HOME};
96 my $HOSTNAME=qx(hostname);
97 $HOSTNAME=~s/\n//g;
98 my $CAN_IGNORE_EXTERNALS = 0;
99 qx(svn --version) =~ /\(r(\d+)\)/;
100 if ($1 > 13838) {
101      $CAN_IGNORE_EXTERNALS = 1;
102 }
103
104 sub available_config_files{
105      my $dir;
106      opendir($dir, "$HOME/.sa.d/") or return ();
107      return map {"$HOME/.sa.d/$_"} grep /^[\w\d][\w\d_.-]+$/, readdir $dir;
108 }
109
110 my %repos;
111 for ('/etc/sa.conf', available_config_files()) {
112      if (-e $_) {
113           parse_config_file(\%repos,$_,$HOME);
114      }
115 }
116
117 $DEBUG = $options{debug};
118 $VERBOSE = $options{verbose};
119
120 my $command;
121
122 if (@ARGV and $ARGV[0] =~ /^(st(?:atus)?|up(?:date)?|checkout)$/) {
123      $command = $ARGV[0];
124 }
125 else {
126      $command = 'status';
127 }
128
129 while (my ($repo,$run_after) = each(%repos)) {
130      if (-e "$repo/.svn") {
131           print "Checking [$repo]\n";
132           system('svn',
133                  $command,
134                  (not $VERBOSE and $options{quiet})?'-q':(),
135                  ($VERBOSE >= 2)?'-v':(),
136                  ($options{quick} and $CAN_IGNORE_EXTERNALS)?'--ignore-externals':(),
137                  $options{quick}>1?'-N':(),
138                  $repo,
139                 );
140      }
141      if ($command =~ /^up(?:date)|checkout$/) {
142           system('sh', '-c',"$run_after $repo") if defined $run_after and length $run_after;
143      }
144 }