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, darcs and fossil repositories, and support for other revision
41 control systems can easily be added.
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.
120 List the repositories that mr will act on.
124 Register an existing repository in a mrconfig file. By default, the
125 repository in the current directory is registered, or you can specify a
126 directory to register.
128 The mrconfig file that is modified is chosen by either the -c option, or by
129 looking for the closest known one at or below the current directory.
133 Adds, modifies, removes, or prints a value from a mrconfig file. The next
134 parameter is the name of the section the value is in. To add or modify
135 values, use one or more instances of "parameter=value". Use "parameter=" to
136 remove a parameter. Use just "parameter" to get the value of a parameter.
138 For example, to add (or edit) a repository in src/foo:
140 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
142 To show the command that mr uses to update the repository in src/foo:
144 mr config src/foo update
146 To see the built-in library of shell functions contained in mr:
148 mr config DEFAULT lib
150 The ~/.mrconfig file is used by default. To use a different config file,
155 Advises mr that it is in offline mode. Any commands that fail in
156 offline mode will be remembered, and retried when mr is told it's online.
160 Advices mr that it is in online mode again. Commands that failed while in
161 offline mode will be re-run.
165 Remember a command, to be run later when mr re-enters online mode. This
166 implicitly puts mr into offline mode. The command can be any regular mr
167 command. This is useful when you know that a command will fail due to being
168 offline, and so don't want to run it right now at all, but just remember
169 to run it when you go back online.
177 Actions can be abbreviated to any unambiguous substring, so
178 "mr st" is equivalent to "mr status", and "mr up" is equivalent to "mr
181 Additional parameters can be passed to most commands, and are passed on
182 unchanged to the underlying revision control system. This is mostly useful
183 if the repositories mr will act on all use the same revision control
192 =item --directory directory
194 Specifies the topmost directory that B<mr> should work in. The default is
195 the current working directory.
199 =item --config mrconfig
201 Use the specified mrconfig file. The default is B<~/.mrconfig>
207 Search in the current directory, and its parent directories and use
208 the first B<.mrconfig> found, instead of the default B<~/.mrconfig>.
226 Accept untrusted SSL certificates when bootstrapping.
232 Expand the statistics line displayed at the end to include information
233 about exactly which repositories failed and were skipped, if any.
239 Interactive mode. If a repository fails to be processed, a subshell will be
240 started which you can use to resolve or investigate the problem. Exit the
241 subshell to continue the mr run.
245 =item --no-recurse [number]
247 If no number if specified, just operate on the repository for the current
248 directory, do not recurse into deeper repositories.
250 If a number is specified, will recurse into repositories at most that many
251 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
252 but not ./src/packages/bar.
256 =item --jobs [number]
258 Run the specified number of jobs in parallel, or an unlimited number of jobs
259 with no number specified. This can greatly speed up operations such as updates.
260 It is not recommended for interactive operations.
262 Note that running more than 10 jobs at a time is likely to run afoul of
263 ssh connection limits. Running between 3 and 5 jobs at a time will yield
264 a good speedup in updates without loading the machine too much.
270 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
275 =head1 MRCONFIG FILES
277 Here is an example .mrconfig file:
280 checkout = svn co svn://svn.example.com/src/trunk src
284 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
286 git checkout -b mybranch origin/master
288 The .mrconfig file uses a variant of the INI file format. Lines starting with
289 "#" are comments. Values can be continued to the following line by
290 indenting the line with whitespace.
292 The "DEFAULT" section allows setting default values for the sections that
295 The "ALIAS" section allows adding aliases for actions. Each parameter
296 is an alias, and its value is the action to use.
298 All other sections add repositories. The section header specifies the
299 directory where the repository is located. This is relative to the directory
300 that contains the mrconfig file, but you can also choose to use absolute
301 paths. (Note that you can use environment variables in section names; they
302 will be passed through the shell for expansion. For example,
303 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
305 Within a section, each parameter defines a shell command to run to handle a
306 given action. mr contains default handlers for "update", "status",
307 "commit", and other standard actions. Normally you only need to specify what
308 to do for "checkout".
310 Note that these shell commands are run in a "set -e" shell
311 environment, where any additional parameters you pass are available in
312 "$@". The "checkout" command is run in the parent of the repository
313 directory, since the repository isn't checked out yet. All other commands
314 are run inside the repository, though not necessarily at the top of it.
316 The "MR_REPO" environment variable is set to the path to the top of the
317 repository. (For the "register" action, "MR_REPO" is instead set to the
318 basename of the directory that should be created when checking the
321 The "MR_CONFIG" environment variable is set to the .mrconfig file
322 that defines the repo being acted on, or, if the repo is not yet in a config
323 file, the .mrconfig file that should be modified to register the repo.
325 A few parameters have special meanings:
331 If the "skip" parameter is set and its command returns true, then B<mr>
332 will skip acting on that repository. The command is passed the action
335 Here are two examples. The first skips the repo unless
336 mr is run by joey. The second uses the hours_since function
337 (included in mr's built-in library) to skip updating the repo unless it's
338 been at least 12 hours since the last update.
340 skip = test `whoami` != joey
341 skip = [ "$1" = update ] && ! hours_since "$1" 12
345 The "order" parameter can be used to override the default ordering of
346 repositories. The default order value is 10. Use smaller values to make
347 repositories be processed earlier, and larger values to make repositories
350 Note that if a repository is located in a subdirectory of another
351 repository, ordering it to be processed earlier is not recommended.
355 If the "chain" parameter is set and its command returns true, then B<mr>
356 will try to load a .mrconfig file from the root of the repository.
360 If the "include" parameter is set, its command is ran, and should output
361 additional mrconfig file content. The content is included as if it were
362 part of the including file.
364 Unlike all other parameters, this parameter does not need to be placed
369 The "lib" parameter can specify some shell code that will be run before each
370 command, this can be a useful way to define shell functions for other commands
375 If the "fixups" parameter is set, its command is run whenever a repository
376 is checked out, or updated. This provides an easy way to do things
377 like permissions fixups, or other tweaks to the repository content,
378 whenever the repository is changed.
382 When looking for a command to run for a given action, mr first looks for
383 a parameter with the same name as the action. If that is not found, it
384 looks for a parameter named "rcs_action" (substituting in the name of the
385 revision control system and the action). The name of the revision control
386 system is itself determined by running each defined "rcs_test" action,
389 Internally, mr has settings for "git_update", "svn_update", etc. To change
390 the action that is performed for a given revision control system, you can
391 override these rcs specific actions. To add a new revision control system,
392 you can just add rcs specific actions for it.
394 The ~/.mrlog file contains commands that mr has remembered to run later,
395 due to being offline. You can delete or edit this file to remove commands,
396 or even to add other commands for 'mr online' to run. If the file is
397 present, mr assumes it is in offline mode.
399 =head1 UNTRUSTED MRCONFIG FILES
401 Since mrconfig files can contain arbitrary shell commands, they can do
402 anything. This flexibility is good, but it also allows a malicious mrconfig
403 file to delete your whole home directory. Such a file might be contained
404 inside a repository that your main ~/.mrconfig checks out and chains to. To
405 avoid worries about evil commands in a mrconfig file, mr
406 has the ability to read mrconfig files in untrusted mode. Such files are
407 limited to running only known safe commands (like "git clone") in a
408 carefully checked manner.
410 By default, mr trusts all mrconfig files. (This default will change in a
411 future release!) But if you have a ~/.mrtrust file, mr will only trust
412 mrconfig files that are listed within it. (One file per line.) All other
413 files will be treated as untrusted.
417 mr can be extended to support things such as unison and git-svn. Some
418 files providing such extensions are available in /usr/share/mr/. See
419 the documentation in the files for details about using them.
423 Copyright 2007-2010 Joey Hess <joey@kitenet.net>
425 Licensed under the GNU GPL version 2 or higher.
427 http://kitenet.net/~joey/code/mr/
434 use Cwd qw(getcwd abs_path);
436 # things that can happen when mr runs a command
445 my $config_overridden=0;
455 my $directory=getcwd();
456 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
463 my (@ok, @failed, @skipped);
469 my ($action, $dir, $topdir, $subdir) = @_;
471 if (exists $rcs{$dir}) {
476 foreach my $rcs_test (
478 length $a <=> length $b
481 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
482 my ($rcs)=$rcs_test=~/(.*)_test/;
483 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
484 $test.="if my_$rcs_test; then echo $rcs; fi\n";
486 $test=$config{$topdir}{$subdir}{lib}."\n".$test
487 if exists $config{$topdir}{$subdir}{lib};
489 print "mr $action: running rcs test >>$test<<\n" if $verbose;
494 print STDERR "mr $action: found multiple possible repository types ($rcs) for $topdir$subdir\n";
498 return $rcs{$dir}=undef;
501 return $rcs{$dir}=$rcs;
506 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
508 if (exists $config{$topdir}{$subdir}{$action}) {
509 return $config{$topdir}{$subdir}{$action};
516 my $rcs=rcs_test(@_);
519 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
520 return $config{$topdir}{$subdir}{$rcs."_".$action};
528 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
530 $ENV{MR_CONFIG}=$configfiles{$topdir};
531 my $lib=exists $config{$topdir}{$subdir}{lib} ?
532 $config{$topdir}{$subdir}{lib}."\n" : "";
533 my $is_checkout=($action eq 'checkout');
534 my $is_update=($action =~ /update/);
539 if (! $force_checkout) {
541 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
545 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
550 return action("checkout", $dir, $topdir, $subdir);
554 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
555 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
557 if (defined $skiptest) {
558 my $test="set -e;".$lib.
559 "my_action(){ $skiptest\n }; my_action '$action'";
560 print "mr $action: running skip test >>$test<<\n" if $verbose;
561 my $ret=system($test);
563 if (($? & 127) == 2) {
564 print STDERR "mr $action: interrupted\n";
568 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
572 if ($ret >> 8 == 0) {
573 print "mr $action: $dir skipped per config file\n" if $verbose;
578 if ($is_checkout && ! -d $dir) {
579 print "mr $action: creating parent directory $dir\n" if $verbose;
580 system("mkdir", "-p", $dir);
583 if (! $no_chdir && ! chdir($dir)) {
584 print STDERR "mr $action: failed to chdir to $dir: $!\n";
587 elsif (! defined $command) {
588 my $rcs=rcs_test(@_);
589 if (! defined $rcs) {
590 print STDERR "mr $action: unknown repository type and no defined $action command for $topdir$subdir\n";
594 print STDERR "mr $action: no defined action for $rcs repository $topdir$subdir, skipping\n";
600 print "mr $action: $topdir$subdir\n" unless $quiet;
604 $s=~s/^\Q$topdir$subdir\E\/?//;
605 print "mr $action: $topdir$subdir (in subdir $s)\n" unless $quiet;
607 $command="set -e; ".$lib.
608 "my_action(){ $command\n }; my_action ".
609 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
610 print "mr $action: running >>$command<<\n" if $verbose;
611 my $ret=system($command);
613 if (($? & 127) == 2) {
614 print STDERR "mr $action: interrupted\n";
618 print STDERR "mr $action: received signal ".($? & 127)."\n";
621 print STDERR "mr $action: failed ($ret)\n" if $verbose;
622 if ($ret >> 8 != 0) {
623 print STDERR "mr $action: command failed\n";
624 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
625 # recreate original command line to
626 # remember, and avoid recursing
628 @ARGV=('-n', $action, @orig);
629 action("remember", $dir, $topdir, $subdir);
634 print STDERR "mr $action: command died ($ret)\n";
639 if ($is_checkout && ! -d $dir) {
640 print STDERR "mr $action: $dir missing after checkout\n";;
644 if (($is_checkout || $is_update)) {
645 my $ret=hook("fixups", $topdir, $subdir);
646 return $ret if $ret != OK;
655 my ($hook, $topdir, $subdir) = @_;
657 my $command=$config{$topdir}{$subdir}{$hook};
658 return OK unless defined $command;
659 my $lib=exists $config{$topdir}{$subdir}{lib} ?
660 $config{$topdir}{$subdir}{lib}."\n" : "";
661 my $shell="set -e;".$lib.
662 "my_hook(){ $command\n }; my_hook";
663 print "mr $hook: running >>$shell<<\n" if $verbose;
664 my $ret=system($shell);
666 if (($? & 127) == 2) {
667 print STDERR "mr $hook: interrupted\n";
671 print STDERR "mr $hook: received signal ".($? & 127)."\n";
679 # run actions on multiple repos, in parallel
689 while (@fhs or @repos) {
690 while ((!$jobs || $running < $jobs) && @repos) {
692 my $repo = shift @repos;
693 pipe(my $outfh, CHILD_STDOUT);
694 pipe(my $errfh, CHILD_STDERR);
696 unless ($pid = fork) {
697 die "mr $action: cannot fork: $!" unless defined $pid;
698 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
699 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
704 exit action($action, @$repo);
708 push @active, [$pid, $repo];
709 push @fhs, [$outfh, $errfh];
712 my ($rin, $rout) = ('','');
714 foreach my $fh (@fhs) {
715 next unless defined $fh;
716 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
717 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
719 $nfound = select($rout=$rin, undef, undef, 1);
720 foreach my $channel (0, 1) {
721 foreach my $i (0..$#fhs) {
722 next unless defined $fhs[$i];
723 my $fh = $fhs[$i][$channel];
724 next unless defined $fh;
725 if (vec($rout, fileno($fh), 1) == 1) {
727 if (sysread($fh, $r, 1024) == 0) {
729 $fhs[$i][$channel] = undef;
730 if (! defined $fhs[$i][0] &&
731 ! defined $fhs[$i][1]) {
732 waitpid($active[$i][0], 0);
733 print STDOUT $out[$i][0];
734 print STDERR $out[$i][1];
735 record($active[$i][1], $? >> 8);
737 splice(@active, $i, 1);
742 $out[$i][$channel] .= $r;
750 my $dir=shift()->[0];
755 print "\n" unless $quiet;
757 elsif ($ret == FAILED) {
759 chdir($dir) unless $no_chdir;
760 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
761 system((getpwuid($<))[8], "-i");
764 print "\n" unless $quiet;
766 elsif ($ret == SKIPPED) {
769 elsif ($ret == ABORT) {
773 die "unknown exit status $ret";
779 if (! @ok && ! @failed && ! @skipped) {
780 die "mr $action: no repositories found to work on\n";
782 print "mr $action: finished (".join("; ",
783 showstat($#ok+1, "ok", "ok"),
784 showstat($#failed+1, "failed", "failed"),
785 showstat($#skipped+1, "skipped", "skipped"),
786 ).")\n" unless $quiet;
789 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
792 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
802 return "$count ".($count > 1 ? $plural : $singular);
807 # an ordered list of repos
810 foreach my $topdir (sort keys %config) {
811 foreach my $subdir (sort keys %{$config{$topdir}}) {
815 order => $config{$topdir}{$subdir}{order},
820 $a->{order} <=> $b->{order}
822 $a->{topdir} cmp $b->{topdir}
824 $a->{subdir} cmp $b->{subdir}
830 my $topdir=$repo->{topdir};
831 my $subdir=$repo->{subdir};
832 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
837 # figure out which repos to act on
840 foreach my $repo (repolist()) {
841 my $topdir=$repo->{topdir};
842 my $subdir=$repo->{subdir};
844 next if $subdir eq 'DEFAULT';
845 my $dir=repodir($repo);
847 $dir.="/" unless $dir=~/\/$/;
848 $d.="/" unless $d=~/\/$/;
849 next if $dir ne $d && $dir !~ /^\Q$d\E/;
850 if (defined $max_depth) {
851 my @a=split('/', $dir);
852 my @b=split('/', $d);
853 do { } while (@a && @b && shift(@a) eq shift(@b));
854 next if @a > $max_depth || @b > $max_depth;
856 push @repos, [$dir, $topdir, $subdir];
859 # fallback to find a leaf repo
860 foreach my $repo (reverse repolist()) {
861 my $topdir=$repo->{topdir};
862 my $subdir=$repo->{subdir};
864 next if $subdir eq 'DEFAULT';
865 my $dir=repodir($repo);
867 $dir.="/" unless $dir=~/\/$/;
868 $d.="/" unless $d=~/\/$/;
869 if ($d=~/^\Q$dir\E/) {
870 push @repos, [$dir, $topdir, $subdir];
892 sub is_trusted_config {
893 my $config=shift; # must be abs_pathed already
895 # We always trust ~/.mrconfig.
896 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
898 return 1 if $trust_all;
900 my $trustfile=$ENV{HOME}."/.mrtrust";
902 if (! -e $trustfile) {
903 print "mr: Assuming $config is trusted.\n";
904 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
905 print "mr: and list all trusted mrconfig files in it.\n";
910 $trusted{"$ENV{HOME}/.mrconfig"}=1;
911 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
914 s/^~\//$ENV{HOME}\//;
915 $trusted{abs_path($_)}=1;
920 return $trusted{$config};
924 sub is_trusted_repo {
927 # Tightly limit what is allowed in a repo name.
928 # No ../, no absolute paths, and no unusual filenames
929 # that might try to escape to the shell.
930 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
931 $repo !~ /\.\./ && $repo !~ /^\//;
934 sub is_trusted_checkout {
937 # To determine if the command is safe, compare it with the
938 # *_trusted_checkout config settings. Those settings are
939 # templates for allowed commands, so make sure that each word
940 # of the command matches the corresponding word of the template.
943 foreach my $word (split(' ', $command)) {
945 if ($word=~/^'(.*)'$/) {
948 elsif ($word=~/^"(.*)"$/) {
955 foreach my $key (grep { /_trusted_checkout$/ }
956 keys %{$config{''}{DEFAULT}}) {
957 my @twords=split(' ', $config{''}{DEFAULT}{$key});
958 next if @words > @twords;
962 for (my $c=0; $c < @twords && $match; $c++) {
963 if ($twords[$c] eq '$url') {
964 # Match all the typical characters found in
965 # urls, plus @ which svn can use. Note
966 # that the "url" might also be a local
969 defined $words[$c] &&
970 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
974 elsif ($twords[$c] eq '$repo') {
975 # If a repo is not specified, assume it
976 # will be the last path component of the
977 # url, or something derived from it, and
979 if (! defined $words[$c] && defined $url) {
980 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
984 defined $words[$c] &&
985 is_trusted_repo($words[$c])
988 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1010 if (ref $f eq 'GLOB') {
1020 my $absf=abs_path($f);
1021 if ($loaded{$absf}) {
1026 $trusted=is_trusted_config($absf);
1028 if (! defined $dir) {
1029 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1030 if (! defined $dir) {
1035 $dir=abs_path($dir)."/";
1037 if (! exists $configfiles{$dir}) {
1038 $configfiles{$dir}=$f;
1041 # copy in defaults from first parent
1043 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1044 if ($parent eq '/') {
1047 if (exists $config{$parent} &&
1048 exists $config{$parent}{DEFAULT}) {
1049 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1054 print "mr: loading config $f\n" if $verbose;
1055 open($in, "<", $f) || die "mr: open $f: $!\n";
1058 close $in unless ref $f eq 'GLOB';
1066 next if /^\s*\#/ || /^\s*$/;
1067 if (/^\[([^\]]*)\]\s*$/) {
1071 if (! is_trusted_repo($section) ||
1072 $section eq 'ALIAS' ||
1073 $section eq 'DEFAULT') {
1074 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1077 $section=expandenv($section) if $trusted;
1078 if ($section ne 'ALIAS' &&
1079 ! exists $config{$dir}{$section} &&
1080 exists $config{$dir}{DEFAULT}) {
1082 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1085 elsif (/^(\w+)\s*=\s*(.*)/) {
1090 while (@lines && $lines[0]=~/^\s(.+)/) {
1098 # Untrusted files can only contain checkout
1100 if ($parameter ne 'checkout') {
1101 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1103 if (! is_trusted_checkout($value)) {
1104 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1108 if ($parameter eq "include") {
1109 print "mr: including output of \"$value\"\n" if $verbose;
1110 unshift @lines, `$value`;
1112 print STDERR "mr: include command exited nonzero ($?)\n";
1117 if (! defined $section) {
1118 die "$f line $.: parameter ($parameter) not in section\n";
1120 if ($section eq 'ALIAS') {
1121 $alias{$parameter}=$value;
1123 elsif ($parameter eq 'lib') {
1124 $config{$dir}{$section}{lib}.=$value."\n";
1127 $config{$dir}{$section}{$parameter}=$value;
1128 if ($parameter =~ /.*_(.*)/) {
1129 $knownactions{$1}=1;
1132 $knownactions{$parameter}=1;
1134 if ($parameter eq 'chain' &&
1135 length $dir && $section ne "DEFAULT" &&
1136 -e $dir.$section."/.mrconfig") {
1137 my $ret=system($value);
1139 if (($? & 127) == 2) {
1140 print STDERR "mr: chain test interrupted\n";
1144 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1148 push @toload, $dir.$section."/.mrconfig";
1154 die "$f line $line: parse error\n";
1163 sub startingconfig {
1164 %alias=%config=%configfiles=%knownactions=%loaded=();
1165 my $datapos=tell(DATA);
1167 seek(DATA,$datapos,0); # rewind
1172 # the section to modify or add
1173 my $targetsection=shift;
1174 # fields to change in the section
1175 # To remove a field, set its value to "".
1176 my %changefields=@_;
1182 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1187 my $formatfield=sub {
1189 my @value=split(/\n/, shift);
1191 return "$field = ".shift(@value)."\n".
1192 join("", map { "\t$_\n" } @value);
1196 while ($out[$#out] =~ /^\s*$/) {
1197 unshift @blanks, pop @out;
1199 foreach my $field (sort keys %changefields) {
1200 if (length $changefields{$field}) {
1201 push @out, "$field = $changefields{$field}\n";
1202 delete $changefields{$field};
1212 if (/^\s*\#/ || /^\s*$/) {
1215 elsif (/^\[([^\]]*)\]\s*$/) {
1216 if (defined $section &&
1217 $section eq $targetsection) {
1221 $section=expandenv($1);
1225 elsif (/^(\w+)\s*=\s(.*)/) {
1230 while (@lines && $lines[0]=~/^\s(.+)/) {
1236 if ($section eq $targetsection) {
1237 if (exists $changefields{$parameter}) {
1238 if (length $changefields{$parameter}) {
1239 $value=$changefields{$parameter};
1241 delete $changefields{$parameter};
1245 push @out, $formatfield->($parameter, $value);
1249 if (defined $section &&
1250 $section eq $targetsection) {
1253 elsif (%changefields) {
1254 push @out, "\n[$targetsection]\n";
1255 foreach my $field (sort keys %changefields) {
1256 if (length $changefields{$field}) {
1257 push @out, $formatfield->($field, $changefields{$field});
1262 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1270 # actions that do not operate on all repos
1271 if ($action eq 'help') {
1274 elsif ($action eq 'config') {
1277 elsif ($action eq 'register') {
1280 elsif ($action eq 'bootstrap') {
1283 elsif ($action eq 'remember' ||
1284 $action eq 'offline' ||
1285 $action eq 'online') {
1286 my @repos=selectrepos;
1287 action($action, @{$repos[0]}) if @repos;
1291 if (!$jobs || $jobs > 1) {
1292 mrs($action, selectrepos());
1295 foreach my $repo (selectrepos()) {
1296 record($repo, action($action, @$repo));
1302 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1307 die "mr config: not enough parameters\n";
1310 if ($section=~/^\//) {
1311 # try to convert to a path relative to the config file
1312 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1313 $dir=abs_path($dir);
1314 $dir.="/" unless $dir=~/\/$/;
1315 if ($section=~/^\Q$dir\E(.*)/) {
1321 if (/^([^=]+)=(.*)$/) {
1322 $changefields{$1}=$2;
1326 foreach my $topdir (sort keys %config) {
1327 if (exists $config{$topdir}{$section} &&
1328 exists $config{$topdir}{$section}{$_}) {
1329 print $config{$topdir}{$section}{$_}."\n";
1331 last if $section eq 'DEFAULT';
1335 die "mr config: $section $_ not set\n";
1339 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1344 if ($config_overridden) {
1345 # Find the directory that the specified config file is
1347 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1350 # Find the closest known mrconfig file to the current
1352 $directory.="/" unless $directory=~/\/$/;
1354 foreach my $topdir (reverse sort keys %config) {
1355 next unless length $topdir;
1356 if ($directory=~/^\Q$topdir\E/) {
1357 $ENV{MR_CONFIG}=$configfiles{$topdir};
1363 if (! $foundconfig) {
1364 $directory=""; # no config file, use builtin
1368 my $subdir=shift @ARGV;
1369 if (! chdir($subdir)) {
1370 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1374 $ENV{MR_REPO}=getcwd();
1375 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1376 if (! defined $command) {
1377 die "mr register: unknown repository type\n";
1380 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1381 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1382 "my_action(){ $command\n }; my_action ".
1383 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1384 print "mr register: running >>$command<<\n" if $verbose;
1385 exec($command) || die "exec: $!";
1389 my $url=shift @ARGV;
1390 my $dir=shift @ARGV || ".";
1392 if (! defined $url || ! length $url) {
1393 die "mr: bootstrap requires url\n";
1396 # Download the config file to a temporary location.
1397 eval q{use File::Temp};
1399 my $tmpconfig=File::Temp->new();
1400 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1401 push(@curlargs, "-k") if $insecure;
1402 my $curlstatus = system(@curlargs);
1403 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1404 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1407 system("mkdir", "-p", $dir);
1409 chdir($dir) || die "chdir $dir: $!";
1411 # Special case to handle checkout of the "." repo, which
1412 # would normally be skipped.
1413 my $topdir=abs_path(".")."/";
1414 my @repo=($topdir, $topdir, ".");
1415 loadconfig($tmpconfig, $topdir);
1416 record(\@repo, action("checkout", @repo, 1))
1417 if exists $config{$topdir}{"."}{"checkout"};
1419 if (-e ".mrconfig") {
1420 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1423 eval q{use File::Copy};
1425 move($tmpconfig, ".mrconfig") || die "rename: $!";
1428 # Reload the config file (in case we got a different version)
1429 # and checkout everything else.
1431 loadconfig(".mrconfig");
1432 dispatch("checkout");
1433 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1434 showstats("bootstrap");
1438 # alias expansion and command stemming
1441 if (exists $alias{$action}) {
1442 $action=$alias{$action};
1444 if (! exists $knownactions{$action}) {
1445 my @matches = grep { /^\Q$action\E/ }
1446 keys %knownactions, keys %alias;
1447 if (@matches == 1) {
1448 $action=$matches[0];
1450 elsif (@matches == 0) {
1451 die "mr: unknown action \"$action\" (known actions: ".
1452 join(", ", sort keys %knownactions).")\n";
1455 die "mr: ambiguous action \"$action\" (matches: ".
1456 join(", ", @matches).")\n";
1462 sub find_nearest_mrconfig {
1464 while (length $dir) {
1465 if (-e "$dir/.mrconfig") {
1466 return "$dir/.mrconfig";
1468 $dir=~s/\/[^\/]*$//;
1470 die "no .mrconfig found in path\n";
1475 Getopt::Long::Configure("bundling", "no_permute");
1476 my $result=GetOptions(
1477 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1478 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1479 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1480 "v|verbose" => \$verbose,
1481 "q|quiet" => \$quiet,
1482 "s|stats" => \$stats,
1483 "k|insecure" => \$insecure,
1484 "i|interactive" => \$interactive,
1485 "n|no-recurse:i" => \$max_depth,
1486 "j|jobs:i" => \$jobs,
1487 "t|trust-all" => \$trust_all,
1489 if (! $result || @ARGV < 1) {
1490 die("Usage: mr [options] action [params ...]\n".
1491 "(Use mr help for man page.)\n");
1494 $ENV{MR_SWITCHES}="";
1495 foreach my $option (@saved) {
1496 last if $option eq $ARGV[0];
1497 $ENV{MR_SWITCHES}.="$option ";
1503 print STDERR "mr: interrupted\n";
1507 # This can happen if it's run in a directory that was removed
1508 # or other strangeness.
1509 if (! defined $directory) {
1510 die("mr: failed to determine working directory\n");
1512 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1513 # the config file might be a symlink to elsewhere, and the directory it's
1514 # in is significant.
1515 if ($ENV{MR_CONFIG} !~ /^\//) {
1516 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1518 # Try to set MR_PATH to the path to the program.
1520 use FindBin qw($Bin $Script);
1521 $ENV{MR_PATH}=$Bin."/".$Script;
1529 elsif (! @ok && @skipped) {
1542 loadconfig($ENV{MR_CONFIG});
1543 #use Data::Dumper; print Dumper(\%config);
1545 my $action=expandaction(shift @ARGV);
1552 # Finally, some useful actions that mr knows about by default.
1553 # These can be overridden in ~/.mrconfig.
1568 echo "mr (warning): $@" >&2
1574 if [ -z "$1" ] || [ -z "$2" ]; then
1575 error "mr: usage: hours_since action num"
1577 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1578 if [ -e "$MR_REPO/$dir" ]; then
1579 flagfile="$MR_REPO/$dir/.mr_last$1"
1583 if [ -z "$flagfile" ]; then
1584 error "cannot determine flag filename"
1586 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1587 if [ "$delta" -lt "$2" ]; then
1595 svn_test = test -d "$MR_REPO"/.svn
1596 git_test = test -d "$MR_REPO"/.git
1597 bzr_test = test -d "$MR_REPO"/.bzr
1598 cvs_test = test -d "$MR_REPO"/CVS
1599 hg_test = test -d "$MR_REPO"/.hg
1600 darcs_test = test -d "$MR_REPO"/_darcs
1601 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1603 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1604 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1605 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1607 svn_update = svn update "$@"
1608 git_update = git pull "$@"
1609 bzr_update = bzr merge --pull "$@"
1610 cvs_update = cvs update "$@"
1611 hg_update = hg pull "$@" && hg update "$@"
1612 darcs_update = darcs pull -a "$@"
1613 fossil_update = fossil pull "$@"
1615 svn_status = svn status "$@"
1616 git_status = git status -s "$@" || true
1617 bzr_status = bzr status --short "$@"
1618 cvs_status = cvs status "$@"
1619 hg_status = hg status "$@"
1620 darcs_status = darcs whatsnew -ls "$@" || true
1621 fossil_status = fossil changes "$@"
1623 svn_commit = svn commit "$@"
1624 git_commit = git commit -a "$@" && git push --all
1625 bzr_commit = bzr commit "$@" && bzr push
1626 cvs_commit = cvs commit "$@"
1627 hg_commit = hg commit -m "$@" && hg push
1628 darcs_commit = darcs record -a -m "$@" && darcs push -a
1629 fossil_commit = fossil commit "$@"
1631 git_record = git commit -a "$@"
1632 bzr_record = bzr commit "$@"
1633 hg_record = hg commit -m "$@"
1634 darcs_record = darcs record -a -m "$@"
1635 fossil_record = fossil commit "$@"
1638 git_push = git push "$@"
1639 bzr_push = bzr push "$@"
1641 hg_push = hg push "$@"
1642 darcs_push = darcs push -a "$@"
1643 fossil_push = fossil push "$@"
1645 svn_diff = svn diff "$@"
1646 git_diff = git diff "$@"
1647 bzr_diff = bzr diff "$@"
1648 cvs_diff = cvs diff "$@"
1649 hg_diff = hg diff "$@"
1650 darcs_diff = darcs diff -u "$@"
1651 fossil_diff = fossil diff "$@"
1653 svn_log = svn log "$@"
1654 git_log = git log "$@"
1655 bzr_log = bzr log "$@"
1656 cvs_log = cvs log "$@"
1657 hg_log = hg log "$@"
1658 darcs_log = darcs changes "$@"
1659 git_bare_log = git log "$@"
1660 fossil_log = fossil timeline "$@"
1663 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1664 if [ -z "$url" ]; then
1665 error "cannot determine svn url"
1667 echo "Registering svn url: $url in $MR_CONFIG"
1668 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1670 url="`LC_ALL=C git config --get remote.origin.url`" || true
1671 if [ -z "$url" ]; then
1672 error "cannot determine git url"
1674 echo "Registering git url: $url in $MR_CONFIG"
1675 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1677 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1678 if [ -z "$url" ]; then
1679 error "cannot determine bzr url"
1681 echo "Registering bzr url: $url in $MR_CONFIG"
1682 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1684 repo=`cat CVS/Repository`
1686 if [ -z "$root" ]; then
1687 error "cannot determine cvs root"
1689 echo "Registering cvs repository $repo at root $root"
1690 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1692 url=`hg showconfig paths.default`
1693 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1694 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1696 url=`cat _darcs/prefs/defaultrepo`
1697 echo "Registering darcs repository $url in $MR_CONFIG"
1698 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1700 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1701 if [ -z "$url" ]; then
1702 error "cannot determine git url"
1704 echo "Registering git url: $url in $MR_CONFIG"
1705 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1707 url=`fossil remote-url`
1708 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1709 echo "Registering fossil repository $url in $MR_CONFIG"
1710 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1712 svn_trusted_checkout = svn co $url $repo
1713 svn_alt_trusted_checkout = svn checkout $url $repo
1714 git_trusted_checkout = git clone $url $repo
1715 bzr_trusted_checkout = bzr clone $url $repo
1717 hg_trusted_checkout = hg clone $url $repo
1718 darcs_trusted_checkout = darcs get $url $repo
1719 git_bare_trusted_checkout = git clone --bare $url $repo
1720 # fossil: messy to do
1726 SHOWMANFILE="man -f"
1732 SHOWMANFILE="man -l"
1735 if [ ! -e "$MR_PATH" ]; then
1736 error "cannot find program path"
1738 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1739 trap "rm -f $tmp" exit
1740 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1741 $SHOWMANFILE "$tmp" || error "man failed"
1747 if [ -s ~/.mrlog ]; then
1748 info "running offline commands"
1749 mv -f ~/.mrlog ~/.mrlog.old
1750 if ! sh -e ~/.mrlog.old; then
1751 error "offline command failed; left in ~/.mrlog.old"
1755 info "no offline commands to run"
1760 info "offline mode enabled"
1762 info "remembering command: 'mr $@'"
1763 command="mr -d '$(pwd)' $MR_SWITCHES"
1765 command="$command '$w'"
1767 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1768 echo "$command" >> ~/.mrlog
1771 ed = echo "A horse is a horse, of course, of course.."
1772 T = echo "I pity the fool."
1773 right = echo "Not found."
1775 # vim:sw=8:sts=0:ts=8:noet