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.
7 mr - a Multiple Repository management tool
11 B<mr> [options] checkout
13 B<mr> [options] update
15 B<mr> [options] status
17 B<mr> [options] commit [-m "message"]
23 B<mr> [options] register [repository]
25 B<mr> [options] config section [parameter=[value] ...]
27 B<mr> [options] action [params ...]
31 B<mr> is a Multiple Repository management tool. It
32 can checkout, update, or perform other actions on
33 a set of repositories as if they were one combined respository. It
34 supports any combination of subversion, git, cvs, and bzr repositories,
35 and support for other revision control systems can easily be added.
37 B<mr> cds into and operates on all registered repositories at or below your
38 working directory. Or, if you are in a subdirectory of a repository that
39 contains no other registered repositories, it will stay in that directory,
40 and work on only that repository,
42 These predefined commands should be fairly familiar to users of any revision
47 =item checkout (or co)
49 Checks out any repositories that are not already checked out.
53 Updates each repository from its configured remote repository.
55 If a repository isn't checked out yet, it will first check it out.
59 Displays a status report for each repository, showing what
60 uncommitted changes are present in the repository.
64 Commits changes to each repository. (By default, changes are pushed to the
65 remote repository too, when using distributed systems like git.)
67 The optional -m parameter allows specifying a commit message.
71 Show a diff of uncommitted changes.
79 These commands are also available:
85 List the repositories that mr will act on.
89 Register an existing repository in the mrconfig file. By default, the
90 repository in the current directory is registered, or you can specify a
91 directory to register.
93 By default it registers it to the ~/.mrconfig file. To make it write to a
94 different file, use the -c option.
98 Adds, modifies, removes, or prints a value from the mrconfig file. The next
99 parameter is the name of the section the value is in. To add or modify
100 values, use one or more instances of "parameter=value". Use "parameter=" to
101 remove a parameter. Use just "parameter" to get the value of a parameter.
103 For example, to add (or edit) a repository in src/foo:
105 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
107 To show the command that mr uses to update the repository in src/foo:
109 mr config src/foo update
117 Actions can be abbreviated to any unambiguous subsctring, so
118 "mr st" is equivilant to "mr status", and "mr up" is equivilant to "mr
121 Additional parameters can be passed to most commands, and are passed on
122 unchanged to the underlying revision control system. This is mostly useful
123 if the repositories mr will act on all use the same revision control
132 Specifies the topmost directory that B<mr> should work in. The default is
133 the current working directory.
137 Use the specified mrconfig file, instead of looking for one in your home
146 Expand the statistics line displayed at the end to include information
147 about exactly which repositories failed and were skipped, if any.
151 Just operate on the repository for the current directory, do not
152 recurse into deeper repositories.
158 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
159 file in your home directory, and this can in turn chain load .mrconfig files
162 Here is an example .mrconfig file:
165 checkout = svn co svn://svn.example.com/src/trunk src
169 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
171 git checkout -b mybranch origin/master
173 The .mrconfig file uses a variant of the INI file format. Lines starting with
174 "#" are comments. Values can be continued to the following line by
175 indenting the line with whitespace.
177 The "DEFAULT" section allows setting default values for the sections that
180 The "ALIAS" section allows adding aliases for actions. Each parameter
181 is an alias, and its value is the action to use.
183 All other sections add repositories. The section header specifies the
184 directory where the repository is located. This is relative to the directory
185 that contains the mrconfig file, but you can also choose to use absolute
188 Within a section, each parameter defines a shell command to run to handle a
189 given action. mr contains default handlers for the "update", "status", and
190 "commit" actions, so normally you only need to specify what to do for
193 Note that these shell commands are run in a "set -e" shell
194 environment, where any additional parameters you pass are available in
195 "$@". The "checkout" command is run in the parent of the repository
196 directory, since the repository isn't checked out yet. All other commands
197 are run inside the repository, though not necessarily at the top of it.
198 The "MR_REPO" environment variable is set to the path to the top of the
199 repository, and "MR_CONFIG" is set to the topmost .mrconfig file used.
201 A few parameters have special meanings:
207 If the "skip" parameter is set and its command returns true, then B<mr>
208 will skip acting on that repository. The command is passed the action
211 Here are two examples. The first skips the repo unless
212 mr is run by joey. The second uses the hours_since function
213 (included in mr's built-in library) to skip updating the repo unless it's
214 been at least 12 hours since the last update.
216 skip = test $(whoami) != joey
217 skip = [ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]
221 If the "chain" parameter is set and its command returns true, then B<mr>
222 will try to load a .mrconfig file from the root of the repository. (You
223 should avoid chaining from repositories with untrusted committers.)
227 If the "deleted" parameter is set and its command returns true, then
228 B<mr> will treat the repository as deleted. It won't ever actually delete
229 the repository, but it will warn if it sees the repository's directory.
230 This is useful when one mrconfig file is shared amoung multiple machines,
231 to keep track of and remember to delete old repositories.
235 The "lib" parameter can specify some shell code that will be run before each
236 command, this can be a useful way to define shell functions for other commands
243 Copyright 2007 Joey Hess <joey@kitenet.net>
245 Licensed under the GNU GPL version 2 or higher.
247 http://kitenet.net/~joey/code/mr/
256 use Cwd qw(getcwd abs_path);
259 print STDERR "mr: interrupted\n";
263 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
264 my $directory=getcwd();
272 Getopt::Long::Configure("no_permute");
273 my $result=GetOptions(
274 "d|directory=s" => sub { $directory=abs_path($_[1]) },
275 "c|config=s" => \$ENV{MR_CONFIG},
276 "v|verbose" => \$verbose,
277 "s|stats" => \$stats,
278 "n|no-recurse" => \$no_recurse,
280 if (! $result || @ARGV < 1) {
281 die("Usage: mr [-d directory] action [params ...]\n".
282 "(Use mr help for man page.)\n");
286 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
287 # the config file might be a symlink to elsewhere, and the directory it's
289 if ($ENV{MR_CONFIG} !~ /^\//) {
290 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
294 loadconfig($ENV{MR_CONFIG});
296 #print Dumper(\%config);
299 use FindBin qw($Bin $Script);
300 $ENV{MR_PATH}=$Bin."/".$Script;
303 # alias expansion and command stemming
304 my $action=shift @ARGV;
305 if (exists $alias{$action}) {
306 $action=$alias{$action};
308 if (! exists $knownactions{$action}) {
309 my @matches = grep { /^\Q$action\E/ }
310 keys %knownactions, keys %alias;
314 elsif (@matches == 0) {
315 die "mr: unknown action \"$action\" (known actions: ".
316 join(", ", sort keys %knownactions).")\n";
319 die "mr: ambiguous action \"$action\" (matches: ".
320 join(", ", @matches).")\n";
324 if ($action eq 'help') {
325 exec($config{''}{DEFAULT}{$action}) || die "exec: $!";
327 elsif ($action eq 'config') {
329 die "mr config: not enough parameters\n";
332 if ($section=~/^\//) {
333 # try to convert to a path relative to the config file
334 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
336 $dir.="/" unless $dir=~/\/$/;
337 if ($section=~/^\Q$dir\E(.*)/) {
343 if (/^([^=]+)=(.*)$/) {
344 $changefields{$1}=$2;
348 foreach my $topdir (sort keys %config) {
349 if (exists $config{$topdir}{$section} &&
350 exists $config{$topdir}{$section}{$_}) {
351 print $config{$topdir}{$section}{$_}."\n";
356 die "mr $action: $section $_ not set\n";
360 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
363 elsif ($action eq 'register') {
364 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
365 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
366 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
367 print STDERR "mr $action: running >>$command<<\n" if $verbose;
368 exec($command) || die "exec: $!";
371 # work out what repos to act on
374 foreach my $topdir (sort keys %config) {
375 foreach my $subdir (sort keys %{$config{$topdir}}) {
376 next if $subdir eq 'DEFAULT';
377 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
379 $dir.="/" unless $dir=~/\/$/;
380 $d.="/" unless $d=~/\/$/;
381 next if $no_recurse && $d ne $dir;
382 next if $dir ne $d && $dir !~ /^\Q$d\E/;
383 push @repos, [$dir, $topdir, $subdir];
387 # fallback to find a leaf repo
388 LEAF: foreach my $topdir (reverse sort keys %config) {
389 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
390 next if $subdir eq 'DEFAULT';
391 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
393 $dir.="/" unless $dir=~/\/$/;
394 $d.="/" unless $d=~/\/$/;
395 if ($d=~/^\Q$dir\E/) {
396 push @repos, [$dir, $topdir, $subdir];
404 my (@failed, @ok, @skipped);
405 foreach my $repo (@repos) {
406 action($action, @$repo);
410 my ($action, $dir, $topdir, $subdir) = @_;
412 my $lib= exists $config{$topdir}{$subdir}{lib} ?
413 $config{$topdir}{$subdir}{lib}."\n" : "";
415 if (exists $config{$topdir}{$subdir}{deleted}) {
416 my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
417 print "mr $action: running deleted test >>$test<<\n" if $verbose;
418 my $ret=system($test);
420 if (($? & 127) == 2) {
421 print STDERR "mr $action: interrupted\n";
425 print STDERR "mr $action: deleted test received signal ".($? & 127)."\n";
428 if ($ret >> 8 == 0) {
430 print STDERR "mr error: $dir should be deleted yet still exists\n\n";
435 print "mr $action: $dir skipped (as deleted) per config file\n" if $verbose;
442 if ($action eq 'checkout') {
444 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
449 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
452 print "mr $action: creating parent directory $dir\n" if $verbose;
453 my $ret=system("mkdir", "-p", $dir);
456 elsif ($action eq 'update') {
458 return action("checkout", $dir, $topdir, $subdir);
464 if (exists $config{$topdir}{$subdir}{skip}) {
465 my $test="set -e;".$lib.
466 "my_action(){ $config{$topdir}{$subdir}{skip}\n }; my_action '$action'";
467 print "mr $action: running skip test >>$test<<\n" if $verbose;
468 my $ret=system($test);
470 if (($? & 127) == 2) {
471 print STDERR "mr $action: interrupted\n";
475 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
479 if ($ret >> 8 == 0) {
480 print "mr $action: $dir skipped per config file\n" if $verbose;
486 if (! $nochdir && ! chdir($dir)) {
487 print STDERR "mr $action: failed to chdir to $dir: $!\n";
490 elsif (! exists $config{$topdir}{$subdir}{$action}) {
491 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
496 print "mr $action: $topdir$subdir\n";
499 print "mr $action: $topdir$subdir (in subdir $directory)\n";
501 my $command="set -e; ".$lib.
502 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
503 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
504 print STDERR "mr $action: running >>$command<<\n" if $verbose;
505 my $ret=system($command);
507 if (($? & 127) == 2) {
508 print STDERR "mr $action: interrupted\n";
512 print STDERR "mr $action: received signal ".($? & 127)."\n";
514 print STDERR "mr $action: failed ($ret)\n" if $verbose;
516 if ($ret >> 8 != 0) {
517 print STDERR "mr $action: command failed\n";
520 print STDERR "mr $action: command died ($ret)\n";
524 if ($action eq 'checkout' && ! -d $dir) {
525 print STDERR "mr $action: $dir missing after checkout\n";;
542 return "$count ".($count > 1 ? $plural : $singular);
546 if (! @ok && ! @failed && ! @skipped) {
547 die "mr $action: no repositories found to work on\n";
549 print "mr $action: finished (".join("; ",
550 showstat($#ok+1, "ok", "ok"),
551 showstat($#failed+1, "failed", "failed"),
552 showstat($#skipped+1, "skipped", "skipped"),
556 print "mr $action: (skipped: ".join(" ", @skipped).")\n";
559 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
565 elsif (! @ok && @skipped) {
571 sub loadconfig { #{{{
578 if (ref $f eq 'GLOB') {
587 my $absf=abs_path($f);
588 if ($loaded{$absf}) {
593 ($dir)=$f=~/^(.*\/)[^\/]+$/;
594 if (! defined $dir) {
597 $dir=abs_path($dir)."/";
599 # copy in defaults from first parent
601 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
602 if ($parent eq '/') {
605 if (exists $config{$parent} &&
606 exists $config{$parent}{DEFAULT}) {
607 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
612 print "mr: loading config $f\n" if $verbose;
613 open($in, "<", $f) || die "mr: open $f: $!\n";
624 next if /^\s*\#/ || /^\s*$/;
625 if (/^\[([^\]]*)\]\s*$/) {
628 elsif (/^(\w+)\s*=\s*(.*)/) {
633 while (@lines && $lines[0]=~/^\s(.+)/) {
640 if (! defined $section) {
641 die "$f line $.: parameter ($parameter) not in section\n";
643 if ($section ne 'ALIAS' &&
644 ! exists $config{$dir}{$section} &&
645 exists $config{$dir}{DEFAULT}) {
647 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
649 if ($section eq 'ALIAS') {
650 $alias{$parameter}=$value;
652 elsif ($parameter eq 'lib') {
653 $config{$dir}{$section}{lib}.=$value."\n";
656 $config{$dir}{$section}{$parameter}=$value;
657 $knownactions{$parameter}=1;
658 if ($parameter eq 'chain' &&
659 length $dir && $section ne "DEFAULT" &&
660 -e $dir.$section."/.mrconfig") {
661 my $ret=system($value);
663 if (($? & 127) == 2) {
664 print STDERR "mr $action: chain test interrupted\n";
668 print STDERR "mr $action: chain test received signal ".($? & 127)."\n";
672 push @toload, $dir.$section."/.mrconfig";
678 die "$f line $line: parse error\n";
687 sub modifyconfig { #{{{
689 # the section to modify or add
690 my $targetsection=shift;
691 # fields to change in the section
692 # To remove a field, set its value to "".
699 open(my $in, "<", $f) || die "mr: open $f: $!\n";
704 my $formatfield=sub {
706 my @value=split(/\n/, shift);
708 return "$field = ".shift(@value)."\n".
709 join("", map { "\t$_\n" } @value);
713 while ($out[$#out] =~ /^\s*$/) {
714 unshift @blanks, pop @out;
716 foreach my $field (sort keys %changefields) {
717 if (length $changefields{$field}) {
718 push @out, "$field = $changefields{$field}\n";
719 delete $changefields{$field};
729 if (/^\s*\#/ || /^\s*$/) {
732 elsif (/^\[([^\]]*)\]\s*$/) {
733 if (defined $section &&
734 $section eq $targetsection) {
742 elsif (/^(\w+)\s*=\s(.*)/) {
747 while (@lines && $lines[0]=~/^\s(.+)/) {
753 if ($section eq $targetsection) {
754 if (exists $changefields{$parameter}) {
755 if (length $changefields{$parameter}) {
756 $value=$changefields{$parameter};
758 delete $changefields{$parameter};
762 push @out, $formatfield->($parameter, $value);
766 if (defined $section &&
767 $section eq $targetsection) {
770 elsif (%changefields) {
771 push @out, "\n[$targetsection]\n";
772 foreach my $field (sort keys %changefields) {
773 if (length $changefields{$field}) {
774 push @out, $formatfield->($field, $changefields{$field});
779 open(my $out, ">", $f) || die "mr: write $f: $!\n";
784 # Finally, some useful actions that mr knows about by default.
785 # These can be overridden in ~/.mrconfig.
800 for dir in .git .svn .bzr CVS; do
801 if [ -e "$MR_REPO/$dir" ]; then
802 flagfile="$MR_REPO/$dir/.mr_last$1"
806 if [ -z "$flagfile" ]; then
807 error "cannot determine flag filename"
809 perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"
814 if [ -d "$MR_REPO"/.svn ]; then
816 elif [ -d "$MR_REPO"/.git ]; then
817 git pull origin master "$@"
818 elif [ -d "$MR_REPO"/.bzr ]; then
820 elif [ -d "$MR_REPO"/CVS ]; then
823 error "unknown repo type"
826 if [ -d "$MR_REPO"/.svn ]; then
828 elif [ -d "$MR_REPO"/.git ]; then
829 git status "$@" || true
830 elif [ -d "$MR_REPO"/.bzr ]; then
832 elif [ -d "$MR_REPO"/CVS ]; then
835 error "unknown repo type"
838 if [ -d "$MR_REPO"/.svn ]; then
840 elif [ -d "$MR_REPO"/.git ]; then
841 git commit -a "$@" && git push --all
842 elif [ -d "$MR_REPO"/.bzr ]; then
843 bzr commit "$@" && bzr push
844 elif [ -d "$MR_REPO"/CVS ]; then
847 error "unknown repo type"
850 if [ -d "$MR_REPO"/.svn ]; then
852 elif [ -d "$MR_REPO"/.git ]; then
854 elif [ -d "$MR_REPO"/.bzr ]; then
856 elif [ -d "$MR_REPO"/CVS ]; then
859 error "unknown repo type"
862 if [ -d "$MR_REPO"/.svn ]; then
864 elif [ -d "$MR_REPO"/.git ]; then
866 elif [ -d "$MR_REPO"/.bzr ]; then
868 elif [ -d "$MR_REPO"/CVS ]; then
871 error "unknown repo type"
877 basedir="$(basename $(pwd))"
879 url=$(LANG=C svn info . | grep -i ^URL: | cut -d ' ' -f 2)
880 if [ -z "$url" ]; then
881 error "cannot determine svn url"
883 echo "Registering svn url: $url"
884 mr -c "$MR_CONFIG" config "$(pwd)" checkout="svn co $url $basedir"
885 elif [ -d .git ]; then
886 url=$(LANG=C git-config --get remote.origin.url)
887 if [ -z "$url" ]; then
888 error "cannot determine git url"
890 echo "Registering git url: $url"
891 mr -c "$MR_CONFIG" config "$(pwd)" checkout="git clone $url $basedir"
892 elif [ -d .bzr ]; then
893 url=$(cat .bzr/branch/parent)
894 if [ -z "$url" ]; then
895 error "cannot determine bzr url"
897 echo "Registering bzr url: $url"
898 mr -c "$MR_CONFIG" config "$(pwd)" checkout="bzr clone $url $basedir"
899 elif [ -d CVS ]; then
900 repo=$(cat CVS/Repository)
902 if [ -z "$root" ]; then
903 error "cannot determine cvs root"
905 echo "Registering cvs repository $repo at root $root"
906 mr -c "$MR_CONFIG" config "$(pwd)" \
907 checkout="cvs -d '$root' co -d $basedir $repo"
909 error "unable to register this repo type"
912 if [ ! -e "$MR_PATH" ]; then
913 error "cannot find program path"
915 (pod2man -c mr "$MR_PATH" | man -l -) || error "pod2man or man failed"
919 ed = echo "A horse is a horse, of course, of course.."
920 T = echo "I pity the fool."
921 right = echo "Not found."