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"]
17 B<mr> [options] record [-m "message"]
23 B<mr> [options] bootstrap url [directory]
25 B<mr> [options] register [repository]
27 B<mr> [options] config section ["parameter=[value]" ...]
29 B<mr> [options] action [params ...]
31 B<mr> [options] [online|offline]
33 B<mr> [options] remember action [params ...]
37 B<mr> is a Multiple Repository management tool. It can checkout, update, or
38 perform other actions on a set of repositories as if they were one combined
39 repository. It supports any combination of subversion, git, cvs, mercurial,
40 bzr and darcs repositories, and support for other revision control systems can
43 B<mr> cds into and operates on all registered repositories at or below your
44 working directory. Or, if you are in a subdirectory of a repository that
45 contains no other registered repositories, it will stay in that directory,
46 and work on only that repository,
48 B<mr> is configured by .mrconfig files, which list the repositories. It
49 starts by reading the .mrconfig file in your home directory, and this can
50 in turn chain load .mrconfig files from repositories.
52 These predefined commands should be fairly familiar to users of any revision
57 =item checkout (or co)
59 Checks out any repositories that are not already checked out.
63 Updates each repository from its configured remote repository.
65 If a repository isn't checked out yet, it will first check it out.
69 Displays a status report for each repository, showing what
70 uncommitted changes are present in the repository.
74 Commits changes to each repository. (By default, changes are pushed to the
75 remote repository too, when using distributed systems like git. If you
76 don't like this default, you can change it in your .mrconfig, or use record
79 The optional -m parameter allows specifying a commit message.
83 Records changes to the local repository, but does not push them to the
84 remote repository. Only supported for distributed revision control systems.
86 The optional -m parameter allows specifying a commit message.
90 Pushes committed local changes to the remote repository. A no-op for
91 centralized revision control systems.
95 Show a diff of uncommitted changes.
103 These commands are also available:
107 =item bootstrap url [directory]
109 Causes mr to download the url, and use it as a .mrconfig file
110 to checkout the repositories listed in it, into the specified directory.
112 The directory will be created if it does not exist. If no directory is
113 specified, the current directory will be used.
115 If the .mrconfig file includes a repository named ".", that
116 is checked out into the top of the specified directory.
118 To bootstrap from a SSL url with an untrusted certificate, you may
119 need to specify the -k option.
123 List the repositories that mr will act on.
127 Register an existing repository in a mrconfig file. By default, the
128 repository in the current directory is registered, or you can specify a
129 directory to register.
131 The mrconfig file that is modified is chosen by either the -c option, or by
132 looking for the closest known one at or below the current directory.
136 Adds, modifies, removes, or prints a value from a mrconfig file. The next
137 parameter is the name of the section the value is in. To add or modify
138 values, use one or more instances of "parameter=value". Use "parameter=" to
139 remove a parameter. Use just "parameter" to get the value of a parameter.
141 For example, to add (or edit) a repository in src/foo:
143 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
145 To show the command that mr uses to update the repository in src/foo:
147 mr config src/foo update
149 To see the built-in library of shell functions contained in mr:
151 mr config DEFAULT lib
153 The ~/.mrconfig file is used by default. To use a different config file,
158 Advises mr that it is in offline mode. Any commands that fail in
159 offline mode will be remembered, and retried when mr is told it's online.
163 Advices mr that it is in online mode again. Commands that failed while in
164 offline mode will be re-run.
168 Remember a command, to be run later when mr re-enters online mode. This
169 implicitly puts mr into offline mode. The command can be any regular mr
170 command. This is useful when you know that a command will fail due to being
171 offline, and so don't want to run it right now at all, but just remember
172 to run it when you go back online.
180 Actions can be abbreviated to any unambiguous substring, so
181 "mr st" is equivalent to "mr status", and "mr up" is equivalent to "mr
184 Additional parameters can be passed to most commands, and are passed on
185 unchanged to the underlying revision control system. This is mostly useful
186 if the repositories mr will act on all use the same revision control
195 =item --directory directory
197 Specifies the topmost directory that B<mr> should work in. The default is
198 the current working directory.
202 =item --config mrconfig
204 Use the specified mrconfig file. The default is B<~/.mrconfig>
210 Search in the current directory, and its parent directories and use
211 the first B<.mrconfig> found, instead of the default B<~/.mrconfig>.
229 Accept untrusted SSL certificates when bootstrapping.
235 Expand the statistics line displayed at the end to include information
236 about exactly which repositories failed and were skipped, if any.
242 Interactive mode. If a repository fails to be processed, a subshell will be
243 started which you can use to resolve or investigate the problem. Exit the
244 subshell to continue the mr run.
248 =item --no-recurse [number]
250 If no number if specified, just operate on the repository for the current
251 directory, do not recurse into deeper repositories.
253 If a number is specified, will recurse into repositories at most that many
254 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
255 but not ./src/packages/bar.
259 =item --jobs [number]
261 Run the specified number of jobs in parallel, or an unlimited number of jobs
262 with no number specified. This can greatly speed up operations such as updates.
263 It is not recommended for interactive operations.
265 Note that running more than 10 jobs at a time is likely to run afoul of
266 ssh connection limits. Running between 3 and 5 jobs at a time will yield
267 a good speedup in updates without loading the machine too much.
273 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
278 =head1 "MRCONFIG FILES"
280 Here is an example .mrconfig file:
283 checkout = svn co svn://svn.example.com/src/trunk src
287 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
289 git checkout -b mybranch origin/master
291 The .mrconfig file uses a variant of the INI file format. Lines starting with
292 "#" are comments. Values can be continued to the following line by
293 indenting the line with whitespace.
295 The "DEFAULT" section allows setting default values for the sections that
298 The "ALIAS" section allows adding aliases for actions. Each parameter
299 is an alias, and its value is the action to use.
301 All other sections add repositories. The section header specifies the
302 directory where the repository is located. This is relative to the directory
303 that contains the mrconfig file, but you can also choose to use absolute
304 paths. (Note that you can use environment variables in section names; they
305 will be passed through the shell for expansion. For example,
306 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
308 Within a section, each parameter defines a shell command to run to handle a
309 given action. mr contains default handlers for "update", "status",
310 "commit", and other standard actions. Normally you only need to specify what
311 to do for "checkout".
313 Note that these shell commands are run in a "set -e" shell
314 environment, where any additional parameters you pass are available in
315 "$@". The "checkout" command is run in the parent of the repository
316 directory, since the repository isn't checked out yet. All other commands
317 are run inside the repository, though not necessarily at the top of it.
319 The "MR_REPO" environment variable is set to the path to the top of the
320 repository. (For the "register" action, "MR_REPO" is instead set to the
321 basename of the directory that should be created when checking the
324 The "MR_CONFIG" environment variable is set to the .mrconfig file
325 that defines the repo being acted on, or, if the repo is not yet in a config
326 file, the .mrconfig file that should be modified to register the repo.
328 A few parameters have special meanings:
334 If the "skip" parameter is set and its command returns true, then B<mr>
335 will skip acting on that repository. The command is passed the action
338 Here are two examples. The first skips the repo unless
339 mr is run by joey. The second uses the hours_since function
340 (included in mr's built-in library) to skip updating the repo unless it's
341 been at least 12 hours since the last update.
343 skip = test `whoami` != joey
344 skip = [ "$1" = update ] && ! hours_since "$1" 12
348 The "order" parameter can be used to override the default ordering of
349 repositories. The default order value is 10. Use smaller values to make
350 repositories be processed earlier, and larger values to make repositories
353 Note that if a repository is located in a subdirectory of another
354 repository, ordering it to be processed earlier is not recommended.
358 If the "chain" parameter is set and its command returns true, then B<mr>
359 will try to load a .mrconfig file from the root of the repository.
363 If the "include" parameter is set, its command is ran, and should output
364 additional mrconfig file content. The content is included as if it were
365 part of the including file.
367 Unlike all other parameters, this parameter does not need to be placed
372 The "lib" parameter can specify some shell code that will be run before each
373 command, this can be a useful way to define shell functions for other commands
378 When looking for a command to run for a given action, mr first looks for
379 a parameter with the same name as the action. If that is not found, it
380 looks for a parameter named "rcs_action" (substituting in the name of the
381 revision control system and the action). The name of the revision control
382 system is itself determined by running each defined "rcs_test" action,
385 Internally, mr has settings for "git_update", "svn_update", etc. To change
386 the action that is performed for a given revision control system, you can
387 override these rcs specific actions. To add a new revision control system,
388 you can just add rcs specific actions for it.
390 The ~/.mrlog file contains commands that mr has remembered to run later,
391 due to being offline. You can delete or edit this file to remove commands,
392 or even to add other commands for 'mr online' to run. If the file is
393 present, mr assumes it is in offline mode.
395 =head1 "UNTRUSTED MRCONFIG FILES"
397 Since mrconfig files can contain arbitrary shell commands, they can do
398 anything. This flexability is good, but it also allows a malicious mrconfig
399 file to delete your whole home directory. Such a file might be contained
400 inside a repository that your main ~/.mrconfig checks out and chains to. To
401 avoid worries about evil commands in a mrconfig file, mr
402 has the ability to read mrconfig files in untrusted mode. Such files are
403 limited to running only known safe commands (like "git clone") in a
404 carefully checked manner.
406 By default, mr trusts all mrconfig files. (This default will change in a
407 future release!) But if you have a ~/.mrtrust file, mr will only trust
408 mrconfig files that are listed within it. (One file per line.) All other
409 files will be treated as untrusted.
413 mr can be extended to support things such as unison and git-svn. Some
414 files providing such extensions are available in /usr/share/mr/. See
415 the documentation in the files for details about using them.
419 Copyright 2007-2009 Joey Hess <joey@kitenet.net>
421 Licensed under the GNU GPL version 2 or higher.
423 http://kitenet.net/~joey/code/mr/
430 use Cwd qw(getcwd abs_path);
432 # things that can happen when mr runs a command
441 my $config_overridden=0;
451 my $directory=getcwd();
452 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
459 my (@ok, @failed, @skipped);
465 my ($action, $dir, $topdir, $subdir) = @_;
467 if (exists $rcs{$dir}) {
472 foreach my $rcs_test (
474 length $a <=> length $b
477 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
478 my ($rcs)=$rcs_test=~/(.*)_test/;
479 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
480 $test.="if my_$rcs_test; then echo $rcs; fi\n";
482 $test=$config{$topdir}{$subdir}{lib}."\n".$test
483 if exists $config{$topdir}{$subdir}{lib};
485 print "mr $action: running rcs test >>$test<<\n" if $verbose;
490 print STDERR "mr $action: found multiple possible repository types ($rcs) for $topdir$subdir\n";
494 return $rcs{$dir}=undef;
497 return $rcs{$dir}=$rcs;
502 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
504 if (exists $config{$topdir}{$subdir}{$action}) {
505 return $config{$topdir}{$subdir}{$action};
512 my $rcs=rcs_test(@_);
515 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
516 return $config{$topdir}{$subdir}{$rcs."_".$action};
524 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
526 $ENV{MR_CONFIG}=$configfiles{$topdir};
527 my $lib=exists $config{$topdir}{$subdir}{lib} ?
528 $config{$topdir}{$subdir}{lib}."\n" : "";
529 my $is_checkout=($action eq 'checkout');
534 if (! $force_checkout) {
536 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
540 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
543 elsif ($action =~ /update/) {
545 return action("checkout", $dir, $topdir, $subdir);
549 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
550 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
552 if (defined $skiptest) {
553 my $test="set -e;".$lib.
554 "my_action(){ $skiptest\n }; my_action '$action'";
555 print "mr $action: running skip test >>$test<<\n" if $verbose;
556 my $ret=system($test);
558 if (($? & 127) == 2) {
559 print STDERR "mr $action: interrupted\n";
563 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
567 if ($ret >> 8 == 0) {
568 print "mr $action: $dir skipped per config file\n" if $verbose;
573 if ($is_checkout && ! -d $dir) {
574 print "mr $action: creating parent directory $dir\n" if $verbose;
575 system("mkdir", "-p", $dir);
578 if (! $no_chdir && ! chdir($dir)) {
579 print STDERR "mr $action: failed to chdir to $dir: $!\n";
582 elsif (! defined $command) {
583 my $rcs=rcs_test(@_);
584 if (! defined $rcs) {
585 print STDERR "mr $action: unknown repository type and no defined $action command for $topdir$subdir\n";
589 print STDERR "mr $action: no defined action for $rcs repository $topdir$subdir, skipping\n";
595 print "mr $action: $topdir$subdir\n" unless $quiet;
599 $s=~s/^\Q$topdir$subdir\E\/?//;
600 print "mr $action: $topdir$subdir (in subdir $s)\n" unless $quiet;
602 $command="set -e; ".$lib.
603 "my_action(){ $command\n }; my_action ".
604 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
605 print "mr $action: running >>$command<<\n" if $verbose;
606 my $ret=system($command);
608 if (($? & 127) == 2) {
609 print STDERR "mr $action: interrupted\n";
613 print STDERR "mr $action: received signal ".($? & 127)."\n";
616 print STDERR "mr $action: failed ($ret)\n" if $verbose;
617 if ($ret >> 8 != 0) {
618 print STDERR "mr $action: command failed\n";
619 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
620 # recreate original command line to
621 # remember, and avoid recursing
623 @ARGV=('-n', $action, @orig);
624 action("remember", $dir, $topdir, $subdir);
629 print STDERR "mr $action: command died ($ret)\n";
634 if ($action eq 'checkout' && ! -d $dir) {
635 print STDERR "mr $action: $dir missing after checkout\n";;
644 # run actions on multiple repos, in parallel
654 while (@fhs or @repos) {
655 while ((!$jobs || $running < $jobs) && @repos) {
657 my $repo = shift @repos;
658 pipe(my $outfh, CHILD_STDOUT);
659 pipe(my $errfh, CHILD_STDERR);
661 unless ($pid = fork) {
662 die "mr $action: cannot fork: $!" unless defined $pid;
663 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
664 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
669 exit action($action, @$repo);
673 push @active, [$pid, $repo];
674 push @fhs, [$outfh, $errfh];
677 my ($rin, $rout) = ('','');
679 foreach my $fh (@fhs) {
680 next unless defined $fh;
681 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
682 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
684 $nfound = select($rout=$rin, undef, undef, 1);
685 foreach my $channel (0, 1) {
686 foreach my $i (0..$#fhs) {
687 next unless defined $fhs[$i];
688 my $fh = $fhs[$i][$channel];
689 next unless defined $fh;
690 if (vec($rout, fileno($fh), 1) == 1) {
692 if (sysread($fh, $r, 1024) == 0) {
694 $fhs[$i][$channel] = undef;
695 if (! defined $fhs[$i][0] &&
696 ! defined $fhs[$i][1]) {
697 waitpid($active[$i][0], 0);
698 print STDOUT $out[$i][0];
699 print STDERR $out[$i][1];
700 record($active[$i][1], $? >> 8);
702 splice(@active, $i, 1);
707 $out[$i][$channel] .= $r;
715 my $dir=shift()->[0];
720 print "\n" unless $quiet;
722 elsif ($ret == FAILED) {
724 chdir($dir) unless $no_chdir;
725 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
726 system((getpwuid($<))[8], "-i");
729 print "\n" unless $quiet;
731 elsif ($ret == SKIPPED) {
734 elsif ($ret == ABORT) {
738 die "unknown exit status $ret";
744 if (! @ok && ! @failed && ! @skipped) {
745 die "mr $action: no repositories found to work on\n";
747 print "mr $action: finished (".join("; ",
748 showstat($#ok+1, "ok", "ok"),
749 showstat($#failed+1, "failed", "failed"),
750 showstat($#skipped+1, "skipped", "skipped"),
751 ).")\n" unless $quiet;
754 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
757 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
767 return "$count ".($count > 1 ? $plural : $singular);
772 # an ordered list of repos
775 foreach my $topdir (sort keys %config) {
776 foreach my $subdir (sort keys %{$config{$topdir}}) {
780 order => $config{$topdir}{$subdir}{order},
785 $a->{order} <=> $b->{order}
787 $a->{topdir} cmp $b->{topdir}
789 $a->{subdir} cmp $b->{subdir}
793 # figure out which repos to act on
796 foreach my $repo (repolist()) {
797 my $topdir=$repo->{topdir};
798 my $subdir=$repo->{subdir};
800 next if $subdir eq 'DEFAULT';
801 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
803 $dir.="/" unless $dir=~/\/$/;
804 $d.="/" unless $d=~/\/$/;
805 next if $dir ne $d && $dir !~ /^\Q$d\E/;
806 if (defined $max_depth) {
807 my @a=split('/', $dir);
808 my @b=split('/', $d);
809 do { } while (@a && @b && shift(@a) eq shift(@b));
810 next if @a > $max_depth || @b > $max_depth;
812 push @repos, [$dir, $topdir, $subdir];
815 # fallback to find a leaf repo
816 foreach my $repo (reverse repolist()) {
817 my $topdir=$repo->{topdir};
818 my $subdir=$repo->{subdir};
820 next if $subdir eq 'DEFAULT';
821 my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
823 $dir.="/" unless $dir=~/\/$/;
824 $d.="/" unless $d=~/\/$/;
825 if ($d=~/^\Q$dir\E/) {
826 push @repos, [$dir, $topdir, $subdir];
848 sub is_trusted_config {
849 my $config=shift; # must be abs_pathed already
851 # We always trust ~/.mrconfig.
852 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
854 return 1 if $trust_all;
856 my $trustfile=$ENV{HOME}."/.mrtrust";
858 if (! -e $trustfile) {
859 print "mr: Assuming $config is trusted.\n";
860 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
861 print "mr: and list all trusted mrconfig files in it.\n";
866 $trusted{"$ENV{HOME}/.mrconfig"}=1;
867 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
870 s/^~\//$ENV{HOME}\//;
871 $trusted{abs_path($_)}=1;
876 return $trusted{$config};
880 sub is_trusted_repo {
883 # Tightly limit what is allowed in a repo name.
884 # No ../, no absolute paths, and no unusual filenames
885 # that might try to escape to the shell.
886 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
887 $repo !~ /\.\./ && $repo !~ /^\//;
890 sub is_trusted_checkout {
893 # To determine if the command is safe, compare it with the
894 # *_trusted_checkout config settings. Those settings are
895 # templates for allowed commands, so make sure that each word
896 # of the command matches the corresponding word of the template.
899 foreach my $word (split(' ', $command)) {
901 if ($word=~/^'(.*)'$/) {
904 elsif ($word=~/^"(.*)"$/) {
911 foreach my $key (grep { /_trusted_checkout$/ }
912 keys %{$config{''}{DEFAULT}}) {
913 my @twords=split(' ', $config{''}{DEFAULT}{$key});
914 next if @words > @twords;
918 for (my $c=0; $c < @twords && $match; $c++) {
919 if ($twords[$c] eq '$url') {
920 # Match all the typical characters found in
921 # urls, plus @ which svn can use. Note
922 # that the "url" might also be a local
925 defined $words[$c] &&
926 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
930 elsif ($twords[$c] eq '$repo') {
931 # If a repo is not specified, assume it
932 # will be the last path component of the
933 # url, or something derived from it, and
935 if (! defined $words[$c] && defined $url) {
936 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
940 defined $words[$c] &&
941 is_trusted_repo($words[$c])
944 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
966 if (ref $f eq 'GLOB') {
976 my $absf=abs_path($f);
977 if ($loaded{$absf}) {
982 $trusted=is_trusted_config($absf);
984 if (! defined $dir) {
985 ($dir)=$f=~/^(.*\/)[^\/]+$/;
986 if (! defined $dir) {
991 $dir=abs_path($dir)."/";
993 if (! exists $configfiles{$dir}) {
994 $configfiles{$dir}=$f;
997 # copy in defaults from first parent
999 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1000 if ($parent eq '/') {
1003 if (exists $config{$parent} &&
1004 exists $config{$parent}{DEFAULT}) {
1005 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1010 print "mr: loading config $f\n" if $verbose;
1011 open($in, "<", $f) || die "mr: open $f: $!\n";
1014 close $in unless ref $f eq 'GLOB';
1022 next if /^\s*\#/ || /^\s*$/;
1023 if (/^\[([^\]]*)\]\s*$/) {
1027 if (! is_trusted_repo($section) ||
1028 $section eq 'ALIAS' ||
1029 $section eq 'DEFAULT') {
1030 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1033 $section=expandenv($section) if $trusted;
1035 elsif (/^(\w+)\s*=\s*(.*)/) {
1040 while (@lines && $lines[0]=~/^\s(.+)/) {
1048 # Untrusted files can only contain checkout
1050 if ($parameter ne 'checkout') {
1051 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1053 if (! is_trusted_checkout($value)) {
1054 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1058 if ($parameter eq "include") {
1059 print "mr: including output of \"$value\"\n" if $verbose;
1060 unshift @lines, `$value`;
1062 print STDERR "mr: include command exited nonzero ($?)\n";
1067 if (! defined $section) {
1068 die "$f line $.: parameter ($parameter) not in section\n";
1070 if ($section ne 'ALIAS' &&
1071 ! exists $config{$dir}{$section} &&
1072 exists $config{$dir}{DEFAULT}) {
1074 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1076 if ($section eq 'ALIAS') {
1077 $alias{$parameter}=$value;
1079 elsif ($parameter eq 'lib') {
1080 $config{$dir}{$section}{lib}.=$value."\n";
1083 $config{$dir}{$section}{$parameter}=$value;
1084 if ($parameter =~ /.*_(.*)/) {
1085 $knownactions{$1}=1;
1088 $knownactions{$parameter}=1;
1090 if ($parameter eq 'chain' &&
1091 length $dir && $section ne "DEFAULT" &&
1092 -e $dir.$section."/.mrconfig") {
1093 my $ret=system($value);
1095 if (($? & 127) == 2) {
1096 print STDERR "mr: chain test interrupted\n";
1100 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1104 push @toload, $dir.$section."/.mrconfig";
1110 die "$f line $line: parse error\n";
1119 sub startingconfig {
1120 %alias=%config=%configfiles=%knownactions=%loaded=();
1121 my $datapos=tell(DATA);
1123 seek(DATA,$datapos,0); # rewind
1128 # the section to modify or add
1129 my $targetsection=shift;
1130 # fields to change in the section
1131 # To remove a field, set its value to "".
1132 my %changefields=@_;
1138 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1143 my $formatfield=sub {
1145 my @value=split(/\n/, shift);
1147 return "$field = ".shift(@value)."\n".
1148 join("", map { "\t$_\n" } @value);
1152 while ($out[$#out] =~ /^\s*$/) {
1153 unshift @blanks, pop @out;
1155 foreach my $field (sort keys %changefields) {
1156 if (length $changefields{$field}) {
1157 push @out, "$field = $changefields{$field}\n";
1158 delete $changefields{$field};
1168 if (/^\s*\#/ || /^\s*$/) {
1171 elsif (/^\[([^\]]*)\]\s*$/) {
1172 if (defined $section &&
1173 $section eq $targetsection) {
1177 $section=expandenv($1);
1181 elsif (/^(\w+)\s*=\s(.*)/) {
1186 while (@lines && $lines[0]=~/^\s(.+)/) {
1192 if ($section eq $targetsection) {
1193 if (exists $changefields{$parameter}) {
1194 if (length $changefields{$parameter}) {
1195 $value=$changefields{$parameter};
1197 delete $changefields{$parameter};
1201 push @out, $formatfield->($parameter, $value);
1205 if (defined $section &&
1206 $section eq $targetsection) {
1209 elsif (%changefields) {
1210 push @out, "\n[$targetsection]\n";
1211 foreach my $field (sort keys %changefields) {
1212 if (length $changefields{$field}) {
1213 push @out, $formatfield->($field, $changefields{$field});
1218 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1226 # actions that do not operate on all repos
1227 if ($action eq 'help') {
1230 elsif ($action eq 'config') {
1233 elsif ($action eq 'register') {
1236 elsif ($action eq 'bootstrap') {
1239 elsif ($action eq 'remember' ||
1240 $action eq 'offline' ||
1241 $action eq 'online') {
1242 my @repos=selectrepos;
1243 action($action, @{$repos[0]}) if @repos;
1247 if (!$jobs || $jobs > 1) {
1248 mrs($action, selectrepos());
1251 foreach my $repo (selectrepos()) {
1252 record($repo, action($action, @$repo));
1258 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1263 die "mr config: not enough parameters\n";
1266 if ($section=~/^\//) {
1267 # try to convert to a path relative to the config file
1268 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1269 $dir=abs_path($dir);
1270 $dir.="/" unless $dir=~/\/$/;
1271 if ($section=~/^\Q$dir\E(.*)/) {
1277 if (/^([^=]+)=(.*)$/) {
1278 $changefields{$1}=$2;
1282 foreach my $topdir (sort keys %config) {
1283 if (exists $config{$topdir}{$section} &&
1284 exists $config{$topdir}{$section}{$_}) {
1285 print $config{$topdir}{$section}{$_}."\n";
1287 last if $section eq 'DEFAULT';
1291 die "mr config: $section $_ not set\n";
1295 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1300 if ($config_overridden) {
1301 # Find the directory that the specified config file is
1303 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1306 # Find the closest known mrconfig file to the current
1308 $directory.="/" unless $directory=~/\/$/;
1310 foreach my $topdir (reverse sort keys %config) {
1311 next unless length $topdir;
1312 if ($directory=~/^\Q$topdir\E/) {
1313 $ENV{MR_CONFIG}=$configfiles{$topdir};
1319 if (! $foundconfig) {
1320 $directory=""; # no config file, use builtin
1324 my $subdir=shift @ARGV;
1325 if (! chdir($subdir)) {
1326 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1330 $ENV{MR_REPO}=getcwd();
1331 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1332 if (! defined $command) {
1333 die "mr register: unknown repository type\n";
1336 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1337 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1338 "my_action(){ $command\n }; my_action ".
1339 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1340 print "mr register: running >>$command<<\n" if $verbose;
1341 exec($command) || die "exec: $!";
1345 my $url=shift @ARGV;
1346 my $dir=shift @ARGV || ".";
1348 if (! defined $url || ! length $url) {
1349 die "mr: bootstrap requires url\n";
1352 # Download the config file to a temporary location.
1353 eval q{use File::Temp};
1355 my $tmpconfig=File::Temp->new();
1356 my @curlargs = ("curl", "-A", "mr", "-s", $url, "-o", $tmpconfig);
1357 push(@curlargs, "-k") if $insecure;
1358 my $curlstatus = system(@curlargs);
1359 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1360 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1363 system("mkdir", "-p", $dir);
1365 chdir($dir) || die "chdir $dir: $!";
1367 # Special case to handle checkout of the "." repo, which
1368 # would normally be skipped.
1369 my $topdir=abs_path(".")."/";
1370 my @repo=($topdir, $topdir, ".");
1371 loadconfig($tmpconfig, $topdir);
1372 record(\@repo, action("checkout", @repo, 1))
1373 if exists $config{$topdir}{"."}{"checkout"};
1375 if (-e ".mrconfig") {
1376 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1379 eval q{use File::Copy};
1381 move($tmpconfig, ".mrconfig") || die "rename: $!";
1384 # Reload the config file (in case we got a different version)
1385 # and checkout everything else.
1387 loadconfig(".mrconfig");
1388 dispatch("checkout");
1389 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1390 showstats("bootstrap");
1394 # alias expansion and command stemming
1397 if (exists $alias{$action}) {
1398 $action=$alias{$action};
1400 if (! exists $knownactions{$action}) {
1401 my @matches = grep { /^\Q$action\E/ }
1402 keys %knownactions, keys %alias;
1403 if (@matches == 1) {
1404 $action=$matches[0];
1406 elsif (@matches == 0) {
1407 die "mr: unknown action \"$action\" (known actions: ".
1408 join(", ", sort keys %knownactions).")\n";
1411 die "mr: ambiguous action \"$action\" (matches: ".
1412 join(", ", @matches).")\n";
1418 sub find_nearest_mrconfig {
1420 while (length $dir) {
1421 if (-e "$dir/.mrconfig") {
1422 return "$dir/.mrconfig";
1424 $dir=~s/\/[^\/]*$//;
1426 die "no .mrconfig found in path\n";
1431 Getopt::Long::Configure("bundling", "no_permute");
1432 my $result=GetOptions(
1433 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1434 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1435 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1436 "v|verbose" => \$verbose,
1437 "q|quiet" => \$quiet,
1438 "s|stats" => \$stats,
1439 "k|insecure" => \$insecure,
1440 "i|interactive" => \$interactive,
1441 "n|no-recurse:i" => \$max_depth,
1442 "j|jobs:i" => \$jobs,
1443 "t|trust-all" => \$trust_all,
1445 if (! $result || @ARGV < 1) {
1446 die("Usage: mr [options] action [params ...]\n".
1447 "(Use mr help for man page.)\n");
1450 $ENV{MR_SWITCHES}="";
1451 foreach my $option (@saved) {
1452 last if $option eq $ARGV[0];
1453 $ENV{MR_SWITCHES}.="$option ";
1459 print STDERR "mr: interrupted\n";
1463 # This can happen if it's run in a directory that was removed
1464 # or other strangeness.
1465 if (! defined $directory) {
1466 die("mr: failed to determine working directory\n");
1468 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1469 # the config file might be a symlink to elsewhere, and the directory it's
1470 # in is significant.
1471 if ($ENV{MR_CONFIG} !~ /^\//) {
1472 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1474 # Try to set MR_PATH to the path to the program.
1476 use FindBin qw($Bin $Script);
1477 $ENV{MR_PATH}=$Bin."/".$Script;
1485 elsif (! @ok && @skipped) {
1498 loadconfig($ENV{MR_CONFIG});
1499 #use Data::Dumper; print Dumper(\%config);
1501 my $action=expandaction(shift @ARGV);
1508 # Finally, some useful actions that mr knows about by default.
1509 # These can be overridden in ~/.mrconfig.
1524 echo "mr (warning): $@" >&2
1530 if [ -z "$1" ] || [ -z "$2" ]; then
1531 error "mr: usage: hours_since action num"
1533 for dir in .git .svn .bzr CVS .hg _darcs; do
1534 if [ -e "$MR_REPO/$dir" ]; then
1535 flagfile="$MR_REPO/$dir/.mr_last$1"
1539 if [ -z "$flagfile" ]; then
1540 error "cannot determine flag filename"
1542 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1543 if [ "$delta" -lt "$2" ]; then
1551 svn_test = test -d "$MR_REPO"/.svn
1552 git_test = test -d "$MR_REPO"/.git
1553 bzr_test = test -d "$MR_REPO"/.bzr
1554 cvs_test = test -d "$MR_REPO"/CVS
1555 hg_test = test -d "$MR_REPO"/.hg
1556 darcs_test = test -d "$MR_REPO"/_darcs
1558 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1559 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1560 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1562 svn_update = svn update "$@"
1563 git_update = git pull "$@"
1564 bzr_update = bzr merge --pull "$@"
1565 cvs_update = cvs update "$@"
1566 hg_update = hg pull "$@" && hg update "$@"
1567 darcs_update = darcs pull -a "$@"
1569 svn_status = svn status "$@"
1570 git_status = git status "$@" || true
1571 bzr_status = bzr status "$@"
1572 cvs_status = cvs status "$@"
1573 hg_status = hg status "$@"
1574 darcs_status = darcs whatsnew -ls "$@" || true
1576 svn_commit = svn commit "$@"
1577 git_commit = git commit -a "$@" && git push --all
1578 bzr_commit = bzr commit "$@" && bzr push
1579 cvs_commit = cvs commit "$@"
1580 hg_commit = hg commit -m "$@" && hg push
1581 darcs_commit = darcs record -a -m "$@" && darcs push -a
1583 git_record = git commit -a "$@"
1584 bzr_record = bzr commit "$@"
1585 hg_record = hg commit -m "$@"
1586 darcs_record = darcs record -a -m "$@"
1589 git_push = git push "$@"
1590 bzr_push = bzr push "$@"
1592 hg_push = hg push "$@"
1593 darcs_push = darcs push -a "$@"
1595 svn_diff = svn diff "$@"
1596 git_diff = git diff "$@"
1597 bzr_diff = bzr diff "$@"
1598 cvs_diff = cvs diff "$@"
1599 hg_diff = hg diff "$@"
1600 darcs_diff = darcs diff -u "$@"
1602 svn_log = svn log "$@"
1603 git_log = git log "$@"
1604 bzr_log = bzr log "$@"
1605 cvs_log = cvs log "$@"
1606 hg_log = hg log "$@"
1607 darcs_log = darcs changes "$@"
1608 git_bare_log = git log "$@"
1611 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1612 if [ -z "$url" ]; then
1613 error "cannot determine svn url"
1615 echo "Registering svn url: $url in $MR_CONFIG"
1616 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1618 url="`LC_ALL=C git config --get remote.origin.url`" || true
1619 if [ -z "$url" ]; then
1620 error "cannot determine git url"
1622 echo "Registering git url: $url in $MR_CONFIG"
1623 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1625 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1626 if [ -z "$url" ]; then
1627 error "cannot determine bzr url"
1629 echo "Registering bzr url: $url in $MR_CONFIG"
1630 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1632 repo=`cat CVS/Repository`
1634 if [ -z "$root" ]; then
1635 error "cannot determine cvs root"
1637 echo "Registering cvs repository $repo at root $root"
1638 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1640 url=`hg showconfig paths.default`
1641 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1642 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1644 url=`cat _darcs/prefs/defaultrepo`
1645 echo "Registering darcs repository $url in $MR_CONFIG"
1646 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1648 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1649 if [ -z "$url" ]; then
1650 error "cannot determine git url"
1652 echo "Registering git url: $url in $MR_CONFIG"
1653 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1655 svn_trusted_checkout = svn co $url $repo
1656 svn_alt_trusted_checkout = svn checkout $url $repo
1657 git_trusted_checkout = git clone $url $repo
1658 bzr_trusted_checkout = bzr clone $url $repo
1660 hg_trusted_checkout = hg clone $url $repo
1661 darcs_trusted_checkout = darcs get $url $repo
1662 git_bare_trusted_checkout = git clone --bare $url $repo
1668 SHOWMANFILE="man -f"
1674 SHOWMANFILE="man -l"
1677 if [ ! -e "$MR_PATH" ]; then
1678 error "cannot find program path"
1680 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1681 trap "rm -f $tmp" exit
1682 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1683 $SHOWMANFILE "$tmp" || error "man failed"
1689 if [ -s ~/.mrlog ]; then
1690 info "running offline commands"
1691 mv -f ~/.mrlog ~/.mrlog.old
1692 if ! sh -e ~/.mrlog.old; then
1693 error "offline command failed; left in ~/.mrlog.old"
1697 info "no offline commands to run"
1702 info "offline mode enabled"
1704 info "remembering command: 'mr $@'"
1705 command="mr -d '$(pwd)' $MR_SWITCHES"
1707 command="$command '$w'"
1709 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1710 echo "$command" >> ~/.mrlog
1713 ed = echo "A horse is a horse, of course, of course.."
1714 T = echo "I pity the fool."
1715 right = echo "Not found."
1717 # vim:sw=8:sts=0:ts=8:noet