]> git.madduck.net Git - code/myrepos.git/blob - mr

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

make mrconfig chain-loading explicit
[code/myrepos.git] / mr
1 #!/usr/bin/perl
2
3 =head1 NAME
4
5 mr - a Multiple Repository management tool
6
7 =head1 SYNOPSIS
8
9 B<mr> [options] checkout
10
11 B<mr> [options] update
12
13 B<mr> [options] status
14
15 B<mr> [options] commit [-m "message"]
16
17 B<mr> [options] action [params ...]
18
19 =head1 DESCRIPTION
20
21 B<mr> is a Multiple Repository management tool. It allows you to register a
22 set of repositories in a .mrconfig file, and then checkout, update, or
23 perform other actions on all of the repositories at once.
24
25 Any mix of revision control systems can be used with B<mr>, and you can
26 define arbitrary actions for commands like "update", "checkout", or "commit".
27
28 The predefined commands should be fairly familiar to users of any revision
29 control system:
30
31 =over 4
32
33 =item checkout
34
35 Checks out all the registered repositories that are not already checked
36 out.
37
38 =item update
39
40 Updates each registered repository from its configured remote repository.
41
42 If a repository isn't checked out yet, it will first check it out.
43
44 =item status
45
46 Displays a status report for each registered repository, showing what
47 uncommitted changes are present in the repository.
48
49 =item commit
50
51 Commits changes to each registered repository. (By default, changes
52 are pushed to the remote repository too, when using distributed systems
53 like git.)
54
55 The optional -m parameter allows specifying a commit message.
56
57 =back
58
59 Actions can be abbreviated to any unambiguous subsctring, so
60 "mr st" is equivilant to "mr status".
61
62 =head1 OPTIONS
63
64 =over 4
65
66 =item -d directory
67
68 Specifies the topmost directory that B<mr> should work in. The default is
69 the current working directory. B<mr> will operate on all registered
70 repositories at or under the directory.
71
72 =item -c mrconfig
73
74 Use the specified mrconfig file, instead of looking for on in your home
75 directory.
76
77 =item -v
78
79 Be verbose.
80
81 =back
82
83 =head1 FILES
84
85 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
86 file in your home directory. Each repository specified in a .mrconfig file
87 can also have its own .mrconfig file in its root directory that can
88 optionally be used as well. So you could have a ~/.mrconfig that registers a
89 repository ~/src, that itself contains a ~/src/.mrconfig file, that in turn
90 registers several additional repositories.
91
92 The .mrconfig file uses a variant of the INI file format. Lines starting with
93 "#" are comments. Lines ending with "\" are continued on to the next line.
94 Sections specify where each repository is located, relative to the
95 directory that contains the .mrconfig file.
96
97 Within a section, each parameter defines a shell command to run to handle a
98 given action. Note that these shell commands are run in a "set -e" shell
99 environment, where any additional parameters you pass are available in
100 "$@". B<mr> cds into the repository directory before running
101 a command, except for the "checkout" command, which is run in the parent
102 of the repository directory, since the repository isn't checked out yet.
103
104 There are two special parameters. If the "skip" parameter is set and
105 its command returns nonzero, then B<mr> will skip acting on that repository.
106 If the "chain" parameter is set and its command returns nonzero, then B<mr>
107 will try to load a .mrconfig file from the root of the repository. (You
108 should avoid chaining from repositories with untrusted committers.)
109
110 The "default" section allows setting up default handlers for each action,
111 and is overridden by the contents of other sections. mr contains default
112 handlers for the "update", "status", and "commit" actions, so normally
113 you only need to specify what to do for "checkout".
114
115 For example:
116
117   [src]
118   checkout = svn co svn://svn.example.com/src/trunk src
119   chain = true
120
121   [src/linux-2.6]
122   # only check this out on kodama
123   skip = test $(hostname) != kodama
124   checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
125
126 =head1 AUTHOR
127
128 Copyright 2007 Joey Hess <joey@kitenet.net>
129
130 Licensed under the GNU GPL version 2 or higher.
131
132 http://kitenet.net/~joey/code/mr/
133
134 =cut
135
136 use warnings;
137 use strict;
138 use Getopt::Long;
139 use Cwd qw(getcwd abs_path);
140
141 my $directory=getcwd();
142 my $config="$ENV{HOME}/.mrconfig";
143 my $verbose=0;
144 my %config;
145 my %knownactions;
146
147 Getopt::Long::Configure("no_permute");
148 my $result=GetOptions(
149         "d=s" => sub { $directory=abs_path($_[1]) },
150         "c=s" => \$config,
151         "v" => \$verbose,
152 );
153 if (! $result || @ARGV < 1) {
154         die("Usage: mr [-d directory] action [params ...]\n");
155 }
156
157 loadconfig(\*DATA);
158 loadconfig($config);
159 #use Data::Dumper;
160 #print Dumper(\%config);
161
162 my $action=shift @ARGV;
163 if (! $knownactions{$action}) {
164         my @matches = grep { /^\Q$action\E/ } keys %knownactions;
165         if (@matches == 1) {
166                 $action=$matches[0];
167         }
168         else {
169                 die "mr: ambiguous action \"$action\" (matches @matches)\n";
170         }
171 }
172
173 my (@failed, @successful, @skipped);
174 my $first=1;
175 foreach my $topdir (sort keys %config) {
176         foreach my $subdir (sort keys %{$config{$topdir}}) {
177                 next if $subdir eq 'default';
178                 
179                 my $dir=$topdir.$subdir;
180
181                 if (defined $directory &&
182                     $dir ne $directory &&
183                     $dir !~ /^\Q$directory\E\//) {
184                         print "mr $action: $dir skipped per -d parameter ($directory)\n" if $verbose;
185                         push @skipped, $dir;
186                         next;
187                 }
188
189                 print "\n" unless $first;
190                 $first=0;
191
192                 if (exists $config{$topdir}{$subdir}{skip}) {
193                         my $ret=system($config{$topdir}{$subdir}{skip});
194                         if ($ret >> 8 == 0) {
195                                 print "mr $action: $dir skipped per config file\n" if $verbose;
196                                 push @skipped, $dir;
197                                 next;
198                         }
199                 }
200
201                 action($action, $dir, $topdir, $subdir);
202
203         }
204 }
205
206 sub action {
207         my ($action, $dir, $topdir, $subdir) = @_;
208
209         if ($action eq 'checkout') {
210                 if (-d $dir) {
211                         print "mr $action: $dir already exists, skipping checkout\n";
212                         push @skipped, $dir;
213                         return;
214                 }
215                 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
216         }
217         if ($action eq 'update') {
218                 if (! -d $dir) {
219                         return action("checkout", $dir, $topdir, $subdir);
220                 }
221         }
222
223         if (! chdir($dir)) {
224                 print STDERR "mr $action: failed to chdir to $dir: $!\n";
225                 push @skipped, $dir;
226         }
227         elsif (! exists $config{$topdir}{$subdir}{$action}) {
228                 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
229                 push @skipped, $dir;
230         }
231         else {
232                 print "mr $action: in $dir\n";
233                 my $command="set -e; my_action(){ $config{$topdir}{$subdir}{$action} ; }; my_action ".
234                         join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
235                 my $ret=system($command);
236                 if ($ret != 0) {
237                         print STDERR "mr $action: failed to run: $command\n" if $verbose;
238                         push @failed, $topdir.$subdir;
239                         if ($ret >> 8 != 0) {
240                                 print STDERR "mr $action: command failed\n";
241                         }
242                         elsif ($ret != 0) {
243                                 print STDERR "mr $action: command died ($ret)\n";
244                         }
245                 }
246                 else {
247                         push @successful, $dir;
248                 }
249         }
250 }
251
252 sub showstat {
253         my $count=shift;
254         my $singular=shift;
255         my $plural=shift;
256         if ($count) {
257                 return "$count ".($count > 1 ? $plural : $singular);
258         }
259         return;
260 }
261 print "\nmr $action: finished (".join("; ",
262         showstat($#successful+1, "successful", "successful"),
263         showstat($#failed+1, "failed", "failed"),
264         showstat($#skipped+1, "skipped", "skipped"),
265 ).")\n";
266 exit @failed ? 1 : 0;
267
268 my %loaded;
269 sub loadconfig {
270         my $f=shift;
271
272         my @toload;
273
274         my $in;
275         my $dir;
276         if (ref $f eq 'GLOB') {
277                 $in=$f; 
278                 $dir="";
279         }
280         else {
281                 # $f might be a symlink
282                 my $absf=abs_path($f);
283                 if ($loaded{$absf}) {
284                         return;
285                 }
286                 $loaded{$absf}=1;
287
288                 print "mr: loading config $f\n" if $verbose;
289                 open($in, "<", $f) || die "mr: open $f: $!\n";
290                 ($dir)=$f=~/^(.*\/)[^\/]+$/;
291                 if (! defined $dir) {
292                         $dir=".";
293                 }
294                 $dir=abs_path($dir)."/";
295
296                 # copy in defaults from first parent
297                 my $parent=$dir;
298                 while ($parent=~s/^(.*)\/[^\/]+\/?$/$1/) {
299                         if (exists $config{$parent} &&
300                             exists $config{$parent}{default}) {
301                                 $config{$dir}{default}={ %{$config{$parent}{default}} };
302                                 last;
303                         }
304                 }
305         }
306
307         my $section;
308         while (<$in>) {
309                 chomp;
310                 next if /^\s*\#/ || /^\s*$/;
311                 if (/^\s*\[([^\]]*)\]\s*$/) {
312                         $section=$1;
313                 }
314                 elsif (/^\s*(\w+)\s*=\s*(.*)/) {
315                         my $parameter=$1;
316                         my $value=$2;
317
318                         # continuation line
319                         while ($value=~/(.*)\\$/) {
320                                 $value=$1.<$in>;
321                                 chomp $value;
322                         }
323
324                         if (! defined $section) {
325                                 die "$f line $.: parameter ($parameter) not in section\n";
326                         }
327                         if (! exists $config{$dir}{$section} &&
328                               exists $config{$dir}{default}) {
329                                 # copy in defaults
330                                 $config{$dir}{$section}={ %{$config{$dir}{default}} };
331                         }
332                         $config{$dir}{$section}{$parameter}=$value;
333                         $knownactions{$parameter}=1;
334
335                         if ($parameter eq 'chain' &&
336                             length $dir && $section ne "default" &&
337                             -e $dir.$section."/.mrconfig" &&
338                             system($value) >> 8 == 0) {
339                                 push @toload, $dir.$section."/.mrconfig";
340                         }
341                 }
342                 else {
343                                 die "$f line $.: parse error\n";
344                 }
345         }
346         close $in;
347
348         foreach (@toload) {
349                 loadconfig($_);
350         }
351 }
352
353 __DATA__
354 # Some useful actions that mr knows about by default.
355 # These can be overridden in ~/.mrconfig.
356 [default]
357 update = \
358         if [ -d .svn ]; then \
359                 svn update; \
360         elif [ -d .git ]; then \
361                 git pull origin master; \
362         else \
363                 echo "mr update: unknown repo type"; \
364                 exit 1; \
365         fi
366 status = \
367         if [ -d .svn ]; then \
368                 svn status; \
369         elif [ -d .git ]; then \
370                 git status || true; \
371         else \
372                 echo "mr status: unknown repo type"; \
373                 exit 1; \
374         fi
375 commit = \
376         if [ -d .svn ]; then \
377                 svn commit "$@"; \
378         elif [ -d .git ]; then \
379                 git commit -a "$@" && git push --all \
380         else \
381                 echo "mr commit: unknown repo type"; \
382                 exit 1; \
383         fi