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 a mrconfig file. By default, the
90 repository in the current directory is registered, or you can specify a
91 directory to register.
93 The mrconfig file that is modified is chosen by either the -c option, or by
94 looking for the closest known one at or below the current directory.
98 Adds, modifies, removes, or prints a value from a 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
111 The ~/.mrconfig file is used by default. To use a different config file,
120 Actions can be abbreviated to any unambiguous subsctring, so
121 "mr st" is equivilant to "mr status", and "mr up" is equivilant to "mr
124 Additional parameters can be passed to most commands, and are passed on
125 unchanged to the underlying revision control system. This is mostly useful
126 if the repositories mr will act on all use the same revision control
135 Specifies the topmost directory that B<mr> should work in. The default is
136 the current working directory.
140 Use the specified mrconfig file. The default is B<~/.mrconfig>
148 Expand the statistics line displayed at the end to include information
149 about exactly which repositories failed and were skipped, if any.
153 Just operate on the repository for the current directory, do not
154 recurse into deeper repositories.
160 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
161 file in your home directory, and this can in turn chain load .mrconfig files
164 Here is an example .mrconfig file:
167 checkout = svn co svn://svn.example.com/src/trunk src
171 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
173 git checkout -b mybranch origin/master
175 The .mrconfig file uses a variant of the INI file format. Lines starting with
176 "#" are comments. Values can be continued to the following line by
177 indenting the line with whitespace.
179 The "DEFAULT" section allows setting default values for the sections that
182 The "ALIAS" section allows adding aliases for actions. Each parameter
183 is an alias, and its value is the action to use.
185 All other sections add repositories. The section header specifies the
186 directory where the repository is located. This is relative to the directory
187 that contains the mrconfig file, but you can also choose to use absolute
190 Within a section, each parameter defines a shell command to run to handle a
191 given action. mr contains default handlers for "update", "status",
192 "commit", and other standard actions. Normally you only need to specify what
193 to do for "checkout".
195 Note that these shell commands are run in a "set -e" shell
196 environment, where any additional parameters you pass are available in
197 "$@". The "checkout" command is run in the parent of the repository
198 directory, since the repository isn't checked out yet. All other commands
199 are run inside the repository, though not necessarily at the top of it.
201 The "MR_REPO" environment variable is set to the path to the top of the
202 repository. The "MR_CONFIG" environment variable is set to the .mrconfig file
203 that defines the repo being acted on, or, if the repo is not yet in a config
204 file, the .mrconfig file that should be modified to register the repo.
206 A few parameters have special meanings:
212 If the "skip" parameter is set and its command returns true, then B<mr>
213 will skip acting on that repository. The command is passed the action
216 Here are two examples. The first skips the repo unless
217 mr is run by joey. The second uses the hours_since function
218 (included in mr's built-in library) to skip updating the repo unless it's
219 been at least 12 hours since the last update.
221 skip = test $(whoami) != joey
222 skip = [ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]
226 If the "chain" parameter is set and its command returns true, then B<mr>
227 will try to load a .mrconfig file from the root of the repository. (You
228 should avoid chaining from repositories with untrusted committers.)
232 The "lib" parameter can specify some shell code that will be run before each
233 command, this can be a useful way to define shell functions for other commands
240 Copyright 2007 Joey Hess <joey@kitenet.net>
242 Licensed under the GNU GPL version 2 or higher.
244 http://kitenet.net/~joey/code/mr/
253 use Cwd qw(getcwd abs_path);
256 print STDERR "mr: interrupted\n";
260 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
261 my $config_overridden=0;
262 my $directory=getcwd();
271 Getopt::Long::Configure("no_permute");
272 my $result=GetOptions(
273 "d|directory=s" => sub { $directory=abs_path($_[1]) },
274 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
275 "v|verbose" => \$verbose,
276 "s|stats" => \$stats,
277 "n|no-recurse" => \$no_recurse,
279 if (! $result || @ARGV < 1) {
280 die("Usage: mr [-d directory] action [params ...]\n".
281 "(Use mr help for man page.)\n");
285 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
286 # the config file might be a symlink to elsewhere, and the directory it's
288 if ($ENV{MR_CONFIG} !~ /^\//) {
289 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
293 loadconfig($ENV{MR_CONFIG});
295 #print Dumper(\%config);
298 use FindBin qw($Bin $Script);
299 $ENV{MR_PATH}=$Bin."/".$Script;
302 # alias expansion and command stemming
303 my $action=shift @ARGV;
304 if (exists $alias{$action}) {
305 $action=$alias{$action};
307 if (! exists $knownactions{$action}) {
308 my @matches = grep { /^\Q$action\E/ }
309 keys %knownactions, keys %alias;
313 elsif (@matches == 0) {
314 die "mr: unknown action \"$action\" (known actions: ".
315 join(", ", sort keys %knownactions).")\n";
318 die "mr: ambiguous action \"$action\" (matches: ".
319 join(", ", @matches).")\n";
323 # commands that do not operate on all repos
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 if (! $config_overridden) {
365 # Find the closest known mrconfig file to the current
367 $directory.="/" unless $directory=~/\/$/;
368 foreach my $topdir (reverse sort keys %config) {
369 next unless length $topdir;
370 if ($directory=~/^\Q$topdir\E/) {
371 $ENV{MR_CONFIG}=$configfiles{$topdir};
376 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
377 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
378 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
379 print STDERR "mr $action: running >>$command<<\n" if $verbose;
380 exec($command) || die "exec: $!";
383 # work out what repos to act on
386 foreach my $topdir (sort keys %config) {
387 foreach my $subdir (sort keys %{$config{$topdir}}) {
388 next if $subdir eq 'DEFAULT';
389 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
391 $dir.="/" unless $dir=~/\/$/;
392 $d.="/" unless $d=~/\/$/;
393 next if $no_recurse && $d ne $dir;
394 next if $dir ne $d && $dir !~ /^\Q$d\E/;
395 push @repos, [$dir, $topdir, $subdir];
399 # fallback to find a leaf repo
400 LEAF: foreach my $topdir (reverse sort keys %config) {
401 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
402 next if $subdir eq 'DEFAULT';
403 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
405 $dir.="/" unless $dir=~/\/$/;
406 $d.="/" unless $d=~/\/$/;
407 if ($d=~/^\Q$dir\E/) {
408 push @repos, [$dir, $topdir, $subdir];
416 my (@failed, @ok, @skipped);
417 foreach my $repo (@repos) {
418 action($action, @$repo);
422 my ($action, $dir, $topdir, $subdir) = @_;
424 $ENV{MR_CONFIG}=$configfiles{$topdir};
425 my $lib=exists $config{$topdir}{$subdir}{lib} ?
426 $config{$topdir}{$subdir}{lib}."\n" : "";
428 if ($action eq 'checkout') {
430 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
435 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
438 print "mr $action: creating parent directory $dir\n" if $verbose;
439 my $ret=system("mkdir", "-p", $dir);
442 elsif ($action eq 'update') {
444 return action("checkout", $dir, $topdir, $subdir);
450 if (exists $config{$topdir}{$subdir}{skip}) {
451 my $test="set -e;".$lib.
452 "my_action(){ $config{$topdir}{$subdir}{skip}\n }; my_action '$action'";
453 print "mr $action: running skip test >>$test<<\n" if $verbose;
454 my $ret=system($test);
456 if (($? & 127) == 2) {
457 print STDERR "mr $action: interrupted\n";
461 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
465 if ($ret >> 8 == 0) {
466 print "mr $action: $dir skipped per config file\n" if $verbose;
472 if (! $nochdir && ! chdir($dir)) {
473 print STDERR "mr $action: failed to chdir to $dir: $!\n";
476 elsif (! exists $config{$topdir}{$subdir}{$action}) {
477 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
482 print "mr $action: $topdir$subdir\n";
485 print "mr $action: $topdir$subdir (in subdir $directory)\n";
487 my $command="set -e; ".$lib.
488 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
489 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
490 print STDERR "mr $action: running >>$command<<\n" if $verbose;
491 my $ret=system($command);
493 if (($? & 127) == 2) {
494 print STDERR "mr $action: interrupted\n";
498 print STDERR "mr $action: received signal ".($? & 127)."\n";
500 print STDERR "mr $action: failed ($ret)\n" if $verbose;
502 if ($ret >> 8 != 0) {
503 print STDERR "mr $action: command failed\n";
506 print STDERR "mr $action: command died ($ret)\n";
510 if ($action eq 'checkout' && ! -d $dir) {
511 print STDERR "mr $action: $dir missing after checkout\n";;
528 return "$count ".($count > 1 ? $plural : $singular);
532 if (! @ok && ! @failed && ! @skipped) {
533 die "mr $action: no repositories found to work on\n";
535 print "mr $action: finished (".join("; ",
536 showstat($#ok+1, "ok", "ok"),
537 showstat($#failed+1, "failed", "failed"),
538 showstat($#skipped+1, "skipped", "skipped"),
542 print "mr $action: (skipped: ".join(" ", @skipped).")\n";
545 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
551 elsif (! @ok && @skipped) {
557 sub loadconfig { #{{{
564 if (ref $f eq 'GLOB') {
573 my $absf=abs_path($f);
574 if ($loaded{$absf}) {
579 ($dir)=$f=~/^(.*\/)[^\/]+$/;
580 if (! defined $dir) {
583 $dir=abs_path($dir)."/";
585 if (! exists $configfiles{$dir}) {
586 $configfiles{$dir}=$f;
589 # copy in defaults from first parent
591 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
592 if ($parent eq '/') {
595 if (exists $config{$parent} &&
596 exists $config{$parent}{DEFAULT}) {
597 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
602 print "mr: loading config $f\n" if $verbose;
603 open($in, "<", $f) || die "mr: open $f: $!\n";
614 next if /^\s*\#/ || /^\s*$/;
615 if (/^\[([^\]]*)\]\s*$/) {
618 elsif (/^(\w+)\s*=\s*(.*)/) {
623 while (@lines && $lines[0]=~/^\s(.+)/) {
630 if (! defined $section) {
631 die "$f line $.: parameter ($parameter) not in section\n";
633 if ($section ne 'ALIAS' &&
634 ! exists $config{$dir}{$section} &&
635 exists $config{$dir}{DEFAULT}) {
637 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
639 if ($section eq 'ALIAS') {
640 $alias{$parameter}=$value;
642 elsif ($parameter eq 'lib') {
643 $config{$dir}{$section}{lib}.=$value."\n";
646 $config{$dir}{$section}{$parameter}=$value;
647 $knownactions{$parameter}=1;
648 if ($parameter eq 'chain' &&
649 length $dir && $section ne "DEFAULT" &&
650 -e $dir.$section."/.mrconfig") {
651 my $ret=system($value);
653 if (($? & 127) == 2) {
654 print STDERR "mr $action: chain test interrupted\n";
658 print STDERR "mr $action: chain test received signal ".($? & 127)."\n";
662 push @toload, $dir.$section."/.mrconfig";
668 die "$f line $line: parse error\n";
677 sub modifyconfig { #{{{
679 # the section to modify or add
680 my $targetsection=shift;
681 # fields to change in the section
682 # To remove a field, set its value to "".
689 open(my $in, "<", $f) || die "mr: open $f: $!\n";
694 my $formatfield=sub {
696 my @value=split(/\n/, shift);
698 return "$field = ".shift(@value)."\n".
699 join("", map { "\t$_\n" } @value);
703 while ($out[$#out] =~ /^\s*$/) {
704 unshift @blanks, pop @out;
706 foreach my $field (sort keys %changefields) {
707 if (length $changefields{$field}) {
708 push @out, "$field = $changefields{$field}\n";
709 delete $changefields{$field};
719 if (/^\s*\#/ || /^\s*$/) {
722 elsif (/^\[([^\]]*)\]\s*$/) {
723 if (defined $section &&
724 $section eq $targetsection) {
732 elsif (/^(\w+)\s*=\s(.*)/) {
737 while (@lines && $lines[0]=~/^\s(.+)/) {
743 if ($section eq $targetsection) {
744 if (exists $changefields{$parameter}) {
745 if (length $changefields{$parameter}) {
746 $value=$changefields{$parameter};
748 delete $changefields{$parameter};
752 push @out, $formatfield->($parameter, $value);
756 if (defined $section &&
757 $section eq $targetsection) {
760 elsif (%changefields) {
761 push @out, "\n[$targetsection]\n";
762 foreach my $field (sort keys %changefields) {
763 if (length $changefields{$field}) {
764 push @out, $formatfield->($field, $changefields{$field});
769 open(my $out, ">", $f) || die "mr: write $f: $!\n";
774 # Finally, some useful actions that mr knows about by default.
775 # These can be overridden in ~/.mrconfig.
790 for dir in .git .svn .bzr CVS; do
791 if [ -e "$MR_REPO/$dir" ]; then
792 flagfile="$MR_REPO/$dir/.mr_last$1"
796 if [ -z "$flagfile" ]; then
797 error "cannot determine flag filename"
799 perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"
804 if [ -d "$MR_REPO"/.svn ]; then
806 elif [ -d "$MR_REPO"/.git ]; then
807 git pull origin master "$@"
808 elif [ -d "$MR_REPO"/.bzr ]; then
810 elif [ -d "$MR_REPO"/CVS ]; then
813 error "unknown repo type"
816 if [ -d "$MR_REPO"/.svn ]; then
818 elif [ -d "$MR_REPO"/.git ]; then
819 git status "$@" || true
820 elif [ -d "$MR_REPO"/.bzr ]; then
822 elif [ -d "$MR_REPO"/CVS ]; then
825 error "unknown repo type"
828 if [ -d "$MR_REPO"/.svn ]; then
830 elif [ -d "$MR_REPO"/.git ]; then
831 git commit -a "$@" && git push --all
832 elif [ -d "$MR_REPO"/.bzr ]; then
833 bzr commit "$@" && bzr push
834 elif [ -d "$MR_REPO"/CVS ]; then
837 error "unknown repo type"
840 if [ -d "$MR_REPO"/.svn ]; then
842 elif [ -d "$MR_REPO"/.git ]; then
844 elif [ -d "$MR_REPO"/.bzr ]; then
846 elif [ -d "$MR_REPO"/CVS ]; then
849 error "unknown repo type"
852 if [ -d "$MR_REPO"/.svn ]; then
854 elif [ -d "$MR_REPO"/.git ]; then
856 elif [ -d "$MR_REPO"/.bzr ]; then
858 elif [ -d "$MR_REPO"/CVS ]; then
861 error "unknown repo type"
867 basedir="$(basename $(pwd))"
869 url=$(LANG=C svn info . | grep -i ^URL: | cut -d ' ' -f 2)
870 if [ -z "$url" ]; then
871 error "cannot determine svn url"
873 echo "Registering svn url: $url in $MR_CONFIG"
874 mr -c "$MR_CONFIG" config "$(pwd)" checkout="svn co $url $basedir"
875 elif [ -d .git ]; then
876 url=$(LANG=C git-config --get remote.origin.url)
877 if [ -z "$url" ]; then
878 error "cannot determine git url"
880 echo "Registering git url: $url in $MR_CONFIG"
881 mr -c "$MR_CONFIG" config "$(pwd)" checkout="git clone $url $basedir"
882 elif [ -d .bzr ]; then
883 url=$(cat .bzr/branch/parent)
884 if [ -z "$url" ]; then
885 error "cannot determine bzr url"
887 echo "Registering bzr url: $url in $MR_CONFIG"
888 mr -c "$MR_CONFIG" config "$(pwd)" checkout="bzr clone $url $basedir"
889 elif [ -d CVS ]; then
890 repo=$(cat CVS/Repository)
892 if [ -z "$root" ]; then
893 error "cannot determine cvs root"
895 echo "Registering cvs repository $repo at root $root"
896 mr -c "$MR_CONFIG" config "$(pwd)" \
897 checkout="cvs -d '$root' co -d $basedir $repo"
899 error "unable to register this repo type"
902 if [ ! -e "$MR_PATH" ]; then
903 error "cannot find program path"
905 (pod2man -c mr "$MR_PATH" | man -l -) || error "pod2man or man failed"
909 ed = echo "A horse is a horse, of course, of course.."
910 T = echo "I pity the fool."
911 right = echo "Not found."