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 revision
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 revision
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 revision 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 revision 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 revision control system. This is mostly useful
193 if the repositories mr will act on all use the same revision 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 B<~/.mrconfig>
212 as well as look for a .mrconfig file in the current directory, or in one
213 of its parent directories.
225 Be quiet. This supresses 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 ~/.mrtrust.
284 This obsolete flag is ignored.
288 =head1 MRCONFIG FILES
290 Here is an example .mrconfig file:
293 checkout = svn co 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 .mrconfig file uses a variant of the INI file format. Lines starting with
302 "#" are comments. Values can be continued to the following line by
303 indenting the line with whitespace.
305 The "DEFAULT" section allows setting default values for the sections that
308 The "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 "[$HOSTNAME]", or "[${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. Normally you only need to specify what
321 to do for "checkout".
323 Note that these shell commands are run in a "set -e" shell
324 environment, where any additional parameters you pass are available in
325 "$@". The "checkout" command is run in the parent of the repository
326 directory, since the repository isn't checked out yet. All other commands
327 are run inside the repository, though not necessarily at the top of it.
329 The "MR_REPO" environment variable is set to the path to the top of the
330 repository. (For the "register" action, "MR_REPO" is instead set to the
331 basename of the directory that should be created when checking the
334 The "MR_CONFIG" environment variable is set to the .mrconfig file
335 that defines the repo being acted on, or, if the repo is not yet in a config
336 file, the .mrconfig file that should be modified to register the repo.
338 A few parameters have special meanings:
344 If the "skip" parameter is set and its command returns true, then B<mr>
345 will skip acting on that repository. The command is passed the action
348 Here are two examples. The first skips the repo unless
349 mr is run by joey. The second uses the hours_since function
350 (included in mr's built-in library) to skip updating the repo unless it's
351 been at least 12 hours since the last update.
353 skip = test `whoami` != joey
354 skip = [ "$1" = update ] && ! hours_since "$1" 12
358 The "order" parameter can be used to override the default ordering of
359 repositories. The default order value is 10. Use smaller values to make
360 repositories be processed earlier, and larger values to make repositories
363 Note that if a repository is located in a subdirectory of another
364 repository, ordering it to be processed earlier is not recommended.
368 If the "chain" parameter is set and its command returns true, then B<mr>
369 will try to load a .mrconfig file from the root of the repository.
373 If the "include" parameter is set, its command is ran, and should output
374 additional mrconfig file content. The content is included as if it were
375 part of the including file.
377 Unlike all other parameters, this parameter does not need to be placed
382 If the "deleted" parameter is set and its command returns true, then
383 B<mr> will treat the repository as deleted. It won't ever actually delete
384 the repository, but it will warn if it sees the repository's directory.
385 This is useful when one mrconfig file is shared amoung multiple machines,
386 to keep track of and remember to delete old repositories.
390 The "lib" parameter can specify some shell code that will be run before each
391 command, this can be a useful way to define shell functions for other commands
396 If the "fixups" parameter is set, its command is run whenever a repository
397 is checked out, or updated. This provides an easy way to do things
398 like permissions fixups, or other tweaks to the repository content,
399 whenever the repository is changed.
403 If a "pre_action" parameter is set, its command is run before mr performs the
404 specified action. Similarly, "post_action" parameters are run after mr
405 successfully performs the specified action. For example, "pre_commit" is
406 run before committing; "post_update" is run after updating.
410 When looking for a command to run for a given action, mr first looks for
411 a parameter with the same name as the action. If that is not found, it
412 looks for a parameter named "rcs_action" (substituting in the name of the
413 revision control system and the action). The name of the revision control
414 system is itself determined by running each defined "rcs_test" action,
417 Internally, mr has settings for "git_update", "svn_update", etc. To change
418 the action that is performed for a given revision control system, you can
419 override these rcs specific actions. To add a new revision control system,
420 you can just add rcs specific actions for it.
422 =head1 UNTRUSTED MRCONFIG FILES
424 Since mrconfig files can contain arbitrary shell commands, they can do
425 anything. This flexibility is good, but it also allows a malicious mrconfig
426 file to delete your whole home directory. Such a file might be contained
427 inside a repository that your main ~/.mrconfig checks out. To
428 avoid worries about evil commands in a mrconfig file, mr defaults to
429 reading all mrconfig files other than the main ~/.mrconfig in untrusted
430 mode. In untrusted mode, mrconfig files are limited to running only known
431 safe commands (like "git clone") in a carefully checked manner.
433 To configure mr to trust other mrconfig files, list them in ~/.mrtrust.
434 One mrconfig file should be listed per line. Either the full pathname
435 should be listed, or the pathname can start with "~/" to specify a file
436 relative to your home directory.
438 =head1 OFFLINE LOG FILE
440 The ~/.mrlog file contains commands that mr has remembered to run later,
441 due to being offline. You can delete or edit this file to remove commands,
442 or even to add other commands for 'mr online' to run. If the file is
443 present, mr assumes it is in offline mode.
447 mr can be extended to support things such as unison and git-svn. Some
448 files providing such extensions are available in /usr/share/mr/. See
449 the documentation in the files for details about using them.
453 mr returns nonzero if a command failed in any of the repositories.
457 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
459 Licensed under the GNU GPL version 2 or higher.
461 http://kitenet.net/~joey/code/mr/
468 use Cwd qw(getcwd abs_path);
470 # things that can happen when mr runs a command
479 my $config_overridden=0;
489 my $directory=getcwd();
491 $ENV{MR_CONFIG}=find_mrconfig();
498 my (@ok, @failed, @skipped);
504 my ($action, $dir, $topdir, $subdir) = @_;
506 if (exists $rcs{$dir}) {
511 foreach my $rcs_test (
513 length $a <=> length $b
516 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
517 my ($rcs)=$rcs_test=~/(.*)_test/;
518 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
519 $test.="if my_$rcs_test; then echo $rcs; fi\n";
521 $test=$config{$topdir}{$subdir}{lib}."\n".$test
522 if exists $config{$topdir}{$subdir}{lib};
524 print "mr $action: running rcs test >>$test<<\n" if $verbose;
529 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
533 return $rcs{$dir}=undef;
536 return $rcs{$dir}=$rcs;
541 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
543 if (exists $config{$topdir}{$subdir}{$action}) {
544 return $config{$topdir}{$subdir}{$action};
551 my $rcs=rcs_test(@_);
554 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
555 return $config{$topdir}{$subdir}{$rcs."_".$action};
563 my ($topdir, $subdir) = @_;
564 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
568 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
569 my $fulldir=fulldir($topdir, $subdir);
571 $ENV{MR_CONFIG}=$configfiles{$topdir};
572 my $lib=exists $config{$topdir}{$subdir}{lib} ?
573 $config{$topdir}{$subdir}{lib}."\n" : "";
574 my $is_checkout=($action eq 'checkout');
575 my $is_update=($action =~ /update/);
579 foreach my $testname ("skip", "deleted") {
580 my $testcommand=findcommand($testname, $dir, $topdir, $subdir, $is_checkout);
582 if (defined $testcommand) {
583 my $test="set -e;".$lib.
584 "my_action(){ $testcommand\n }; my_action '$action'";
585 print "mr $action: running $testname test >>$test<<\n" if $verbose;
586 my $ret=system($test);
588 if (($? & 127) == 2) {
589 print STDERR "mr $action: interrupted\n";
593 print STDERR "mr $action: $testname test received signal ".($? & 127)."\n";
597 if ($ret >> 8 == 0) {
598 if ($testname eq "deleted") {
600 print STDERR "mr error: $dir should be deleted yet still exists\n";
604 print "mr $action: skip $dir skipped\n" if $verbose;
611 if (! $force_checkout) {
613 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
617 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
622 return action("checkout", $dir, $topdir, $subdir);
626 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
628 if ($is_checkout && ! -d $dir) {
629 print "mr $action: creating parent directory $dir\n" if $verbose;
630 system("mkdir", "-p", $dir);
633 if (! $no_chdir && ! chdir($dir)) {
634 print STDERR "mr $action: failed to chdir to $dir: $!\n";
637 elsif (! defined $command) {
638 my $rcs=rcs_test(@_);
639 if (! defined $rcs) {
640 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
644 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
651 $actionmsg="mr $action: $fulldir";
655 $s=~s/^\Q$fulldir\E\/?//;
656 $actionmsg="mr $action: $fulldir (in subdir $s)";
658 print "$actionmsg\n" unless $quiet;
660 my $hookret=hook("pre_$action", $topdir, $subdir);
661 return $hookret if $hookret != OK;
663 $command="set -e; ".$lib.
664 "my_action(){ $command\n }; my_action ".
665 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
666 print "mr $action: running >>$command<<\n" if $verbose;
669 my $output = qx/$command 2>&1/;
672 print "$actionmsg\n";
673 print STDERR $output;
677 $ret=system($command);
680 if (($? & 127) == 2) {
681 print STDERR "mr $action: interrupted\n";
685 print STDERR "mr $action: received signal ".($? & 127)."\n";
688 print STDERR "mr $action: failed ($ret)\n" if $verbose;
689 if ($ret >> 8 != 0) {
690 print STDERR "mr $action: command failed\n";
691 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
692 # recreate original command line to
693 # remember, and avoid recursing
695 @ARGV=('-n', $action, @orig);
696 action("remember", $dir, $topdir, $subdir);
701 print STDERR "mr $action: command died ($ret)\n";
706 if ($is_checkout && ! -d $dir) {
707 print STDERR "mr $action: $dir missing after checkout\n";;
711 my $ret=hook("post_$action", $topdir, $subdir);
712 return $ret if $ret != OK;
714 if (($is_checkout || $is_update)) {
715 my $ret=hook("fixups", $topdir, $subdir);
716 return $ret if $ret != OK;
725 my ($hook, $topdir, $subdir) = @_;
727 my $command=$config{$topdir}{$subdir}{$hook};
728 return OK unless defined $command;
729 my $lib=exists $config{$topdir}{$subdir}{lib} ?
730 $config{$topdir}{$subdir}{lib}."\n" : "";
731 my $shell="set -e;".$lib.
732 "my_hook(){ $command\n }; my_hook";
733 print "mr $hook: running >>$shell<<\n" if $verbose;
736 my $output = qx/$shell 2>&1/;
739 print STDERR $output;
746 if (($? & 127) == 2) {
747 print STDERR "mr $hook: interrupted\n";
751 print STDERR "mr $hook: received signal ".($? & 127)."\n";
759 # run actions on multiple repos, in parallel
769 while (@fhs or @repos) {
770 while ((!$jobs || $running < $jobs) && @repos) {
772 my $repo = shift @repos;
773 pipe(my $outfh, CHILD_STDOUT);
774 pipe(my $errfh, CHILD_STDERR);
776 unless ($pid = fork) {
777 die "mr $action: cannot fork: $!" unless defined $pid;
778 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
779 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
784 exit action($action, @$repo);
788 push @active, [$pid, $repo];
789 push @fhs, [$outfh, $errfh];
792 my ($rin, $rout) = ('','');
794 foreach my $fh (@fhs) {
795 next unless defined $fh;
796 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
797 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
799 $nfound = select($rout=$rin, undef, undef, 1);
800 foreach my $channel (0, 1) {
801 foreach my $i (0..$#fhs) {
802 next unless defined $fhs[$i];
803 my $fh = $fhs[$i][$channel];
804 next unless defined $fh;
805 if (vec($rout, fileno($fh), 1) == 1) {
807 if (sysread($fh, $r, 1024) == 0) {
809 $fhs[$i][$channel] = undef;
810 if (! defined $fhs[$i][0] &&
811 ! defined $fhs[$i][1]) {
812 waitpid($active[$i][0], 0);
813 print STDOUT $out[$i][0];
814 print STDERR $out[$i][1];
815 record($active[$i][1], $? >> 8);
817 splice(@active, $i, 1);
822 $out[$i][$channel] .= $r;
830 my $dir=shift()->[0];
835 print "\n" unless $quiet;
837 elsif ($ret == FAILED) {
839 chdir($dir) unless $no_chdir;
840 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
841 system((getpwuid($<))[8], "-i");
844 print "\n" unless $quiet;
846 elsif ($ret == SKIPPED) {
849 elsif ($ret == ABORT) {
853 die "unknown exit status $ret";
859 if (! @ok && ! @failed && ! @skipped) {
860 die "mr $action: no repositories found to work on\n";
862 print "mr $action: finished (".join("; ",
863 showstat($#ok+1, "ok", "ok"),
864 showstat($#failed+1, "failed", "failed"),
865 showstat($#skipped+1, "skipped", "skipped"),
866 ).")\n" unless $quiet;
869 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
872 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
882 return "$count ".($count > 1 ? $plural : $singular);
887 # an ordered list of repos
890 foreach my $topdir (sort keys %config) {
891 foreach my $subdir (sort keys %{$config{$topdir}}) {
895 order => $config{$topdir}{$subdir}{order},
900 $a->{order} <=> $b->{order}
902 $a->{topdir} cmp $b->{topdir}
904 $a->{subdir} cmp $b->{subdir}
910 my $topdir=$repo->{topdir};
911 my $subdir=$repo->{subdir};
912 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
917 # figure out which repos to act on
920 foreach my $repo (repolist()) {
921 my $topdir=$repo->{topdir};
922 my $subdir=$repo->{subdir};
924 next if $subdir eq 'DEFAULT';
925 my $dir=repodir($repo);
927 $dir.="/" unless $dir=~/\/$/;
928 $d.="/" unless $d=~/\/$/;
929 next if $dir ne $d && $dir !~ /^\Q$d\E/;
930 if (defined $max_depth) {
931 my @a=split('/', $dir);
932 my @b=split('/', $d);
933 do { } while (@a && @b && shift(@a) eq shift(@b));
934 next if @a > $max_depth || @b > $max_depth;
936 push @repos, [$dir, $topdir, $subdir];
939 # fallback to find a leaf repo
940 foreach my $repo (reverse repolist()) {
941 my $topdir=$repo->{topdir};
942 my $subdir=$repo->{subdir};
944 next if $subdir eq 'DEFAULT';
945 my $dir=repodir($repo);
947 $dir.="/" unless $dir=~/\/$/;
948 $d.="/" unless $d=~/\/$/;
949 if ($d=~/^\Q$dir\E/) {
950 push @repos, [$dir, $topdir, $subdir];
972 sub is_trusted_config {
973 my $config=shift; # must be abs_pathed already
975 # We always trust ~/.mrconfig.
976 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
978 return 1 if $trust_all;
980 my $trustfile=$ENV{HOME}."/.mrtrust";
983 $trusted{"$ENV{HOME}/.mrconfig"}=1;
984 if (open (TRUST, "<", $trustfile)) {
987 s/^~\//$ENV{HOME}\//;
988 $trusted{abs_path($_)}=1;
994 return $trusted{$config};
998 sub is_trusted_repo {
1001 # Tightly limit what is allowed in a repo name.
1002 # No ../, no absolute paths, and no unusual filenames
1003 # that might try to escape to the shell.
1004 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
1005 $repo !~ /\.\./ && $repo !~ /^\//;
1008 sub is_trusted_checkout {
1011 # To determine if the command is safe, compare it with the
1012 # *_trusted_checkout config settings. Those settings are
1013 # templates for allowed commands, so make sure that each word
1014 # of the command matches the corresponding word of the template.
1017 foreach my $word (split(' ', $command)) {
1019 if ($word=~/^'(.*)'$/) {
1022 elsif ($word=~/^"(.*)"$/) {
1029 foreach my $key (grep { /_trusted_checkout$/ }
1030 keys %{$config{''}{DEFAULT}}) {
1031 my @twords=split(' ', $config{''}{DEFAULT}{$key});
1032 next if @words > @twords;
1036 for (my $c=0; $c < @twords && $match; $c++) {
1037 if ($twords[$c] eq '$url') {
1038 # Match all the typical characters found in
1039 # urls, plus @ which svn can use. Note
1040 # that the "url" might also be a local
1043 defined $words[$c] &&
1044 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1048 elsif ($twords[$c] eq '$repo') {
1049 # If a repo is not specified, assume it
1050 # will be the last path component of the
1051 # url, or something derived from it, and
1053 if (! defined $words[$c] && defined $url) {
1054 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1058 defined $words[$c] &&
1059 is_trusted_repo($words[$c])
1062 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1076 my ($err, $file, $line, $url)=@_;
1079 die "$err in untrusted $url line $line\n".
1080 "(To trust this url, --trust-all can be used; but please use caution;\n".
1081 "this can allow arbitrary code execution!)\n";
1084 die "$err in untrusted $file line $line\n".
1085 "(To trust this file, list it in ~/.mrtrust.)\n";
1093 my $bootstrap_url=shift;
1099 if (ref $f eq 'GLOB') {
1105 my $absf=abs_path($f);
1106 if ($loaded{$absf}) {
1111 $trusted=is_trusted_config($absf);
1113 if (! defined $dir) {
1114 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1115 if (! defined $dir) {
1120 $dir=abs_path($dir)."/";
1122 if (! exists $configfiles{$dir}) {
1123 $configfiles{$dir}=$f;
1126 # copy in defaults from first parent
1128 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1129 if ($parent eq '/') {
1132 if (exists $config{$parent} &&
1133 exists $config{$parent}{DEFAULT}) {
1134 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1143 print "mr: loading config $f\n" if $verbose;
1144 open($in, "<", $f) || die "mr: open $f: $!\n";
1147 close $in unless ref $f eq 'GLOB';
1155 next if /^\s*\#/ || /^\s*$/;
1156 if (/^\[([^\]]*)\]\s*$/) {
1160 if (! is_trusted_repo($section) ||
1161 $section eq 'ALIAS' ||
1162 $section eq 'DEFAULT') {
1163 trusterror("mr: illegal section \"[$section]\"", $f, $line, $bootstrap_url)
1166 $section=expandenv($section) if $trusted;
1167 if ($section ne 'ALIAS' &&
1168 ! exists $config{$dir}{$section} &&
1169 exists $config{$dir}{DEFAULT}) {
1171 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1174 elsif (/^(\w+)\s*=\s*(.*)/) {
1179 while (@lines && $lines[0]=~/^\s(.+)/) {
1187 # Untrusted files can only contain a few
1188 # settings in specific known-safe formats.
1189 if ($parameter eq 'checkout') {
1190 if (! is_trusted_checkout($value)) {
1191 trusterror("mr: illegal checkout command \"$value\"", $f, $line, $bootstrap_url);
1194 elsif ($parameter eq 'order') {
1195 # not interpreted as a command, so
1198 elsif ($value eq 'true' || $value eq 'false') {
1199 # skip=true , deleted=true etc are
1203 trusterror("mr: illegal setting \"$parameter=$value\"", $f, $line, $bootstrap_url);
1207 if ($parameter eq "include") {
1208 print "mr: including output of \"$value\"\n" if $verbose;
1209 unshift @lines, `$value`;
1211 print STDERR "mr: include command exited nonzero ($?)\n";
1216 if (! defined $section) {
1217 die "$f line $.: parameter ($parameter) not in section\n";
1219 if ($section eq 'ALIAS') {
1220 $alias{$parameter}=$value;
1222 elsif ($parameter eq 'lib') {
1223 $config{$dir}{$section}{lib}.=$value."\n";
1226 $config{$dir}{$section}{$parameter}=$value;
1227 if ($parameter =~ /.*_(.*)/) {
1228 $knownactions{$1}=1;
1231 $knownactions{$parameter}=1;
1233 if ($parameter eq 'chain' &&
1234 length $dir && $section ne "DEFAULT" &&
1235 -e $dir.$section."/.mrconfig") {
1236 my $ret=system($value);
1238 if (($? & 127) == 2) {
1239 print STDERR "mr: chain test interrupted\n";
1243 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1247 push @toload, $dir.$section."/.mrconfig";
1253 die "$f line $line: parse error\n";
1262 sub startingconfig {
1263 %alias=%config=%configfiles=%knownactions=%loaded=();
1264 my $datapos=tell(DATA);
1266 seek(DATA,$datapos,0); # rewind
1271 # the section to modify or add
1272 my $targetsection=shift;
1273 # fields to change in the section
1274 # To remove a field, set its value to "".
1275 my %changefields=@_;
1281 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1286 my $formatfield=sub {
1288 my @value=split(/\n/, shift);
1290 return "$field = ".shift(@value)."\n".
1291 join("", map { "\t$_\n" } @value);
1295 while ($out[$#out] =~ /^\s*$/) {
1296 unshift @blanks, pop @out;
1298 foreach my $field (sort keys %changefields) {
1299 if (length $changefields{$field}) {
1300 push @out, "$field = $changefields{$field}\n";
1301 delete $changefields{$field};
1311 if (/^\s*\#/ || /^\s*$/) {
1314 elsif (/^\[([^\]]*)\]\s*$/) {
1315 if (defined $section &&
1316 $section eq $targetsection) {
1320 $section=expandenv($1);
1324 elsif (/^(\w+)\s*=\s(.*)/) {
1329 while (@lines && $lines[0]=~/^\s(.+)/) {
1335 if ($section eq $targetsection) {
1336 if (exists $changefields{$parameter}) {
1337 if (length $changefields{$parameter}) {
1338 $value=$changefields{$parameter};
1340 delete $changefields{$parameter};
1344 push @out, $formatfield->($parameter, $value);
1348 if (defined $section &&
1349 $section eq $targetsection) {
1352 elsif (%changefields) {
1353 push @out, "\n[$targetsection]\n";
1354 foreach my $field (sort keys %changefields) {
1355 if (length $changefields{$field}) {
1356 push @out, $formatfield->($field, $changefields{$field});
1361 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1369 # actions that do not operate on all repos
1370 if ($action eq 'help') {
1373 elsif ($action eq 'config') {
1376 elsif ($action eq 'register') {
1379 elsif ($action eq 'bootstrap') {
1382 elsif ($action eq 'remember' ||
1383 $action eq 'offline' ||
1384 $action eq 'online') {
1385 my @repos=selectrepos;
1386 action($action, @{$repos[0]}) if @repos;
1390 if (!$jobs || $jobs > 1) {
1391 mrs($action, selectrepos());
1394 foreach my $repo (selectrepos()) {
1395 record($repo, action($action, @$repo));
1401 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1406 die "mr config: not enough parameters\n";
1409 if ($section=~/^\//) {
1410 # try to convert to a path relative to the config file
1411 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1412 $dir=abs_path($dir);
1413 $dir.="/" unless $dir=~/\/$/;
1414 if ($section=~/^\Q$dir\E(.*)/) {
1420 if (/^([^=]+)=(.*)$/) {
1421 $changefields{$1}=$2;
1425 foreach my $topdir (sort keys %config) {
1426 if (exists $config{$topdir}{$section} &&
1427 exists $config{$topdir}{$section}{$_}) {
1428 print $config{$topdir}{$section}{$_}."\n";
1430 last if $section eq 'DEFAULT';
1434 die "mr config: $section $_ not set\n";
1438 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1443 if ($config_overridden) {
1444 # Find the directory that the specified config file is
1446 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1449 # Find the closest known mrconfig file to the current
1451 $directory.="/" unless $directory=~/\/$/;
1453 foreach my $topdir (reverse sort keys %config) {
1454 next unless length $topdir;
1455 if ($directory=~/^\Q$topdir\E/) {
1456 $ENV{MR_CONFIG}=$configfiles{$topdir};
1462 if (! $foundconfig) {
1463 $directory=""; # no config file, use builtin
1467 my $subdir=shift @ARGV;
1468 if (! chdir($subdir)) {
1469 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1473 $ENV{MR_REPO}=getcwd();
1474 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1475 if (! defined $command) {
1476 die "mr register: unknown repository type\n";
1479 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1480 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1481 "my_action(){ $command\n }; my_action ".
1482 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1483 print "mr register: running >>$command<<\n" if $verbose;
1484 exec($command) || die "exec: $!";
1488 my $url=shift @ARGV;
1489 my $dir=shift @ARGV || ".";
1491 if (! defined $url || ! length $url) {
1492 die "mr: bootstrap requires url\n";
1495 # Download the config file to a temporary location.
1496 eval q{use File::Temp};
1498 my $tmpconfig=File::Temp->new();
1499 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1500 push(@curlargs, "-k") if $insecure;
1501 my $curlstatus = system(@curlargs);
1502 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1503 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1506 system("mkdir", "-p", $dir);
1508 chdir($dir) || die "chdir $dir: $!";
1510 # Special case to handle checkout of the "." repo, which
1511 # would normally be skipped.
1512 my $topdir=abs_path(".")."/";
1513 my @repo=($topdir, $topdir, ".");
1514 loadconfig($tmpconfig, $topdir, $url);
1515 record(\@repo, action("checkout", @repo, 1))
1516 if exists $config{$topdir}{"."}{"checkout"};
1518 if (-e ".mrconfig") {
1519 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1522 eval q{use File::Copy};
1524 move($tmpconfig, ".mrconfig") || die "rename: $!";
1527 # Reload the config file (in case we got a different version)
1528 # and checkout everything else.
1530 loadconfig(".mrconfig");
1531 dispatch("checkout");
1532 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1533 showstats("bootstrap");
1537 # alias expansion and command stemming
1540 if (exists $alias{$action}) {
1541 $action=$alias{$action};
1543 if (! exists $knownactions{$action}) {
1544 my @matches = grep { /^\Q$action\E/ }
1545 keys %knownactions, keys %alias;
1546 if (@matches == 1) {
1547 $action=$matches[0];
1549 elsif (@matches == 0) {
1550 die "mr: unknown action \"$action\" (known actions: ".
1551 join(", ", sort keys %knownactions).")\n";
1554 die "mr: ambiguous action \"$action\" (matches: ".
1555 join(", ", @matches).")\n";
1563 while (length $dir) {
1564 if (-e "$dir/.mrconfig") {
1565 return "$dir/.mrconfig";
1567 $dir=~s/\/[^\/]*$//;
1569 return "$ENV{HOME}/.mrconfig";
1574 Getopt::Long::Configure("bundling", "no_permute");
1575 my $result=GetOptions(
1576 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1577 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1578 "p|path" => sub { }, # now default, ignore
1579 "v|verbose" => \$verbose,
1580 "q|quiet" => \$quiet,
1581 "s|stats" => \$stats,
1582 "k|insecure" => \$insecure,
1583 "i|interactive" => \$interactive,
1584 "n|no-recurse:i" => \$max_depth,
1585 "j|jobs:i" => \$jobs,
1586 "t|trust-all" => \$trust_all,
1588 if (! $result || @ARGV < 1) {
1589 die("Usage: mr [options] action [params ...]\n".
1590 "(Use mr help for man page.)\n");
1593 $ENV{MR_SWITCHES}="";
1594 foreach my $option (@saved) {
1595 last if $option eq $ARGV[0];
1596 $ENV{MR_SWITCHES}.="$option ";
1602 print STDERR "mr: interrupted\n";
1606 # This can happen if it's run in a directory that was removed
1607 # or other strangeness.
1608 if (! defined $directory) {
1609 die("mr: failed to determine working directory\n");
1611 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1612 # the config file might be a symlink to elsewhere, and the directory it's
1613 # in is significant.
1614 if ($ENV{MR_CONFIG} !~ /^\//) {
1615 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1617 # Try to set MR_PATH to the path to the program.
1619 use FindBin qw($Bin $Script);
1620 $ENV{MR_PATH}=$Bin."/".$Script;
1638 loadconfig("$ENV{HOME}/.mrconfig");
1639 loadconfig($ENV{MR_CONFIG});
1640 #use Data::Dumper; print Dumper(\%config);
1642 my $action=expandaction(shift @ARGV);
1649 # Finally, some useful actions that mr knows about by default.
1650 # These can be overridden in ~/.mrconfig.
1665 echo "mr (warning): $@" >&2
1671 if [ -z "$1" ] || [ -z "$2" ]; then
1672 error "mr: usage: hours_since action num"
1674 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1675 if [ -e "$MR_REPO/$dir" ]; then
1676 flagfile="$MR_REPO/$dir/.mr_last$1"
1680 if [ -z "$flagfile" ]; then
1681 error "cannot determine flag filename"
1683 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1684 if [ "$delta" -lt "$2" ]; then
1692 LANG=C bzr info | egrep -q '^Checkout'
1695 svn_test = test -d "$MR_REPO"/.svn
1696 git_test = test -d "$MR_REPO"/.git
1697 bzr_test = test -d "$MR_REPO"/.bzr
1698 cvs_test = test -d "$MR_REPO"/CVS
1699 hg_test = test -d "$MR_REPO"/.hg
1700 darcs_test = test -d "$MR_REPO"/_darcs
1701 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1703 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1704 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1705 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1707 svn_update = svn update "$@"
1708 git_update = git pull "$@"
1710 if is_bzr_checkout; then
1713 bzr merge --pull "$@"
1715 cvs_update = cvs update "$@"
1716 hg_update = hg pull "$@" && hg update "$@"
1717 darcs_update = darcs pull -a "$@"
1718 fossil_update = fossil pull "$@"
1720 svn_status = svn status "$@"
1721 git_status = git status -s "$@" || true
1722 bzr_status = bzr status --short "$@"
1723 cvs_status = cvs status "$@"
1724 hg_status = hg status "$@"
1725 darcs_status = darcs whatsnew -ls "$@" || true
1726 fossil_status = fossil changes "$@"
1728 svn_commit = svn commit "$@"
1729 git_commit = git commit -a "$@" && git push --all
1731 if is_bzr_checkout; then
1734 bzr commit "$@" && bzr push
1736 cvs_commit = cvs commit "$@"
1737 hg_commit = hg commit -m "$@" && hg push
1738 darcs_commit = darcs record -a -m "$@" && darcs push -a
1739 fossil_commit = fossil commit "$@"
1741 git_record = git commit -a "$@"
1742 bzr_record = bzr commit "$@"
1743 hg_record = hg commit -m "$@"
1744 darcs_record = darcs record -a -m "$@"
1745 fossil_record = fossil commit "$@"
1748 git_push = git push "$@"
1749 bzr_push = bzr push "$@"
1751 hg_push = hg push "$@"
1752 darcs_push = darcs push -a "$@"
1753 fossil_push = fossil push "$@"
1755 svn_diff = svn diff "$@"
1756 git_diff = git diff "$@"
1757 bzr_diff = bzr diff "$@"
1758 cvs_diff = cvs diff "$@"
1759 hg_diff = hg diff "$@"
1760 darcs_diff = darcs diff -u "$@"
1761 fossil_diff = fossil diff "$@"
1763 svn_log = svn log "$@"
1764 git_log = git log "$@"
1765 bzr_log = bzr log "$@"
1766 cvs_log = cvs log "$@"
1767 hg_log = hg log "$@"
1768 darcs_log = darcs changes "$@"
1769 git_bare_log = git log "$@"
1770 fossil_log = fossil timeline "$@"
1775 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1776 if [ -z "$url" ]; then
1777 error "cannot determine svn url"
1779 echo "Registering svn url: $url in $MR_CONFIG"
1780 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1782 url="`LC_ALL=C git config --get remote.origin.url`" || true
1783 if [ -z "$url" ]; then
1784 error "cannot determine git url"
1786 echo "Registering git url: $url in $MR_CONFIG"
1787 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1789 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1790 if [ -z "$url" ]; then
1791 error "cannot determine bzr url"
1793 echo "Registering bzr url: $url in $MR_CONFIG"
1794 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1796 repo=`cat CVS/Repository`
1798 if [ -z "$root" ]; then
1799 error "cannot determine cvs root"
1801 echo "Registering cvs repository $repo at root $root"
1802 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1804 url=`hg showconfig paths.default`
1805 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1806 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1808 url=`cat _darcs/prefs/defaultrepo`
1809 echo "Registering darcs repository $url in $MR_CONFIG"
1810 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1812 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1813 if [ -z "$url" ]; then
1814 error "cannot determine git url"
1816 echo "Registering git url: $url in $MR_CONFIG"
1817 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1819 url=`fossil remote-url`
1820 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1821 echo "Registering fossil repository $url in $MR_CONFIG"
1822 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1824 svn_trusted_checkout = svn co $url $repo
1825 svn_alt_trusted_checkout = svn checkout $url $repo
1826 git_trusted_checkout = git clone $url $repo
1827 bzr_trusted_checkout = bzr clone $url $repo
1829 hg_trusted_checkout = hg clone $url $repo
1830 darcs_trusted_checkout = darcs get $url $repo
1831 git_bare_trusted_checkout = git clone --bare $url $repo
1832 # fossil: messy to do
1838 SHOWMANFILE="man -f"
1844 SHOWMANFILE="man -l"
1847 if [ ! -e "$MR_PATH" ]; then
1848 error "cannot find program path"
1850 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1851 trap "rm -f $tmp" exit
1852 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1853 $SHOWMANFILE "$tmp" || error "man failed"
1859 if [ -s ~/.mrlog ]; then
1860 info "running offline commands"
1861 mv -f ~/.mrlog ~/.mrlog.old
1862 if ! sh -e ~/.mrlog.old; then
1863 error "offline command failed; left in ~/.mrlog.old"
1867 info "no offline commands to run"
1872 info "offline mode enabled"
1874 info "remembering command: 'mr $@'"
1875 command="mr -d '$(pwd)' $MR_SWITCHES"
1877 command="$command '$w'"
1879 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1880 echo "$command" >> ~/.mrlog
1883 ed = echo "A horse is a horse, of course, of course.."
1884 T = echo "I pity the fool."
1885 right = echo "Not found."
1887 # vim:sw=8:sts=0:ts=8:noet