]> git.donarmstrong.com Git - bin.git/blob - sa
add a hook-only mode to 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                hooks_only      => 0,
60               );
61
62 GetOptions(\%options,'quick|Q+','quiet|q!','debug|d+','help|h|?','man|m','verbose|v+','hooks_only|hooks-only');
63
64 pod2usage() if $options{help};
65 pod2usage({verbose=>2}) if $options{man};
66
67 # parse configuration file
68
69 =head1 CONFIGURATION
70
71 Reads configuration information from Start by parsing /etc/sa.conf,
72 then the contents of ~/sa.d/ which match ^[\w\d][\w\d_.-]+$
73
74 The configuration file contains a list of svn repositories which
75 should be queried; each line can contain a tab, which indicates that
76 the command following the tab should be run after the svn directory is
77 updated.
78
79 The configuration files are read in the order given above.
80
81 =cut
82
83 sub parse_config_file {
84      my ($repos,$filename,$home) = @_;
85      return unless -e $filename and -r _;
86      my $fh = new IO::File $filename, 'r' or die "Unable to read configuration file $filename $!";
87      while (<$fh>) {
88           chomp;
89           next if /^#/;
90           my ($repo,$command) = split /\t/,$_,2;
91           $repo =~ s/^\~/$home/;
92           $$repos{$repo} = $command;
93      }
94 }
95
96 my $HOME=$ENV{HOME};
97 my $HOSTNAME=qx(hostname);
98 $HOSTNAME=~s/\n//g;
99 my $CAN_IGNORE_EXTERNALS = 0;
100 qx(svn --version) =~ /\(r(\d+)\)/;
101 if ($1 > 13838) {
102      $CAN_IGNORE_EXTERNALS = 1;
103 }
104
105 sub available_config_files{
106      my $dir;
107      opendir($dir, "$HOME/.sa.d/") or return ();
108      return map {"$HOME/.sa.d/$_"} grep /^[\w\d][\w\d_.-]+$/, readdir $dir;
109 }
110
111 my %repos;
112 for ('/etc/sa.conf', available_config_files()) {
113      if (-e $_) {
114           parse_config_file(\%repos,$_,$HOME);
115      }
116 }
117
118 $DEBUG = $options{debug};
119 $VERBOSE = $options{verbose};
120
121 my $command;
122
123 if (@ARGV and $ARGV[0] =~ /^(st(?:atus)?|up(?:date)?|checkout)$/) {
124      $command = $ARGV[0];
125 }
126 else {
127      $command = 'status';
128 }
129
130 while (my ($repo,$run_after) = each(%repos)) {
131      print "Checking [$repo]\n";
132      if (-e "$repo/.svn" and not $options{hooks_only}) {
133           system('svn',
134                  $command,
135                  (not $VERBOSE and $options{quiet})?'-q':(),
136                  ($VERBOSE >= 2)?'-v':(),
137                  ($options{quick} and $CAN_IGNORE_EXTERNALS)?'--ignore-externals':(),
138                  $options{quick}>1?'-N':(),
139                  $repo,
140                 );
141      }
142      if ($command =~ /^up(?:date)?|checkout$/) {
143           system('sh', '-c',"$run_after $repo") if defined $run_after and length $run_after;
144      }
145 }