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"]
25 B<mr> [options] run command [param ...]
27 B<mr> [options] bootstrap url [directory]
29 B<mr> [options] register [repository]
31 B<mr> [options] config section ["parameter=[value]" ...]
33 B<mr> [options] action [params ...]
35 B<mr> [options] [online|offline]
37 B<mr> [options] remember action [params ...]
41 B<mr> is a Multiple Repository management tool. It can checkout, update, or
42 perform other actions on a set of repositories as if they were one combined
43 repository. It supports any combination of subversion, git, cvs, mercurial,
44 bzr, darcs and fossil repositories, and support for other version
45 control systems can easily be added.
47 B<mr> cds into and operates on all registered repositories at or below your
48 working directory. Or, if you are in a subdirectory of a repository that
49 contains no other registered repositories, it will stay in that directory,
50 and work on only that repository,
52 B<mr> is configured by .mrconfig files, which list the repositories. It
53 starts by reading the .mrconfig file in your home directory, and this can
54 in turn chain load .mrconfig files from repositories. It also automatically
55 looks for a .mrconfig file in the current directory, or in one of its
58 These predefined commands should be fairly familiar to users of any version
63 =item checkout (or co)
65 Checks out any repositories that are not already checked out.
69 Updates each repository from its configured remote repository.
71 If a repository isn't checked out yet, it will first check it out.
75 Displays a status report for each repository, showing what
76 uncommitted changes are present in the repository.
80 Commits changes to each repository. (By default, changes are pushed to the
81 remote repository too, when using distributed systems like git. If you
82 don't like this default, you can change it in your .mrconfig, or use record
85 The optional -m parameter allows specifying a commit message.
89 Records changes to the local repository, but does not push them to the
90 remote repository. Only supported for distributed version control systems.
92 The optional -m parameter allows specifying a commit message.
96 Pushes committed local changes to the remote repository. A no-op for
97 centralized version control systems.
101 Show a diff of uncommitted changes.
107 =item run command [param ...]
109 Runs the specified command in each repository.
113 These commands are also available:
117 =item bootstrap url [directory]
119 Causes mr to download the url, and use it as a .mrconfig file
120 to checkout the repositories listed in it, into the specified directory.
122 The directory will be created if it does not exist. If no directory is
123 specified, the current directory will be used.
125 If the .mrconfig file includes a repository named ".", that
126 is checked out into the top of the specified directory.
130 List the repositories that mr will act on.
134 Register an existing repository in a mrconfig file. By default, the
135 repository in the current directory is registered, or you can specify a
136 directory to register.
138 The mrconfig file that is modified is chosen by either the -c option, or by
139 looking for the closest known one at or in a parent of the current directory.
143 Adds, modifies, removes, or prints a value from a mrconfig file. The next
144 parameter is the name of the section the value is in. To add or modify
145 values, use one or more instances of "parameter=value". Use "parameter=" to
146 remove a parameter. Use just "parameter" to get the value of a parameter.
148 For example, to add (or edit) a repository in src/foo:
150 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
152 To show the command that mr uses to update the repository in src/foo:
154 mr config src/foo update
156 To see the built-in library of shell functions contained in mr:
158 mr config DEFAULT lib
160 The mrconfig file that is used is chosen by either the -c option, or by
161 looking for the closest known one at or in a parent of the current directory.
165 Advises mr that it is in offline mode. Any commands that fail in
166 offline mode will be remembered, and retried when mr is told it's online.
170 Advices mr that it is in online mode again. Commands that failed while in
171 offline mode will be re-run.
175 Remember a command, to be run later when mr re-enters online mode. This
176 implicitly puts mr into offline mode. The command can be any regular mr
177 command. This is useful when you know that a command will fail due to being
178 offline, and so don't want to run it right now at all, but just remember
179 to run it when you go back online.
187 Actions can be abbreviated to any unambiguous substring, so
188 "mr st" is equivalent to "mr status", and "mr up" is equivalent to "mr
191 Additional parameters can be passed to most commands, and are passed on
192 unchanged to the underlying version control system. This is mostly useful
193 if the repositories mr will act on all use the same version control
202 =item --directory directory
204 Specifies the topmost directory that B<mr> should work in. The default is
205 the current working directory.
209 =item --config mrconfig
211 Use the specified mrconfig file. The default is to use both F<~/.mrconfig>
212 as well as look for a F<.mrconfig> file in the current directory, or in one
213 of its parent directories.
225 Be quiet. This suppresses mr's usual output, as well as any output from
226 commands that are run (including stderr output). If a command fails,
227 the output will be shown.
233 Accept untrusted SSL certificates when bootstrapping.
239 Expand the statistics line displayed at the end to include information
240 about exactly which repositories failed and were skipped, if any.
246 Interactive mode. If a repository fails to be processed, a subshell will be
247 started which you can use to resolve or investigate the problem. Exit the
248 subshell to continue the mr run.
252 =item --no-recurse [number]
254 If no number if specified, just operate on the repository for the current
255 directory, do not recurse into deeper repositories.
257 If a number is specified, will recurse into repositories at most that many
258 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
259 but not ./src/packages/bar.
263 =item --jobs [number]
265 Run the specified number of jobs in parallel, or an unlimited number of jobs
266 with no number specified. This can greatly speed up operations such as updates.
267 It is not recommended for interactive operations.
269 Note that running more than 10 jobs at a time is likely to run afoul of
270 ssh connection limits. Running between 3 and 5 jobs at a time will yield
271 a good speedup in updates without loading the machine too much.
277 Trust all mrconfig files even if they are not listed in F<~/.mrtrust>.
284 This obsolete flag is ignored.
288 =head1 MRCONFIG FILES
290 Here is an example F<.mrconfig> file:
293 checkout = svn checkout svn://svn.example.com/src/trunk src
297 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
299 git checkout -b mybranch origin/master
301 The F<.mrconfig> file uses a variant of the INI file format. Lines
302 starting with "#" are comments. Values can be continued to the
303 following line by indenting the line with whitespace.
305 The C<DEFAULT> section allows setting default values for the sections that
308 The C<ALIAS> section allows adding aliases for actions. Each parameter
309 is an alias, and its value is the action to use.
311 All other sections add repositories. The section header specifies the
312 directory where the repository is located. This is relative to the directory
313 that contains the mrconfig file, but you can also choose to use absolute
314 paths. (Note that you can use environment variables in section names; they
315 will be passed through the shell for expansion. For example,
316 C<[$HOSTNAME]>, or C<[${HOSTNAME}foo]>).
318 Within a section, each parameter defines a shell command to run to handle a
319 given action. mr contains default handlers for "update", "status",
320 "commit", and other standard actions.
322 Normally you only need to specify what to do for "checkout". Here you
323 specify the command to run in order to create a checkout of the repository.
324 The command will be run in the parent directory, and must create the
325 repository's directory. So use C<git clone>, C<svn checkout>, C<bzr branch>
326 or C<bzr checkout> (for a bound branch), etc.
328 Note that these shell commands are run in a C<set -e> shell
329 environment, where any additional parameters you pass are available in
330 C<$@>. All commands other than "checkout" are run inside the repository,
331 though not necessarily at the top of it.
333 The C<MR_REPO> environment variable is set to the path to the top of the
334 repository. (For the "register" action, "MR_REPO" is instead set to the
335 basename of the directory that should be created when checking the
338 The C<MR_CONFIG> environment variable is set to the .mrconfig file
339 that defines the repo being acted on, or, if the repo is not yet in a config
340 file, the F<.mrconfig> file that should be modified to register the repo.
342 The C<MR_ACTION> environment variable is set to the command being run
343 (update, checkout, etc).
345 A few parameters have special meanings:
351 If the "skip" parameter is set and its command returns true, then B<mr>
352 will skip acting on that repository. The command is passed the action
355 Here are two examples. The first skips the repo unless
356 mr is run by joey. The second uses the hours_since function
357 (included in mr's built-in library) to skip updating the repo unless it's
358 been at least 12 hours since the last update.
362 skip = test `whoami` != joey
366 skip = [ "$1" = update ] && ! hours_since "$1" 12
368 Another way to use skip is for a lazy checkout. This makes mr skip
369 operating on a repo unless it already exists. To enable the
370 repo, you have to explicitly check it out (using "mr -d foo checkout").
378 The "order" parameter can be used to override the default ordering of
379 repositories. The default order value is 10. Use smaller values to make
380 repositories be processed earlier, and larger values to make repositories
383 Note that if a repository is located in a subdirectory of another
384 repository, ordering it to be processed earlier is not recommended.
388 If the "chain" parameter is set and its command returns true, then B<mr>
389 will try to load a F<.mrconfig> file from the root of the repository.
393 If the "include" parameter is set, its command is ran, and should output
394 additional mrconfig file content. The content is included as if it were
395 part of the including file.
397 Unlike all other parameters, this parameter does not need to be placed
400 B<mr> ships several libraries that can be included to add support for
401 additional version control type things (unison, git-svn, vcsh, git-fake-bare,
402 git-subtree). To include them all, you could use:
404 include = cat /usr/share/mr/*
406 See the individual files for details.
410 If the "deleted" parameter is set and its command returns true, then
411 B<mr> will treat the repository as deleted. It won't ever actually delete
412 the repository, but it will warn if it sees the repository's directory.
413 This is useful when one mrconfig file is shared among multiple machines,
414 to keep track of and remember to delete old repositories.
418 The "lib" parameter can specify some shell code that will be run
419 before each command, this can be a useful way to define shell
420 functions for other commands to use. Unlike most other parameters,
421 this can be specified multiple times, in which case the chunks of
422 shell code are accumulatively concatenated together. This is
423 particularly useful because it allows the user to build a library of
424 shell functions defined in the [DEFAULT] section scattered across
425 various mr modules (files referenced by an C<include> directive).
429 If the "fixups" parameter is set, its command is run whenever a repository
430 is checked out, or updated. This provides an easy way to do things
431 like permissions fixups, or other tweaks to the repository content,
432 whenever the repository is changed.
436 If a "pre_action" parameter is set, its command is run before mr performs the
437 specified action. Similarly, "post_action" parameters are run after mr
438 successfully performs the specified action. For example, "pre_commit" is
439 run before committing; "post_update" is run after updating.
443 When looking for a command to run for a given action, mr first looks for
444 a parameter with the same name as the action. If that is not found, it
445 looks for a parameter named "VCS_action" (substituting in the name of the
446 version control system and the action). The name of the version control
447 system is itself determined by running each defined "VCS_test" action,
450 Internally, mr has settings for "git_update", "svn_update", etc. To change
451 the action that is performed for a given version control system, you can
452 override these VCS specific actions. To add a new version control system,
453 you can just add VCS specific actions for it.
455 As with the C<lib> parameter, if a parameter is suffixed with
456 C<_append>, its value is concatenated to the existing value of the
457 parameter for that config block, rather than overwriting it. In this
458 way, actions such as C<fixup> can be constructed accumulatively.
460 =head1 UNTRUSTED MRCONFIG FILES
462 Since mrconfig files can contain arbitrary shell commands, they can do
463 anything. This flexibility is good, but it also allows a malicious mrconfig
464 file to delete your whole home directory. Such a file might be contained
465 inside a repository that your main F<~/.mrconfig> checks out. To
466 avoid worries about evil commands in a mrconfig file, mr defaults to
467 reading all mrconfig files other than the main F<~/.mrconfig> in untrusted
468 mode. In untrusted mode, mrconfig files are limited to running only known
469 safe commands (like "git clone") in a carefully checked manner.
471 To configure mr to trust other mrconfig files, list them in F<~/.mrtrust>.
472 One mrconfig file should be listed per line. Either the full pathname
473 should be listed, or the pathname can start with F<~/> to specify a file
474 relative to your home directory.
476 =head1 OFFLINE LOG FILE
478 The F<~/.mrlog> file contains commands that mr has remembered to run later,
479 due to being offline. You can delete or edit this file to remove commands,
480 or even to add other commands for 'mr online' to run. If the file is
481 present, mr assumes it is in offline mode.
485 mr can be extended to support things such as unison and git-svn. Some
486 files providing such extensions are available in F</usr/share/mr/>. See
487 the documentation in the files for details about using them.
491 mr returns nonzero if a command failed in any of the repositories.
495 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
497 Licensed under the GNU GPL version 2 or higher.
499 http://kitenet.net/~joey/code/mr/
506 use Cwd qw(getcwd abs_path);
508 # things that can happen when mr runs a command
517 my $config_overridden=0;
527 my $directory=getcwd();
529 $ENV{MR_CONFIG}=find_mrconfig();
536 my (@ok, @failed, @skipped);
542 my ($action, $dir, $topdir, $subdir) = @_;
544 if (exists $vcs{$dir}) {
549 foreach my $vcs_test (
551 length $a <=> length $b
554 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
555 my ($vcs)=$vcs_test=~/(.*)_test/;
556 $test="my_$vcs_test() {\n$config{$topdir}{$subdir}{$vcs_test}\n}\n".$test;
557 $test.="if my_$vcs_test; then echo $vcs; fi\n";
559 $test=$config{$topdir}{$subdir}{lib}."\n".$test
560 if exists $config{$topdir}{$subdir}{lib};
562 print "mr $action: running vcs test >>$test<<\n" if $verbose;
567 print STDERR "mr $action: found multiple possible repository types ($vcs) for ".fulldir($topdir, $subdir)."\n";
571 return $vcs{$dir}=undef;
574 return $vcs{$dir}=$vcs;
579 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
581 if (exists $config{$topdir}{$subdir}{$action}) {
582 return $config{$topdir}{$subdir}{$action};
589 my $vcs=vcs_test(@_);
592 exists $config{$topdir}{$subdir}{$vcs."_".$action}) {
593 return $config{$topdir}{$subdir}{$vcs."_".$action};
601 my ($topdir, $subdir) = @_;
602 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
606 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
607 my $fulldir=fulldir($topdir, $subdir);
609 $ENV{MR_CONFIG}=$configfiles{$topdir};
610 my $lib=exists $config{$topdir}{$subdir}{lib} ?
611 $config{$topdir}{$subdir}{lib}."\n" : "";
612 my $is_checkout=($action eq 'checkout');
613 my $is_update=($action =~ /update/);
615 ($ENV{MR_REPO}=$dir) =~ s!/$!!;
616 $ENV{MR_ACTION}=$action;
618 foreach my $testname ("skip", "deleted") {
619 my $testcommand=findcommand($testname, $dir, $topdir, $subdir, $is_checkout);
621 if (defined $testcommand) {
622 my $test="set -e;".$lib.
623 "my_action(){ $testcommand\n }; my_action '$action'";
624 print "mr $action: running $testname test >>$test<<\n" if $verbose;
625 my $ret=system($test);
627 if (($? & 127) == 2) {
628 print STDERR "mr $action: interrupted\n";
632 print STDERR "mr $action: $testname test received signal ".($? & 127)."\n";
636 if ($ret >> 8 == 0) {
637 if ($testname eq "deleted") {
639 print STDERR "mr error: $dir should be deleted yet still exists\n";
643 print "mr $action: skip $dir skipped\n" if $verbose;
650 if (! $force_checkout) {
652 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
656 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
661 return action("checkout", $dir, $topdir, $subdir);
665 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
667 if ($is_checkout && ! -d $dir) {
668 print "mr $action: creating parent directory $dir\n" if $verbose;
669 system("mkdir", "-p", $dir);
672 if (! $no_chdir && ! chdir($dir)) {
673 print STDERR "mr $action: failed to chdir to $dir: $!\n";
676 elsif (! defined $command) {
677 my $vcs=vcs_test(@_);
678 if (! defined $vcs) {
679 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
683 print STDERR "mr $action: no defined action for $vcs repository $fulldir, skipping\n";
690 $actionmsg="mr $action: $fulldir";
694 $s=~s/^\Q$fulldir\E\/?//;
695 $actionmsg="mr $action: $fulldir (in subdir $s)";
697 print "$actionmsg\n" unless $quiet;
699 my $hookret=hook("pre_$action", $topdir, $subdir);
700 return $hookret if $hookret != OK;
702 $command="set -e; ".$lib.
703 "my_action(){ $command\n }; my_action ".
704 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
705 print "mr $action: running >>$command<<\n" if $verbose;
708 my $output = qx/$command 2>&1/;
711 print "$actionmsg\n";
712 print STDERR $output;
716 $ret=system($command);
719 if (($? & 127) == 2) {
720 print STDERR "mr $action: interrupted\n";
724 print STDERR "mr $action: received signal ".($? & 127)."\n";
727 print STDERR "mr $action: failed ($ret)\n" if $verbose;
728 if ($ret >> 8 != 0) {
729 print STDERR "mr $action: command failed\n";
730 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
731 # recreate original command line to
732 # remember, and avoid recursing
734 @ARGV=('-n', $action, @orig);
735 action("remember", $dir, $topdir, $subdir);
740 print STDERR "mr $action: command died ($ret)\n";
745 if ($is_checkout && ! -d $dir) {
746 print STDERR "mr $action: $dir missing after checkout\n";;
750 my $ret=hook("post_$action", $topdir, $subdir);
751 return $ret if $ret != OK;
753 if (($is_checkout || $is_update)) {
754 my $ret=hook("fixups", $topdir, $subdir);
755 return $ret if $ret != OK;
764 my ($hook, $topdir, $subdir) = @_;
766 my $command=$config{$topdir}{$subdir}{$hook};
767 return OK unless defined $command;
768 my $lib=exists $config{$topdir}{$subdir}{lib} ?
769 $config{$topdir}{$subdir}{lib}."\n" : "";
770 my $shell="set -e;".$lib.
771 "my_hook(){ $command\n }; my_hook";
772 print "mr $hook: running >>$shell<<\n" if $verbose;
775 my $output = qx/$shell 2>&1/;
778 print STDERR $output;
785 if (($? & 127) == 2) {
786 print STDERR "mr $hook: interrupted\n";
790 print STDERR "mr $hook: received signal ".($? & 127)."\n";
801 # run actions on multiple repos, in parallel
811 while (@fhs or @repos) {
812 while ((!$jobs || $running < $jobs) && @repos) {
814 my $repo = shift @repos;
815 pipe(my $outfh, CHILD_STDOUT);
816 pipe(my $errfh, CHILD_STDERR);
818 unless ($pid = fork) {
819 die "mr $action: cannot fork: $!" unless defined $pid;
820 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
821 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
826 exit action($action, @$repo);
830 push @active, [$pid, $repo];
831 push @fhs, [$outfh, $errfh];
834 my ($rin, $rout) = ('','');
836 foreach my $fh (@fhs) {
837 next unless defined $fh;
838 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
839 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
841 $nfound = select($rout=$rin, undef, undef, 1);
842 foreach my $channel (0, 1) {
843 foreach my $i (0..$#fhs) {
844 next unless defined $fhs[$i];
845 my $fh = $fhs[$i][$channel];
846 next unless defined $fh;
847 if (vec($rout, fileno($fh), 1) == 1) {
849 if (sysread($fh, $r, 1024) == 0) {
851 $fhs[$i][$channel] = undef;
852 if (! defined $fhs[$i][0] &&
853 ! defined $fhs[$i][1]) {
854 waitpid($active[$i][0], 0);
855 print STDOUT $out[$i][0];
856 print STDERR $out[$i][1];
857 record($active[$i][1], $? >> 8);
859 splice(@active, $i, 1);
864 $out[$i][$channel] .= $r;
872 my $dir=shift()->[0];
877 print "\n" unless $quiet;
879 elsif ($ret == FAILED) {
881 chdir($dir) unless $no_chdir;
882 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
883 system((getpwuid($<))[8], "-i");
886 print "\n" unless $quiet;
888 elsif ($ret == SKIPPED) {
891 elsif ($ret == ABORT) {
895 die "unknown exit status $ret";
901 if (! @ok && ! @failed && ! @skipped) {
902 die "mr $action: no repositories found to work on\n";
904 print "mr $action: finished (".join("; ",
905 showstat($#ok+1, "ok", "ok"),
906 showstat($#failed+1, "failed", "failed"),
907 showstat($#skipped+1, "skipped", "skipped"),
908 ).")\n" unless $quiet;
911 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
914 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
924 return "$count ".($count > 1 ? $plural : $singular);
929 # an ordered list of repos
932 foreach my $topdir (sort keys %config) {
933 foreach my $subdir (sort keys %{$config{$topdir}}) {
937 order => $config{$topdir}{$subdir}{order},
942 $a->{order} <=> $b->{order}
944 $a->{topdir} cmp $b->{topdir}
946 $a->{subdir} cmp $b->{subdir}
952 my $topdir=$repo->{topdir};
953 my $subdir=$repo->{subdir};
954 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
959 # Figure out which repos to act on. Returns a list of array refs
962 # [ "$full_repo_path/", "$mr_config_path/", $section_header ]
965 foreach my $repo (repolist()) {
966 my $topdir=$repo->{topdir};
967 my $subdir=$repo->{subdir};
969 next if $subdir eq 'DEFAULT';
970 my $dir=repodir($repo);
972 $dir.="/" unless $dir=~/\/$/;
973 $d.="/" unless $d=~/\/$/;
974 next if $dir ne $d && $dir !~ /^\Q$d\E/;
975 if (defined $max_depth) {
976 my @a=split('/', $dir);
977 my @b=split('/', $d);
978 do { } while (@a && @b && shift(@a) eq shift(@b));
979 next if @a > $max_depth || @b > $max_depth;
981 push @repos, [$dir, $topdir, $subdir];
984 # fallback to find a leaf repo
985 foreach my $repo (reverse repolist()) {
986 my $topdir=$repo->{topdir};
987 my $subdir=$repo->{subdir};
989 next if $subdir eq 'DEFAULT';
990 my $dir=repodir($repo);
992 $dir.="/" unless $dir=~/\/$/;
993 $d.="/" unless $d=~/\/$/;
994 if ($d=~/^\Q$dir\E/) {
995 push @repos, [$dir, $topdir, $subdir];
1017 sub is_trusted_config {
1018 my $config=shift; # must be abs_pathed already
1020 # We always trust ~/.mrconfig.
1021 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
1023 return 1 if $trust_all;
1025 my $trustfile=$ENV{HOME}."/.mrtrust";
1028 $trusted{"$ENV{HOME}/.mrconfig"}=1;
1029 if (open (TRUST, "<", $trustfile)) {
1032 s/^~\//$ENV{HOME}\//;
1033 $trusted{abs_path($_)}=1;
1039 return $trusted{$config};
1043 sub is_trusted_repo {
1046 # Tightly limit what is allowed in a repo name.
1047 # No ../, no absolute paths, and no unusual filenames
1048 # that might try to escape to the shell.
1049 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
1050 $repo !~ /\.\./ && $repo !~ /^\//;
1053 sub is_trusted_checkout {
1056 # To determine if the command is safe, compare it with the
1057 # *_trusted_checkout config settings. Those settings are
1058 # templates for allowed commands, so make sure that each word
1059 # of the command matches the corresponding word of the template.
1062 foreach my $word (split(' ', $command)) {
1064 if ($word=~/^'(.*)'$/) {
1067 elsif ($word=~/^"(.*)"$/) {
1074 foreach my $key (grep { /_trusted_checkout$/ }
1075 keys %{$config{''}{DEFAULT}}) {
1076 my @twords=split(' ', $config{''}{DEFAULT}{$key});
1077 next if @words > @twords;
1081 for (my $c=0; $c < @twords && $match; $c++) {
1082 if ($twords[$c] eq '$url') {
1083 # Match all the typical characters found in
1084 # urls, plus @ which svn can use. Note
1085 # that the "url" might also be a local
1088 defined $words[$c] &&
1089 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1093 elsif ($twords[$c] eq '$repo') {
1094 # If a repo is not specified, assume it
1095 # will be the last path component of the
1096 # url, or something derived from it, and
1098 if (! defined $words[$c] && defined $url) {
1099 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1103 defined $words[$c] &&
1104 is_trusted_repo($words[$c])
1107 elsif (defined $words[$c] && $words[$c]=~/^($twords[$c])$/) {
1121 my ($err, $file, $line, $url)=@_;
1124 die "$err in untrusted $url line $line\n".
1125 "(To trust this url, --trust-all can be used; but please use caution;\n".
1126 "this can allow arbitrary code execution!)\n";
1129 die "$err in untrusted $file line $line\n".
1130 "(To trust this file, list it in ~/.mrtrust.)\n";
1138 my $bootstrap_url=shift;
1144 if (ref $f eq 'GLOB') {
1150 my $absf=abs_path($f);
1151 if ($loaded{$absf}) {
1156 $trusted=is_trusted_config($absf);
1158 if (! defined $dir) {
1159 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1160 if (! defined $dir) {
1165 $dir=abs_path($dir)."/";
1167 if (! exists $configfiles{$dir}) {
1168 $configfiles{$dir}=$f;
1171 # copy in defaults from first parent
1173 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1174 if ($parent eq '/') {
1177 if (exists $config{$parent} &&
1178 exists $config{$parent}{DEFAULT}) {
1179 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1188 print "mr: loading config $f\n" if $verbose;
1189 open($in, "<", $f) || die "mr: open $f: $!\n";
1192 close $in unless ref $f eq 'GLOB';
1196 # Keep track of the current line in the config file;
1197 # when a file is included track the current line from the include.
1201 my $nextline = sub {
1215 my $lineerror = sub {
1217 if (defined $included) {
1218 die "mr: $f line $line include line $includeline: $msg\n";
1221 die "mr: $f line $line: $msg\n";
1228 if (! $trusted && /[[:cntrl:]]/) {
1229 trusterror("mr: illegal control character", $f, $line, $bootstrap_url);
1232 next if /^\s*\#/ || /^\s*$/;
1233 if (/^\[([^\]]*)\]\s*$/) {
1237 if (! is_trusted_repo($section) ||
1238 $section eq 'ALIAS' ||
1239 $section eq 'DEFAULT') {
1240 trusterror("mr: illegal section \"[$section]\"", $f, $line, $bootstrap_url)
1243 $section=expandenv($section) if $trusted;
1244 if ($section ne 'ALIAS' &&
1245 ! exists $config{$dir}{$section} &&
1246 exists $config{$dir}{DEFAULT}) {
1248 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1251 elsif (/^(\w+)\s*=\s*(.*)/) {
1256 while (@lines && $lines[0]=~/^\s(.+)/) {
1263 # Untrusted files can only contain a few
1264 # settings in specific known-safe formats.
1265 if ($parameter eq 'checkout') {
1266 if (! is_trusted_checkout($value)) {
1267 trusterror("mr: illegal checkout command \"$value\"", $f, $line, $bootstrap_url);
1270 elsif ($parameter eq 'order') {
1271 # not interpreted as a command, so
1274 elsif ($value eq 'true' || $value eq 'false') {
1275 # skip=true , deleted=true etc are
1279 trusterror("mr: illegal setting \"$parameter=$value\"", $f, $line, $bootstrap_url);
1283 if ($parameter eq "include") {
1284 print "mr: including output of \"$value\"\n" if $verbose;
1287 print STDERR "mr: include command exited nonzero ($?)\n";
1290 unshift @lines, @inc;
1294 if (! defined $section) {
1295 $lineerror->("parameter ($parameter) not in section");
1297 if ($section eq 'ALIAS') {
1298 $alias{$parameter}=$value;
1300 elsif ($parameter eq 'lib' or $parameter =~ s/_append$//) {
1301 $config{$dir}{$section}{$parameter}.="\n".$value."\n";
1304 $config{$dir}{$section}{$parameter}=$value;
1305 if ($parameter =~ /.*_(.*)/) {
1306 $knownactions{$1}=1;
1309 $knownactions{$parameter}=1;
1311 if ($parameter eq 'chain' &&
1312 length $dir && $section ne "DEFAULT") {
1313 my $chaindir="$section";
1314 if ($chaindir !~ m!^/!) {
1315 $chaindir=$dir.$chaindir;
1317 if (-e "$chaindir/.mrconfig") {
1318 my $ret=system($value);
1320 if (($? & 127) == 2) {
1321 print STDERR "mr: chain test interrupted\n";
1325 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1329 push @toload, ["$chaindir/.mrconfig", $chaindir];
1336 $lineerror->("parse error");
1340 foreach my $c (@toload) {
1345 sub startingconfig {
1346 %alias=%config=%configfiles=%knownactions=%loaded=();
1347 my $datapos=tell(DATA);
1349 seek(DATA,$datapos,0); # rewind
1354 # the section to modify or add
1355 my $targetsection=shift;
1356 # fields to change in the section
1357 # To remove a field, set its value to "".
1358 my %changefields=@_;
1364 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1369 my $formatfield=sub {
1371 my @value=split(/\n/, shift);
1373 return "$field = ".shift(@value)."\n".
1374 join("", map { "\t$_\n" } @value);
1378 while ($out[$#out] =~ /^\s*$/) {
1379 unshift @blanks, pop @out;
1381 foreach my $field (sort keys %changefields) {
1382 if (length $changefields{$field}) {
1383 push @out, "$field = $changefields{$field}\n";
1384 delete $changefields{$field};
1394 if (/^\s*\#/ || /^\s*$/) {
1397 elsif (/^\[([^\]]*)\]\s*$/) {
1398 if (defined $section &&
1399 $section eq $targetsection) {
1403 $section=expandenv($1);
1407 elsif (/^(\w+)\s*=\s(.*)/) {
1412 while (@lines && $lines[0]=~/^\s(.+)/) {
1418 if ($section eq $targetsection) {
1419 if (exists $changefields{$parameter}) {
1420 if (length $changefields{$parameter}) {
1421 $value=$changefields{$parameter};
1423 delete $changefields{$parameter};
1427 push @out, $formatfield->($parameter, $value);
1431 if (defined $section &&
1432 $section eq $targetsection) {
1435 elsif (%changefields) {
1436 push @out, "\n[$targetsection]\n";
1437 foreach my $field (sort keys %changefields) {
1438 if (length $changefields{$field}) {
1439 push @out, $formatfield->($field, $changefields{$field});
1444 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1452 # actions that do not operate on all repos
1453 if ($action eq 'help') {
1456 elsif ($action eq 'config') {
1459 elsif ($action eq 'register') {
1462 elsif ($action eq 'bootstrap') {
1465 elsif ($action eq 'remember' ||
1466 $action eq 'offline' ||
1467 $action eq 'online') {
1468 my @repos=selectrepos;
1469 action($action, @{$repos[0]}) if @repos;
1473 if (!$jobs || $jobs > 1) {
1474 mrs($action, selectrepos());
1477 foreach my $repo (selectrepos()) {
1478 record($repo, action($action, @$repo));
1484 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1489 die "mr config: not enough parameters\n";
1492 if ($section=~/^\//) {
1493 # try to convert to a path relative to the config file
1494 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1495 $dir=abs_path($dir);
1496 $dir.="/" unless $dir=~/\/$/;
1497 if ($section=~/^\Q$dir\E(.*)/) {
1503 if (/^([^=]+)=(.*)$/) {
1504 $changefields{$1}=$2;
1508 foreach my $topdir (sort keys %config) {
1509 if (exists $config{$topdir}{$section} &&
1510 exists $config{$topdir}{$section}{$_}) {
1511 print $config{$topdir}{$section}{$_}."\n";
1513 last if $section eq 'DEFAULT';
1517 die "mr config: $section $_ not set\n";
1521 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1526 if ($config_overridden) {
1527 # Find the directory that the specified config file is
1529 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1532 # Find the closest known mrconfig file to the current
1534 $directory.="/" unless $directory=~/\/$/;
1536 foreach my $topdir (reverse sort keys %config) {
1537 next unless length $topdir;
1538 if ($directory=~/^\Q$topdir\E/) {
1539 $ENV{MR_CONFIG}=$configfiles{$topdir};
1545 if (! $foundconfig) {
1546 $directory=""; # no config file, use builtin
1550 my $subdir=shift @ARGV;
1551 if (! chdir($subdir)) {
1552 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1556 $ENV{MR_REPO}=getcwd();
1557 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1558 if (! defined $command) {
1559 die "mr register: unknown repository type\n";
1562 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1563 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1564 "my_action(){ $command\n }; my_action ".
1565 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1566 print "mr register: running >>$command<<\n" if $verbose;
1567 exec($command) || die "exec: $!";
1571 my $url=shift @ARGV;
1572 my $dir=shift @ARGV || ".";
1574 if (! defined $url || ! length $url) {
1575 die "mr: bootstrap requires url\n";
1578 # Download the config file to a temporary location.
1579 eval q{use File::Temp};
1581 my $tmpconfig=File::Temp->new();
1582 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1583 push(@curlargs, "-k") if $insecure;
1584 my $curlstatus = system(@curlargs);
1585 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1586 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1589 system("mkdir", "-p", $dir);
1591 chdir($dir) || die "chdir $dir: $!";
1593 # Special case to handle checkout of the "." repo, which
1594 # would normally be skipped.
1595 my $topdir=abs_path(".")."/";
1596 my @repo=($topdir, $topdir, ".");
1597 loadconfig($tmpconfig, $topdir, $url);
1598 record(\@repo, action("checkout", @repo, 1))
1599 if exists $config{$topdir}{"."}{"checkout"};
1601 if (-e ".mrconfig") {
1602 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1605 eval q{use File::Copy};
1607 move($tmpconfig, ".mrconfig") || die "rename: $!";
1610 # Reload the config file (in case we got a different version)
1611 # and checkout everything else.
1613 loadconfig(".mrconfig");
1614 dispatch("checkout");
1615 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1616 showstats("bootstrap");
1620 # alias expansion and command stemming
1623 if (exists $alias{$action}) {
1624 $action=$alias{$action};
1626 if (! exists $knownactions{$action}) {
1627 my @matches = grep { /^\Q$action\E/ }
1628 keys %knownactions, keys %alias;
1629 if (@matches == 1) {
1630 $action=$matches[0];
1632 elsif (@matches == 0) {
1633 die "mr: unknown action \"$action\" (known actions: ".
1634 join(", ", sort keys %knownactions).")\n";
1637 die "mr: ambiguous action \"$action\" (matches: ".
1638 join(", ", @matches).")\n";
1646 while (length $dir) {
1647 if (-e "$dir/.mrconfig") {
1648 return "$dir/.mrconfig";
1650 $dir=~s/\/[^\/]*$//;
1652 return "$ENV{HOME}/.mrconfig";
1657 Getopt::Long::Configure("bundling", "no_permute");
1658 my $result=GetOptions(
1659 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1660 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1661 "p|path" => sub { }, # now default, ignore
1662 "v|verbose" => \$verbose,
1663 "q|quiet" => \$quiet,
1664 "s|stats" => \$stats,
1665 "k|insecure" => \$insecure,
1666 "i|interactive" => \$interactive,
1667 "n|no-recurse:i" => \$max_depth,
1668 "j|jobs:i" => \$jobs,
1669 "t|trust-all" => \$trust_all,
1671 if (! $result || @ARGV < 1) {
1672 die("Usage: mr [options] action [params ...]\n".
1673 "(Use mr help for man page.)\n");
1676 $ENV{MR_SWITCHES}="";
1677 foreach my $option (@saved) {
1678 last if $option eq $ARGV[0];
1679 $ENV{MR_SWITCHES}.="$option ";
1685 print STDERR "mr: interrupted\n";
1689 # This can happen if it's run in a directory that was removed
1690 # or other strangeness.
1691 if (! defined $directory) {
1692 die("mr: failed to determine working directory\n");
1694 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1695 # the config file might be a symlink to elsewhere, and the directory it's
1696 # in is significant.
1697 if ($ENV{MR_CONFIG} !~ /^\//) {
1698 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1700 # Try to set MR_PATH to the path to the program.
1702 use FindBin qw($Bin $Script);
1703 $ENV{MR_PATH}=$Bin."/".$Script;
1721 loadconfig("$ENV{HOME}/.mrconfig");
1722 loadconfig($ENV{MR_CONFIG});
1723 #use Data::Dumper; print Dumper(\%config);
1725 my $action=expandaction(shift @ARGV);
1732 # Finally, some useful actions that mr knows about by default.
1733 # These can be overridden in ~/.mrconfig.
1748 echo "mr (warning): $@" >&2
1754 if [ -z "$1" ] || [ -z "$2" ]; then
1755 error "mr: usage: hours_since action num"
1757 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1758 if [ -e "$MR_REPO/$dir" ]; then
1759 flagfile="$MR_REPO/$dir/.mr_last$1"
1763 if [ -z "$flagfile" ]; then
1764 error "cannot determine flag filename"
1766 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1767 if [ "$delta" -lt "$2" ]; then
1775 LANG=C bzr info | egrep -q '^Checkout'
1778 if [ "$MR_ACTION" = checkout ] || [ -d "$MR_REPO" ]; then
1785 svn_test = test -d "$MR_REPO"/.svn
1786 git_test = test -d "$MR_REPO"/.git
1787 bzr_test = test -d "$MR_REPO"/.bzr
1788 cvs_test = test -d "$MR_REPO"/CVS
1789 hg_test = test -d "$MR_REPO"/.hg
1790 darcs_test = test -d "$MR_REPO"/_darcs
1791 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1793 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1794 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1795 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1797 svn_update = svn update "$@"
1798 git_update = git pull "$@"
1800 if is_bzr_checkout; then
1803 bzr merge --pull "$@"
1805 cvs_update = cvs update "$@"
1806 hg_update = hg pull "$@" && hg update "$@"
1807 darcs_update = darcs pull -a "$@"
1808 fossil_update = fossil pull "$@"
1810 svn_status = svn status "$@"
1811 git_status = git status -s "$@" || true
1812 bzr_status = bzr status --short "$@"
1813 cvs_status = cvs status "$@"
1814 hg_status = hg status "$@"
1815 darcs_status = darcs whatsnew -ls "$@" || true
1816 fossil_status = fossil changes "$@"
1818 svn_commit = svn commit "$@"
1819 git_commit = git commit -a "$@" && git push --all
1821 if is_bzr_checkout; then
1824 bzr commit "$@" && bzr push
1826 cvs_commit = cvs commit "$@"
1827 hg_commit = hg commit -m "$@" && hg push
1828 darcs_commit = darcs record -a -m "$@" && darcs push -a
1829 fossil_commit = fossil commit "$@"
1831 git_record = git commit -a "$@"
1833 if is_bzr_checkout; then
1834 bzr commit --local "$@"
1838 hg_record = hg commit -m "$@"
1839 darcs_record = darcs record -a -m "$@"
1840 fossil_record = fossil commit "$@"
1843 git_push = git push "$@"
1844 bzr_push = bzr push "$@"
1846 hg_push = hg push "$@"
1847 darcs_push = darcs push -a "$@"
1848 fossil_push = fossil push "$@"
1850 svn_diff = svn diff "$@"
1851 git_diff = git diff "$@"
1852 bzr_diff = bzr diff "$@"
1853 cvs_diff = cvs diff "$@"
1854 hg_diff = hg diff "$@"
1855 darcs_diff = darcs diff -u "$@"
1856 fossil_diff = fossil diff "$@"
1858 svn_log = svn log "$@"
1859 git_log = git log "$@"
1860 bzr_log = bzr log "$@"
1861 cvs_log = cvs log "$@"
1862 hg_log = hg log "$@"
1863 darcs_log = darcs changes "$@"
1864 git_bare_log = git log "$@"
1865 fossil_log = fossil timeline "$@"
1870 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1871 if [ -z "$url" ]; then
1872 error "cannot determine svn url"
1874 echo "Registering svn url: $url in $MR_CONFIG"
1875 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1877 url="`LC_ALL=C git config --get remote.origin.url`" || true
1878 if [ -z "$url" ]; then
1879 error "cannot determine git url"
1881 echo "Registering git url: $url in $MR_CONFIG"
1882 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1884 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1885 if [ -z "$url" ]; then
1886 error "cannot determine bzr url"
1888 echo "Registering bzr url: $url in $MR_CONFIG"
1889 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr branch '$url' '$MR_REPO'"
1891 repo=`cat CVS/Repository`
1893 if [ -z "$root" ]; then
1894 error "cannot determine cvs root"
1896 echo "Registering cvs repository $repo at root $root"
1897 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1899 url=`hg showconfig paths.default`
1900 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1901 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1903 url=`cat _darcs/prefs/defaultrepo`
1904 echo "Registering darcs repository $url in $MR_CONFIG"
1905 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1907 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1908 if [ -z "$url" ]; then
1909 error "cannot determine git url"
1911 echo "Registering git url: $url in $MR_CONFIG"
1912 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1914 url=`fossil remote-url`
1915 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1916 echo "Registering fossil repository $url in $MR_CONFIG"
1917 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1919 svn_trusted_checkout = svn co $url $repo
1920 svn_alt_trusted_checkout = svn checkout $url $repo
1921 git_trusted_checkout = git clone $url $repo
1922 bzr_trusted_checkout = bzr checkout|clone|branch|get $url $repo
1924 hg_trusted_checkout = hg clone $url $repo
1925 darcs_trusted_checkout = darcs get $url $repo
1926 git_bare_trusted_checkout = git clone --bare $url $repo
1927 # fossil: messy to do
1933 SHOWMANFILE="man -f"
1939 SHOWMANFILE="man -l"
1942 if [ ! -e "$MR_PATH" ]; then
1943 error "cannot find program path"
1945 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1946 trap "rm -f $tmp" exit
1947 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1948 $SHOWMANFILE "$tmp" || error "man failed"
1954 if [ -s ~/.mrlog ]; then
1955 info "running offline commands"
1956 mv -f ~/.mrlog ~/.mrlog.old
1957 if ! sh -e ~/.mrlog.old; then
1958 error "offline command failed; left in ~/.mrlog.old"
1962 info "no offline commands to run"
1967 info "offline mode enabled"
1969 info "remembering command: 'mr $@'"
1970 command="mr -d '$(pwd)' $MR_SWITCHES"
1972 command="$command '$w'"
1974 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1975 echo "$command" >> ~/.mrlog
1978 ed = echo "A horse is a horse, of course, of course.."
1979 T = echo "I pity the fool."
1980 right = echo "Not found."
1982 # vim:sw=8:sts=0:ts=8:noet
1984 # indent-tabs-mode: t
1985 # cperl-indent-level: 8