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] bootstrap url [directory]
27 B<mr> [options] register [repository]
29 B<mr> [options] config section ["parameter=[value]" ...]
31 B<mr> [options] action [params ...]
33 B<mr> [options] [online|offline]
35 B<mr> [options] remember action [params ...]
39 B<mr> is a Multiple Repository management tool. It can checkout, update, or
40 perform other actions on a set of repositories as if they were one combined
41 repository. It supports any combination of subversion, git, cvs, mercurial,
42 bzr, darcs and fossil repositories, and support for other revision
43 control systems can easily be added.
45 B<mr> cds into and operates on all registered repositories at or below your
46 working directory. Or, if you are in a subdirectory of a repository that
47 contains no other registered repositories, it will stay in that directory,
48 and work on only that repository,
50 B<mr> is configured by .mrconfig files, which list the repositories. It
51 starts by reading the .mrconfig file in your home directory, and this can
52 in turn chain load .mrconfig files from repositories.
54 These predefined commands should be fairly familiar to users of any revision
59 =item checkout (or co)
61 Checks out any repositories that are not already checked out.
65 Updates each repository from its configured remote repository.
67 If a repository isn't checked out yet, it will first check it out.
71 Displays a status report for each repository, showing what
72 uncommitted changes are present in the repository.
76 Commits changes to each repository. (By default, changes are pushed to the
77 remote repository too, when using distributed systems like git. If you
78 don't like this default, you can change it in your .mrconfig, or use record
81 The optional -m parameter allows specifying a commit message.
85 Records changes to the local repository, but does not push them to the
86 remote repository. Only supported for distributed revision control systems.
88 The optional -m parameter allows specifying a commit message.
92 Pushes committed local changes to the remote repository. A no-op for
93 centralized revision control systems.
97 Show a diff of uncommitted changes.
105 These commands are also available:
109 =item bootstrap url [directory]
111 Causes mr to download the url, and use it as a .mrconfig file
112 to checkout the repositories listed in it, into the specified directory.
114 The directory will be created if it does not exist. If no directory is
115 specified, the current directory will be used.
117 If the .mrconfig file includes a repository named ".", that
118 is checked out into the top of the specified directory.
122 List the repositories that mr will act on.
126 Register an existing repository in a mrconfig file. By default, the
127 repository in the current directory is registered, or you can specify a
128 directory to register.
130 The mrconfig file that is modified is chosen by either the -c option, or by
131 looking for the closest known one at or below the current directory.
135 Adds, modifies, removes, or prints a value from a mrconfig file. The next
136 parameter is the name of the section the value is in. To add or modify
137 values, use one or more instances of "parameter=value". Use "parameter=" to
138 remove a parameter. Use just "parameter" to get the value of a parameter.
140 For example, to add (or edit) a repository in src/foo:
142 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
144 To show the command that mr uses to update the repository in src/foo:
146 mr config src/foo update
148 To see the built-in library of shell functions contained in mr:
150 mr config DEFAULT lib
152 The ~/.mrconfig file is used by default. To use a different config file,
157 Advises mr that it is in offline mode. Any commands that fail in
158 offline mode will be remembered, and retried when mr is told it's online.
162 Advices mr that it is in online mode again. Commands that failed while in
163 offline mode will be re-run.
167 Remember a command, to be run later when mr re-enters online mode. This
168 implicitly puts mr into offline mode. The command can be any regular mr
169 command. This is useful when you know that a command will fail due to being
170 offline, and so don't want to run it right now at all, but just remember
171 to run it when you go back online.
179 Actions can be abbreviated to any unambiguous substring, so
180 "mr st" is equivalent to "mr status", and "mr up" is equivalent to "mr
183 Additional parameters can be passed to most commands, and are passed on
184 unchanged to the underlying revision control system. This is mostly useful
185 if the repositories mr will act on all use the same revision control
194 =item --directory directory
196 Specifies the topmost directory that B<mr> should work in. The default is
197 the current working directory.
201 =item --config mrconfig
203 Use the specified mrconfig file. The default is B<~/.mrconfig>
209 Search in the current directory, and its parent directories and use
210 the first B<.mrconfig> found, instead of the default B<~/.mrconfig>.
228 Accept untrusted SSL certificates when bootstrapping.
234 Expand the statistics line displayed at the end to include information
235 about exactly which repositories failed and were skipped, if any.
241 Interactive mode. If a repository fails to be processed, a subshell will be
242 started which you can use to resolve or investigate the problem. Exit the
243 subshell to continue the mr run.
247 =item --no-recurse [number]
249 If no number if specified, just operate on the repository for the current
250 directory, do not recurse into deeper repositories.
252 If a number is specified, will recurse into repositories at most that many
253 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
254 but not ./src/packages/bar.
258 =item --jobs [number]
260 Run the specified number of jobs in parallel, or an unlimited number of jobs
261 with no number specified. This can greatly speed up operations such as updates.
262 It is not recommended for interactive operations.
264 Note that running more than 10 jobs at a time is likely to run afoul of
265 ssh connection limits. Running between 3 and 5 jobs at a time will yield
266 a good speedup in updates without loading the machine too much.
272 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
277 =head1 MRCONFIG FILES
279 Here is an example .mrconfig file:
282 checkout = svn co svn://svn.example.com/src/trunk src
286 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
288 git checkout -b mybranch origin/master
290 The .mrconfig file uses a variant of the INI file format. Lines starting with
291 "#" are comments. Values can be continued to the following line by
292 indenting the line with whitespace.
294 The "DEFAULT" section allows setting default values for the sections that
297 The "ALIAS" section allows adding aliases for actions. Each parameter
298 is an alias, and its value is the action to use.
300 All other sections add repositories. The section header specifies the
301 directory where the repository is located. This is relative to the directory
302 that contains the mrconfig file, but you can also choose to use absolute
303 paths. (Note that you can use environment variables in section names; they
304 will be passed through the shell for expansion. For example,
305 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
307 Within a section, each parameter defines a shell command to run to handle a
308 given action. mr contains default handlers for "update", "status",
309 "commit", and other standard actions. Normally you only need to specify what
310 to do for "checkout".
312 Note that these shell commands are run in a "set -e" shell
313 environment, where any additional parameters you pass are available in
314 "$@". The "checkout" command is run in the parent of the repository
315 directory, since the repository isn't checked out yet. All other commands
316 are run inside the repository, though not necessarily at the top of it.
318 The "MR_REPO" environment variable is set to the path to the top of the
319 repository. (For the "register" action, "MR_REPO" is instead set to the
320 basename of the directory that should be created when checking the
323 The "MR_CONFIG" environment variable is set to the .mrconfig file
324 that defines the repo being acted on, or, if the repo is not yet in a config
325 file, the .mrconfig file that should be modified to register the repo.
327 A few parameters have special meanings:
333 If the "skip" parameter is set and its command returns true, then B<mr>
334 will skip acting on that repository. The command is passed the action
337 Here are two examples. The first skips the repo unless
338 mr is run by joey. The second uses the hours_since function
339 (included in mr's built-in library) to skip updating the repo unless it's
340 been at least 12 hours since the last update.
342 skip = test `whoami` != joey
343 skip = [ "$1" = update ] && ! hours_since "$1" 12
347 The "order" parameter can be used to override the default ordering of
348 repositories. The default order value is 10. Use smaller values to make
349 repositories be processed earlier, and larger values to make repositories
352 Note that if a repository is located in a subdirectory of another
353 repository, ordering it to be processed earlier is not recommended.
357 If the "chain" parameter is set and its command returns true, then B<mr>
358 will try to load a .mrconfig file from the root of the repository.
362 If the "include" parameter is set, its command is ran, and should output
363 additional mrconfig file content. The content is included as if it were
364 part of the including file.
366 Unlike all other parameters, this parameter does not need to be placed
371 The "lib" parameter can specify some shell code that will be run before each
372 command, this can be a useful way to define shell functions for other commands
377 If the "fixups" parameter is set, its command is run whenever a repository
378 is checked out, or updated. This provides an easy way to do things
379 like permissions fixups, or other tweaks to the repository content,
380 whenever the repository is changed.
384 If a "pre_action" parameter is set, its command is run before mr performs the
385 specified action. Similarly, "post_action" parameters are run after mr
386 successfully performs the specified action. For example, "pre_commit" is
387 run before committing; "post_update" is run after updating.
391 When looking for a command to run for a given action, mr first looks for
392 a parameter with the same name as the action. If that is not found, it
393 looks for a parameter named "rcs_action" (substituting in the name of the
394 revision control system and the action). The name of the revision control
395 system is itself determined by running each defined "rcs_test" action,
398 Internally, mr has settings for "git_update", "svn_update", etc. To change
399 the action that is performed for a given revision control system, you can
400 override these rcs specific actions. To add a new revision control system,
401 you can just add rcs specific actions for it.
403 The ~/.mrlog file contains commands that mr has remembered to run later,
404 due to being offline. You can delete or edit this file to remove commands,
405 or even to add other commands for 'mr online' to run. If the file is
406 present, mr assumes it is in offline mode.
408 =head1 UNTRUSTED MRCONFIG FILES
410 Since mrconfig files can contain arbitrary shell commands, they can do
411 anything. This flexibility is good, but it also allows a malicious mrconfig
412 file to delete your whole home directory. Such a file might be contained
413 inside a repository that your main ~/.mrconfig checks out and chains to. To
414 avoid worries about evil commands in a mrconfig file, mr
415 has the ability to read mrconfig files in untrusted mode. Such files are
416 limited to running only known safe commands (like "git clone") in a
417 carefully checked manner.
419 By default, mr trusts all mrconfig files. (This default will change in a
420 future release!) But if you have a ~/.mrtrust file, mr will only trust
421 mrconfig files that are listed within it. (One file per line.) All other
422 files will be treated as untrusted.
426 mr can be extended to support things such as unison and git-svn. Some
427 files providing such extensions are available in /usr/share/mr/. See
428 the documentation in the files for details about using them.
432 mr returns nonzero if a command failed in any of the repositories.
436 Copyright 2007-2010 Joey Hess <joey@kitenet.net>
438 Licensed under the GNU GPL version 2 or higher.
440 http://kitenet.net/~joey/code/mr/
447 use Cwd qw(getcwd abs_path);
449 # things that can happen when mr runs a command
458 my $config_overridden=0;
468 my $directory=getcwd();
469 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
476 my (@ok, @failed, @skipped);
482 my ($action, $dir, $topdir, $subdir) = @_;
484 if (exists $rcs{$dir}) {
489 foreach my $rcs_test (
491 length $a <=> length $b
494 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
495 my ($rcs)=$rcs_test=~/(.*)_test/;
496 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
497 $test.="if my_$rcs_test; then echo $rcs; fi\n";
499 $test=$config{$topdir}{$subdir}{lib}."\n".$test
500 if exists $config{$topdir}{$subdir}{lib};
502 print "mr $action: running rcs test >>$test<<\n" if $verbose;
507 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
511 return $rcs{$dir}=undef;
514 return $rcs{$dir}=$rcs;
519 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
521 if (exists $config{$topdir}{$subdir}{$action}) {
522 return $config{$topdir}{$subdir}{$action};
529 my $rcs=rcs_test(@_);
532 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
533 return $config{$topdir}{$subdir}{$rcs."_".$action};
541 my ($topdir, $subdir) = @_;
542 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
546 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
547 my $fulldir=fulldir($topdir, $subdir);
549 $ENV{MR_CONFIG}=$configfiles{$topdir};
550 my $lib=exists $config{$topdir}{$subdir}{lib} ?
551 $config{$topdir}{$subdir}{lib}."\n" : "";
552 my $is_checkout=($action eq 'checkout');
553 my $is_update=($action =~ /update/);
558 if (! $force_checkout) {
560 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
564 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
569 return action("checkout", $dir, $topdir, $subdir);
573 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
574 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
576 if (defined $skiptest) {
577 my $test="set -e;".$lib.
578 "my_action(){ $skiptest\n }; my_action '$action'";
579 print "mr $action: running skip test >>$test<<\n" if $verbose;
580 my $ret=system($test);
582 if (($? & 127) == 2) {
583 print STDERR "mr $action: interrupted\n";
587 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
591 if ($ret >> 8 == 0) {
592 print "mr $action: $dir skipped per config file\n" if $verbose;
597 if ($is_checkout && ! -d $dir) {
598 print "mr $action: creating parent directory $dir\n" if $verbose;
599 system("mkdir", "-p", $dir);
602 if (! $no_chdir && ! chdir($dir)) {
603 print STDERR "mr $action: failed to chdir to $dir: $!\n";
606 elsif (! defined $command) {
607 my $rcs=rcs_test(@_);
608 if (! defined $rcs) {
609 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
613 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
619 print "mr $action: $fulldir\n" unless $quiet;
623 $s=~s/^\Q$fulldir\E\/?//;
624 print "mr $action: $fulldir (in subdir $s)\n" unless $quiet;
627 my $hookret=hook("pre_$action", $topdir, $subdir);
628 return $hookret if $hookret != OK;
630 $command="set -e; ".$lib.
631 "my_action(){ $command\n }; my_action ".
632 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
633 print "mr $action: running >>$command<<\n" if $verbose;
634 my $ret=system($command);
636 if (($? & 127) == 2) {
637 print STDERR "mr $action: interrupted\n";
641 print STDERR "mr $action: received signal ".($? & 127)."\n";
644 print STDERR "mr $action: failed ($ret)\n" if $verbose;
645 if ($ret >> 8 != 0) {
646 print STDERR "mr $action: command failed\n";
647 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
648 # recreate original command line to
649 # remember, and avoid recursing
651 @ARGV=('-n', $action, @orig);
652 action("remember", $dir, $topdir, $subdir);
657 print STDERR "mr $action: command died ($ret)\n";
662 if ($is_checkout && ! -d $dir) {
663 print STDERR "mr $action: $dir missing after checkout\n";;
667 my $ret=hook("post_$action", $topdir, $subdir);
668 return $ret if $ret != OK;
670 if (($is_checkout || $is_update)) {
671 my $ret=hook("fixups", $topdir, $subdir);
672 return $ret if $ret != OK;
681 my ($hook, $topdir, $subdir) = @_;
683 my $command=$config{$topdir}{$subdir}{$hook};
684 return OK unless defined $command;
685 my $lib=exists $config{$topdir}{$subdir}{lib} ?
686 $config{$topdir}{$subdir}{lib}."\n" : "";
687 my $shell="set -e;".$lib.
688 "my_hook(){ $command\n }; my_hook";
689 print "mr $hook: running >>$shell<<\n" if $verbose;
690 my $ret=system($shell);
692 if (($? & 127) == 2) {
693 print STDERR "mr $hook: interrupted\n";
697 print STDERR "mr $hook: received signal ".($? & 127)."\n";
705 # run actions on multiple repos, in parallel
715 while (@fhs or @repos) {
716 while ((!$jobs || $running < $jobs) && @repos) {
718 my $repo = shift @repos;
719 pipe(my $outfh, CHILD_STDOUT);
720 pipe(my $errfh, CHILD_STDERR);
722 unless ($pid = fork) {
723 die "mr $action: cannot fork: $!" unless defined $pid;
724 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
725 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
730 exit action($action, @$repo);
734 push @active, [$pid, $repo];
735 push @fhs, [$outfh, $errfh];
738 my ($rin, $rout) = ('','');
740 foreach my $fh (@fhs) {
741 next unless defined $fh;
742 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
743 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
745 $nfound = select($rout=$rin, undef, undef, 1);
746 foreach my $channel (0, 1) {
747 foreach my $i (0..$#fhs) {
748 next unless defined $fhs[$i];
749 my $fh = $fhs[$i][$channel];
750 next unless defined $fh;
751 if (vec($rout, fileno($fh), 1) == 1) {
753 if (sysread($fh, $r, 1024) == 0) {
755 $fhs[$i][$channel] = undef;
756 if (! defined $fhs[$i][0] &&
757 ! defined $fhs[$i][1]) {
758 waitpid($active[$i][0], 0);
759 print STDOUT $out[$i][0];
760 print STDERR $out[$i][1];
761 record($active[$i][1], $? >> 8);
763 splice(@active, $i, 1);
768 $out[$i][$channel] .= $r;
776 my $dir=shift()->[0];
781 print "\n" unless $quiet;
783 elsif ($ret == FAILED) {
785 chdir($dir) unless $no_chdir;
786 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
787 system((getpwuid($<))[8], "-i");
790 print "\n" unless $quiet;
792 elsif ($ret == SKIPPED) {
795 elsif ($ret == ABORT) {
799 die "unknown exit status $ret";
805 if (! @ok && ! @failed && ! @skipped) {
806 die "mr $action: no repositories found to work on\n";
808 print "mr $action: finished (".join("; ",
809 showstat($#ok+1, "ok", "ok"),
810 showstat($#failed+1, "failed", "failed"),
811 showstat($#skipped+1, "skipped", "skipped"),
812 ).")\n" unless $quiet;
815 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
818 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
828 return "$count ".($count > 1 ? $plural : $singular);
833 # an ordered list of repos
836 foreach my $topdir (sort keys %config) {
837 foreach my $subdir (sort keys %{$config{$topdir}}) {
841 order => $config{$topdir}{$subdir}{order},
846 $a->{order} <=> $b->{order}
848 $a->{topdir} cmp $b->{topdir}
850 $a->{subdir} cmp $b->{subdir}
856 my $topdir=$repo->{topdir};
857 my $subdir=$repo->{subdir};
858 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
863 # figure out which repos to act on
866 foreach my $repo (repolist()) {
867 my $topdir=$repo->{topdir};
868 my $subdir=$repo->{subdir};
870 next if $subdir eq 'DEFAULT';
871 my $dir=repodir($repo);
873 $dir.="/" unless $dir=~/\/$/;
874 $d.="/" unless $d=~/\/$/;
875 next if $dir ne $d && $dir !~ /^\Q$d\E/;
876 if (defined $max_depth) {
877 my @a=split('/', $dir);
878 my @b=split('/', $d);
879 do { } while (@a && @b && shift(@a) eq shift(@b));
880 next if @a > $max_depth || @b > $max_depth;
882 push @repos, [$dir, $topdir, $subdir];
885 # fallback to find a leaf repo
886 foreach my $repo (reverse repolist()) {
887 my $topdir=$repo->{topdir};
888 my $subdir=$repo->{subdir};
890 next if $subdir eq 'DEFAULT';
891 my $dir=repodir($repo);
893 $dir.="/" unless $dir=~/\/$/;
894 $d.="/" unless $d=~/\/$/;
895 if ($d=~/^\Q$dir\E/) {
896 push @repos, [$dir, $topdir, $subdir];
918 sub is_trusted_config {
919 my $config=shift; # must be abs_pathed already
921 # We always trust ~/.mrconfig.
922 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
924 return 1 if $trust_all;
926 my $trustfile=$ENV{HOME}."/.mrtrust";
928 if (! -e $trustfile) {
929 print "mr: Assuming $config is trusted.\n";
930 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
931 print "mr: and list all trusted mrconfig files in it.\n";
936 $trusted{"$ENV{HOME}/.mrconfig"}=1;
937 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
940 s/^~\//$ENV{HOME}\//;
941 $trusted{abs_path($_)}=1;
946 return $trusted{$config};
950 sub is_trusted_repo {
953 # Tightly limit what is allowed in a repo name.
954 # No ../, no absolute paths, and no unusual filenames
955 # that might try to escape to the shell.
956 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
957 $repo !~ /\.\./ && $repo !~ /^\//;
960 sub is_trusted_checkout {
963 # To determine if the command is safe, compare it with the
964 # *_trusted_checkout config settings. Those settings are
965 # templates for allowed commands, so make sure that each word
966 # of the command matches the corresponding word of the template.
969 foreach my $word (split(' ', $command)) {
971 if ($word=~/^'(.*)'$/) {
974 elsif ($word=~/^"(.*)"$/) {
981 foreach my $key (grep { /_trusted_checkout$/ }
982 keys %{$config{''}{DEFAULT}}) {
983 my @twords=split(' ', $config{''}{DEFAULT}{$key});
984 next if @words > @twords;
988 for (my $c=0; $c < @twords && $match; $c++) {
989 if ($twords[$c] eq '$url') {
990 # Match all the typical characters found in
991 # urls, plus @ which svn can use. Note
992 # that the "url" might also be a local
995 defined $words[$c] &&
996 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1000 elsif ($twords[$c] eq '$repo') {
1001 # If a repo is not specified, assume it
1002 # will be the last path component of the
1003 # url, or something derived from it, and
1005 if (! defined $words[$c] && defined $url) {
1006 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1010 defined $words[$c] &&
1011 is_trusted_repo($words[$c])
1014 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1036 if (ref $f eq 'GLOB') {
1046 my $absf=abs_path($f);
1047 if ($loaded{$absf}) {
1052 $trusted=is_trusted_config($absf);
1054 if (! defined $dir) {
1055 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1056 if (! defined $dir) {
1061 $dir=abs_path($dir)."/";
1063 if (! exists $configfiles{$dir}) {
1064 $configfiles{$dir}=$f;
1067 # copy in defaults from first parent
1069 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1070 if ($parent eq '/') {
1073 if (exists $config{$parent} &&
1074 exists $config{$parent}{DEFAULT}) {
1075 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1080 print "mr: loading config $f\n" if $verbose;
1081 open($in, "<", $f) || die "mr: open $f: $!\n";
1084 close $in unless ref $f eq 'GLOB';
1092 next if /^\s*\#/ || /^\s*$/;
1093 if (/^\[([^\]]*)\]\s*$/) {
1097 if (! is_trusted_repo($section) ||
1098 $section eq 'ALIAS' ||
1099 $section eq 'DEFAULT') {
1100 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1103 $section=expandenv($section) if $trusted;
1104 if ($section ne 'ALIAS' &&
1105 ! exists $config{$dir}{$section} &&
1106 exists $config{$dir}{DEFAULT}) {
1108 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1111 elsif (/^(\w+)\s*=\s*(.*)/) {
1116 while (@lines && $lines[0]=~/^\s(.+)/) {
1124 # Untrusted files can only contain checkout
1126 if ($parameter ne 'checkout') {
1127 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1129 if (! is_trusted_checkout($value)) {
1130 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1134 if ($parameter eq "include") {
1135 print "mr: including output of \"$value\"\n" if $verbose;
1136 unshift @lines, `$value`;
1138 print STDERR "mr: include command exited nonzero ($?)\n";
1143 if (! defined $section) {
1144 die "$f line $.: parameter ($parameter) not in section\n";
1146 if ($section eq 'ALIAS') {
1147 $alias{$parameter}=$value;
1149 elsif ($parameter eq 'lib') {
1150 $config{$dir}{$section}{lib}.=$value."\n";
1153 $config{$dir}{$section}{$parameter}=$value;
1154 if ($parameter =~ /.*_(.*)/) {
1155 $knownactions{$1}=1;
1158 $knownactions{$parameter}=1;
1160 if ($parameter eq 'chain' &&
1161 length $dir && $section ne "DEFAULT" &&
1162 -e $dir.$section."/.mrconfig") {
1163 my $ret=system($value);
1165 if (($? & 127) == 2) {
1166 print STDERR "mr: chain test interrupted\n";
1170 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1174 push @toload, $dir.$section."/.mrconfig";
1180 die "$f line $line: parse error\n";
1189 sub startingconfig {
1190 %alias=%config=%configfiles=%knownactions=%loaded=();
1191 my $datapos=tell(DATA);
1193 seek(DATA,$datapos,0); # rewind
1198 # the section to modify or add
1199 my $targetsection=shift;
1200 # fields to change in the section
1201 # To remove a field, set its value to "".
1202 my %changefields=@_;
1208 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1213 my $formatfield=sub {
1215 my @value=split(/\n/, shift);
1217 return "$field = ".shift(@value)."\n".
1218 join("", map { "\t$_\n" } @value);
1222 while ($out[$#out] =~ /^\s*$/) {
1223 unshift @blanks, pop @out;
1225 foreach my $field (sort keys %changefields) {
1226 if (length $changefields{$field}) {
1227 push @out, "$field = $changefields{$field}\n";
1228 delete $changefields{$field};
1238 if (/^\s*\#/ || /^\s*$/) {
1241 elsif (/^\[([^\]]*)\]\s*$/) {
1242 if (defined $section &&
1243 $section eq $targetsection) {
1247 $section=expandenv($1);
1251 elsif (/^(\w+)\s*=\s(.*)/) {
1256 while (@lines && $lines[0]=~/^\s(.+)/) {
1262 if ($section eq $targetsection) {
1263 if (exists $changefields{$parameter}) {
1264 if (length $changefields{$parameter}) {
1265 $value=$changefields{$parameter};
1267 delete $changefields{$parameter};
1271 push @out, $formatfield->($parameter, $value);
1275 if (defined $section &&
1276 $section eq $targetsection) {
1279 elsif (%changefields) {
1280 push @out, "\n[$targetsection]\n";
1281 foreach my $field (sort keys %changefields) {
1282 if (length $changefields{$field}) {
1283 push @out, $formatfield->($field, $changefields{$field});
1288 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1296 # actions that do not operate on all repos
1297 if ($action eq 'help') {
1300 elsif ($action eq 'config') {
1303 elsif ($action eq 'register') {
1306 elsif ($action eq 'bootstrap') {
1309 elsif ($action eq 'remember' ||
1310 $action eq 'offline' ||
1311 $action eq 'online') {
1312 my @repos=selectrepos;
1313 action($action, @{$repos[0]}) if @repos;
1317 if (!$jobs || $jobs > 1) {
1318 mrs($action, selectrepos());
1321 foreach my $repo (selectrepos()) {
1322 record($repo, action($action, @$repo));
1328 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1333 die "mr config: not enough parameters\n";
1336 if ($section=~/^\//) {
1337 # try to convert to a path relative to the config file
1338 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1339 $dir=abs_path($dir);
1340 $dir.="/" unless $dir=~/\/$/;
1341 if ($section=~/^\Q$dir\E(.*)/) {
1347 if (/^([^=]+)=(.*)$/) {
1348 $changefields{$1}=$2;
1352 foreach my $topdir (sort keys %config) {
1353 if (exists $config{$topdir}{$section} &&
1354 exists $config{$topdir}{$section}{$_}) {
1355 print $config{$topdir}{$section}{$_}."\n";
1357 last if $section eq 'DEFAULT';
1361 die "mr config: $section $_ not set\n";
1365 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1370 if ($config_overridden) {
1371 # Find the directory that the specified config file is
1373 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1376 # Find the closest known mrconfig file to the current
1378 $directory.="/" unless $directory=~/\/$/;
1380 foreach my $topdir (reverse sort keys %config) {
1381 next unless length $topdir;
1382 if ($directory=~/^\Q$topdir\E/) {
1383 $ENV{MR_CONFIG}=$configfiles{$topdir};
1389 if (! $foundconfig) {
1390 $directory=""; # no config file, use builtin
1394 my $subdir=shift @ARGV;
1395 if (! chdir($subdir)) {
1396 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1400 $ENV{MR_REPO}=getcwd();
1401 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1402 if (! defined $command) {
1403 die "mr register: unknown repository type\n";
1406 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1407 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1408 "my_action(){ $command\n }; my_action ".
1409 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1410 print "mr register: running >>$command<<\n" if $verbose;
1411 exec($command) || die "exec: $!";
1415 my $url=shift @ARGV;
1416 my $dir=shift @ARGV || ".";
1418 if (! defined $url || ! length $url) {
1419 die "mr: bootstrap requires url\n";
1422 # Download the config file to a temporary location.
1423 eval q{use File::Temp};
1425 my $tmpconfig=File::Temp->new();
1426 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1427 push(@curlargs, "-k") if $insecure;
1428 my $curlstatus = system(@curlargs);
1429 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1430 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1433 system("mkdir", "-p", $dir);
1435 chdir($dir) || die "chdir $dir: $!";
1437 # Special case to handle checkout of the "." repo, which
1438 # would normally be skipped.
1439 my $topdir=abs_path(".")."/";
1440 my @repo=($topdir, $topdir, ".");
1441 loadconfig($tmpconfig, $topdir);
1442 record(\@repo, action("checkout", @repo, 1))
1443 if exists $config{$topdir}{"."}{"checkout"};
1445 if (-e ".mrconfig") {
1446 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1449 eval q{use File::Copy};
1451 move($tmpconfig, ".mrconfig") || die "rename: $!";
1454 # Reload the config file (in case we got a different version)
1455 # and checkout everything else.
1457 loadconfig(".mrconfig");
1458 dispatch("checkout");
1459 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1460 showstats("bootstrap");
1464 # alias expansion and command stemming
1467 if (exists $alias{$action}) {
1468 $action=$alias{$action};
1470 if (! exists $knownactions{$action}) {
1471 my @matches = grep { /^\Q$action\E/ }
1472 keys %knownactions, keys %alias;
1473 if (@matches == 1) {
1474 $action=$matches[0];
1476 elsif (@matches == 0) {
1477 die "mr: unknown action \"$action\" (known actions: ".
1478 join(", ", sort keys %knownactions).")\n";
1481 die "mr: ambiguous action \"$action\" (matches: ".
1482 join(", ", @matches).")\n";
1488 sub find_nearest_mrconfig {
1490 while (length $dir) {
1491 if (-e "$dir/.mrconfig") {
1492 return "$dir/.mrconfig";
1494 $dir=~s/\/[^\/]*$//;
1496 die "no .mrconfig found in path\n";
1501 Getopt::Long::Configure("bundling", "no_permute");
1502 my $result=GetOptions(
1503 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1504 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1505 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1506 "v|verbose" => \$verbose,
1507 "q|quiet" => \$quiet,
1508 "s|stats" => \$stats,
1509 "k|insecure" => \$insecure,
1510 "i|interactive" => \$interactive,
1511 "n|no-recurse:i" => \$max_depth,
1512 "j|jobs:i" => \$jobs,
1513 "t|trust-all" => \$trust_all,
1515 if (! $result || @ARGV < 1) {
1516 die("Usage: mr [options] action [params ...]\n".
1517 "(Use mr help for man page.)\n");
1520 $ENV{MR_SWITCHES}="";
1521 foreach my $option (@saved) {
1522 last if $option eq $ARGV[0];
1523 $ENV{MR_SWITCHES}.="$option ";
1529 print STDERR "mr: interrupted\n";
1533 # This can happen if it's run in a directory that was removed
1534 # or other strangeness.
1535 if (! defined $directory) {
1536 die("mr: failed to determine working directory\n");
1538 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1539 # the config file might be a symlink to elsewhere, and the directory it's
1540 # in is significant.
1541 if ($ENV{MR_CONFIG} !~ /^\//) {
1542 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1544 # Try to set MR_PATH to the path to the program.
1546 use FindBin qw($Bin $Script);
1547 $ENV{MR_PATH}=$Bin."/".$Script;
1565 loadconfig($ENV{MR_CONFIG});
1566 #use Data::Dumper; print Dumper(\%config);
1568 my $action=expandaction(shift @ARGV);
1575 # Finally, some useful actions that mr knows about by default.
1576 # These can be overridden in ~/.mrconfig.
1591 echo "mr (warning): $@" >&2
1597 if [ -z "$1" ] || [ -z "$2" ]; then
1598 error "mr: usage: hours_since action num"
1600 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1601 if [ -e "$MR_REPO/$dir" ]; then
1602 flagfile="$MR_REPO/$dir/.mr_last$1"
1606 if [ -z "$flagfile" ]; then
1607 error "cannot determine flag filename"
1609 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1610 if [ "$delta" -lt "$2" ]; then
1618 svn_test = test -d "$MR_REPO"/.svn
1619 git_test = test -d "$MR_REPO"/.git
1620 bzr_test = test -d "$MR_REPO"/.bzr
1621 cvs_test = test -d "$MR_REPO"/CVS
1622 hg_test = test -d "$MR_REPO"/.hg
1623 darcs_test = test -d "$MR_REPO"/_darcs
1624 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1626 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1627 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1628 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1630 svn_update = svn update "$@"
1631 git_update = git pull "$@"
1632 bzr_update = bzr merge --pull "$@"
1633 cvs_update = cvs update "$@"
1634 hg_update = hg pull "$@" && hg update "$@"
1635 darcs_update = darcs pull -a "$@"
1636 fossil_update = fossil pull "$@"
1638 svn_status = svn status "$@"
1639 git_status = git status -s "$@" || true
1640 bzr_status = bzr status --short "$@"
1641 cvs_status = cvs status "$@"
1642 hg_status = hg status "$@"
1643 darcs_status = darcs whatsnew -ls "$@" || true
1644 fossil_status = fossil changes "$@"
1646 svn_commit = svn commit "$@"
1647 git_commit = git commit -a "$@" && git push --all
1648 bzr_commit = bzr commit "$@" && bzr push
1649 cvs_commit = cvs commit "$@"
1650 hg_commit = hg commit -m "$@" && hg push
1651 darcs_commit = darcs record -a -m "$@" && darcs push -a
1652 fossil_commit = fossil commit "$@"
1654 git_record = git commit -a "$@"
1655 bzr_record = bzr commit "$@"
1656 hg_record = hg commit -m "$@"
1657 darcs_record = darcs record -a -m "$@"
1658 fossil_record = fossil commit "$@"
1661 git_push = git push "$@"
1662 bzr_push = bzr push "$@"
1664 hg_push = hg push "$@"
1665 darcs_push = darcs push -a "$@"
1666 fossil_push = fossil push "$@"
1668 svn_diff = svn diff "$@"
1669 git_diff = git diff "$@"
1670 bzr_diff = bzr diff "$@"
1671 cvs_diff = cvs diff "$@"
1672 hg_diff = hg diff "$@"
1673 darcs_diff = darcs diff -u "$@"
1674 fossil_diff = fossil diff "$@"
1676 svn_log = svn log "$@"
1677 git_log = git log "$@"
1678 bzr_log = bzr log "$@"
1679 cvs_log = cvs log "$@"
1680 hg_log = hg log "$@"
1681 darcs_log = darcs changes "$@"
1682 git_bare_log = git log "$@"
1683 fossil_log = fossil timeline "$@"
1686 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1687 if [ -z "$url" ]; then
1688 error "cannot determine svn url"
1690 echo "Registering svn url: $url in $MR_CONFIG"
1691 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1693 url="`LC_ALL=C git config --get remote.origin.url`" || true
1694 if [ -z "$url" ]; then
1695 error "cannot determine git url"
1697 echo "Registering git url: $url in $MR_CONFIG"
1698 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1700 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1701 if [ -z "$url" ]; then
1702 error "cannot determine bzr url"
1704 echo "Registering bzr url: $url in $MR_CONFIG"
1705 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1707 repo=`cat CVS/Repository`
1709 if [ -z "$root" ]; then
1710 error "cannot determine cvs root"
1712 echo "Registering cvs repository $repo at root $root"
1713 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1715 url=`hg showconfig paths.default`
1716 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1717 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1719 url=`cat _darcs/prefs/defaultrepo`
1720 echo "Registering darcs repository $url in $MR_CONFIG"
1721 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1723 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1724 if [ -z "$url" ]; then
1725 error "cannot determine git url"
1727 echo "Registering git url: $url in $MR_CONFIG"
1728 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1730 url=`fossil remote-url`
1731 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1732 echo "Registering fossil repository $url in $MR_CONFIG"
1733 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1735 svn_trusted_checkout = svn co $url $repo
1736 svn_alt_trusted_checkout = svn checkout $url $repo
1737 git_trusted_checkout = git clone $url $repo
1738 bzr_trusted_checkout = bzr clone $url $repo
1740 hg_trusted_checkout = hg clone $url $repo
1741 darcs_trusted_checkout = darcs get $url $repo
1742 git_bare_trusted_checkout = git clone --bare $url $repo
1743 # fossil: messy to do
1749 SHOWMANFILE="man -f"
1755 SHOWMANFILE="man -l"
1758 if [ ! -e "$MR_PATH" ]; then
1759 error "cannot find program path"
1761 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1762 trap "rm -f $tmp" exit
1763 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1764 $SHOWMANFILE "$tmp" || error "man failed"
1770 if [ -s ~/.mrlog ]; then
1771 info "running offline commands"
1772 mv -f ~/.mrlog ~/.mrlog.old
1773 if ! sh -e ~/.mrlog.old; then
1774 error "offline command failed; left in ~/.mrlog.old"
1778 info "no offline commands to run"
1783 info "offline mode enabled"
1785 info "remembering command: 'mr $@'"
1786 command="mr -d '$(pwd)' $MR_SWITCHES"
1788 command="$command '$w'"
1790 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1791 echo "$command" >> ~/.mrlog
1794 ed = echo "A horse is a horse, of course, of course.."
1795 T = echo "I pity the fool."
1796 right = echo "Not found."
1798 # vim:sw=8:sts=0:ts=8:noet