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.
5 mr - a Multiple Repository management tool
9 B<mr> [options] checkout
11 B<mr> [options] update
13 B<mr> [options] status
15 B<mr> [options] commit [-m "message"]
21 B<mr> [options] register repository
23 B<mr> [options] config section [parameter=[value] ...]
25 B<mr> [options] action [params ...]
29 B<mr> is a Multiple Repository management tool. It allows you to register a
30 set of repositories in a .mrconfig file, and then checkout, update, or
31 perform other actions on the repositories as if they were one big
34 Any mix of revision control systems can be used with B<mr>, and you can
35 define arbitrary actions for commands like "update", "checkout", or "commit".
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 The next parameter is the directory of an existing repository. The
84 repository will be registered in the mrconfig file.
88 Modifies the mrconfig file. The next parameter is the name of the section
89 to add or modify, and it is followed by one or more instances of
90 "parameter=value". Use "parameter=" to remove a parameter.
92 For example, to add (or edit) a repository in src/foo:
94 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
102 Actions can be abbreviated to any unambiguous subsctring, so
103 "mr st" is equivilant to "mr status", and "mr up" is equivilant to "mr
106 Additional parameters can be passed to most commands, and are passed on
107 unchanged to the underlying revision control system. This is mostly useful
108 if the repositories mr will act on all use the same revision control
117 Specifies the topmost directory that B<mr> should work in. The default is
118 the current working directory.
122 Use the specified mrconfig file, instead of looking for one in your home
133 B<mr> is configured by .mrconfig files. It starts by reading the .mrconfig
134 file in your home directory, and this can in turn chain load .mrconfig files
137 Here is an example .mrconfig file:
140 checkout = svn co svn://svn.example.com/src/trunk src
144 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
146 The .mrconfig file uses a variant of the INI file format. Lines starting with
147 "#" are comments. Lines ending with "\" are continued on to the next line.
149 The "DEFAULT" section allows setting default values for the sections that
152 The "ALIAS" section allows adding aliases for actions. Each parameter
153 is an alias, and its value is the action to use.
155 All other sections add repositories. The section header specifies the
156 directory where the repository is located. This is relative to the directory
157 that contains the mrconfig file, but you can also choose to use absolute
160 Within a section, each parameter defines a shell command to run to handle a
161 given action. mr contains default handlers for the "update", "status", and
162 "commit" actions, so normally you only need to specify what to do for
165 Note that these shell commands are run in a "set -e" shell
166 environment, where any additional parameters you pass are available in
167 "$@". The "checkout" command is run in the parent of the repository
168 directory, since the repository isn't checked out yet. All other commands
169 are run inside the repository, though not necessarily at the top of it.
170 The "MR_REPO" environment variable is set to the path to the top of the
173 A few parameters have special meanings:
179 If the "skip" parameter is set and its command returns nonzero, then B<mr>
180 will skip acting on that repository.
184 If the "chain" parameter is set and its command returns nonzero, then B<mr>
185 will try to load a .mrconfig file from the root of the repository. (You
186 should avoid chaining from repositories with untrusted committers.)
190 If the "deleted" parameter is set and its command returns nonzero, then
191 B<mr> will treat the repository as deleted. It won't ever actually delete
192 the repository, but it will warn if it sees the repsoitory's directory.
193 This is useful when one mrconfig file is shared amoung multiple machines,
194 to keep track of and remember to delete old repositories.
198 The "lib" parameter can specify some shell code that will be run before each
199 command, this can be a useful way to define shell functions for other commands
206 Copyright 2007 Joey Hess <joey@kitenet.net>
208 Licensed under the GNU GPL version 2 or higher.
210 http://kitenet.net/~joey/code/mr/
217 use Cwd qw(getcwd abs_path);
219 my $directory=getcwd();
220 my $config="$ENV{HOME}/.mrconfig";
226 Getopt::Long::Configure("no_permute");
227 my $result=GetOptions(
228 "d|directory=s" => sub { $directory=abs_path($_[1]) },
229 "c|config=s" => \$config,
230 "verbose" => \$verbose,
232 if (! $result || @ARGV < 1) {
233 die("Usage: mr [-d directory] action [params ...]\n".
234 "(Use mr help for man page.)\n");
241 #print Dumper(\%config);
244 use FindBin qw($Bin $Script);
245 $ENV{MR_PATH}=$Bin."/".$Script;
248 # alias expansion and command stemming
249 my $action=shift @ARGV;
250 if (exists $alias{$action}) {
251 $action=$alias{$action};
253 if (! exists $knownactions{$action}) {
254 my @matches = grep { /^\Q$action\E/ }
255 keys %knownactions, keys %alias;
259 elsif (@matches == 0) {
260 die "mr: unknown action \"$action\" (known actions: ".
261 join(", ", sort keys %knownactions).")\n";
264 die "mr: ambiguous action \"$action\" (matches: ".
265 join(", ", @matches).")\n";
269 if ($action eq 'help') {
270 exec($config{''}{DEFAULT}{$action}) || die "exec: $!";
272 elsif ($action eq 'config') {
274 die "mr config: not enough parameters\n";
277 if ($section=~/^\//) {
278 # try to convert to a path relative to $config's dir
279 my ($dir)=$config=~/^(.*\/)[^\/]+$/;
280 if ($section=~/^\Q$dir\E(.*)/) {
286 if (/^([^=]+)=(.*)$/) {
290 die "mr config: expected parameter=value, not \"$_\"\n";
293 modifyconfig($config, $section, %fields);
296 elsif ($action eq 'register') {
297 my $command="set -e; ".$config{''}{DEFAULT}{lib}."\n".
298 "my_action(){ $config{''}{DEFAULT}{$action}\n }; my_action ".
299 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
300 print STDERR "mr $action: running >>$command<<\n" if $verbose;
301 exec($command) || die "exec: $!";
304 # work out what repos to act on
307 foreach my $topdir (sort keys %config) {
308 foreach my $subdir (sort keys %{$config{$topdir}}) {
309 next if $subdir eq 'DEFAULT';
310 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
312 $dir.="/" unless $dir=~/\/$/;
313 $d.="/" unless $d=~/\/$/;
314 next if $dir ne $directory && $dir !~ /^\Q$directory\E/;
315 push @repos, [$dir, $topdir, $subdir];
319 # fallback to find a leaf repo
320 LEAF: foreach my $topdir (reverse sort keys %config) {
321 foreach my $subdir (reverse sort keys %{$config{$topdir}}) {
322 next if $subdir eq 'DEFAULT';
323 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
325 $dir.="/" unless $dir=~/\/$/;
326 $d.="/" unless $d=~/\/$/;
327 if ($d=~/^\Q$dir\E/) {
328 push @repos, [$dir, $topdir, $subdir];
336 my (@failed, @successful, @skipped);
337 foreach my $repo (@repos) {
338 action($action, @$repo);
342 my ($action, $dir, $topdir, $subdir) = @_;
344 my $lib= exists $config{$topdir}{$subdir}{lib} ?
345 $config{$topdir}{$subdir}{lib}."\n" : "";
347 if (exists $config{$topdir}{$subdir}{deleted}) {
352 my $test="set -e;".$lib.$config{$topdir}{$subdir}{deleted};
353 print "mr $action: running deleted test >>$test<<\n" if $verbose;
354 my $ret=system($test);
355 if ($ret >> 8 == 0) {
356 print STDERR "mr error: $dir should be deleted yet still exists\n\n";
363 if ($action eq 'checkout') {
365 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
369 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
371 elsif ($action eq 'update') {
373 return action("checkout", $dir, $topdir, $subdir);
379 if (exists $config{$topdir}{$subdir}{skip}) {
380 my $test="set -e;".$lib.$config{$topdir}{$subdir}{skip};
381 print "mr $action: running skip test >>$test<<\n" if $verbose;
382 my $ret=system($test);
383 if ($ret >> 8 == 0) {
384 print "mr $action: $dir skipped per config file\n" if $verbose;
390 if (! $nochdir && ! chdir($dir)) {
391 print STDERR "mr $action: failed to chdir to $dir: $!\n";
394 elsif (! exists $config{$topdir}{$subdir}{$action}) {
395 print STDERR "mr $action: no defined $action command for $topdir$subdir, skipping\n";
400 print "mr $action: $topdir$subdir\n";
403 print "mr $action: $topdir$subdir (in subdir $directory)\n";
405 my $command="set -e; ".$lib.
406 "my_action(){ $config{$topdir}{$subdir}{$action}\n }; my_action ".
407 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
408 print STDERR "mr $action: running >>$command<<\n" if $verbose;
409 my $ret=system($command);
411 print STDERR "mr $action: failed ($ret)\n" if $verbose;
413 if ($ret >> 8 != 0) {
414 print STDERR "mr $action: command failed\n";
417 print STDERR "mr $action: command died ($ret)\n";
421 push @successful, $dir;
433 return "$count ".($count > 1 ? $plural : $singular);
437 if (! @successful && ! @failed && ! @skipped) {
438 die "mr $action: no repositories found to work on\n";
440 print "mr $action: finished (".join("; ",
441 showstat($#successful+1, "successful", "successful"),
442 showstat($#failed+1, "failed", "failed"),
443 showstat($#skipped+1, "skipped", "skipped"),
448 elsif (! @successful && @skipped) {
461 if (ref $f eq 'GLOB') {
470 my $absf=abs_path($f);
471 if ($loaded{$absf}) {
476 print "mr: loading config $f\n" if $verbose;
477 open($in, "<", $f) || die "mr: open $f: $!\n";
478 ($dir)=$f=~/^(.*\/)[^\/]+$/;
479 if (! defined $dir) {
482 $dir=abs_path($dir)."/";
484 # copy in defaults from first parent
486 while ($parent=~s/^(.*)\/[^\/]+\/?$/$1/) {
487 if (exists $config{$parent} &&
488 exists $config{$parent}{DEFAULT}) {
489 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
498 next if /^\s*\#/ || /^\s*$/;
499 if (/^\s*\[([^\]]*)\]\s*$/) {
502 elsif (/^\s*(\w+)\s*=\s*(.*)/) {
507 while ($value=~/(.*)\\$/s) {
508 $value=$1."\n".<$in>;
512 if (! defined $section) {
513 die "$f line $.: parameter ($parameter) not in section\n";
515 if ($section ne 'ALIAS' &&
516 ! exists $config{$dir}{$section} &&
517 exists $config{$dir}{DEFAULT}) {
519 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
521 if ($section eq 'ALIAS') {
522 $alias{$parameter}=$value;
524 elsif ($parameter eq 'lib') {
525 $config{$dir}{$section}{lib}.=$value."\n";
528 $config{$dir}{$section}{$parameter}=$value;
529 $knownactions{$parameter}=1;
530 if ($parameter eq 'chain' &&
531 length $dir && $section ne "DEFAULT" &&
532 -e $dir.$section."/.mrconfig" &&
533 system($value) >> 8 == 0) {
534 push @toload, $dir.$section."/.mrconfig";
539 die "$f line $.: parse error\n";
551 # the section to modify or add
552 my $targetsection=shift;
553 # fields to change in the section
554 # To remove a field, set its value to "".
561 open(my $in, "<", $f) || die "mr: open $f: $!\n";
568 while ($out[$#out] =~ /^\s*$/) {
569 unshift @blanks, pop @out;
571 foreach my $field (sort keys %changefields) {
572 if (length $changefields{$field}) {
573 push @out, "$field = $changefields{$field}\n";
583 if (/^\s*\#/ || /^\s*$/) {
586 elsif (/^\s*\[([^\]]*)\]\s*$/) {
587 if (defined $section &&
588 $section eq $targetsection) {
596 elsif (/^\s*(\w+)\s*=\s(.*)/) {
601 while ($value=~/(.*\\)$/s) {
602 $value=$1."\n".shift(@lines);
606 if ($section eq $targetsection) {
607 if (exists $changefields{$parameter}) {
608 if (length $changefields{$parameter}) {
609 $value=$changefields{$parameter};
611 delete $changefields{$parameter};
615 push @out, "$parameter = $value\n";
619 if (defined $section &&
620 $section eq $targetsection) {
623 elsif (%changefields) {
624 push @out, "\n[$targetsection]\n";
625 foreach my $field (sort keys %changefields) {
626 if (length $changefields{$field}) {
627 push @out, "$field = $changefields{$field}\n";
632 open(my $out, ">", $f) || die "mr: write $f: $!\n";
637 # Finally, some useful actions that mr knows about by default.
638 # These can be overridden in ~/.mrconfig.
653 if [ -d "$MR_REPO"/.svn ]; then \
655 elif [ -d "$MR_REPO"/.git ]; then \
656 git pull origin master "$@" \
657 elif [ -d "$MR_REPO"/.bzr ]; then \
659 elif [ -d "$MR_REPO"/CVS ]; then \
662 error "unknown repo type" \
665 if [ -d "$MR_REPO"/.svn ]; then \
667 elif [ -d "$MR_REPO"/.git ]; then \
668 git status "$@" || true \
669 elif [ -d "$MR_REPO"/.bzr ]; then \
671 elif [ -d "$MR_REPO"/CVS ]; then \
674 error "unknown repo type" \
677 if [ -d "$MR_REPO"/.svn ]; then \
679 elif [ -d "$MR_REPO"/.git ]; then \
680 git commit -a "$@" && git push --all \
681 elif [ -d "$MR_REPO"/.bzr ]; then \
682 bzr commit "$@" && bzr push \
683 elif [ -d "$MR_REPO"/CVS ]; then \
686 error "unknown repo type" \
689 if [ -d "$MR_REPO"/.svn ]; then \
691 elif [ -d "$MR_REPO"/.git ]; then \
693 elif [ -d "$MR_REPO"/.bzr ]; then \
695 elif [ -d "$MR_REPO"/CVS ]; then \
698 error "unknown repo type" \
701 if [ -d "$MR_REPO"/.svn ]; then \
703 elif [ -d "$MR_REPO"/.git ]; then \
705 elif [ -d "$MR_REPO"/.bzr ]; then \
707 elif [ -d "$MR_REPO"/CVS ]; then \
710 error "unknown repo type" \
713 if [ -z "$1" ]; then \
714 error "repository directory not specified" \
717 basedir="$(basename $(pwd))" \
718 if [ -d .svn ]; then \
720 grep -i ^URL: | cut -d ' ' -f 2) \
721 if [ -z "$url" ]; then \
722 error "cannot determine svn url" \
724 echo "Registering svn url: $url" \
725 mr config "$(pwd)" checkout="svn co $url $basedir" \
726 elif [ -d .git ]; then \
727 url=$(git-config --get remote.origin.url) \
728 if [ -z "$url" ]; then \
729 error "cannot determine git url" \
731 echo "Registering git url: $url" \
732 mr config "$(pwd)" checkout="git clone $url $basedir" \
733 elif [ -d .bzr ]; then \
734 url=$(cat .bzr/branch/parent) \
735 if [ -z "$url" ]; then \
736 error "cannot determine bzr url" \
738 echo "Registering bzr url: $url" \
739 mr config "$(pwd)" checkout="bzr clone $url $basedir" \
741 error "unable to register this repo type" \
746 if [ ! -e "$MR_PATH" ]; then \
747 error "cannot find program path" \
749 (pod2man -c mr "$MR_PATH" | man -l -) || \
750 error "pod2man or man failed"
752 ed = echo "A horse is a horse, of course, of course.."
753 T = echo "I pity the fool."