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 The "lib" parameter can specify some shell code that will be run before each
383 command, this can be a useful way to define shell functions for other commands
388 If the "fixups" parameter is set, its command is run whenever a repository
389 is checked out, or updated. This provides an easy way to do things
390 like permissions fixups, or other tweaks to the repository content,
391 whenever the repository is changed.
395 If a "pre_action" parameter is set, its command is run before mr performs the
396 specified action. Similarly, "post_action" parameters are run after mr
397 successfully performs the specified action. For example, "pre_commit" is
398 run before committing; "post_update" is run after updating.
402 When looking for a command to run for a given action, mr first looks for
403 a parameter with the same name as the action. If that is not found, it
404 looks for a parameter named "rcs_action" (substituting in the name of the
405 revision control system and the action). The name of the revision control
406 system is itself determined by running each defined "rcs_test" action,
409 Internally, mr has settings for "git_update", "svn_update", etc. To change
410 the action that is performed for a given revision control system, you can
411 override these rcs specific actions. To add a new revision control system,
412 you can just add rcs specific actions for it.
414 =head1 UNTRUSTED MRCONFIG FILES
416 Since mrconfig files can contain arbitrary shell commands, they can do
417 anything. This flexibility is good, but it also allows a malicious mrconfig
418 file to delete your whole home directory. Such a file might be contained
419 inside a repository that your main ~/.mrconfig checks out. To
420 avoid worries about evil commands in a mrconfig file, mr defaults to
421 reading all mrconfig files other than the main ~/.mrconfig in untrusted
422 mode. In untrusted mode, mrconfig files are limited to running only known
423 safe commands (like "git clone") in a carefully checked manner.
425 To configure mr to trust other mrconfig files, list them in ~/.mrtrust.
426 One mrconfig file should be listed per line. Either the full pathname
427 should be listed, or the pathname can start with "~/" to specify a file
428 relative to your home directory.
430 =head1 OFFLINE LOG FILE
432 The ~/.mrlog file contains commands that mr has remembered to run later,
433 due to being offline. You can delete or edit this file to remove commands,
434 or even to add other commands for 'mr online' to run. If the file is
435 present, mr assumes it is in offline mode.
439 mr can be extended to support things such as unison and git-svn. Some
440 files providing such extensions are available in /usr/share/mr/. See
441 the documentation in the files for details about using them.
445 mr returns nonzero if a command failed in any of the repositories.
449 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
451 Licensed under the GNU GPL version 2 or higher.
453 http://kitenet.net/~joey/code/mr/
460 use Cwd qw(getcwd abs_path);
462 # things that can happen when mr runs a command
471 my $config_overridden=0;
481 my $directory=getcwd();
483 $ENV{MR_CONFIG}=find_mrconfig();
490 my (@ok, @failed, @skipped);
496 my ($action, $dir, $topdir, $subdir) = @_;
498 if (exists $rcs{$dir}) {
503 foreach my $rcs_test (
505 length $a <=> length $b
508 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
509 my ($rcs)=$rcs_test=~/(.*)_test/;
510 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
511 $test.="if my_$rcs_test; then echo $rcs; fi\n";
513 $test=$config{$topdir}{$subdir}{lib}."\n".$test
514 if exists $config{$topdir}{$subdir}{lib};
516 print "mr $action: running rcs test >>$test<<\n" if $verbose;
521 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
525 return $rcs{$dir}=undef;
528 return $rcs{$dir}=$rcs;
533 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
535 if (exists $config{$topdir}{$subdir}{$action}) {
536 return $config{$topdir}{$subdir}{$action};
543 my $rcs=rcs_test(@_);
546 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
547 return $config{$topdir}{$subdir}{$rcs."_".$action};
555 my ($topdir, $subdir) = @_;
556 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
560 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
561 my $fulldir=fulldir($topdir, $subdir);
563 $ENV{MR_CONFIG}=$configfiles{$topdir};
564 my $lib=exists $config{$topdir}{$subdir}{lib} ?
565 $config{$topdir}{$subdir}{lib}."\n" : "";
566 my $is_checkout=($action eq 'checkout');
567 my $is_update=($action =~ /update/);
572 if (! $force_checkout) {
574 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
578 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
583 return action("checkout", $dir, $topdir, $subdir);
587 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
588 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
590 if (defined $skiptest) {
591 my $test="set -e;".$lib.
592 "my_action(){ $skiptest\n }; my_action '$action'";
593 print "mr $action: running skip test >>$test<<\n" if $verbose;
594 my $ret=system($test);
596 if (($? & 127) == 2) {
597 print STDERR "mr $action: interrupted\n";
601 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
605 if ($ret >> 8 == 0) {
606 print "mr $action: $dir skipped per config file\n" if $verbose;
611 if ($is_checkout && ! -d $dir) {
612 print "mr $action: creating parent directory $dir\n" if $verbose;
613 system("mkdir", "-p", $dir);
616 if (! $no_chdir && ! chdir($dir)) {
617 print STDERR "mr $action: failed to chdir to $dir: $!\n";
620 elsif (! defined $command) {
621 my $rcs=rcs_test(@_);
622 if (! defined $rcs) {
623 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
627 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
634 $actionmsg="mr $action: $fulldir";
638 $s=~s/^\Q$fulldir\E\/?//;
639 $actionmsg="mr $action: $fulldir (in subdir $s)";
641 print "$actionmsg\n" unless $quiet;
643 my $hookret=hook("pre_$action", $topdir, $subdir);
644 return $hookret if $hookret != OK;
646 $command="set -e; ".$lib.
647 "my_action(){ $command\n }; my_action ".
648 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
649 print "mr $action: running >>$command<<\n" if $verbose;
652 my $output = qx/$command 2>&1/;
655 print "$actionmsg\n";
656 print STDERR $output;
660 $ret=system($command);
663 if (($? & 127) == 2) {
664 print STDERR "mr $action: interrupted\n";
668 print STDERR "mr $action: received signal ".($? & 127)."\n";
671 print STDERR "mr $action: failed ($ret)\n" if $verbose;
672 if ($ret >> 8 != 0) {
673 print STDERR "mr $action: command failed\n";
674 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
675 # recreate original command line to
676 # remember, and avoid recursing
678 @ARGV=('-n', $action, @orig);
679 action("remember", $dir, $topdir, $subdir);
684 print STDERR "mr $action: command died ($ret)\n";
689 if ($is_checkout && ! -d $dir) {
690 print STDERR "mr $action: $dir missing after checkout\n";;
694 my $ret=hook("post_$action", $topdir, $subdir);
695 return $ret if $ret != OK;
697 if (($is_checkout || $is_update)) {
698 my $ret=hook("fixups", $topdir, $subdir);
699 return $ret if $ret != OK;
708 my ($hook, $topdir, $subdir) = @_;
710 my $command=$config{$topdir}{$subdir}{$hook};
711 return OK unless defined $command;
712 my $lib=exists $config{$topdir}{$subdir}{lib} ?
713 $config{$topdir}{$subdir}{lib}."\n" : "";
714 my $shell="set -e;".$lib.
715 "my_hook(){ $command\n }; my_hook";
716 print "mr $hook: running >>$shell<<\n" if $verbose;
719 my $output = qx/$shell 2>&1/;
722 print STDERR $output;
729 if (($? & 127) == 2) {
730 print STDERR "mr $hook: interrupted\n";
734 print STDERR "mr $hook: received signal ".($? & 127)."\n";
742 # run actions on multiple repos, in parallel
752 while (@fhs or @repos) {
753 while ((!$jobs || $running < $jobs) && @repos) {
755 my $repo = shift @repos;
756 pipe(my $outfh, CHILD_STDOUT);
757 pipe(my $errfh, CHILD_STDERR);
759 unless ($pid = fork) {
760 die "mr $action: cannot fork: $!" unless defined $pid;
761 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
762 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
767 exit action($action, @$repo);
771 push @active, [$pid, $repo];
772 push @fhs, [$outfh, $errfh];
775 my ($rin, $rout) = ('','');
777 foreach my $fh (@fhs) {
778 next unless defined $fh;
779 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
780 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
782 $nfound = select($rout=$rin, undef, undef, 1);
783 foreach my $channel (0, 1) {
784 foreach my $i (0..$#fhs) {
785 next unless defined $fhs[$i];
786 my $fh = $fhs[$i][$channel];
787 next unless defined $fh;
788 if (vec($rout, fileno($fh), 1) == 1) {
790 if (sysread($fh, $r, 1024) == 0) {
792 $fhs[$i][$channel] = undef;
793 if (! defined $fhs[$i][0] &&
794 ! defined $fhs[$i][1]) {
795 waitpid($active[$i][0], 0);
796 print STDOUT $out[$i][0];
797 print STDERR $out[$i][1];
798 record($active[$i][1], $? >> 8);
800 splice(@active, $i, 1);
805 $out[$i][$channel] .= $r;
813 my $dir=shift()->[0];
818 print "\n" unless $quiet;
820 elsif ($ret == FAILED) {
822 chdir($dir) unless $no_chdir;
823 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
824 system((getpwuid($<))[8], "-i");
827 print "\n" unless $quiet;
829 elsif ($ret == SKIPPED) {
832 elsif ($ret == ABORT) {
836 die "unknown exit status $ret";
842 if (! @ok && ! @failed && ! @skipped) {
843 die "mr $action: no repositories found to work on\n";
845 print "mr $action: finished (".join("; ",
846 showstat($#ok+1, "ok", "ok"),
847 showstat($#failed+1, "failed", "failed"),
848 showstat($#skipped+1, "skipped", "skipped"),
849 ).")\n" unless $quiet;
852 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
855 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
865 return "$count ".($count > 1 ? $plural : $singular);
870 # an ordered list of repos
873 foreach my $topdir (sort keys %config) {
874 foreach my $subdir (sort keys %{$config{$topdir}}) {
878 order => $config{$topdir}{$subdir}{order},
883 $a->{order} <=> $b->{order}
885 $a->{topdir} cmp $b->{topdir}
887 $a->{subdir} cmp $b->{subdir}
893 my $topdir=$repo->{topdir};
894 my $subdir=$repo->{subdir};
895 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
900 # figure out which repos to act on
903 foreach my $repo (repolist()) {
904 my $topdir=$repo->{topdir};
905 my $subdir=$repo->{subdir};
907 next if $subdir eq 'DEFAULT';
908 my $dir=repodir($repo);
910 $dir.="/" unless $dir=~/\/$/;
911 $d.="/" unless $d=~/\/$/;
912 next if $dir ne $d && $dir !~ /^\Q$d\E/;
913 if (defined $max_depth) {
914 my @a=split('/', $dir);
915 my @b=split('/', $d);
916 do { } while (@a && @b && shift(@a) eq shift(@b));
917 next if @a > $max_depth || @b > $max_depth;
919 push @repos, [$dir, $topdir, $subdir];
922 # fallback to find a leaf repo
923 foreach my $repo (reverse repolist()) {
924 my $topdir=$repo->{topdir};
925 my $subdir=$repo->{subdir};
927 next if $subdir eq 'DEFAULT';
928 my $dir=repodir($repo);
930 $dir.="/" unless $dir=~/\/$/;
931 $d.="/" unless $d=~/\/$/;
932 if ($d=~/^\Q$dir\E/) {
933 push @repos, [$dir, $topdir, $subdir];
955 sub is_trusted_config {
956 my $config=shift; # must be abs_pathed already
958 # We always trust ~/.mrconfig.
959 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
961 return 1 if $trust_all;
963 my $trustfile=$ENV{HOME}."/.mrtrust";
966 $trusted{"$ENV{HOME}/.mrconfig"}=1;
967 if (open (TRUST, "<", $trustfile)) {
970 s/^~\//$ENV{HOME}\//;
971 $trusted{abs_path($_)}=1;
977 return $trusted{$config};
981 sub is_trusted_repo {
984 # Tightly limit what is allowed in a repo name.
985 # No ../, no absolute paths, and no unusual filenames
986 # that might try to escape to the shell.
987 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
988 $repo !~ /\.\./ && $repo !~ /^\//;
991 sub is_trusted_checkout {
994 # To determine if the command is safe, compare it with the
995 # *_trusted_checkout config settings. Those settings are
996 # templates for allowed commands, so make sure that each word
997 # of the command matches the corresponding word of the template.
1000 foreach my $word (split(' ', $command)) {
1002 if ($word=~/^'(.*)'$/) {
1005 elsif ($word=~/^"(.*)"$/) {
1012 foreach my $key (grep { /_trusted_checkout$/ }
1013 keys %{$config{''}{DEFAULT}}) {
1014 my @twords=split(' ', $config{''}{DEFAULT}{$key});
1015 next if @words > @twords;
1019 for (my $c=0; $c < @twords && $match; $c++) {
1020 if ($twords[$c] eq '$url') {
1021 # Match all the typical characters found in
1022 # urls, plus @ which svn can use. Note
1023 # that the "url" might also be a local
1026 defined $words[$c] &&
1027 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1031 elsif ($twords[$c] eq '$repo') {
1032 # If a repo is not specified, assume it
1033 # will be the last path component of the
1034 # url, or something derived from it, and
1036 if (! defined $words[$c] && defined $url) {
1037 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1041 defined $words[$c] &&
1042 is_trusted_repo($words[$c])
1045 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1060 "(To trust this file, list it in ~/.mrtrust.)\n";
1072 if (ref $f eq 'GLOB') {
1082 my $absf=abs_path($f);
1083 if ($loaded{$absf}) {
1088 $trusted=is_trusted_config($absf);
1090 if (! defined $dir) {
1091 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1092 if (! defined $dir) {
1097 $dir=abs_path($dir)."/";
1099 if (! exists $configfiles{$dir}) {
1100 $configfiles{$dir}=$f;
1103 # copy in defaults from first parent
1105 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1106 if ($parent eq '/') {
1109 if (exists $config{$parent} &&
1110 exists $config{$parent}{DEFAULT}) {
1111 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1116 print "mr: loading config $f\n" if $verbose;
1117 open($in, "<", $f) || die "mr: open $f: $!\n";
1120 close $in unless ref $f eq 'GLOB';
1128 next if /^\s*\#/ || /^\s*$/;
1129 if (/^\[([^\]]*)\]\s*$/) {
1133 if (! is_trusted_repo($section) ||
1134 $section eq 'ALIAS' ||
1135 $section eq 'DEFAULT') {
1136 trusterror "mr: illegal section \"[$section]\" in untrusted $f line $line";
1139 $section=expandenv($section) if $trusted;
1140 if ($section ne 'ALIAS' &&
1141 ! exists $config{$dir}{$section} &&
1142 exists $config{$dir}{DEFAULT}) {
1144 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1147 elsif (/^(\w+)\s*=\s*(.*)/) {
1152 while (@lines && $lines[0]=~/^\s(.+)/) {
1160 # Untrusted files can only contain checkout
1162 if ($parameter ne 'checkout') {
1163 trusterror "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line";
1165 if (! is_trusted_checkout($value)) {
1166 trusterror "mr: illegal checkout command \"$value\" in untrusted $f line $line";
1170 if ($parameter eq "include") {
1171 print "mr: including output of \"$value\"\n" if $verbose;
1172 unshift @lines, `$value`;
1174 print STDERR "mr: include command exited nonzero ($?)\n";
1179 if (! defined $section) {
1180 die "$f line $.: parameter ($parameter) not in section\n";
1182 if ($section eq 'ALIAS') {
1183 $alias{$parameter}=$value;
1185 elsif ($parameter eq 'lib') {
1186 $config{$dir}{$section}{lib}.=$value."\n";
1189 $config{$dir}{$section}{$parameter}=$value;
1190 if ($parameter =~ /.*_(.*)/) {
1191 $knownactions{$1}=1;
1194 $knownactions{$parameter}=1;
1196 if ($parameter eq 'chain' &&
1197 length $dir && $section ne "DEFAULT" &&
1198 -e $dir.$section."/.mrconfig") {
1199 my $ret=system($value);
1201 if (($? & 127) == 2) {
1202 print STDERR "mr: chain test interrupted\n";
1206 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1210 push @toload, $dir.$section."/.mrconfig";
1216 die "$f line $line: parse error\n";
1225 sub startingconfig {
1226 %alias=%config=%configfiles=%knownactions=%loaded=();
1227 my $datapos=tell(DATA);
1229 seek(DATA,$datapos,0); # rewind
1234 # the section to modify or add
1235 my $targetsection=shift;
1236 # fields to change in the section
1237 # To remove a field, set its value to "".
1238 my %changefields=@_;
1244 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1249 my $formatfield=sub {
1251 my @value=split(/\n/, shift);
1253 return "$field = ".shift(@value)."\n".
1254 join("", map { "\t$_\n" } @value);
1258 while ($out[$#out] =~ /^\s*$/) {
1259 unshift @blanks, pop @out;
1261 foreach my $field (sort keys %changefields) {
1262 if (length $changefields{$field}) {
1263 push @out, "$field = $changefields{$field}\n";
1264 delete $changefields{$field};
1274 if (/^\s*\#/ || /^\s*$/) {
1277 elsif (/^\[([^\]]*)\]\s*$/) {
1278 if (defined $section &&
1279 $section eq $targetsection) {
1283 $section=expandenv($1);
1287 elsif (/^(\w+)\s*=\s(.*)/) {
1292 while (@lines && $lines[0]=~/^\s(.+)/) {
1298 if ($section eq $targetsection) {
1299 if (exists $changefields{$parameter}) {
1300 if (length $changefields{$parameter}) {
1301 $value=$changefields{$parameter};
1303 delete $changefields{$parameter};
1307 push @out, $formatfield->($parameter, $value);
1311 if (defined $section &&
1312 $section eq $targetsection) {
1315 elsif (%changefields) {
1316 push @out, "\n[$targetsection]\n";
1317 foreach my $field (sort keys %changefields) {
1318 if (length $changefields{$field}) {
1319 push @out, $formatfield->($field, $changefields{$field});
1324 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1332 # actions that do not operate on all repos
1333 if ($action eq 'help') {
1336 elsif ($action eq 'config') {
1339 elsif ($action eq 'register') {
1342 elsif ($action eq 'bootstrap') {
1345 elsif ($action eq 'remember' ||
1346 $action eq 'offline' ||
1347 $action eq 'online') {
1348 my @repos=selectrepos;
1349 action($action, @{$repos[0]}) if @repos;
1353 if (!$jobs || $jobs > 1) {
1354 mrs($action, selectrepos());
1357 foreach my $repo (selectrepos()) {
1358 record($repo, action($action, @$repo));
1364 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1369 die "mr config: not enough parameters\n";
1372 if ($section=~/^\//) {
1373 # try to convert to a path relative to the config file
1374 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1375 $dir=abs_path($dir);
1376 $dir.="/" unless $dir=~/\/$/;
1377 if ($section=~/^\Q$dir\E(.*)/) {
1383 if (/^([^=]+)=(.*)$/) {
1384 $changefields{$1}=$2;
1388 foreach my $topdir (sort keys %config) {
1389 if (exists $config{$topdir}{$section} &&
1390 exists $config{$topdir}{$section}{$_}) {
1391 print $config{$topdir}{$section}{$_}."\n";
1393 last if $section eq 'DEFAULT';
1397 die "mr config: $section $_ not set\n";
1401 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1406 if ($config_overridden) {
1407 # Find the directory that the specified config file is
1409 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1412 # Find the closest known mrconfig file to the current
1414 $directory.="/" unless $directory=~/\/$/;
1416 foreach my $topdir (reverse sort keys %config) {
1417 next unless length $topdir;
1418 if ($directory=~/^\Q$topdir\E/) {
1419 $ENV{MR_CONFIG}=$configfiles{$topdir};
1425 if (! $foundconfig) {
1426 $directory=""; # no config file, use builtin
1430 my $subdir=shift @ARGV;
1431 if (! chdir($subdir)) {
1432 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1436 $ENV{MR_REPO}=getcwd();
1437 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1438 if (! defined $command) {
1439 die "mr register: unknown repository type\n";
1442 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1443 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1444 "my_action(){ $command\n }; my_action ".
1445 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1446 print "mr register: running >>$command<<\n" if $verbose;
1447 exec($command) || die "exec: $!";
1451 my $url=shift @ARGV;
1452 my $dir=shift @ARGV || ".";
1454 if (! defined $url || ! length $url) {
1455 die "mr: bootstrap requires url\n";
1458 # Download the config file to a temporary location.
1459 eval q{use File::Temp};
1461 my $tmpconfig=File::Temp->new();
1462 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1463 push(@curlargs, "-k") if $insecure;
1464 my $curlstatus = system(@curlargs);
1465 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1466 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1469 system("mkdir", "-p", $dir);
1471 chdir($dir) || die "chdir $dir: $!";
1473 # Special case to handle checkout of the "." repo, which
1474 # would normally be skipped.
1475 my $topdir=abs_path(".")."/";
1476 my @repo=($topdir, $topdir, ".");
1477 loadconfig($tmpconfig, $topdir);
1478 record(\@repo, action("checkout", @repo, 1))
1479 if exists $config{$topdir}{"."}{"checkout"};
1481 if (-e ".mrconfig") {
1482 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1485 eval q{use File::Copy};
1487 move($tmpconfig, ".mrconfig") || die "rename: $!";
1490 # Reload the config file (in case we got a different version)
1491 # and checkout everything else.
1493 loadconfig(".mrconfig");
1494 dispatch("checkout");
1495 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1496 showstats("bootstrap");
1500 # alias expansion and command stemming
1503 if (exists $alias{$action}) {
1504 $action=$alias{$action};
1506 if (! exists $knownactions{$action}) {
1507 my @matches = grep { /^\Q$action\E/ }
1508 keys %knownactions, keys %alias;
1509 if (@matches == 1) {
1510 $action=$matches[0];
1512 elsif (@matches == 0) {
1513 die "mr: unknown action \"$action\" (known actions: ".
1514 join(", ", sort keys %knownactions).")\n";
1517 die "mr: ambiguous action \"$action\" (matches: ".
1518 join(", ", @matches).")\n";
1526 while (length $dir) {
1527 if (-e "$dir/.mrconfig") {
1528 return "$dir/.mrconfig";
1530 $dir=~s/\/[^\/]*$//;
1532 return "$ENV{HOME}/.mrconfig";
1537 Getopt::Long::Configure("bundling", "no_permute");
1538 my $result=GetOptions(
1539 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1540 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1541 "p|path" => sub { }, # now default, ignore
1542 "v|verbose" => \$verbose,
1543 "q|quiet" => \$quiet,
1544 "s|stats" => \$stats,
1545 "k|insecure" => \$insecure,
1546 "i|interactive" => \$interactive,
1547 "n|no-recurse:i" => \$max_depth,
1548 "j|jobs:i" => \$jobs,
1549 "t|trust-all" => \$trust_all,
1551 if (! $result || @ARGV < 1) {
1552 die("Usage: mr [options] action [params ...]\n".
1553 "(Use mr help for man page.)\n");
1556 $ENV{MR_SWITCHES}="";
1557 foreach my $option (@saved) {
1558 last if $option eq $ARGV[0];
1559 $ENV{MR_SWITCHES}.="$option ";
1565 print STDERR "mr: interrupted\n";
1569 # This can happen if it's run in a directory that was removed
1570 # or other strangeness.
1571 if (! defined $directory) {
1572 die("mr: failed to determine working directory\n");
1574 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1575 # the config file might be a symlink to elsewhere, and the directory it's
1576 # in is significant.
1577 if ($ENV{MR_CONFIG} !~ /^\//) {
1578 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1580 # Try to set MR_PATH to the path to the program.
1582 use FindBin qw($Bin $Script);
1583 $ENV{MR_PATH}=$Bin."/".$Script;
1601 loadconfig("$ENV{HOME}/.mrconfig");
1602 loadconfig($ENV{MR_CONFIG});
1603 #use Data::Dumper; print Dumper(\%config);
1605 my $action=expandaction(shift @ARGV);
1612 # Finally, some useful actions that mr knows about by default.
1613 # These can be overridden in ~/.mrconfig.
1628 echo "mr (warning): $@" >&2
1634 if [ -z "$1" ] || [ -z "$2" ]; then
1635 error "mr: usage: hours_since action num"
1637 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1638 if [ -e "$MR_REPO/$dir" ]; then
1639 flagfile="$MR_REPO/$dir/.mr_last$1"
1643 if [ -z "$flagfile" ]; then
1644 error "cannot determine flag filename"
1646 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1647 if [ "$delta" -lt "$2" ]; then
1655 svn_test = test -d "$MR_REPO"/.svn
1656 git_test = test -d "$MR_REPO"/.git
1657 bzr_test = test -d "$MR_REPO"/.bzr
1658 cvs_test = test -d "$MR_REPO"/CVS
1659 hg_test = test -d "$MR_REPO"/.hg
1660 darcs_test = test -d "$MR_REPO"/_darcs
1661 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1663 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1664 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1665 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1667 svn_update = svn update "$@"
1668 git_update = git pull "$@"
1669 bzr_update = bzr merge --pull "$@"
1670 cvs_update = cvs update "$@"
1671 hg_update = hg pull "$@" && hg update "$@"
1672 darcs_update = darcs pull -a "$@"
1673 fossil_update = fossil pull "$@"
1675 svn_status = svn status "$@"
1676 git_status = git status -s "$@" || true
1677 bzr_status = bzr status --short "$@"
1678 cvs_status = cvs status "$@"
1679 hg_status = hg status "$@"
1680 darcs_status = darcs whatsnew -ls "$@" || true
1681 fossil_status = fossil changes "$@"
1683 svn_commit = svn commit "$@"
1684 git_commit = git commit -a "$@" && git push --all
1685 bzr_commit = bzr commit "$@" && bzr push
1686 cvs_commit = cvs commit "$@"
1687 hg_commit = hg commit -m "$@" && hg push
1688 darcs_commit = darcs record -a -m "$@" && darcs push -a
1689 fossil_commit = fossil commit "$@"
1691 git_record = git commit -a "$@"
1692 bzr_record = bzr commit "$@"
1693 hg_record = hg commit -m "$@"
1694 darcs_record = darcs record -a -m "$@"
1695 fossil_record = fossil commit "$@"
1698 git_push = git push "$@"
1699 bzr_push = bzr push "$@"
1701 hg_push = hg push "$@"
1702 darcs_push = darcs push -a "$@"
1703 fossil_push = fossil push "$@"
1705 svn_diff = svn diff "$@"
1706 git_diff = git diff "$@"
1707 bzr_diff = bzr diff "$@"
1708 cvs_diff = cvs diff "$@"
1709 hg_diff = hg diff "$@"
1710 darcs_diff = darcs diff -u "$@"
1711 fossil_diff = fossil diff "$@"
1713 svn_log = svn log "$@"
1714 git_log = git log "$@"
1715 bzr_log = bzr log "$@"
1716 cvs_log = cvs log "$@"
1717 hg_log = hg log "$@"
1718 darcs_log = darcs changes "$@"
1719 git_bare_log = git log "$@"
1720 fossil_log = fossil timeline "$@"
1725 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1726 if [ -z "$url" ]; then
1727 error "cannot determine svn url"
1729 echo "Registering svn url: $url in $MR_CONFIG"
1730 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1732 url="`LC_ALL=C git config --get remote.origin.url`" || true
1733 if [ -z "$url" ]; then
1734 error "cannot determine git url"
1736 echo "Registering git url: $url in $MR_CONFIG"
1737 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1739 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1740 if [ -z "$url" ]; then
1741 error "cannot determine bzr url"
1743 echo "Registering bzr url: $url in $MR_CONFIG"
1744 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1746 repo=`cat CVS/Repository`
1748 if [ -z "$root" ]; then
1749 error "cannot determine cvs root"
1751 echo "Registering cvs repository $repo at root $root"
1752 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1754 url=`hg showconfig paths.default`
1755 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1756 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1758 url=`cat _darcs/prefs/defaultrepo`
1759 echo "Registering darcs repository $url in $MR_CONFIG"
1760 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1762 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1763 if [ -z "$url" ]; then
1764 error "cannot determine git url"
1766 echo "Registering git url: $url in $MR_CONFIG"
1767 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1769 url=`fossil remote-url`
1770 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1771 echo "Registering fossil repository $url in $MR_CONFIG"
1772 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1774 svn_trusted_checkout = svn co $url $repo
1775 svn_alt_trusted_checkout = svn checkout $url $repo
1776 git_trusted_checkout = git clone $url $repo
1777 bzr_trusted_checkout = bzr clone $url $repo
1779 hg_trusted_checkout = hg clone $url $repo
1780 darcs_trusted_checkout = darcs get $url $repo
1781 git_bare_trusted_checkout = git clone --bare $url $repo
1782 # fossil: messy to do
1788 SHOWMANFILE="man -f"
1794 SHOWMANFILE="man -l"
1797 if [ ! -e "$MR_PATH" ]; then
1798 error "cannot find program path"
1800 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1801 trap "rm -f $tmp" exit
1802 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1803 $SHOWMANFILE "$tmp" || error "man failed"
1809 if [ -s ~/.mrlog ]; then
1810 info "running offline commands"
1811 mv -f ~/.mrlog ~/.mrlog.old
1812 if ! sh -e ~/.mrlog.old; then
1813 error "offline command failed; left in ~/.mrlog.old"
1817 info "no offline commands to run"
1822 info "offline mode enabled"
1824 info "remembering command: 'mr $@'"
1825 command="mr -d '$(pwd)' $MR_SWITCHES"
1827 command="$command '$w'"
1829 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1830 echo "$command" >> ~/.mrlog
1833 ed = echo "A horse is a horse, of course, of course.."
1834 T = echo "I pity the fool."
1835 right = echo "Not found."
1837 # vim:sw=8:sts=0:ts=8:noet