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 =head1 UNTRUSTED MRCONFIG FILES
405 Since mrconfig files can contain arbitrary shell commands, they can do
406 anything. This flexibility is good, but it also allows a malicious mrconfig
407 file to delete your whole home directory. Such a file might be contained
408 inside a repository that your main ~/.mrconfig checks out. To
409 avoid worries about evil commands in a mrconfig file, mr defaults to
410 reading all mrconfig files other than the main ~/.mrconfig in untrusted
411 mode. In untrusted mode, mrconfig files are limited to running only known
412 safe commands (like "git clone") in a carefully checked manner.
414 To configure mr to trust other mrconfig files, list them in ~/.mrtrust.
415 One mrconfig file should be listed per line. Either the full pathname
416 should be listed, or the pathname can start with "~/" to specify a file
417 relative to your home directory.
419 =head1 OFFLINE LOG FILE
421 The ~/.mrlog file contains commands that mr has remembered to run later,
422 due to being offline. You can delete or edit this file to remove commands,
423 or even to add other commands for 'mr online' to run. If the file is
424 present, mr assumes it is in offline mode.
428 mr can be extended to support things such as unison and git-svn. Some
429 files providing such extensions are available in /usr/share/mr/. See
430 the documentation in the files for details about using them.
434 mr returns nonzero if a command failed in any of the repositories.
438 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
440 Licensed under the GNU GPL version 2 or higher.
442 http://kitenet.net/~joey/code/mr/
449 use Cwd qw(getcwd abs_path);
451 # things that can happen when mr runs a command
460 my $config_overridden=0;
470 my $directory=getcwd();
471 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
478 my (@ok, @failed, @skipped);
484 my ($action, $dir, $topdir, $subdir) = @_;
486 if (exists $rcs{$dir}) {
491 foreach my $rcs_test (
493 length $a <=> length $b
496 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
497 my ($rcs)=$rcs_test=~/(.*)_test/;
498 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
499 $test.="if my_$rcs_test; then echo $rcs; fi\n";
501 $test=$config{$topdir}{$subdir}{lib}."\n".$test
502 if exists $config{$topdir}{$subdir}{lib};
504 print "mr $action: running rcs test >>$test<<\n" if $verbose;
509 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
513 return $rcs{$dir}=undef;
516 return $rcs{$dir}=$rcs;
521 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
523 if (exists $config{$topdir}{$subdir}{$action}) {
524 return $config{$topdir}{$subdir}{$action};
531 my $rcs=rcs_test(@_);
534 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
535 return $config{$topdir}{$subdir}{$rcs."_".$action};
543 my ($topdir, $subdir) = @_;
544 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
548 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
549 my $fulldir=fulldir($topdir, $subdir);
551 $ENV{MR_CONFIG}=$configfiles{$topdir};
552 my $lib=exists $config{$topdir}{$subdir}{lib} ?
553 $config{$topdir}{$subdir}{lib}."\n" : "";
554 my $is_checkout=($action eq 'checkout');
555 my $is_update=($action =~ /update/);
560 if (! $force_checkout) {
562 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
566 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
571 return action("checkout", $dir, $topdir, $subdir);
575 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
576 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
578 if (defined $skiptest) {
579 my $test="set -e;".$lib.
580 "my_action(){ $skiptest\n }; my_action '$action'";
581 print "mr $action: running skip test >>$test<<\n" if $verbose;
582 my $ret=system($test);
584 if (($? & 127) == 2) {
585 print STDERR "mr $action: interrupted\n";
589 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
593 if ($ret >> 8 == 0) {
594 print "mr $action: $dir skipped per config file\n" if $verbose;
599 if ($is_checkout && ! -d $dir) {
600 print "mr $action: creating parent directory $dir\n" if $verbose;
601 system("mkdir", "-p", $dir);
604 if (! $no_chdir && ! chdir($dir)) {
605 print STDERR "mr $action: failed to chdir to $dir: $!\n";
608 elsif (! defined $command) {
609 my $rcs=rcs_test(@_);
610 if (! defined $rcs) {
611 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
615 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
621 print "mr $action: $fulldir\n" unless $quiet;
625 $s=~s/^\Q$fulldir\E\/?//;
626 print "mr $action: $fulldir (in subdir $s)\n" unless $quiet;
629 my $hookret=hook("pre_$action", $topdir, $subdir);
630 return $hookret if $hookret != OK;
632 $command="set -e; ".$lib.
633 "my_action(){ $command\n }; my_action ".
634 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
635 print "mr $action: running >>$command<<\n" if $verbose;
636 my $ret=system($command);
638 if (($? & 127) == 2) {
639 print STDERR "mr $action: interrupted\n";
643 print STDERR "mr $action: received signal ".($? & 127)."\n";
646 print STDERR "mr $action: failed ($ret)\n" if $verbose;
647 if ($ret >> 8 != 0) {
648 print STDERR "mr $action: command failed\n";
649 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
650 # recreate original command line to
651 # remember, and avoid recursing
653 @ARGV=('-n', $action, @orig);
654 action("remember", $dir, $topdir, $subdir);
659 print STDERR "mr $action: command died ($ret)\n";
664 if ($is_checkout && ! -d $dir) {
665 print STDERR "mr $action: $dir missing after checkout\n";;
669 my $ret=hook("post_$action", $topdir, $subdir);
670 return $ret if $ret != OK;
672 if (($is_checkout || $is_update)) {
673 my $ret=hook("fixups", $topdir, $subdir);
674 return $ret if $ret != OK;
683 my ($hook, $topdir, $subdir) = @_;
685 my $command=$config{$topdir}{$subdir}{$hook};
686 return OK unless defined $command;
687 my $lib=exists $config{$topdir}{$subdir}{lib} ?
688 $config{$topdir}{$subdir}{lib}."\n" : "";
689 my $shell="set -e;".$lib.
690 "my_hook(){ $command\n }; my_hook";
691 print "mr $hook: running >>$shell<<\n" if $verbose;
692 my $ret=system($shell);
694 if (($? & 127) == 2) {
695 print STDERR "mr $hook: interrupted\n";
699 print STDERR "mr $hook: received signal ".($? & 127)."\n";
707 # run actions on multiple repos, in parallel
717 while (@fhs or @repos) {
718 while ((!$jobs || $running < $jobs) && @repos) {
720 my $repo = shift @repos;
721 pipe(my $outfh, CHILD_STDOUT);
722 pipe(my $errfh, CHILD_STDERR);
724 unless ($pid = fork) {
725 die "mr $action: cannot fork: $!" unless defined $pid;
726 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
727 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
732 exit action($action, @$repo);
736 push @active, [$pid, $repo];
737 push @fhs, [$outfh, $errfh];
740 my ($rin, $rout) = ('','');
742 foreach my $fh (@fhs) {
743 next unless defined $fh;
744 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
745 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
747 $nfound = select($rout=$rin, undef, undef, 1);
748 foreach my $channel (0, 1) {
749 foreach my $i (0..$#fhs) {
750 next unless defined $fhs[$i];
751 my $fh = $fhs[$i][$channel];
752 next unless defined $fh;
753 if (vec($rout, fileno($fh), 1) == 1) {
755 if (sysread($fh, $r, 1024) == 0) {
757 $fhs[$i][$channel] = undef;
758 if (! defined $fhs[$i][0] &&
759 ! defined $fhs[$i][1]) {
760 waitpid($active[$i][0], 0);
761 print STDOUT $out[$i][0];
762 print STDERR $out[$i][1];
763 record($active[$i][1], $? >> 8);
765 splice(@active, $i, 1);
770 $out[$i][$channel] .= $r;
778 my $dir=shift()->[0];
783 print "\n" unless $quiet;
785 elsif ($ret == FAILED) {
787 chdir($dir) unless $no_chdir;
788 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
789 system((getpwuid($<))[8], "-i");
792 print "\n" unless $quiet;
794 elsif ($ret == SKIPPED) {
797 elsif ($ret == ABORT) {
801 die "unknown exit status $ret";
807 if (! @ok && ! @failed && ! @skipped) {
808 die "mr $action: no repositories found to work on\n";
810 print "mr $action: finished (".join("; ",
811 showstat($#ok+1, "ok", "ok"),
812 showstat($#failed+1, "failed", "failed"),
813 showstat($#skipped+1, "skipped", "skipped"),
814 ).")\n" unless $quiet;
817 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
820 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
830 return "$count ".($count > 1 ? $plural : $singular);
835 # an ordered list of repos
838 foreach my $topdir (sort keys %config) {
839 foreach my $subdir (sort keys %{$config{$topdir}}) {
843 order => $config{$topdir}{$subdir}{order},
848 $a->{order} <=> $b->{order}
850 $a->{topdir} cmp $b->{topdir}
852 $a->{subdir} cmp $b->{subdir}
858 my $topdir=$repo->{topdir};
859 my $subdir=$repo->{subdir};
860 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
865 # figure out which repos to act on
868 foreach my $repo (repolist()) {
869 my $topdir=$repo->{topdir};
870 my $subdir=$repo->{subdir};
872 next if $subdir eq 'DEFAULT';
873 my $dir=repodir($repo);
875 $dir.="/" unless $dir=~/\/$/;
876 $d.="/" unless $d=~/\/$/;
877 next if $dir ne $d && $dir !~ /^\Q$d\E/;
878 if (defined $max_depth) {
879 my @a=split('/', $dir);
880 my @b=split('/', $d);
881 do { } while (@a && @b && shift(@a) eq shift(@b));
882 next if @a > $max_depth || @b > $max_depth;
884 push @repos, [$dir, $topdir, $subdir];
887 # fallback to find a leaf repo
888 foreach my $repo (reverse repolist()) {
889 my $topdir=$repo->{topdir};
890 my $subdir=$repo->{subdir};
892 next if $subdir eq 'DEFAULT';
893 my $dir=repodir($repo);
895 $dir.="/" unless $dir=~/\/$/;
896 $d.="/" unless $d=~/\/$/;
897 if ($d=~/^\Q$dir\E/) {
898 push @repos, [$dir, $topdir, $subdir];
920 sub is_trusted_config {
921 my $config=shift; # must be abs_pathed already
923 # We always trust ~/.mrconfig.
924 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
926 return 1 if $trust_all;
928 my $trustfile=$ENV{HOME}."/.mrtrust";
931 $trusted{"$ENV{HOME}/.mrconfig"}=1;
932 if (open (TRUST, "<", $trustfile)) {
935 s/^~\//$ENV{HOME}\//;
936 $trusted{abs_path($_)}=1;
942 return $trusted{$config};
946 sub is_trusted_repo {
949 # Tightly limit what is allowed in a repo name.
950 # No ../, no absolute paths, and no unusual filenames
951 # that might try to escape to the shell.
952 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
953 $repo !~ /\.\./ && $repo !~ /^\//;
956 sub is_trusted_checkout {
959 # To determine if the command is safe, compare it with the
960 # *_trusted_checkout config settings. Those settings are
961 # templates for allowed commands, so make sure that each word
962 # of the command matches the corresponding word of the template.
965 foreach my $word (split(' ', $command)) {
967 if ($word=~/^'(.*)'$/) {
970 elsif ($word=~/^"(.*)"$/) {
977 foreach my $key (grep { /_trusted_checkout$/ }
978 keys %{$config{''}{DEFAULT}}) {
979 my @twords=split(' ', $config{''}{DEFAULT}{$key});
980 next if @words > @twords;
984 for (my $c=0; $c < @twords && $match; $c++) {
985 if ($twords[$c] eq '$url') {
986 # Match all the typical characters found in
987 # urls, plus @ which svn can use. Note
988 # that the "url" might also be a local
991 defined $words[$c] &&
992 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
996 elsif ($twords[$c] eq '$repo') {
997 # If a repo is not specified, assume it
998 # will be the last path component of the
999 # url, or something derived from it, and
1001 if (! defined $words[$c] && defined $url) {
1002 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1006 defined $words[$c] &&
1007 is_trusted_repo($words[$c])
1010 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1025 "(To trust this file, list it in ~/.mrtrust.)\n";
1037 if (ref $f eq 'GLOB') {
1047 my $absf=abs_path($f);
1048 if ($loaded{$absf}) {
1053 $trusted=is_trusted_config($absf);
1055 if (! defined $dir) {
1056 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1057 if (! defined $dir) {
1062 $dir=abs_path($dir)."/";
1064 if (! exists $configfiles{$dir}) {
1065 $configfiles{$dir}=$f;
1068 # copy in defaults from first parent
1070 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1071 if ($parent eq '/') {
1074 if (exists $config{$parent} &&
1075 exists $config{$parent}{DEFAULT}) {
1076 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1081 print "mr: loading config $f\n" if $verbose;
1082 open($in, "<", $f) || die "mr: open $f: $!\n";
1085 close $in unless ref $f eq 'GLOB';
1093 next if /^\s*\#/ || /^\s*$/;
1094 if (/^\[([^\]]*)\]\s*$/) {
1098 if (! is_trusted_repo($section) ||
1099 $section eq 'ALIAS' ||
1100 $section eq 'DEFAULT') {
1101 trusterror "mr: illegal section \"[$section]\" in untrusted $f line $line";
1104 $section=expandenv($section) if $trusted;
1105 if ($section ne 'ALIAS' &&
1106 ! exists $config{$dir}{$section} &&
1107 exists $config{$dir}{DEFAULT}) {
1109 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1112 elsif (/^(\w+)\s*=\s*(.*)/) {
1117 while (@lines && $lines[0]=~/^\s(.+)/) {
1125 # Untrusted files can only contain checkout
1127 if ($parameter ne 'checkout') {
1128 trusterror "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line";
1130 if (! is_trusted_checkout($value)) {
1131 trusterror "mr: illegal checkout command \"$value\" in untrusted $f line $line";
1135 if ($parameter eq "include") {
1136 print "mr: including output of \"$value\"\n" if $verbose;
1137 unshift @lines, `$value`;
1139 print STDERR "mr: include command exited nonzero ($?)\n";
1144 if (! defined $section) {
1145 die "$f line $.: parameter ($parameter) not in section\n";
1147 if ($section eq 'ALIAS') {
1148 $alias{$parameter}=$value;
1150 elsif ($parameter eq 'lib') {
1151 $config{$dir}{$section}{lib}.=$value."\n";
1154 $config{$dir}{$section}{$parameter}=$value;
1155 if ($parameter =~ /.*_(.*)/) {
1156 $knownactions{$1}=1;
1159 $knownactions{$parameter}=1;
1161 if ($parameter eq 'chain' &&
1162 length $dir && $section ne "DEFAULT" &&
1163 -e $dir.$section."/.mrconfig") {
1164 my $ret=system($value);
1166 if (($? & 127) == 2) {
1167 print STDERR "mr: chain test interrupted\n";
1171 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1175 push @toload, $dir.$section."/.mrconfig";
1181 die "$f line $line: parse error\n";
1190 sub startingconfig {
1191 %alias=%config=%configfiles=%knownactions=%loaded=();
1192 my $datapos=tell(DATA);
1194 seek(DATA,$datapos,0); # rewind
1199 # the section to modify or add
1200 my $targetsection=shift;
1201 # fields to change in the section
1202 # To remove a field, set its value to "".
1203 my %changefields=@_;
1209 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1214 my $formatfield=sub {
1216 my @value=split(/\n/, shift);
1218 return "$field = ".shift(@value)."\n".
1219 join("", map { "\t$_\n" } @value);
1223 while ($out[$#out] =~ /^\s*$/) {
1224 unshift @blanks, pop @out;
1226 foreach my $field (sort keys %changefields) {
1227 if (length $changefields{$field}) {
1228 push @out, "$field = $changefields{$field}\n";
1229 delete $changefields{$field};
1239 if (/^\s*\#/ || /^\s*$/) {
1242 elsif (/^\[([^\]]*)\]\s*$/) {
1243 if (defined $section &&
1244 $section eq $targetsection) {
1248 $section=expandenv($1);
1252 elsif (/^(\w+)\s*=\s(.*)/) {
1257 while (@lines && $lines[0]=~/^\s(.+)/) {
1263 if ($section eq $targetsection) {
1264 if (exists $changefields{$parameter}) {
1265 if (length $changefields{$parameter}) {
1266 $value=$changefields{$parameter};
1268 delete $changefields{$parameter};
1272 push @out, $formatfield->($parameter, $value);
1276 if (defined $section &&
1277 $section eq $targetsection) {
1280 elsif (%changefields) {
1281 push @out, "\n[$targetsection]\n";
1282 foreach my $field (sort keys %changefields) {
1283 if (length $changefields{$field}) {
1284 push @out, $formatfield->($field, $changefields{$field});
1289 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1297 # actions that do not operate on all repos
1298 if ($action eq 'help') {
1301 elsif ($action eq 'config') {
1304 elsif ($action eq 'register') {
1307 elsif ($action eq 'bootstrap') {
1310 elsif ($action eq 'remember' ||
1311 $action eq 'offline' ||
1312 $action eq 'online') {
1313 my @repos=selectrepos;
1314 action($action, @{$repos[0]}) if @repos;
1318 if (!$jobs || $jobs > 1) {
1319 mrs($action, selectrepos());
1322 foreach my $repo (selectrepos()) {
1323 record($repo, action($action, @$repo));
1329 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1334 die "mr config: not enough parameters\n";
1337 if ($section=~/^\//) {
1338 # try to convert to a path relative to the config file
1339 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1340 $dir=abs_path($dir);
1341 $dir.="/" unless $dir=~/\/$/;
1342 if ($section=~/^\Q$dir\E(.*)/) {
1348 if (/^([^=]+)=(.*)$/) {
1349 $changefields{$1}=$2;
1353 foreach my $topdir (sort keys %config) {
1354 if (exists $config{$topdir}{$section} &&
1355 exists $config{$topdir}{$section}{$_}) {
1356 print $config{$topdir}{$section}{$_}."\n";
1358 last if $section eq 'DEFAULT';
1362 die "mr config: $section $_ not set\n";
1366 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1371 if ($config_overridden) {
1372 # Find the directory that the specified config file is
1374 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1377 # Find the closest known mrconfig file to the current
1379 $directory.="/" unless $directory=~/\/$/;
1381 foreach my $topdir (reverse sort keys %config) {
1382 next unless length $topdir;
1383 if ($directory=~/^\Q$topdir\E/) {
1384 $ENV{MR_CONFIG}=$configfiles{$topdir};
1390 if (! $foundconfig) {
1391 $directory=""; # no config file, use builtin
1395 my $subdir=shift @ARGV;
1396 if (! chdir($subdir)) {
1397 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1401 $ENV{MR_REPO}=getcwd();
1402 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1403 if (! defined $command) {
1404 die "mr register: unknown repository type\n";
1407 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1408 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1409 "my_action(){ $command\n }; my_action ".
1410 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1411 print "mr register: running >>$command<<\n" if $verbose;
1412 exec($command) || die "exec: $!";
1416 my $url=shift @ARGV;
1417 my $dir=shift @ARGV || ".";
1419 if (! defined $url || ! length $url) {
1420 die "mr: bootstrap requires url\n";
1423 # Download the config file to a temporary location.
1424 eval q{use File::Temp};
1426 my $tmpconfig=File::Temp->new();
1427 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1428 push(@curlargs, "-k") if $insecure;
1429 my $curlstatus = system(@curlargs);
1430 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1431 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1434 system("mkdir", "-p", $dir);
1436 chdir($dir) || die "chdir $dir: $!";
1438 # Special case to handle checkout of the "." repo, which
1439 # would normally be skipped.
1440 my $topdir=abs_path(".")."/";
1441 my @repo=($topdir, $topdir, ".");
1442 loadconfig($tmpconfig, $topdir);
1443 record(\@repo, action("checkout", @repo, 1))
1444 if exists $config{$topdir}{"."}{"checkout"};
1446 if (-e ".mrconfig") {
1447 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1450 eval q{use File::Copy};
1452 move($tmpconfig, ".mrconfig") || die "rename: $!";
1455 # Reload the config file (in case we got a different version)
1456 # and checkout everything else.
1458 loadconfig(".mrconfig");
1459 dispatch("checkout");
1460 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1461 showstats("bootstrap");
1465 # alias expansion and command stemming
1468 if (exists $alias{$action}) {
1469 $action=$alias{$action};
1471 if (! exists $knownactions{$action}) {
1472 my @matches = grep { /^\Q$action\E/ }
1473 keys %knownactions, keys %alias;
1474 if (@matches == 1) {
1475 $action=$matches[0];
1477 elsif (@matches == 0) {
1478 die "mr: unknown action \"$action\" (known actions: ".
1479 join(", ", sort keys %knownactions).")\n";
1482 die "mr: ambiguous action \"$action\" (matches: ".
1483 join(", ", @matches).")\n";
1489 sub find_nearest_mrconfig {
1491 while (length $dir) {
1492 if (-e "$dir/.mrconfig") {
1493 return "$dir/.mrconfig";
1495 $dir=~s/\/[^\/]*$//;
1497 die "no .mrconfig found in path\n";
1502 Getopt::Long::Configure("bundling", "no_permute");
1503 my $result=GetOptions(
1504 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1505 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1506 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1507 "v|verbose" => \$verbose,
1508 "q|quiet" => \$quiet,
1509 "s|stats" => \$stats,
1510 "k|insecure" => \$insecure,
1511 "i|interactive" => \$interactive,
1512 "n|no-recurse:i" => \$max_depth,
1513 "j|jobs:i" => \$jobs,
1514 "t|trust-all" => \$trust_all,
1516 if (! $result || @ARGV < 1) {
1517 die("Usage: mr [options] action [params ...]\n".
1518 "(Use mr help for man page.)\n");
1521 $ENV{MR_SWITCHES}="";
1522 foreach my $option (@saved) {
1523 last if $option eq $ARGV[0];
1524 $ENV{MR_SWITCHES}.="$option ";
1530 print STDERR "mr: interrupted\n";
1534 # This can happen if it's run in a directory that was removed
1535 # or other strangeness.
1536 if (! defined $directory) {
1537 die("mr: failed to determine working directory\n");
1539 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1540 # the config file might be a symlink to elsewhere, and the directory it's
1541 # in is significant.
1542 if ($ENV{MR_CONFIG} !~ /^\//) {
1543 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1545 # Try to set MR_PATH to the path to the program.
1547 use FindBin qw($Bin $Script);
1548 $ENV{MR_PATH}=$Bin."/".$Script;
1566 loadconfig($ENV{MR_CONFIG});
1567 #use Data::Dumper; print Dumper(\%config);
1569 my $action=expandaction(shift @ARGV);
1576 # Finally, some useful actions that mr knows about by default.
1577 # These can be overridden in ~/.mrconfig.
1592 echo "mr (warning): $@" >&2
1598 if [ -z "$1" ] || [ -z "$2" ]; then
1599 error "mr: usage: hours_since action num"
1601 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1602 if [ -e "$MR_REPO/$dir" ]; then
1603 flagfile="$MR_REPO/$dir/.mr_last$1"
1607 if [ -z "$flagfile" ]; then
1608 error "cannot determine flag filename"
1610 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1611 if [ "$delta" -lt "$2" ]; then
1619 svn_test = test -d "$MR_REPO"/.svn
1620 git_test = test -d "$MR_REPO"/.git
1621 bzr_test = test -d "$MR_REPO"/.bzr
1622 cvs_test = test -d "$MR_REPO"/CVS
1623 hg_test = test -d "$MR_REPO"/.hg
1624 darcs_test = test -d "$MR_REPO"/_darcs
1625 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1627 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1628 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1629 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1631 svn_update = svn update "$@"
1632 git_update = git pull "$@"
1633 bzr_update = bzr merge --pull "$@"
1634 cvs_update = cvs update "$@"
1635 hg_update = hg pull "$@" && hg update "$@"
1636 darcs_update = darcs pull -a "$@"
1637 fossil_update = fossil pull "$@"
1639 svn_status = svn status "$@"
1640 git_status = git status -s "$@" || true
1641 bzr_status = bzr status --short "$@"
1642 cvs_status = cvs status "$@"
1643 hg_status = hg status "$@"
1644 darcs_status = darcs whatsnew -ls "$@" || true
1645 fossil_status = fossil changes "$@"
1647 svn_commit = svn commit "$@"
1648 git_commit = git commit -a "$@" && git push --all
1649 bzr_commit = bzr commit "$@" && bzr push
1650 cvs_commit = cvs commit "$@"
1651 hg_commit = hg commit -m "$@" && hg push
1652 darcs_commit = darcs record -a -m "$@" && darcs push -a
1653 fossil_commit = fossil commit "$@"
1655 git_record = git commit -a "$@"
1656 bzr_record = bzr commit "$@"
1657 hg_record = hg commit -m "$@"
1658 darcs_record = darcs record -a -m "$@"
1659 fossil_record = fossil commit "$@"
1662 git_push = git push "$@"
1663 bzr_push = bzr push "$@"
1665 hg_push = hg push "$@"
1666 darcs_push = darcs push -a "$@"
1667 fossil_push = fossil push "$@"
1669 svn_diff = svn diff "$@"
1670 git_diff = git diff "$@"
1671 bzr_diff = bzr diff "$@"
1672 cvs_diff = cvs diff "$@"
1673 hg_diff = hg diff "$@"
1674 darcs_diff = darcs diff -u "$@"
1675 fossil_diff = fossil diff "$@"
1677 svn_log = svn log "$@"
1678 git_log = git log "$@"
1679 bzr_log = bzr log "$@"
1680 cvs_log = cvs log "$@"
1681 hg_log = hg log "$@"
1682 darcs_log = darcs changes "$@"
1683 git_bare_log = git log "$@"
1684 fossil_log = fossil timeline "$@"
1687 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1688 if [ -z "$url" ]; then
1689 error "cannot determine svn url"
1691 echo "Registering svn url: $url in $MR_CONFIG"
1692 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1694 url="`LC_ALL=C git config --get remote.origin.url`" || true
1695 if [ -z "$url" ]; then
1696 error "cannot determine git url"
1698 echo "Registering git url: $url in $MR_CONFIG"
1699 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1701 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1702 if [ -z "$url" ]; then
1703 error "cannot determine bzr url"
1705 echo "Registering bzr url: $url in $MR_CONFIG"
1706 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1708 repo=`cat CVS/Repository`
1710 if [ -z "$root" ]; then
1711 error "cannot determine cvs root"
1713 echo "Registering cvs repository $repo at root $root"
1714 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1716 url=`hg showconfig paths.default`
1717 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1718 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1720 url=`cat _darcs/prefs/defaultrepo`
1721 echo "Registering darcs repository $url in $MR_CONFIG"
1722 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1724 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1725 if [ -z "$url" ]; then
1726 error "cannot determine git url"
1728 echo "Registering git url: $url in $MR_CONFIG"
1729 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1731 url=`fossil remote-url`
1732 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1733 echo "Registering fossil repository $url in $MR_CONFIG"
1734 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1736 svn_trusted_checkout = svn co $url $repo
1737 svn_alt_trusted_checkout = svn checkout $url $repo
1738 git_trusted_checkout = git clone $url $repo
1739 bzr_trusted_checkout = bzr clone $url $repo
1741 hg_trusted_checkout = hg clone $url $repo
1742 darcs_trusted_checkout = darcs get $url $repo
1743 git_bare_trusted_checkout = git clone --bare $url $repo
1744 # fossil: messy to do
1750 SHOWMANFILE="man -f"
1756 SHOWMANFILE="man -l"
1759 if [ ! -e "$MR_PATH" ]; then
1760 error "cannot find program path"
1762 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1763 trap "rm -f $tmp" exit
1764 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1765 $SHOWMANFILE "$tmp" || error "man failed"
1771 if [ -s ~/.mrlog ]; then
1772 info "running offline commands"
1773 mv -f ~/.mrlog ~/.mrlog.old
1774 if ! sh -e ~/.mrlog.old; then
1775 error "offline command failed; left in ~/.mrlog.old"
1779 info "no offline commands to run"
1784 info "offline mode enabled"
1786 info "remembering command: 'mr $@'"
1787 command="mr -d '$(pwd)' $MR_SWITCHES"
1789 command="$command '$w'"
1791 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1792 echo "$command" >> ~/.mrlog
1795 ed = echo "A horse is a horse, of course, of course.."
1796 T = echo "I pity the fool."
1797 right = echo "Not found."
1799 # vim:sw=8:sts=0:ts=8:noet