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 repsitories 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 epository in the current directory is registered, or you can specify a
91 directory to register.
95 Adds, modifies, removes, or prints a value from the mrconfig file. The next
96 parameter is the name of the section the value is in. To add or modify
97 values, use one or more instances of "parameter=value". Use "parameter=" to
98 remove a parameter. Use just "parameter" to get the value of a parameter.
100 For example, to add (or edit) a repository in src/foo:
102 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
104 To show the command that mr uses to update the repository in src/foo:
106 mr config src/foo update
114 Actions can be abbreviated to any unambiguous subsctring, so
115 "mr st" is equivilant to "mr status", and "mr up" is equivilant to "mr
118 Additional parameters can be passed to most commands, and are passed on
119 unchanged to the underlying revision control system. This is mostly useful
120 if the repositories mr will act on all use the same revision control
129 Specifies the topmost directory that B<mr> should work in. The default is
130 the current working directory.
134 Use the specified mrconfig file, instead of looking for one in your home
145 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
146 file in your home directory, and this can in turn chain load .mrconfig files
149 Here is an example .mrconfig file:
152 checkout = svn co svn://svn.example.com/src/trunk src
156 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
158 git checkout -b mybranch origin/master
160 The .mrconfig file uses a variant of the INI file format. Lines starting with
161 "#" are comments. Values can be continued to the following line by
162 indenting the line with whitespace.
164 The "DEFAULT" section allows setting default values for the sections that
167 The "ALIAS" section allows adding aliases for actions. Each parameter
168 is an alias, and its value is the action to use.
170 All other sections add repositories. The section header specifies the
171 directory where the repository is located. This is relative to the directory
172 that contains the mrconfig file, but you can also choose to use absolute
175 Within a section, each parameter defines a shell command to run to handle a
176 given action. mr contains default handlers for the "update", "status", and
177 "commit" actions, so normally you only need to specify what to do for
180 Note that these shell commands are run in a "set -e" shell
181 environment, where any additional parameters you pass are available in
182 "$@". The "checkout" command is run in the parent of the repository
183 directory, since the repository isn't checked out yet. All other commands
184 are run inside the repository, though not necessarily at the top of it.
185 The "MR_REPO" environment variable is set to the path to the top of the
188 A few parameters have special meanings:
194 If the "skip" parameter is set and its command returns nonzero, then B<mr>
195 will skip acting on that repository. The command is passed the action
198 Here are two examples. The first skips the repo unless
199 mr is run by joey. The second uses the hours_since function
200 (included in mr's built-in library) to skip updating the repo unless it's
201 been at least 12 hours since the last update.
203 skip = test $(whoami) != joey
204 skip = [ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]
208 If the "chain" parameter is set and its command returns nonzero, then B<mr>
209 will try to load a .mrconfig file from the root of the repository. (You
210 should avoid chaining from repositories with untrusted committers.)
214 If the "deleted" parameter is set and its command returns nonzero, then
215 B<mr> will treat the repository as deleted. It won't ever actually delete
216 the repository, but it will warn if it sees the repsoitory's directory.
217 This is useful when one mrconfig file is shared amoung multiple machines,
218 to keep track of and remember to delete old repositories.
222 The "lib" parameter can specify some shell code that will be run before each
223 command, this can be a useful way to define shell functions for other commands
230 Copyright 2007 Joey Hess <joey@kitenet.net>
232 Licensed under the GNU GPL version 2 or higher.
234 http://kitenet.net/~joey/code/mr/
243 use Cwd qw(getcwd abs_path);
245 my $directory=getcwd();
246 my $config="$ENV{HOME}/.mrconfig";
252 Getopt::Long::Configure("no_permute");
253 my $result=GetOptions(
254 "d|directory=s" => sub { $directory=abs_path($_[1]) },
255 "c|config=s" => \$config,
256 "verbose" => \$verbose,
258 if (! $result || @ARGV < 1) {
259 die("Usage: mr [-d directory] action [params ...]\n".
260 "(Use mr help for man page.)\n");
267 #print Dumper(\%config);
270 use FindBin qw($Bin $Script);
271 $ENV{MR_PATH}=$Bin."/".$Script;
274 # alias expansion and command stemming
275 my $action=shift @ARGV;
276 if (exists $alias{$action}) {
277 $action=$alias{$action};
279 if (! exists $knownactions{$action}) {
280 my @matches = grep { /^\Q$action\E/ }
281 keys %knownactions, keys %alias;
285 elsif (@matches == 0) {
286 die "mr: unknown action \"$action\" (known actions: ".
287 join(", ", sort keys %knownactions).")\n";
290 die "mr: ambiguous action \"$action\" (matches: ".
291 join(", ", @matches).")\n";
295 if ($action eq 'help') {
296 exec($config{''}{DEFAULT}{$action}) || die "exec: $!";
298 elsif ($action eq 'config') {
300 die "mr config: not enough parameters\n";
303 if ($section=~/^\//) {
304 # try to convert to a path relative to $config's dir
305 my ($dir)=$config=~/^(.*\/)[^\/]+$/;
306 if ($section=~/^\Q$dir\E(.*)/) {
312 if (/^([^=]+)=(.*)$/) {
313 $changefields{$1}=$2;
317 foreach my $topdir (sort keys %config) {
318 if (exists $config{$topdir}{$section} &&
319 exists $config{$topdir}{$section}{$_}) {
320 print $config{$topdir}{$section}{$_}."\n";
325 die "mr $action: $section $_ not set\n";
329 modifyconfig($config, $section, %changefields) if %changefields;
332 elsif ($action eq 'register') {
333 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
334 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
335 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
336 print STDERR "mr $action: running >>$command<<\n" if $verbose;
337 exec($command) || die "exec: $!";
340 # work out what repos to act on
343 foreach my $topdir (sort keys %config) {
344 foreach my $subdir (sort keys %{$config{$topdir}}) {
345 next if $subdir eq 'DEFAULT';
346 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
348 $dir.="/" unless $dir=~/\/$/;
349 $d.="/" unless $d=~/\/$/;
350 next if $dir ne $directory && $dir !~ /^\Q$directory\E/;
351 push @repos, [$dir, $topdir, $subdir];
355 # fallback to find a leaf repo
356 LEAF: foreach my $topdir (reverse sort keys %config) {
357 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
358 next if $subdir eq 'DEFAULT';
359 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
361 $dir.="/" unless $dir=~/\/$/;
362 $d.="/" unless $d=~/\/$/;
363 if ($d=~/^\Q$dir\E/) {
364 push @repos, [$dir, $topdir, $subdir];
372 my (@failed, @ok, @skipped);
373 foreach my $repo (@repos) {
374 action($action, @$repo);
378 my ($action, $dir, $topdir, $subdir) = @_;
380 my $lib= exists $config{$topdir}{$subdir}{lib} ?
381 $config{$topdir}{$subdir}{lib}."\n" : "";
383 if (exists $config{$topdir}{$subdir}{deleted}) {
388 my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
389 print "mr $action: running deleted test >>$test<<\n" if $verbose;
390 my $ret=system($test);
391 if ($ret >> 8 == 0) {
392 print STDERR "mr error: $dir should be deleted yet still exists\n\n";
399 if ($action eq 'checkout') {
401 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
405 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
407 elsif ($action eq 'update') {
409 return action("checkout", $dir, $topdir, $subdir);
415 if (exists $config{$topdir}{$subdir}{skip}) {
416 my $test="set -e;".$lib.
417 "my_action(){ $config{$topdir}{$subdir}{skip}\n }; my_action '$action'";
418 print "mr $action: running skip test >>$test<<\n" if $verbose;
419 my $ret=system($test);
420 if ($ret >> 8 == 0) {
421 print "mr $action: $dir skipped per config file\n" if $verbose;
427 if (! $nochdir && ! chdir($dir)) {
428 print STDERR "mr $action: failed to chdir to $dir: $!\n";
431 elsif (! exists $config{$topdir}{$subdir}{$action}) {
432 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
437 print "mr $action: $topdir$subdir\n";
440 print "mr $action: $topdir$subdir (in subdir $directory)\n";
442 my $command="set -e; ".$lib.
443 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
444 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
445 print STDERR "mr $action: running >>$command<<\n" if $verbose;
446 my $ret=system($command);
448 print STDERR "mr $action: failed ($ret)\n" if $verbose;
450 if ($ret >> 8 != 0) {
451 print STDERR "mr $action: command failed\n";
454 print STDERR "mr $action: command died ($ret)\n";
470 return "$count ".($count > 1 ? $plural : $singular);
474 if (! @ok && ! @failed && ! @skipped) {
475 die "mr $action: no repositories found to work on\n";
477 print "mr $action: finished (".join("; ",
478 showstat($#ok+1, "ok", "ok"),
479 showstat($#failed+1, "failed", "failed"),
480 showstat($#skipped+1, "skipped", "skipped"),
485 elsif (! @ok && @skipped) {
491 sub loadconfig { #{{{
498 if (ref $f eq 'GLOB') {
507 my $absf=abs_path($f);
508 if ($loaded{$absf}) {
513 ($dir)=$f=~/^(.*\/)[^\/]+$/;
514 if (! defined $dir) {
517 $dir=abs_path($dir)."/";
519 # copy in defaults from first parent
521 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
522 if ($parent eq '/') {
525 if (exists $config{$parent} &&
526 exists $config{$parent}{DEFAULT}) {
527 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
532 print "mr: loading config $f\n" if $verbose;
533 open($in, "<", $f) || die "mr: open $f: $!\n";
544 next if /^\s*\#/ || /^\s*$/;
545 if (/^\[([^\]]*)\]\s*$/) {
548 elsif (/^(\w+)\s*=\s*(.*)/) {
553 while (@lines && $lines[0]=~/^\s(.+)/) {
560 if (! defined $section) {
561 die "$f line $.: parameter ($parameter) not in section\n";
563 if ($section ne 'ALIAS' &&
564 ! exists $config{$dir}{$section} &&
565 exists $config{$dir}{DEFAULT}) {
567 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
569 if ($section eq 'ALIAS') {
570 $alias{$parameter}=$value;
572 elsif ($parameter eq 'lib') {
573 $config{$dir}{$section}{lib}.=$value."\n";
576 $config{$dir}{$section}{$parameter}=$value;
577 $knownactions{$parameter}=1;
578 if ($parameter eq 'chain' &&
579 length $dir && $section ne "DEFAULT" &&
580 -e $dir.$section."/.mrconfig" &&
581 system($value) >> 8 == 0) {
582 push @toload, $dir.$section."/.mrconfig";
587 die "$f line $line: parse error\n";
596 sub modifyconfig { #{{{
598 # the section to modify or add
599 my $targetsection=shift;
600 # fields to change in the section
601 # To remove a field, set its value to "".
608 open(my $in, "<", $f) || die "mr: open $f: $!\n";
613 my $formatfield=sub {
615 my @value=split(/\n/, shift);
617 return "$field = ".shift(@value)."\n".
618 join("", map { "\t$_\n" } @value);
622 while ($out[$#out] =~ /^\s*$/) {
623 unshift @blanks, pop @out;
625 foreach my $field (sort keys %changefields) {
626 if (length $changefields{$field}) {
627 push @out, "$field = $changefields{$field}\n";
628 delete $changefields{$field};
638 if (/^\s*\#/ || /^\s*$/) {
641 elsif (/^\[([^\]]*)\]\s*$/) {
642 if (defined $section &&
643 $section eq $targetsection) {
651 elsif (/^(\w+)\s*=\s(.*)/) {
656 while (@lines && $lines[0]=~/^\s(.+)/) {
662 if ($section eq $targetsection) {
663 if (exists $changefields{$parameter}) {
664 if (length $changefields{$parameter}) {
665 $value=$changefields{$parameter};
667 delete $changefields{$parameter};
671 push @out, $formatfield->($parameter, $value);
675 if (defined $section &&
676 $section eq $targetsection) {
679 elsif (%changefields) {
680 push @out, "\n[$targetsection]\n";
681 foreach my $field (sort keys %changefields) {
682 if (length $changefields{$field}) {
683 push @out, $formatfield->($field, $changefields{$field});
688 open(my $out, ">", $f) || die "mr: write $f: $!\n";
693 # Finally, some useful actions that mr knows about by default.
694 # These can be overridden in ~/.mrconfig.
709 for dir in .git .svn .bzr CVS; do
710 if [ -e "$MR_REPO/$dir" ]; then
711 flagfile="$MR_REPO/$dir/.mr_last$1"
715 if [ -z "$flagfile" ]; then
716 error "cannot determine flag filename"
718 perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"
723 if [ -d "$MR_REPO"/.svn ]; then
725 elif [ -d "$MR_REPO"/.git ]; then
726 git pull origin master "$@"
727 elif [ -d "$MR_REPO"/.bzr ]; then
729 elif [ -d "$MR_REPO"/CVS ]; then
732 error "unknown repo type"
735 if [ -d "$MR_REPO"/.svn ]; then
737 elif [ -d "$MR_REPO"/.git ]; then
738 git status "$@" || true
739 elif [ -d "$MR_REPO"/.bzr ]; then
741 elif [ -d "$MR_REPO"/CVS ]; then
744 error "unknown repo type"
747 if [ -d "$MR_REPO"/.svn ]; then
749 elif [ -d "$MR_REPO"/.git ]; then
750 git commit -a "$@" && git push --all
751 elif [ -d "$MR_REPO"/.bzr ]; then
752 bzr commit "$@" && bzr push
753 elif [ -d "$MR_REPO"/CVS ]; then
756 error "unknown repo type"
759 if [ -d "$MR_REPO"/.svn ]; then
761 elif [ -d "$MR_REPO"/.git ]; then
763 elif [ -d "$MR_REPO"/.bzr ]; then
765 elif [ -d "$MR_REPO"/CVS ]; then
768 error "unknown repo type"
771 if [ -d "$MR_REPO"/.svn ]; then
773 elif [ -d "$MR_REPO"/.git ]; then
775 elif [ -d "$MR_REPO"/.bzr ]; then
777 elif [ -d "$MR_REPO"/CVS ]; then
780 error "unknown repo type"
786 basedir="$(basename $(pwd))"
788 url=$(LANG=C svn info . | grep -i ^URL: | cut -d ' ' -f 2)
789 if [ -z "$url" ]; then
790 error "cannot determine svn url"
792 echo "Registering svn url: $url"
793 mr config "$(pwd)" checkout="svn co $url $basedir"
794 elif [ -d .git ]; then
795 url=$(LANG=C git-config --get remote.origin.url)
796 if [ -z "$url" ]; then
797 error "cannot determine git url"
799 echo "Registering git url: $url"
800 mr config "$(pwd)" checkout="git clone $url $basedir"
801 elif [ -d .bzr ]; then
802 url=$(cat .bzr/branch/parent)
803 if [ -z "$url" ]; then
804 error "cannot determine bzr url"
806 echo "Registering bzr url: $url"
807 mr config "$(pwd)" checkout="bzr clone $url $basedir"
809 error "unable to register this repo type"
812 if [ ! -e "$MR_PATH" ]; then
813 error "cannot find program path"
815 (pod2man -c mr "$MR_PATH" | man -l -) || error "pod2man or man failed"
819 ed = echo "A horse is a horse, of course, of course.."
820 T = echo "I pity the fool."
821 right = echo "Not found."