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
143 Expand the statistics line displayed at the end to include information
144 about exactly which repositories failed and were skipped, if any.
150 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
151 file in your home directory, and this can in turn chain load .mrconfig files
154 Here is an example .mrconfig file:
157 checkout = svn co svn://svn.example.com/src/trunk src
161 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
163 git checkout -b mybranch origin/master
165 The .mrconfig file uses a variant of the INI file format. Lines starting with
166 "#" are comments. Values can be continued to the following line by
167 indenting the line with whitespace.
169 The "DEFAULT" section allows setting default values for the sections that
172 The "ALIAS" section allows adding aliases for actions. Each parameter
173 is an alias, and its value is the action to use.
175 All other sections add repositories. The section header specifies the
176 directory where the repository is located. This is relative to the directory
177 that contains the mrconfig file, but you can also choose to use absolute
180 Within a section, each parameter defines a shell command to run to handle a
181 given action. mr contains default handlers for the "update", "status", and
182 "commit" actions, so normally you only need to specify what to do for
185 Note that these shell commands are run in a "set -e" shell
186 environment, where any additional parameters you pass are available in
187 "$@". The "checkout" command is run in the parent of the repository
188 directory, since the repository isn't checked out yet. All other commands
189 are run inside the repository, though not necessarily at the top of it.
190 The "MR_REPO" environment variable is set to the path to the top of the
193 A few parameters have special meanings:
199 If the "skip" parameter is set and its command returns nonzero, then B<mr>
200 will skip acting on that repository. The command is passed the action
203 Here are two examples. The first skips the repo unless
204 mr is run by joey. The second uses the hours_since function
205 (included in mr's built-in library) to skip updating the repo unless it's
206 been at least 12 hours since the last update.
208 skip = test $(whoami) != joey
209 skip = [ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]
213 If the "chain" parameter is set and its command returns nonzero, then B<mr>
214 will try to load a .mrconfig file from the root of the repository. (You
215 should avoid chaining from repositories with untrusted committers.)
219 If the "deleted" parameter is set and its command returns nonzero, then
220 B<mr> will treat the repository as deleted. It won't ever actually delete
221 the repository, but it will warn if it sees the repsoitory's directory.
222 This is useful when one mrconfig file is shared amoung multiple machines,
223 to keep track of and remember to delete old repositories.
227 The "lib" parameter can specify some shell code that will be run before each
228 command, this can be a useful way to define shell functions for other commands
235 Copyright 2007 Joey Hess <joey@kitenet.net>
237 Licensed under the GNU GPL version 2 or higher.
239 http://kitenet.net/~joey/code/mr/
248 use Cwd qw(getcwd abs_path);
250 my $directory=getcwd();
251 my $config="$ENV{HOME}/.mrconfig";
258 Getopt::Long::Configure("no_permute");
259 my $result=GetOptions(
260 "d|directory=s" => sub { $directory=abs_path($_[1]) },
261 "c|config=s" => \$config,
262 "v|verbose" => \$verbose,
263 "s|stats" => \$stats,
265 if (! $result || @ARGV < 1) {
266 die("Usage: mr [-d directory] action [params ...]\n".
267 "(Use mr help for man page.)\n");
274 #print Dumper(\%config);
277 use FindBin qw($Bin $Script);
278 $ENV{MR_PATH}=$Bin."/".$Script;
281 # alias expansion and command stemming
282 my $action=shift @ARGV;
283 if (exists $alias{$action}) {
284 $action=$alias{$action};
286 if (! exists $knownactions{$action}) {
287 my @matches = grep { /^\Q$action\E/ }
288 keys %knownactions, keys %alias;
292 elsif (@matches == 0) {
293 die "mr: unknown action \"$action\" (known actions: ".
294 join(", ", sort keys %knownactions).")\n";
297 die "mr: ambiguous action \"$action\" (matches: ".
298 join(", ", @matches).")\n";
302 if ($action eq 'help') {
303 exec($config{''}{DEFAULT}{$action}) || die "exec: $!";
305 elsif ($action eq 'config') {
307 die "mr config: not enough parameters\n";
310 if ($section=~/^\//) {
311 # try to convert to a path relative to $config's dir
312 my ($dir)=$config=~/^(.*\/)[^\/]+$/;
313 if ($section=~/^\Q$dir\E(.*)/) {
319 if (/^([^=]+)=(.*)$/) {
320 $changefields{$1}=$2;
324 foreach my $topdir (sort keys %config) {
325 if (exists $config{$topdir}{$section} &&
326 exists $config{$topdir}{$section}{$_}) {
327 print $config{$topdir}{$section}{$_}."\n";
332 die "mr $action: $section $_ not set\n";
336 modifyconfig($config, $section, %changefields) if %changefields;
339 elsif ($action eq 'register') {
340 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
341 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
342 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
343 print STDERR "mr $action: running >>$command<<\n" if $verbose;
344 exec($command) || die "exec: $!";
347 # work out what repos to act on
350 foreach my $topdir (sort keys %config) {
351 foreach my $subdir (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 next if $dir ne $directory && $dir !~ /^\Q$directory\E/;
358 push @repos, [$dir, $topdir, $subdir];
362 # fallback to find a leaf repo
363 LEAF: foreach my $topdir (reverse sort keys %config) {
364 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
365 next if $subdir eq 'DEFAULT';
366 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
368 $dir.="/" unless $dir=~/\/$/;
369 $d.="/" unless $d=~/\/$/;
370 if ($d=~/^\Q$dir\E/) {
371 push @repos, [$dir, $topdir, $subdir];
379 my (@failed, @ok, @skipped);
380 foreach my $repo (@repos) {
381 action($action, @$repo);
385 my ($action, $dir, $topdir, $subdir) = @_;
387 my $lib= exists $config{$topdir}{$subdir}{lib} ?
388 $config{$topdir}{$subdir}{lib}."\n" : "";
390 if (exists $config{$topdir}{$subdir}{deleted}) {
395 my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
396 print "mr $action: running deleted test >>$test<<\n" if $verbose;
397 my $ret=system($test);
398 if ($ret >> 8 == 0) {
399 print STDERR "mr error: $dir should be deleted yet still exists\n\n";
406 if ($action eq 'checkout') {
408 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
412 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
414 elsif ($action eq 'update') {
416 return action("checkout", $dir, $topdir, $subdir);
422 if (exists $config{$topdir}{$subdir}{skip}) {
423 my $test="set -e;".$lib.
424 "my_action(){ $config{$topdir}{$subdir}{skip}\n }; my_action '$action'";
425 print "mr $action: running skip test >>$test<<\n" if $verbose;
426 my $ret=system($test);
427 if ($ret >> 8 == 0) {
428 print "mr $action: $dir skipped per config file\n" if $verbose;
434 if (! $nochdir && ! chdir($dir)) {
435 print STDERR "mr $action: failed to chdir to $dir: $!\n";
438 elsif (! exists $config{$topdir}{$subdir}{$action}) {
439 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
444 print "mr $action: $topdir$subdir\n";
447 print "mr $action: $topdir$subdir (in subdir $directory)\n";
449 my $command="set -e; ".$lib.
450 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
451 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
452 print STDERR "mr $action: running >>$command<<\n" if $verbose;
453 my $ret=system($command);
455 print STDERR "mr $action: failed ($ret)\n" if $verbose;
457 if ($ret >> 8 != 0) {
458 print STDERR "mr $action: command failed\n";
461 print STDERR "mr $action: command died ($ret)\n";
477 return "$count ".($count > 1 ? $plural : $singular);
481 if (! @ok && ! @failed && ! @skipped) {
482 die "mr $action: no repositories found to work on\n";
484 print "mr $action: finished (".join("; ",
485 showstat($#ok+1, "ok", "ok"),
486 showstat($#failed+1, "failed", "failed"),
487 showstat($#skipped+1, "skipped", "skipped"),
491 print "mr $action: (skipped: ".join(" ", @skipped).")\n";
494 print "mr $action: (failed: ".join(" ", @failed).")\n";
500 elsif (! @ok && @skipped) {
506 sub loadconfig { #{{{
513 if (ref $f eq 'GLOB') {
522 my $absf=abs_path($f);
523 if ($loaded{$absf}) {
528 ($dir)=$f=~/^(.*\/)[^\/]+$/;
529 if (! defined $dir) {
532 $dir=abs_path($dir)."/";
534 # copy in defaults from first parent
536 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
537 if ($parent eq '/') {
540 if (exists $config{$parent} &&
541 exists $config{$parent}{DEFAULT}) {
542 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
547 print "mr: loading config $f\n" if $verbose;
548 open($in, "<", $f) || die "mr: open $f: $!\n";
559 next if /^\s*\#/ || /^\s*$/;
560 if (/^\[([^\]]*)\]\s*$/) {
563 elsif (/^(\w+)\s*=\s*(.*)/) {
568 while (@lines && $lines[0]=~/^\s(.+)/) {
575 if (! defined $section) {
576 die "$f line $.: parameter ($parameter) not in section\n";
578 if ($section ne 'ALIAS' &&
579 ! exists $config{$dir}{$section} &&
580 exists $config{$dir}{DEFAULT}) {
582 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
584 if ($section eq 'ALIAS') {
585 $alias{$parameter}=$value;
587 elsif ($parameter eq 'lib') {
588 $config{$dir}{$section}{lib}.=$value."\n";
591 $config{$dir}{$section}{$parameter}=$value;
592 $knownactions{$parameter}=1;
593 if ($parameter eq 'chain' &&
594 length $dir && $section ne "DEFAULT" &&
595 -e $dir.$section."/.mrconfig" &&
596 system($value) >> 8 == 0) {
597 push @toload, $dir.$section."/.mrconfig";
602 die "$f line $line: parse error\n";
611 sub modifyconfig { #{{{
613 # the section to modify or add
614 my $targetsection=shift;
615 # fields to change in the section
616 # To remove a field, set its value to "".
623 open(my $in, "<", $f) || die "mr: open $f: $!\n";
628 my $formatfield=sub {
630 my @value=split(/\n/, shift);
632 return "$field = ".shift(@value)."\n".
633 join("", map { "\t$_\n" } @value);
637 while ($out[$#out] =~ /^\s*$/) {
638 unshift @blanks, pop @out;
640 foreach my $field (sort keys %changefields) {
641 if (length $changefields{$field}) {
642 push @out, "$field = $changefields{$field}\n";
643 delete $changefields{$field};
653 if (/^\s*\#/ || /^\s*$/) {
656 elsif (/^\[([^\]]*)\]\s*$/) {
657 if (defined $section &&
658 $section eq $targetsection) {
666 elsif (/^(\w+)\s*=\s(.*)/) {
671 while (@lines && $lines[0]=~/^\s(.+)/) {
677 if ($section eq $targetsection) {
678 if (exists $changefields{$parameter}) {
679 if (length $changefields{$parameter}) {
680 $value=$changefields{$parameter};
682 delete $changefields{$parameter};
686 push @out, $formatfield->($parameter, $value);
690 if (defined $section &&
691 $section eq $targetsection) {
694 elsif (%changefields) {
695 push @out, "\n[$targetsection]\n";
696 foreach my $field (sort keys %changefields) {
697 if (length $changefields{$field}) {
698 push @out, $formatfield->($field, $changefields{$field});
703 open(my $out, ">", $f) || die "mr: write $f: $!\n";
708 # Finally, some useful actions that mr knows about by default.
709 # These can be overridden in ~/.mrconfig.
724 for dir in .git .svn .bzr CVS; do
725 if [ -e "$MR_REPO/$dir" ]; then
726 flagfile="$MR_REPO/$dir/.mr_last$1"
730 if [ -z "$flagfile" ]; then
731 error "cannot determine flag filename"
733 perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"
738 if [ -d "$MR_REPO"/.svn ]; then
740 elif [ -d "$MR_REPO"/.git ]; then
741 git pull origin master "$@"
742 elif [ -d "$MR_REPO"/.bzr ]; then
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
753 git status "$@" || true
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
765 git commit -a "$@" && git push --all
766 elif [ -d "$MR_REPO"/.bzr ]; then
767 bzr commit "$@" && bzr push
768 elif [ -d "$MR_REPO"/CVS ]; then
771 error "unknown repo type"
774 if [ -d "$MR_REPO"/.svn ]; then
776 elif [ -d "$MR_REPO"/.git ]; then
778 elif [ -d "$MR_REPO"/.bzr ]; then
780 elif [ -d "$MR_REPO"/CVS ]; then
783 error "unknown repo type"
786 if [ -d "$MR_REPO"/.svn ]; then
788 elif [ -d "$MR_REPO"/.git ]; then
790 elif [ -d "$MR_REPO"/.bzr ]; then
792 elif [ -d "$MR_REPO"/CVS ]; then
795 error "unknown repo type"
801 basedir="$(basename $(pwd))"
803 url=$(LANG=C svn info . | grep -i ^URL: | cut -d ' ' -f 2)
804 if [ -z "$url" ]; then
805 error "cannot determine svn url"
807 echo "Registering svn url: $url"
808 mr config "$(pwd)" checkout="svn co $url $basedir"
809 elif [ -d .git ]; then
810 url=$(LANG=C git-config --get remote.origin.url)
811 if [ -z "$url" ]; then
812 error "cannot determine git url"
814 echo "Registering git url: $url"
815 mr config "$(pwd)" checkout="git clone $url $basedir"
816 elif [ -d .bzr ]; then
817 url=$(cat .bzr/branch/parent)
818 if [ -z "$url" ]; then
819 error "cannot determine bzr url"
821 echo "Registering bzr url: $url"
822 mr config "$(pwd)" checkout="bzr clone $url $basedir"
824 error "unable to register this repo type"
827 if [ ! -e "$MR_PATH" ]; then
828 error "cannot find program path"
830 (pod2man -c mr "$MR_PATH" | man -l -) || error "pod2man or man failed"
834 ed = echo "A horse is a horse, of course, of course.."
835 T = echo "I pity the fool."
836 right = echo "Not found."