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.
231 Accept untrusted SSL certificates when bootstrapping.
237 Expand the statistics line displayed at the end to include information
238 about exactly which repositories failed and were skipped, if any.
244 Interactive mode. If a repository fails to be processed, a subshell will be
245 started which you can use to resolve or investigate the problem. Exit the
246 subshell to continue the mr run.
250 =item --no-recurse [number]
252 If no number if specified, just operate on the repository for the current
253 directory, do not recurse into deeper repositories.
255 If a number is specified, will recurse into repositories at most that many
256 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
257 but not ./src/packages/bar.
261 =item --jobs [number]
263 Run the specified number of jobs in parallel, or an unlimited number of jobs
264 with no number specified. This can greatly speed up operations such as updates.
265 It is not recommended for interactive operations.
267 Note that running more than 10 jobs at a time is likely to run afoul of
268 ssh connection limits. Running between 3 and 5 jobs at a time will yield
269 a good speedup in updates without loading the machine too much.
275 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
282 This obsolete flag is ignored.
286 =head1 MRCONFIG FILES
288 Here is an example .mrconfig file:
291 checkout = svn co svn://svn.example.com/src/trunk src
295 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
297 git checkout -b mybranch origin/master
299 The .mrconfig file uses a variant of the INI file format. Lines starting with
300 "#" are comments. Values can be continued to the following line by
301 indenting the line with whitespace.
303 The "DEFAULT" section allows setting default values for the sections that
306 The "ALIAS" section allows adding aliases for actions. Each parameter
307 is an alias, and its value is the action to use.
309 All other sections add repositories. The section header specifies the
310 directory where the repository is located. This is relative to the directory
311 that contains the mrconfig file, but you can also choose to use absolute
312 paths. (Note that you can use environment variables in section names; they
313 will be passed through the shell for expansion. For example,
314 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
316 Within a section, each parameter defines a shell command to run to handle a
317 given action. mr contains default handlers for "update", "status",
318 "commit", and other standard actions. Normally you only need to specify what
319 to do for "checkout".
321 Note that these shell commands are run in a "set -e" shell
322 environment, where any additional parameters you pass are available in
323 "$@". The "checkout" command is run in the parent of the repository
324 directory, since the repository isn't checked out yet. All other commands
325 are run inside the repository, though not necessarily at the top of it.
327 The "MR_REPO" environment variable is set to the path to the top of the
328 repository. (For the "register" action, "MR_REPO" is instead set to the
329 basename of the directory that should be created when checking the
332 The "MR_CONFIG" environment variable is set to the .mrconfig file
333 that defines the repo being acted on, or, if the repo is not yet in a config
334 file, the .mrconfig file that should be modified to register the repo.
336 A few parameters have special meanings:
342 If the "skip" parameter is set and its command returns true, then B<mr>
343 will skip acting on that repository. The command is passed the action
346 Here are two examples. The first skips the repo unless
347 mr is run by joey. The second uses the hours_since function
348 (included in mr's built-in library) to skip updating the repo unless it's
349 been at least 12 hours since the last update.
351 skip = test `whoami` != joey
352 skip = [ "$1" = update ] && ! hours_since "$1" 12
356 The "order" parameter can be used to override the default ordering of
357 repositories. The default order value is 10. Use smaller values to make
358 repositories be processed earlier, and larger values to make repositories
361 Note that if a repository is located in a subdirectory of another
362 repository, ordering it to be processed earlier is not recommended.
366 If the "chain" parameter is set and its command returns true, then B<mr>
367 will try to load a .mrconfig file from the root of the repository.
371 If the "include" parameter is set, its command is ran, and should output
372 additional mrconfig file content. The content is included as if it were
373 part of the including file.
375 Unlike all other parameters, this parameter does not need to be placed
380 The "lib" parameter can specify some shell code that will be run before each
381 command, this can be a useful way to define shell functions for other commands
386 If the "fixups" parameter is set, its command is run whenever a repository
387 is checked out, or updated. This provides an easy way to do things
388 like permissions fixups, or other tweaks to the repository content,
389 whenever the repository is changed.
393 If a "pre_action" parameter is set, its command is run before mr performs the
394 specified action. Similarly, "post_action" parameters are run after mr
395 successfully performs the specified action. For example, "pre_commit" is
396 run before committing; "post_update" is run after updating.
400 When looking for a command to run for a given action, mr first looks for
401 a parameter with the same name as the action. If that is not found, it
402 looks for a parameter named "rcs_action" (substituting in the name of the
403 revision control system and the action). The name of the revision control
404 system is itself determined by running each defined "rcs_test" action,
407 Internally, mr has settings for "git_update", "svn_update", etc. To change
408 the action that is performed for a given revision control system, you can
409 override these rcs specific actions. To add a new revision control system,
410 you can just add rcs specific actions for it.
412 =head1 UNTRUSTED MRCONFIG FILES
414 Since mrconfig files can contain arbitrary shell commands, they can do
415 anything. This flexibility is good, but it also allows a malicious mrconfig
416 file to delete your whole home directory. Such a file might be contained
417 inside a repository that your main ~/.mrconfig checks out. To
418 avoid worries about evil commands in a mrconfig file, mr defaults to
419 reading all mrconfig files other than the main ~/.mrconfig in untrusted
420 mode. In untrusted mode, mrconfig files are limited to running only known
421 safe commands (like "git clone") in a carefully checked manner.
423 To configure mr to trust other mrconfig files, list them in ~/.mrtrust.
424 One mrconfig file should be listed per line. Either the full pathname
425 should be listed, or the pathname can start with "~/" to specify a file
426 relative to your home directory.
428 =head1 OFFLINE LOG FILE
430 The ~/.mrlog file contains commands that mr has remembered to run later,
431 due to being offline. You can delete or edit this file to remove commands,
432 or even to add other commands for 'mr online' to run. If the file is
433 present, mr assumes it is in offline mode.
437 mr can be extended to support things such as unison and git-svn. Some
438 files providing such extensions are available in /usr/share/mr/. See
439 the documentation in the files for details about using them.
443 mr returns nonzero if a command failed in any of the repositories.
447 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
449 Licensed under the GNU GPL version 2 or higher.
451 http://kitenet.net/~joey/code/mr/
458 use Cwd qw(getcwd abs_path);
460 # things that can happen when mr runs a command
469 my $config_overridden=0;
479 my $directory=getcwd();
481 $ENV{MR_CONFIG}=find_mrconfig();
488 my (@ok, @failed, @skipped);
494 my ($action, $dir, $topdir, $subdir) = @_;
496 if (exists $rcs{$dir}) {
501 foreach my $rcs_test (
503 length $a <=> length $b
506 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
507 my ($rcs)=$rcs_test=~/(.*)_test/;
508 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
509 $test.="if my_$rcs_test; then echo $rcs; fi\n";
511 $test=$config{$topdir}{$subdir}{lib}."\n".$test
512 if exists $config{$topdir}{$subdir}{lib};
514 print "mr $action: running rcs test >>$test<<\n" if $verbose;
519 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
523 return $rcs{$dir}=undef;
526 return $rcs{$dir}=$rcs;
531 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
533 if (exists $config{$topdir}{$subdir}{$action}) {
534 return $config{$topdir}{$subdir}{$action};
541 my $rcs=rcs_test(@_);
544 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
545 return $config{$topdir}{$subdir}{$rcs."_".$action};
553 my ($topdir, $subdir) = @_;
554 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
558 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
559 my $fulldir=fulldir($topdir, $subdir);
561 $ENV{MR_CONFIG}=$configfiles{$topdir};
562 my $lib=exists $config{$topdir}{$subdir}{lib} ?
563 $config{$topdir}{$subdir}{lib}."\n" : "";
564 my $is_checkout=($action eq 'checkout');
565 my $is_update=($action =~ /update/);
570 if (! $force_checkout) {
572 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
576 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
581 return action("checkout", $dir, $topdir, $subdir);
585 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
586 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
588 if (defined $skiptest) {
589 my $test="set -e;".$lib.
590 "my_action(){ $skiptest\n }; my_action '$action'";
591 print "mr $action: running skip test >>$test<<\n" if $verbose;
592 my $ret=system($test);
594 if (($? & 127) == 2) {
595 print STDERR "mr $action: interrupted\n";
599 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
603 if ($ret >> 8 == 0) {
604 print "mr $action: $dir skipped per config file\n" if $verbose;
609 if ($is_checkout && ! -d $dir) {
610 print "mr $action: creating parent directory $dir\n" if $verbose;
611 system("mkdir", "-p", $dir);
614 if (! $no_chdir && ! chdir($dir)) {
615 print STDERR "mr $action: failed to chdir to $dir: $!\n";
618 elsif (! defined $command) {
619 my $rcs=rcs_test(@_);
620 if (! defined $rcs) {
621 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
625 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
631 print "mr $action: $fulldir\n" unless $quiet;
635 $s=~s/^\Q$fulldir\E\/?//;
636 print "mr $action: $fulldir (in subdir $s)\n" unless $quiet;
639 my $hookret=hook("pre_$action", $topdir, $subdir);
640 return $hookret if $hookret != OK;
642 $command="set -e; ".$lib.
643 "my_action(){ $command\n }; my_action ".
644 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
645 print "mr $action: running >>$command<<\n" if $verbose;
646 my $ret=system($command);
648 if (($? & 127) == 2) {
649 print STDERR "mr $action: interrupted\n";
653 print STDERR "mr $action: received signal ".($? & 127)."\n";
656 print STDERR "mr $action: failed ($ret)\n" if $verbose;
657 if ($ret >> 8 != 0) {
658 print STDERR "mr $action: command failed\n";
659 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
660 # recreate original command line to
661 # remember, and avoid recursing
663 @ARGV=('-n', $action, @orig);
664 action("remember", $dir, $topdir, $subdir);
669 print STDERR "mr $action: command died ($ret)\n";
674 if ($is_checkout && ! -d $dir) {
675 print STDERR "mr $action: $dir missing after checkout\n";;
679 my $ret=hook("post_$action", $topdir, $subdir);
680 return $ret if $ret != OK;
682 if (($is_checkout || $is_update)) {
683 my $ret=hook("fixups", $topdir, $subdir);
684 return $ret if $ret != OK;
693 my ($hook, $topdir, $subdir) = @_;
695 my $command=$config{$topdir}{$subdir}{$hook};
696 return OK unless defined $command;
697 my $lib=exists $config{$topdir}{$subdir}{lib} ?
698 $config{$topdir}{$subdir}{lib}."\n" : "";
699 my $shell="set -e;".$lib.
700 "my_hook(){ $command\n }; my_hook";
701 print "mr $hook: running >>$shell<<\n" if $verbose;
702 my $ret=system($shell);
704 if (($? & 127) == 2) {
705 print STDERR "mr $hook: interrupted\n";
709 print STDERR "mr $hook: received signal ".($? & 127)."\n";
717 # run actions on multiple repos, in parallel
727 while (@fhs or @repos) {
728 while ((!$jobs || $running < $jobs) && @repos) {
730 my $repo = shift @repos;
731 pipe(my $outfh, CHILD_STDOUT);
732 pipe(my $errfh, CHILD_STDERR);
734 unless ($pid = fork) {
735 die "mr $action: cannot fork: $!" unless defined $pid;
736 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
737 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
742 exit action($action, @$repo);
746 push @active, [$pid, $repo];
747 push @fhs, [$outfh, $errfh];
750 my ($rin, $rout) = ('','');
752 foreach my $fh (@fhs) {
753 next unless defined $fh;
754 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
755 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
757 $nfound = select($rout=$rin, undef, undef, 1);
758 foreach my $channel (0, 1) {
759 foreach my $i (0..$#fhs) {
760 next unless defined $fhs[$i];
761 my $fh = $fhs[$i][$channel];
762 next unless defined $fh;
763 if (vec($rout, fileno($fh), 1) == 1) {
765 if (sysread($fh, $r, 1024) == 0) {
767 $fhs[$i][$channel] = undef;
768 if (! defined $fhs[$i][0] &&
769 ! defined $fhs[$i][1]) {
770 waitpid($active[$i][0], 0);
771 print STDOUT $out[$i][0];
772 print STDERR $out[$i][1];
773 record($active[$i][1], $? >> 8);
775 splice(@active, $i, 1);
780 $out[$i][$channel] .= $r;
788 my $dir=shift()->[0];
793 print "\n" unless $quiet;
795 elsif ($ret == FAILED) {
797 chdir($dir) unless $no_chdir;
798 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
799 system((getpwuid($<))[8], "-i");
802 print "\n" unless $quiet;
804 elsif ($ret == SKIPPED) {
807 elsif ($ret == ABORT) {
811 die "unknown exit status $ret";
817 if (! @ok && ! @failed && ! @skipped) {
818 die "mr $action: no repositories found to work on\n";
820 print "mr $action: finished (".join("; ",
821 showstat($#ok+1, "ok", "ok"),
822 showstat($#failed+1, "failed", "failed"),
823 showstat($#skipped+1, "skipped", "skipped"),
824 ).")\n" unless $quiet;
827 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
830 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
840 return "$count ".($count > 1 ? $plural : $singular);
845 # an ordered list of repos
848 foreach my $topdir (sort keys %config) {
849 foreach my $subdir (sort keys %{$config{$topdir}}) {
853 order => $config{$topdir}{$subdir}{order},
858 $a->{order} <=> $b->{order}
860 $a->{topdir} cmp $b->{topdir}
862 $a->{subdir} cmp $b->{subdir}
868 my $topdir=$repo->{topdir};
869 my $subdir=$repo->{subdir};
870 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
875 # figure out which repos to act on
878 foreach my $repo (repolist()) {
879 my $topdir=$repo->{topdir};
880 my $subdir=$repo->{subdir};
882 next if $subdir eq 'DEFAULT';
883 my $dir=repodir($repo);
885 $dir.="/" unless $dir=~/\/$/;
886 $d.="/" unless $d=~/\/$/;
887 next if $dir ne $d && $dir !~ /^\Q$d\E/;
888 if (defined $max_depth) {
889 my @a=split('/', $dir);
890 my @b=split('/', $d);
891 do { } while (@a && @b && shift(@a) eq shift(@b));
892 next if @a > $max_depth || @b > $max_depth;
894 push @repos, [$dir, $topdir, $subdir];
897 # fallback to find a leaf repo
898 foreach my $repo (reverse repolist()) {
899 my $topdir=$repo->{topdir};
900 my $subdir=$repo->{subdir};
902 next if $subdir eq 'DEFAULT';
903 my $dir=repodir($repo);
905 $dir.="/" unless $dir=~/\/$/;
906 $d.="/" unless $d=~/\/$/;
907 if ($d=~/^\Q$dir\E/) {
908 push @repos, [$dir, $topdir, $subdir];
930 sub is_trusted_config {
931 my $config=shift; # must be abs_pathed already
933 # We always trust ~/.mrconfig.
934 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
936 return 1 if $trust_all;
938 my $trustfile=$ENV{HOME}."/.mrtrust";
941 $trusted{"$ENV{HOME}/.mrconfig"}=1;
942 if (open (TRUST, "<", $trustfile)) {
945 s/^~\//$ENV{HOME}\//;
946 $trusted{abs_path($_)}=1;
952 return $trusted{$config};
956 sub is_trusted_repo {
959 # Tightly limit what is allowed in a repo name.
960 # No ../, no absolute paths, and no unusual filenames
961 # that might try to escape to the shell.
962 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
963 $repo !~ /\.\./ && $repo !~ /^\//;
966 sub is_trusted_checkout {
969 # To determine if the command is safe, compare it with the
970 # *_trusted_checkout config settings. Those settings are
971 # templates for allowed commands, so make sure that each word
972 # of the command matches the corresponding word of the template.
975 foreach my $word (split(' ', $command)) {
977 if ($word=~/^'(.*)'$/) {
980 elsif ($word=~/^"(.*)"$/) {
987 foreach my $key (grep { /_trusted_checkout$/ }
988 keys %{$config{''}{DEFAULT}}) {
989 my @twords=split(' ', $config{''}{DEFAULT}{$key});
990 next if @words > @twords;
994 for (my $c=0; $c < @twords && $match; $c++) {
995 if ($twords[$c] eq '$url') {
996 # Match all the typical characters found in
997 # urls, plus @ which svn can use. Note
998 # that the "url" might also be a local
1001 defined $words[$c] &&
1002 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1006 elsif ($twords[$c] eq '$repo') {
1007 # If a repo is not specified, assume it
1008 # will be the last path component of the
1009 # url, or something derived from it, and
1011 if (! defined $words[$c] && defined $url) {
1012 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1016 defined $words[$c] &&
1017 is_trusted_repo($words[$c])
1020 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1035 "(To trust this file, list it in ~/.mrtrust.)\n";
1047 if (ref $f eq 'GLOB') {
1057 my $absf=abs_path($f);
1058 if ($loaded{$absf}) {
1063 $trusted=is_trusted_config($absf);
1065 if (! defined $dir) {
1066 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1067 if (! defined $dir) {
1072 $dir=abs_path($dir)."/";
1074 if (! exists $configfiles{$dir}) {
1075 $configfiles{$dir}=$f;
1078 # copy in defaults from first parent
1080 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1081 if ($parent eq '/') {
1084 if (exists $config{$parent} &&
1085 exists $config{$parent}{DEFAULT}) {
1086 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1091 print "mr: loading config $f\n" if $verbose;
1092 open($in, "<", $f) || die "mr: open $f: $!\n";
1095 close $in unless ref $f eq 'GLOB';
1103 next if /^\s*\#/ || /^\s*$/;
1104 if (/^\[([^\]]*)\]\s*$/) {
1108 if (! is_trusted_repo($section) ||
1109 $section eq 'ALIAS' ||
1110 $section eq 'DEFAULT') {
1111 trusterror "mr: illegal section \"[$section]\" in untrusted $f line $line";
1114 $section=expandenv($section) if $trusted;
1115 if ($section ne 'ALIAS' &&
1116 ! exists $config{$dir}{$section} &&
1117 exists $config{$dir}{DEFAULT}) {
1119 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1122 elsif (/^(\w+)\s*=\s*(.*)/) {
1127 while (@lines && $lines[0]=~/^\s(.+)/) {
1135 # Untrusted files can only contain checkout
1137 if ($parameter ne 'checkout') {
1138 trusterror "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line";
1140 if (! is_trusted_checkout($value)) {
1141 trusterror "mr: illegal checkout command \"$value\" in untrusted $f line $line";
1145 if ($parameter eq "include") {
1146 print "mr: including output of \"$value\"\n" if $verbose;
1147 unshift @lines, `$value`;
1149 print STDERR "mr: include command exited nonzero ($?)\n";
1154 if (! defined $section) {
1155 die "$f line $.: parameter ($parameter) not in section\n";
1157 if ($section eq 'ALIAS') {
1158 $alias{$parameter}=$value;
1160 elsif ($parameter eq 'lib') {
1161 $config{$dir}{$section}{lib}.=$value."\n";
1164 $config{$dir}{$section}{$parameter}=$value;
1165 if ($parameter =~ /.*_(.*)/) {
1166 $knownactions{$1}=1;
1169 $knownactions{$parameter}=1;
1171 if ($parameter eq 'chain' &&
1172 length $dir && $section ne "DEFAULT" &&
1173 -e $dir.$section."/.mrconfig") {
1174 my $ret=system($value);
1176 if (($? & 127) == 2) {
1177 print STDERR "mr: chain test interrupted\n";
1181 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1185 push @toload, $dir.$section."/.mrconfig";
1191 die "$f line $line: parse error\n";
1200 sub startingconfig {
1201 %alias=%config=%configfiles=%knownactions=%loaded=();
1202 my $datapos=tell(DATA);
1204 seek(DATA,$datapos,0); # rewind
1209 # the section to modify or add
1210 my $targetsection=shift;
1211 # fields to change in the section
1212 # To remove a field, set its value to "".
1213 my %changefields=@_;
1219 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1224 my $formatfield=sub {
1226 my @value=split(/\n/, shift);
1228 return "$field = ".shift(@value)."\n".
1229 join("", map { "\t$_\n" } @value);
1233 while ($out[$#out] =~ /^\s*$/) {
1234 unshift @blanks, pop @out;
1236 foreach my $field (sort keys %changefields) {
1237 if (length $changefields{$field}) {
1238 push @out, "$field = $changefields{$field}\n";
1239 delete $changefields{$field};
1249 if (/^\s*\#/ || /^\s*$/) {
1252 elsif (/^\[([^\]]*)\]\s*$/) {
1253 if (defined $section &&
1254 $section eq $targetsection) {
1258 $section=expandenv($1);
1262 elsif (/^(\w+)\s*=\s(.*)/) {
1267 while (@lines && $lines[0]=~/^\s(.+)/) {
1273 if ($section eq $targetsection) {
1274 if (exists $changefields{$parameter}) {
1275 if (length $changefields{$parameter}) {
1276 $value=$changefields{$parameter};
1278 delete $changefields{$parameter};
1282 push @out, $formatfield->($parameter, $value);
1286 if (defined $section &&
1287 $section eq $targetsection) {
1290 elsif (%changefields) {
1291 push @out, "\n[$targetsection]\n";
1292 foreach my $field (sort keys %changefields) {
1293 if (length $changefields{$field}) {
1294 push @out, $formatfield->($field, $changefields{$field});
1299 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1307 # actions that do not operate on all repos
1308 if ($action eq 'help') {
1311 elsif ($action eq 'config') {
1314 elsif ($action eq 'register') {
1317 elsif ($action eq 'bootstrap') {
1320 elsif ($action eq 'remember' ||
1321 $action eq 'offline' ||
1322 $action eq 'online') {
1323 my @repos=selectrepos;
1324 action($action, @{$repos[0]}) if @repos;
1328 if (!$jobs || $jobs > 1) {
1329 mrs($action, selectrepos());
1332 foreach my $repo (selectrepos()) {
1333 record($repo, action($action, @$repo));
1339 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1344 die "mr config: not enough parameters\n";
1347 if ($section=~/^\//) {
1348 # try to convert to a path relative to the config file
1349 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1350 $dir=abs_path($dir);
1351 $dir.="/" unless $dir=~/\/$/;
1352 if ($section=~/^\Q$dir\E(.*)/) {
1358 if (/^([^=]+)=(.*)$/) {
1359 $changefields{$1}=$2;
1363 foreach my $topdir (sort keys %config) {
1364 if (exists $config{$topdir}{$section} &&
1365 exists $config{$topdir}{$section}{$_}) {
1366 print $config{$topdir}{$section}{$_}."\n";
1368 last if $section eq 'DEFAULT';
1372 die "mr config: $section $_ not set\n";
1376 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1381 if ($config_overridden) {
1382 # Find the directory that the specified config file is
1384 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1387 # Find the closest known mrconfig file to the current
1389 $directory.="/" unless $directory=~/\/$/;
1391 foreach my $topdir (reverse sort keys %config) {
1392 next unless length $topdir;
1393 if ($directory=~/^\Q$topdir\E/) {
1394 $ENV{MR_CONFIG}=$configfiles{$topdir};
1400 if (! $foundconfig) {
1401 $directory=""; # no config file, use builtin
1405 my $subdir=shift @ARGV;
1406 if (! chdir($subdir)) {
1407 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1411 $ENV{MR_REPO}=getcwd();
1412 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1413 if (! defined $command) {
1414 die "mr register: unknown repository type\n";
1417 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1418 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1419 "my_action(){ $command\n }; my_action ".
1420 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1421 print "mr register: running >>$command<<\n" if $verbose;
1422 exec($command) || die "exec: $!";
1426 my $url=shift @ARGV;
1427 my $dir=shift @ARGV || ".";
1429 if (! defined $url || ! length $url) {
1430 die "mr: bootstrap requires url\n";
1433 # Download the config file to a temporary location.
1434 eval q{use File::Temp};
1436 my $tmpconfig=File::Temp->new();
1437 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1438 push(@curlargs, "-k") if $insecure;
1439 my $curlstatus = system(@curlargs);
1440 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1441 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1444 system("mkdir", "-p", $dir);
1446 chdir($dir) || die "chdir $dir: $!";
1448 # Special case to handle checkout of the "." repo, which
1449 # would normally be skipped.
1450 my $topdir=abs_path(".")."/";
1451 my @repo=($topdir, $topdir, ".");
1452 loadconfig($tmpconfig, $topdir);
1453 record(\@repo, action("checkout", @repo, 1))
1454 if exists $config{$topdir}{"."}{"checkout"};
1456 if (-e ".mrconfig") {
1457 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1460 eval q{use File::Copy};
1462 move($tmpconfig, ".mrconfig") || die "rename: $!";
1465 # Reload the config file (in case we got a different version)
1466 # and checkout everything else.
1468 loadconfig(".mrconfig");
1469 dispatch("checkout");
1470 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1471 showstats("bootstrap");
1475 # alias expansion and command stemming
1478 if (exists $alias{$action}) {
1479 $action=$alias{$action};
1481 if (! exists $knownactions{$action}) {
1482 my @matches = grep { /^\Q$action\E/ }
1483 keys %knownactions, keys %alias;
1484 if (@matches == 1) {
1485 $action=$matches[0];
1487 elsif (@matches == 0) {
1488 die "mr: unknown action \"$action\" (known actions: ".
1489 join(", ", sort keys %knownactions).")\n";
1492 die "mr: ambiguous action \"$action\" (matches: ".
1493 join(", ", @matches).")\n";
1501 while (length $dir) {
1502 if (-e "$dir/.mrconfig") {
1503 return "$dir/.mrconfig";
1505 $dir=~s/\/[^\/]*$//;
1507 return "$ENV{HOME}/.mrconfig";
1512 Getopt::Long::Configure("bundling", "no_permute");
1513 my $result=GetOptions(
1514 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1515 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1516 "p|path" => sub { }, # now default, ignore
1517 "v|verbose" => \$verbose,
1518 "q|quiet" => \$quiet,
1519 "s|stats" => \$stats,
1520 "k|insecure" => \$insecure,
1521 "i|interactive" => \$interactive,
1522 "n|no-recurse:i" => \$max_depth,
1523 "j|jobs:i" => \$jobs,
1524 "t|trust-all" => \$trust_all,
1526 if (! $result || @ARGV < 1) {
1527 die("Usage: mr [options] action [params ...]\n".
1528 "(Use mr help for man page.)\n");
1531 $ENV{MR_SWITCHES}="";
1532 foreach my $option (@saved) {
1533 last if $option eq $ARGV[0];
1534 $ENV{MR_SWITCHES}.="$option ";
1540 print STDERR "mr: interrupted\n";
1544 # This can happen if it's run in a directory that was removed
1545 # or other strangeness.
1546 if (! defined $directory) {
1547 die("mr: failed to determine working directory\n");
1549 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1550 # the config file might be a symlink to elsewhere, and the directory it's
1551 # in is significant.
1552 if ($ENV{MR_CONFIG} !~ /^\//) {
1553 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1555 # Try to set MR_PATH to the path to the program.
1557 use FindBin qw($Bin $Script);
1558 $ENV{MR_PATH}=$Bin."/".$Script;
1576 loadconfig("$ENV{HOME}/.mrconfig");
1577 loadconfig($ENV{MR_CONFIG});
1578 #use Data::Dumper; print Dumper(\%config);
1580 my $action=expandaction(shift @ARGV);
1587 # Finally, some useful actions that mr knows about by default.
1588 # These can be overridden in ~/.mrconfig.
1603 echo "mr (warning): $@" >&2
1609 if [ -z "$1" ] || [ -z "$2" ]; then
1610 error "mr: usage: hours_since action num"
1612 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1613 if [ -e "$MR_REPO/$dir" ]; then
1614 flagfile="$MR_REPO/$dir/.mr_last$1"
1618 if [ -z "$flagfile" ]; then
1619 error "cannot determine flag filename"
1621 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1622 if [ "$delta" -lt "$2" ]; then
1630 svn_test = test -d "$MR_REPO"/.svn
1631 git_test = test -d "$MR_REPO"/.git
1632 bzr_test = test -d "$MR_REPO"/.bzr
1633 cvs_test = test -d "$MR_REPO"/CVS
1634 hg_test = test -d "$MR_REPO"/.hg
1635 darcs_test = test -d "$MR_REPO"/_darcs
1636 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1638 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1639 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1640 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1642 svn_update = svn update "$@"
1643 git_update = git pull "$@"
1644 bzr_update = bzr merge --pull "$@"
1645 cvs_update = cvs update "$@"
1646 hg_update = hg pull "$@" && hg update "$@"
1647 darcs_update = darcs pull -a "$@"
1648 fossil_update = fossil pull "$@"
1650 svn_status = svn status "$@"
1651 git_status = git status -s "$@" || true
1652 bzr_status = bzr status --short "$@"
1653 cvs_status = cvs status "$@"
1654 hg_status = hg status "$@"
1655 darcs_status = darcs whatsnew -ls "$@" || true
1656 fossil_status = fossil changes "$@"
1658 svn_commit = svn commit "$@"
1659 git_commit = git commit -a "$@" && git push --all
1660 bzr_commit = bzr commit "$@" && bzr push
1661 cvs_commit = cvs commit "$@"
1662 hg_commit = hg commit -m "$@" && hg push
1663 darcs_commit = darcs record -a -m "$@" && darcs push -a
1664 fossil_commit = fossil commit "$@"
1666 git_record = git commit -a "$@"
1667 bzr_record = bzr commit "$@"
1668 hg_record = hg commit -m "$@"
1669 darcs_record = darcs record -a -m "$@"
1670 fossil_record = fossil commit "$@"
1673 git_push = git push "$@"
1674 bzr_push = bzr push "$@"
1676 hg_push = hg push "$@"
1677 darcs_push = darcs push -a "$@"
1678 fossil_push = fossil push "$@"
1680 svn_diff = svn diff "$@"
1681 git_diff = git diff "$@"
1682 bzr_diff = bzr diff "$@"
1683 cvs_diff = cvs diff "$@"
1684 hg_diff = hg diff "$@"
1685 darcs_diff = darcs diff -u "$@"
1686 fossil_diff = fossil diff "$@"
1688 svn_log = svn log "$@"
1689 git_log = git log "$@"
1690 bzr_log = bzr log "$@"
1691 cvs_log = cvs log "$@"
1692 hg_log = hg log "$@"
1693 darcs_log = darcs changes "$@"
1694 git_bare_log = git log "$@"
1695 fossil_log = fossil timeline "$@"
1700 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1701 if [ -z "$url" ]; then
1702 error "cannot determine svn url"
1704 echo "Registering svn url: $url in $MR_CONFIG"
1705 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1707 url="`LC_ALL=C git config --get remote.origin.url`" || true
1708 if [ -z "$url" ]; then
1709 error "cannot determine git url"
1711 echo "Registering git url: $url in $MR_CONFIG"
1712 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1714 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1715 if [ -z "$url" ]; then
1716 error "cannot determine bzr url"
1718 echo "Registering bzr url: $url in $MR_CONFIG"
1719 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1721 repo=`cat CVS/Repository`
1723 if [ -z "$root" ]; then
1724 error "cannot determine cvs root"
1726 echo "Registering cvs repository $repo at root $root"
1727 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1729 url=`hg showconfig paths.default`
1730 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1731 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1733 url=`cat _darcs/prefs/defaultrepo`
1734 echo "Registering darcs repository $url in $MR_CONFIG"
1735 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1737 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1738 if [ -z "$url" ]; then
1739 error "cannot determine git url"
1741 echo "Registering git url: $url in $MR_CONFIG"
1742 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1744 url=`fossil remote-url`
1745 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1746 echo "Registering fossil repository $url in $MR_CONFIG"
1747 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1749 svn_trusted_checkout = svn co $url $repo
1750 svn_alt_trusted_checkout = svn checkout $url $repo
1751 git_trusted_checkout = git clone $url $repo
1752 bzr_trusted_checkout = bzr clone $url $repo
1754 hg_trusted_checkout = hg clone $url $repo
1755 darcs_trusted_checkout = darcs get $url $repo
1756 git_bare_trusted_checkout = git clone --bare $url $repo
1757 # fossil: messy to do
1763 SHOWMANFILE="man -f"
1769 SHOWMANFILE="man -l"
1772 if [ ! -e "$MR_PATH" ]; then
1773 error "cannot find program path"
1775 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1776 trap "rm -f $tmp" exit
1777 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1778 $SHOWMANFILE "$tmp" || error "man failed"
1784 if [ -s ~/.mrlog ]; then
1785 info "running offline commands"
1786 mv -f ~/.mrlog ~/.mrlog.old
1787 if ! sh -e ~/.mrlog.old; then
1788 error "offline command failed; left in ~/.mrlog.old"
1792 info "no offline commands to run"
1797 info "offline mode enabled"
1799 info "remembering command: 'mr $@'"
1800 command="mr -d '$(pwd)' $MR_SWITCHES"
1802 command="$command '$w'"
1804 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1805 echo "$command" >> ~/.mrlog
1808 ed = echo "A horse is a horse, of course, of course.."
1809 T = echo "I pity the fool."
1810 right = echo "Not found."
1812 # vim:sw=8:sts=0:ts=8:noet