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 The 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 List the repositories that mr will act on.
83 Register an existing repository in the mrconfig file. By default, the
84 epository in the current directory is registered, or you can specify a
85 directory to register.
89 Adds, modifies, removes, or prints a value from the mrconfig file. The next
90 parameter is the name of the section the value is in. To add or modify
91 values, use one or more instances of "parameter=value". Use "parameter=" to
92 remove a parameter. Use just "parameter" to get the value of a parameter.
94 For example, to add (or edit) a repository in src/foo:
96 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
98 To show the command that mr uses to update the repository in src/foo:
100 mr config src/foo update
108 Actions can be abbreviated to any unambiguous subsctring, so
109 "mr st" is equivilant to "mr status", and "mr up" is equivilant to "mr
112 Additional parameters can be passed to most commands, and are passed on
113 unchanged to the underlying revision control system. This is mostly useful
114 if the repositories mr will act on all use the same revision control
123 Specifies the topmost directory that B<mr> should work in. The default is
124 the current working directory.
128 Use the specified mrconfig file, instead of looking for one in your home
139 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
140 file in your home directory, and this can in turn chain load .mrconfig files
143 Here is an example .mrconfig file:
146 checkout = svn co svn://svn.example.com/src/trunk src
150 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
152 git checkout -b mybranch origin/master
154 The .mrconfig file uses a variant of the INI file format. Lines starting with
155 "#" are comments. Values can be continued to the following line by
156 indenting the line with whitespace.
158 The "DEFAULT" section allows setting default values for the sections that
161 The "ALIAS" section allows adding aliases for actions. Each parameter
162 is an alias, and its value is the action to use.
164 All other sections add repositories. The section header specifies the
165 directory where the repository is located. This is relative to the directory
166 that contains the mrconfig file, but you can also choose to use absolute
169 Within a section, each parameter defines a shell command to run to handle a
170 given action. mr contains default handlers for the "update", "status", and
171 "commit" actions, so normally you only need to specify what to do for
174 Note that these shell commands are run in a "set -e" shell
175 environment, where any additional parameters you pass are available in
176 "$@". The "checkout" command is run in the parent of the repository
177 directory, since the repository isn't checked out yet. All other commands
178 are run inside the repository, though not necessarily at the top of it.
179 The "MR_REPO" environment variable is set to the path to the top of the
182 A few parameters have special meanings:
188 If the "skip" parameter is set and its command returns nonzero, then B<mr>
189 will skip acting on that repository. The command is passed the action
192 Here are two examples. The first skips the repo unless
193 mr is run by joey. The second uses the hours_since function
194 (included in mr's built-in library) to skip updating the repo unless it's
195 been at least 12 hours since the last update.
197 skip = test $(whoami) != joey
198 skip = [ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]
202 If the "chain" parameter is set and its command returns nonzero, then B<mr>
203 will try to load a .mrconfig file from the root of the repository. (You
204 should avoid chaining from repositories with untrusted committers.)
208 If the "deleted" parameter is set and its command returns nonzero, then
209 B<mr> will treat the repository as deleted. It won't ever actually delete
210 the repository, but it will warn if it sees the repsoitory's directory.
211 This is useful when one mrconfig file is shared amoung multiple machines,
212 to keep track of and remember to delete old repositories.
216 The "lib" parameter can specify some shell code that will be run before each
217 command, this can be a useful way to define shell functions for other commands
224 Copyright 2007 Joey Hess <joey@kitenet.net>
226 Licensed under the GNU GPL version 2 or higher.
228 http://kitenet.net/~joey/code/mr/
237 use Cwd qw(getcwd abs_path);
239 my $directory=getcwd();
240 my $config="$ENV{HOME}/.mrconfig";
246 Getopt::Long::Configure("no_permute");
247 my $result=GetOptions(
248 "d|directory=s" => sub { $directory=abs_path($_[1]) },
249 "c|config=s" => \$config,
250 "verbose" => \$verbose,
252 if (! $result || @ARGV < 1) {
253 die("Usage: mr [-d directory] action [params ...]\n".
254 "(Use mr help for man page.)\n");
261 #print Dumper(\%config);
264 use FindBin qw($Bin $Script);
265 $ENV{MR_PATH}=$Bin."/".$Script;
268 # alias expansion and command stemming
269 my $action=shift @ARGV;
270 if (exists $alias{$action}) {
271 $action=$alias{$action};
273 if (! exists $knownactions{$action}) {
274 my @matches = grep { /^\Q$action\E/ }
275 keys %knownactions, keys %alias;
279 elsif (@matches == 0) {
280 die "mr: unknown action \"$action\" (known actions: ".
281 join(", ", sort keys %knownactions).")\n";
284 die "mr: ambiguous action \"$action\" (matches: ".
285 join(", ", @matches).")\n";
289 if ($action eq 'help') {
290 exec($config{''}{DEFAULT}{$action}) || die "exec: $!";
292 elsif ($action eq 'config') {
294 die "mr config: not enough parameters\n";
297 if ($section=~/^\//) {
298 # try to convert to a path relative to $config's dir
299 my ($dir)=$config=~/^(.*\/)[^\/]+$/;
300 if ($section=~/^\Q$dir\E(.*)/) {
306 if (/^([^=]+)=(.*)$/) {
307 $changefields{$1}=$2;
311 foreach my $topdir (sort keys %config) {
312 if (exists $config{$topdir}{$section} &&
313 exists $config{$topdir}{$section}{$_}) {
314 print $config{$topdir}{$section}{$_}."\n";
319 die "mr $action: $section $_ not set\n";
323 modifyconfig($config, $section, %changefields) if %changefields;
326 elsif ($action eq 'register') {
327 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
328 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
329 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
330 print STDERR "mr $action: running >>$command<<\n" if $verbose;
331 exec($command) || die "exec: $!";
334 # work out what repos to act on
337 foreach my $topdir (sort keys %config) {
338 foreach my $subdir (sort keys %{$config{$topdir}}) {
339 next if $subdir eq 'DEFAULT';
340 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
342 $dir.="/" unless $dir=~/\/$/;
343 $d.="/" unless $d=~/\/$/;
344 next if $dir ne $directory && $dir !~ /^\Q$directory\E/;
345 push @repos, [$dir, $topdir, $subdir];
349 # fallback to find a leaf repo
350 LEAF: foreach my $topdir (reverse sort keys %config) {
351 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
352 next if $subdir eq 'DEFAULT';
353 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
355 $dir.="/" unless $dir=~/\/$/;
356 $d.="/" unless $d=~/\/$/;
357 if ($d=~/^\Q$dir\E/) {
358 push @repos, [$dir, $topdir, $subdir];
366 my (@failed, @ok, @skipped);
367 foreach my $repo (@repos) {
368 action($action, @$repo);
372 my ($action, $dir, $topdir, $subdir) = @_;
374 my $lib= exists $config{$topdir}{$subdir}{lib} ?
375 $config{$topdir}{$subdir}{lib}."\n" : "";
377 if (exists $config{$topdir}{$subdir}{deleted}) {
382 my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
383 print "mr $action: running deleted test >>$test<<\n" if $verbose;
384 my $ret=system($test);
385 if ($ret >> 8 == 0) {
386 print STDERR "mr error: $dir should be deleted yet still exists\n\n";
393 if ($action eq 'checkout') {
395 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
399 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
401 elsif ($action eq 'update') {
403 return action("checkout", $dir, $topdir, $subdir);
409 if (exists $config{$topdir}{$subdir}{skip}) {
410 my $test="set -e;".$lib.
411 "my_action(){ $config{$topdir}{$subdir}{skip}\n }; my_action '$action'";
412 print "mr $action: running skip test >>$test<<\n" if $verbose;
413 my $ret=system($test);
414 if ($ret >> 8 == 0) {
415 print "mr $action: $dir skipped per config file\n" if $verbose;
421 if (! $nochdir && ! chdir($dir)) {
422 print STDERR "mr $action: failed to chdir to $dir: $!\n";
425 elsif (! exists $config{$topdir}{$subdir}{$action}) {
426 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
431 print "mr $action: $topdir$subdir\n";
434 print "mr $action: $topdir$subdir (in subdir $directory)\n";
436 my $command="set -e; ".$lib.
437 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
438 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
439 print STDERR "mr $action: running >>$command<<\n" if $verbose;
440 my $ret=system($command);
442 print STDERR "mr $action: failed ($ret)\n" if $verbose;
444 if ($ret >> 8 != 0) {
445 print STDERR "mr $action: command failed\n";
448 print STDERR "mr $action: command died ($ret)\n";
464 return "$count ".($count > 1 ? $plural : $singular);
468 if (! @ok && ! @failed && ! @skipped) {
469 die "mr $action: no repositories found to work on\n";
471 print "mr $action: finished (".join("; ",
472 showstat($#ok+1, "ok", "ok"),
473 showstat($#failed+1, "failed", "failed"),
474 showstat($#skipped+1, "skipped", "skipped"),
479 elsif (! @ok && @skipped) {
485 sub loadconfig { #{{{
492 if (ref $f eq 'GLOB') {
501 my $absf=abs_path($f);
502 if ($loaded{$absf}) {
507 ($dir)=$f=~/^(.*\/)[^\/]+$/;
508 if (! defined $dir) {
511 $dir=abs_path($dir)."/";
513 # copy in defaults from first parent
515 while ($parent=~s/^(.*)\/[^\/]+\/?$/$1/) {
516 if (exists $config{$parent} &&
517 exists $config{$parent}{DEFAULT}) {
518 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
523 print "mr: loading config $f\n" if $verbose;
524 open($in, "<", $f) || die "mr: open $f: $!\n";
535 next if /^\s*\#/ || /^\s*$/;
536 if (/^\[([^\]]*)\]\s*$/) {
539 elsif (/^(\w+)\s*=\s*(.*)/) {
544 while (@lines && $lines[0]=~/^\s(.+)/) {
551 if (! defined $section) {
552 die "$f line $.: parameter ($parameter) not in section\n";
554 if ($section ne 'ALIAS' &&
555 ! exists $config{$dir}{$section} &&
556 exists $config{$dir}{DEFAULT}) {
558 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
560 if ($section eq 'ALIAS') {
561 $alias{$parameter}=$value;
563 elsif ($parameter eq 'lib') {
564 $config{$dir}{$section}{lib}.=$value."\n";
567 $config{$dir}{$section}{$parameter}=$value;
568 $knownactions{$parameter}=1;
569 if ($parameter eq 'chain' &&
570 length $dir && $section ne "DEFAULT" &&
571 -e $dir.$section."/.mrconfig" &&
572 system($value) >> 8 == 0) {
573 push @toload, $dir.$section."/.mrconfig";
578 die "$f line $line: parse error\n";
587 sub modifyconfig { #{{{
589 # the section to modify or add
590 my $targetsection=shift;
591 # fields to change in the section
592 # To remove a field, set its value to "".
599 open(my $in, "<", $f) || die "mr: open $f: $!\n";
604 my $formatfield=sub {
606 my @value=split(/\n/, shift);
608 return "$field = ".shift(@value)."\n".
609 join("", map { "\t$_\n" } @value);
613 while ($out[$#out] =~ /^\s*$/) {
614 unshift @blanks, pop @out;
616 foreach my $field (sort keys %changefields) {
617 if (length $changefields{$field}) {
618 push @out, "$field = $changefields{$field}\n";
619 delete $changefields{$field};
629 if (/^\s*\#/ || /^\s*$/) {
632 elsif (/^\[([^\]]*)\]\s*$/) {
633 if (defined $section &&
634 $section eq $targetsection) {
642 elsif (/^(\w+)\s*=\s(.*)/) {
647 while (@lines && $lines[0]=~/^\s(.+)/) {
653 if ($section eq $targetsection) {
654 if (exists $changefields{$parameter}) {
655 if (length $changefields{$parameter}) {
656 $value=$changefields{$parameter};
658 delete $changefields{$parameter};
662 push @out, $formatfield->($parameter, $value);
666 if (defined $section &&
667 $section eq $targetsection) {
670 elsif (%changefields) {
671 push @out, "\n[$targetsection]\n";
672 foreach my $field (sort keys %changefields) {
673 if (length $changefields{$field}) {
674 push @out, $formatfield->($field, $changefields{$field});
679 open(my $out, ">", $f) || die "mr: write $f: $!\n";
684 # Finally, some useful actions that mr knows about by default.
685 # These can be overridden in ~/.mrconfig.
700 for dir in .git .svn .bzr CVS; do
701 if [ -e "$MR_REPO/$dir" ]; then
702 flagfile="$MR_REPO/$dir/.mr_last$1"
706 if [ -z "$flagfile" ]; then
707 error "cannot determine flag filename"
709 perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"
714 if [ -d "$MR_REPO"/.svn ]; then
716 elif [ -d "$MR_REPO"/.git ]; then
717 git pull origin master "$@"
718 elif [ -d "$MR_REPO"/.bzr ]; then
720 elif [ -d "$MR_REPO"/CVS ]; then
723 error "unknown repo type"
726 if [ -d "$MR_REPO"/.svn ]; then
728 elif [ -d "$MR_REPO"/.git ]; then
729 git status "$@" || true
730 elif [ -d "$MR_REPO"/.bzr ]; then
732 elif [ -d "$MR_REPO"/CVS ]; then
735 error "unknown repo type"
738 if [ -d "$MR_REPO"/.svn ]; then
740 elif [ -d "$MR_REPO"/.git ]; then
741 git commit -a "$@" && git push --all
742 elif [ -d "$MR_REPO"/.bzr ]; then
743 bzr commit "$@" && bzr push
744 elif [ -d "$MR_REPO"/CVS ]; then
747 error "unknown repo type"
750 if [ -d "$MR_REPO"/.svn ]; then
752 elif [ -d "$MR_REPO"/.git ]; then
754 elif [ -d "$MR_REPO"/.bzr ]; then
756 elif [ -d "$MR_REPO"/CVS ]; then
759 error "unknown repo type"
762 if [ -d "$MR_REPO"/.svn ]; then
764 elif [ -d "$MR_REPO"/.git ]; then
766 elif [ -d "$MR_REPO"/.bzr ]; then
768 elif [ -d "$MR_REPO"/CVS ]; then
771 error "unknown repo type"
777 basedir="$(basename $(pwd))"
779 url=$(LANG=C svn info . | grep -i ^URL: | cut -d ' ' -f 2)
780 if [ -z "$url" ]; then
781 error "cannot determine svn url"
783 echo "Registering svn url: $url"
784 mr config "$(pwd)" checkout="svn co $url $basedir"
785 elif [ -d .git ]; then
786 url=$(LANG=C git-config --get remote.origin.url)
787 if [ -z "$url" ]; then
788 error "cannot determine git url"
790 echo "Registering git url: $url"
791 mr config "$(pwd)" checkout="git clone $url $basedir"
792 elif [ -d .bzr ]; then
793 url=$(cat .bzr/branch/parent)
794 if [ -z "$url" ]; then
795 error "cannot determine bzr url"
797 echo "Registering bzr url: $url"
798 mr config "$(pwd)" checkout="bzr clone $url $basedir"
800 error "unable to register this repo type"
803 if [ ! -e "$MR_PATH" ]; then
804 error "cannot find program path"
806 (pod2man -c mr "$MR_PATH" | man -l -) || error "pod2man or man failed"
810 ed = echo "A horse is a horse, of course, of course.."
811 T = echo "I pity the fool."