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.
6 mr - a Multiple Repository management tool
10 B<mr> [options] checkout
12 B<mr> [options] update
14 B<mr> [options] status
16 B<mr> [options] commit [-m "message"]
22 B<mr> [options] register [repository]
24 B<mr> [options] config section [parameter=[value] ...]
26 B<mr> [options] action [params ...]
30 B<mr> is a Multiple Repository management tool. It
31 can checkout, update, or perform other actions on
32 a set of repositories as if they were one combined respository. It
33 supports any combination of subversion, git, cvs, and bzr repositories,
34 and support for other revision control systems can easily be added.
36 B<mr> cds into and operates on all registered repsitories at or below your
37 working directory. Or, if you are in a subdirectory of a repository that
38 contains no other registered repositories, it will stay in that directory,
39 and work on only that repository,
41 The predefined commands should be fairly familiar to users of any revision
46 =item checkout (or co)
48 Checks out any repositories that are not already checked out.
52 Updates each repository from its configured remote repository.
54 If a repository isn't checked out yet, it will first check it out.
58 Displays a status report for each repository, showing what
59 uncommitted changes are present in the repository.
63 Commits changes to each repository. (By default, changes are pushed to the
64 remote repository too, when using distributed systems like git.)
66 The optional -m parameter allows specifying a commit message.
70 Show a diff of uncommitted changes.
78 List the repositories that mr will act on.
82 Register an existing repository in the mrconfig file. By default, the
83 epository in the current directory is registered, or you can specify a
84 directory to register.
88 Adds, modifies, removes, or prints a value from the mrconfig file. The next
89 parameter is the name of the section the value is in. To add or modify
90 values, use one or more instances of "parameter=value". Use "parameter=" to
91 remove a parameter. Use just "parameter" to get the value of a parameter.
93 For example, to add (or edit) a repository in src/foo:
95 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
97 To show the command that mr uses to update the repository in src/foo:
99 mr config src/foo update
107 Actions can be abbreviated to any unambiguous subsctring, so
108 "mr st" is equivilant to "mr status", and "mr up" is equivilant to "mr
111 Additional parameters can be passed to most commands, and are passed on
112 unchanged to the underlying revision control system. This is mostly useful
113 if the repositories mr will act on all use the same revision control
122 Specifies the topmost directory that B<mr> should work in. The default is
123 the current working directory.
127 Use the specified mrconfig file, instead of looking for one in your home
138 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
139 file in your home directory, and this can in turn chain load .mrconfig files
142 Here is an example .mrconfig file:
145 checkout = svn co svn://svn.example.com/src/trunk src
149 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
151 git checkout -b mybranch origin/master
153 The .mrconfig file uses a variant of the INI file format. Lines starting with
154 "#" are comments. Values can be continued to the following line by
155 indenting the line with whitespace.
157 The "DEFAULT" section allows setting default values for the sections that
160 The "ALIAS" section allows adding aliases for actions. Each parameter
161 is an alias, and its value is the action to use.
163 All other sections add repositories. The section header specifies the
164 directory where the repository is located. This is relative to the directory
165 that contains the mrconfig file, but you can also choose to use absolute
168 Within a section, each parameter defines a shell command to run to handle a
169 given action. mr contains default handlers for the "update", "status", and
170 "commit" actions, so normally you only need to specify what to do for
173 Note that these shell commands are run in a "set -e" shell
174 environment, where any additional parameters you pass are available in
175 "$@". The "checkout" command is run in the parent of the repository
176 directory, since the repository isn't checked out yet. All other commands
177 are run inside the repository, though not necessarily at the top of it.
178 The "MR_REPO" environment variable is set to the path to the top of the
181 A few parameters have special meanings:
187 If the "skip" parameter is set and its command returns nonzero, then B<mr>
188 will skip acting on that repository. The command is passed the action
191 Here are two examples. The first skips the repo unless
192 mr is run by joey. The second uses the hours_since function
193 (included in mr's built-in library) to skip updating the repo unless it's
194 been at least 12 hours since the last update.
196 skip = test $(whoami) != joey
197 skip = [ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]
201 If the "chain" parameter is set and its command returns nonzero, then B<mr>
202 will try to load a .mrconfig file from the root of the repository. (You
203 should avoid chaining from repositories with untrusted committers.)
207 If the "deleted" parameter is set and its command returns nonzero, then
208 B<mr> will treat the repository as deleted. It won't ever actually delete
209 the repository, but it will warn if it sees the repsoitory's directory.
210 This is useful when one mrconfig file is shared amoung multiple machines,
211 to keep track of and remember to delete old repositories.
215 The "lib" parameter can specify some shell code that will be run before each
216 command, this can be a useful way to define shell functions for other commands
223 Copyright 2007 Joey Hess <joey@kitenet.net>
225 Licensed under the GNU GPL version 2 or higher.
227 http://kitenet.net/~joey/code/mr/
235 use Cwd qw(getcwd abs_path);
237 my $directory=getcwd();
238 my $config="$ENV{HOME}/.mrconfig";
244 Getopt::Long::Configure("no_permute");
245 my $result=GetOptions(
246 "d|directory=s" => sub { $directory=abs_path($_[1]) },
247 "c|config=s" => \$config,
248 "verbose" => \$verbose,
250 if (! $result || @ARGV < 1) {
251 die("Usage: mr [-d directory] action [params ...]\n".
252 "(Use mr help for man page.)\n");
259 #print Dumper(\%config);
262 use FindBin qw($Bin $Script);
263 $ENV{MR_PATH}=$Bin."/".$Script;
266 # alias expansion and command stemming
267 my $action=shift @ARGV;
268 if (exists $alias{$action}) {
269 $action=$alias{$action};
271 if (! exists $knownactions{$action}) {
272 my @matches = grep { /^\Q$action\E/ }
273 keys %knownactions, keys %alias;
277 elsif (@matches == 0) {
278 die "mr: unknown action \"$action\" (known actions: ".
279 join(", ", sort keys %knownactions).")\n";
282 die "mr: ambiguous action \"$action\" (matches: ".
283 join(", ", @matches).")\n";
287 if ($action eq 'help') {
288 exec($config{''}{DEFAULT}{$action}) || die "exec: $!";
290 elsif ($action eq 'config') {
292 die "mr config: not enough parameters\n";
295 if ($section=~/^\//) {
296 # try to convert to a path relative to $config's dir
297 my ($dir)=$config=~/^(.*\/)[^\/]+$/;
298 if ($section=~/^\Q$dir\E(.*)/) {
304 if (/^([^=]+)=(.*)$/) {
305 $changefields{$1}=$2;
309 foreach my $topdir (sort keys %config) {
310 if (exists $config{$topdir}{$section} &&
311 exists $config{$topdir}{$section}{$_}) {
312 print $config{$topdir}{$section}{$_}."\n";
317 die "mr $action: $section $_ not set\n";
321 modifyconfig($config, $section, %changefields) if %changefields;
324 elsif ($action eq 'register') {
325 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
326 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
327 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
328 print STDERR "mr $action: running >>$command<<\n" if $verbose;
329 exec($command) || die "exec: $!";
332 # work out what repos to act on
335 foreach my $topdir (sort keys %config) {
336 foreach my $subdir (sort keys %{$config{$topdir}}) {
337 next if $subdir eq 'DEFAULT';
338 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
340 $dir.="/" unless $dir=~/\/$/;
341 $d.="/" unless $d=~/\/$/;
342 next if $dir ne $directory && $dir !~ /^\Q$directory\E/;
343 push @repos, [$dir, $topdir, $subdir];
347 # fallback to find a leaf repo
348 LEAF: foreach my $topdir (reverse sort keys %config) {
349 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
350 next if $subdir eq 'DEFAULT';
351 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
353 $dir.="/" unless $dir=~/\/$/;
354 $d.="/" unless $d=~/\/$/;
355 if ($d=~/^\Q$dir\E/) {
356 push @repos, [$dir, $topdir, $subdir];
364 my (@failed, @ok, @skipped);
365 foreach my $repo (@repos) {
366 action($action, @$repo);
370 my ($action, $dir, $topdir, $subdir) = @_;
372 my $lib= exists $config{$topdir}{$subdir}{lib} ?
373 $config{$topdir}{$subdir}{lib}."\n" : "";
375 if (exists $config{$topdir}{$subdir}{deleted}) {
380 my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
381 print "mr $action: running deleted test >>$test<<\n" if $verbose;
382 my $ret=system($test);
383 if ($ret >> 8 == 0) {
384 print STDERR "mr error: $dir should be deleted yet still exists\n\n";
391 if ($action eq 'checkout') {
393 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
397 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
399 elsif ($action eq 'update') {
401 return action("checkout", $dir, $topdir, $subdir);
407 if (exists $config{$topdir}{$subdir}{skip}) {
408 my $test="set -e;".$lib.
409 "my_action(){ $config{$topdir}{$subdir}{skip}\n }; my_action '$action'";
410 print "mr $action: running skip test >>$test<<\n" if $verbose;
411 my $ret=system($test);
412 if ($ret >> 8 == 0) {
413 print "mr $action: $dir skipped per config file\n" if $verbose;
419 if (! $nochdir && ! chdir($dir)) {
420 print STDERR "mr $action: failed to chdir to $dir: $!\n";
423 elsif (! exists $config{$topdir}{$subdir}{$action}) {
424 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
429 print "mr $action: $topdir$subdir\n";
432 print "mr $action: $topdir$subdir (in subdir $directory)\n";
434 my $command="set -e; ".$lib.
435 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
436 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
437 print STDERR "mr $action: running >>$command<<\n" if $verbose;
438 my $ret=system($command);
440 print STDERR "mr $action: failed ($ret)\n" if $verbose;
442 if ($ret >> 8 != 0) {
443 print STDERR "mr $action: command failed\n";
446 print STDERR "mr $action: command died ($ret)\n";
462 return "$count ".($count > 1 ? $plural : $singular);
466 if (! @ok && ! @failed && ! @skipped) {
467 die "mr $action: no repositories found to work on\n";
469 print "mr $action: finished (".join("; ",
470 showstat($#ok+1, "ok", "ok"),
471 showstat($#failed+1, "failed", "failed"),
472 showstat($#skipped+1, "skipped", "skipped"),
477 elsif (! @ok && @skipped) {
483 sub loadconfig { #{{{
490 if (ref $f eq 'GLOB') {
499 my $absf=abs_path($f);
500 if ($loaded{$absf}) {
505 ($dir)=$f=~/^(.*\/)[^\/]+$/;
506 if (! defined $dir) {
509 $dir=abs_path($dir)."/";
511 # copy in defaults from first parent
513 while ($parent=~s/^(.*)\/[^\/]+\/?$/$1/) {
514 if (exists $config{$parent} &&
515 exists $config{$parent}{DEFAULT}) {
516 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
521 print "mr: loading config $f\n" if $verbose;
522 open($in, "<", $f) || die "mr: open $f: $!\n";
531 next if /^\s*\#/ || /^\s*$/;
532 if (/^\[([^\]]*)\]\s*$/) {
535 elsif (/^(\w+)\s*=\s*(.*)/) {
540 while (@lines && $lines[0]=~/^\s(.+)/) {
546 if (! defined $section) {
547 die "$f line $.: parameter ($parameter) not in section\n";
549 if ($section ne 'ALIAS' &&
550 ! exists $config{$dir}{$section} &&
551 exists $config{$dir}{DEFAULT}) {
553 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
555 if ($section eq 'ALIAS') {
556 $alias{$parameter}=$value;
558 elsif ($parameter eq 'lib') {
559 $config{$dir}{$section}{lib}.=$value."\n";
562 $config{$dir}{$section}{$parameter}=$value;
563 $knownactions{$parameter}=1;
564 if ($parameter eq 'chain' &&
565 length $dir && $section ne "DEFAULT" &&
566 -e $dir.$section."/.mrconfig" &&
567 system($value) >> 8 == 0) {
568 push @toload, $dir.$section."/.mrconfig";
573 die "$f line $.: parse error\n";
582 sub modifyconfig { #{{{
584 # the section to modify or add
585 my $targetsection=shift;
586 # fields to change in the section
587 # To remove a field, set its value to "".
594 open(my $in, "<", $f) || die "mr: open $f: $!\n";
599 my $formatfield=sub {
601 my @value=split(/\n/, shift);
603 return "$field = ".shift(@value)."\n".
604 join("", map { "\t$_\n" } @value);
608 while ($out[$#out] =~ /^\s*$/) {
609 unshift @blanks, pop @out;
611 foreach my $field (sort keys %changefields) {
612 if (length $changefields{$field}) {
613 push @out, "$field = $changefields{$field}\n";
614 delete $changefields{$field};
624 if (/^\s*\#/ || /^\s*$/) {
627 elsif (/^\[([^\]]*)\]\s*$/) {
628 if (defined $section &&
629 $section eq $targetsection) {
637 elsif (/^(\w+)\s*=\s(.*)/) {
642 while (@lines && $lines[0]=~/^\s(.+)/) {
648 if ($section eq $targetsection) {
649 if (exists $changefields{$parameter}) {
650 if (length $changefields{$parameter}) {
651 $value=$changefields{$parameter};
653 delete $changefields{$parameter};
657 push @out, $formatfield->($parameter, $value);
661 if (defined $section &&
662 $section eq $targetsection) {
665 elsif (%changefields) {
666 push @out, "\n[$targetsection]\n";
667 foreach my $field (sort keys %changefields) {
668 if (length $changefields{$field}) {
669 push @out, $formatfield->($field, $changefields{$field});
674 open(my $out, ">", $f) || die "mr: write $f: $!\n";
679 # Finally, some useful actions that mr knows about by default.
680 # These can be overridden in ~/.mrconfig.
695 for dir in .git .svn .bzr CVS; do
696 if [ -e "$MR_REPO/$dir" ]; then
697 flagfile="$MR_REPO/$dir/.mr_last$1"
701 if [ -z "$flagfile" ]; then
702 error "cannot determine flag filename"
704 perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"
709 if [ -d "$MR_REPO"/.svn ]; then
711 elif [ -d "$MR_REPO"/.git ]; then
712 git pull origin master "$@"
713 elif [ -d "$MR_REPO"/.bzr ]; then
715 elif [ -d "$MR_REPO"/CVS ]; then
718 error "unknown repo type"
721 if [ -d "$MR_REPO"/.svn ]; then
723 elif [ -d "$MR_REPO"/.git ]; then
724 git status "$@" || true
725 elif [ -d "$MR_REPO"/.bzr ]; then
727 elif [ -d "$MR_REPO"/CVS ]; then
730 error "unknown repo type"
733 if [ -d "$MR_REPO"/.svn ]; then
735 elif [ -d "$MR_REPO"/.git ]; then
736 git commit -a "$@" && git push --all
737 elif [ -d "$MR_REPO"/.bzr ]; then
738 bzr commit "$@" && bzr push
739 elif [ -d "$MR_REPO"/CVS ]; then
742 error "unknown repo type"
745 if [ -d "$MR_REPO"/.svn ]; then
747 elif [ -d "$MR_REPO"/.git ]; then
749 elif [ -d "$MR_REPO"/.bzr ]; then
751 elif [ -d "$MR_REPO"/CVS ]; then
754 error "unknown repo type"
757 if [ -d "$MR_REPO"/.svn ]; then
759 elif [ -d "$MR_REPO"/.git ]; then
761 elif [ -d "$MR_REPO"/.bzr ]; then
763 elif [ -d "$MR_REPO"/CVS ]; then
766 error "unknown repo type"
772 basedir="$(basename $(pwd))"
774 url=$(LANG=C svn info . | grep -i ^URL: | cut -d ' ' -f 2)
775 if [ -z "$url" ]; then
776 error "cannot determine svn url"
778 echo "Registering svn url: $url"
779 mr config "$(pwd)" checkout="svn co $url $basedir"
780 elif [ -d .git ]; then
781 url=$(LANG=C git-config --get remote.origin.url)
782 if [ -z "$url" ]; then
783 error "cannot determine git url"
785 echo "Registering git url: $url"
786 mr config "$(pwd)" checkout="git clone $url $basedir"
787 elif [ -d .bzr ]; then
788 url=$(cat .bzr/branch/parent)
789 if [ -z "$url" ]; then
790 error "cannot determine bzr url"
792 echo "Registering bzr url: $url"
793 mr config "$(pwd)" checkout="bzr clone $url $basedir"
795 error "unable to register this repo type"
798 if [ ! -e "$MR_PATH" ]; then
799 error "cannot find program path"
801 (pod2man -c mr "$MR_PATH" | man -l -) || error "pod2man or man failed"
805 ed = echo "A horse is a horse, of course, of course.."
806 T = echo "I pity the fool."