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 Copyright 2007-2010 Joey Hess <joey@kitenet.net>
434 Licensed under the GNU GPL version 2 or higher.
436 http://kitenet.net/~joey/code/mr/
443 use Cwd qw(getcwd abs_path);
445 # things that can happen when mr runs a command
454 my $config_overridden=0;
464 my $directory=getcwd();
465 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
472 my (@ok, @failed, @skipped);
478 my ($action, $dir, $topdir, $subdir) = @_;
480 if (exists $rcs{$dir}) {
485 foreach my $rcs_test (
487 length $a <=> length $b
490 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
491 my ($rcs)=$rcs_test=~/(.*)_test/;
492 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
493 $test.="if my_$rcs_test; then echo $rcs; fi\n";
495 $test=$config{$topdir}{$subdir}{lib}."\n".$test
496 if exists $config{$topdir}{$subdir}{lib};
498 print "mr $action: running rcs test >>$test<<\n" if $verbose;
503 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
507 return $rcs{$dir}=undef;
510 return $rcs{$dir}=$rcs;
515 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
517 if (exists $config{$topdir}{$subdir}{$action}) {
518 return $config{$topdir}{$subdir}{$action};
525 my $rcs=rcs_test(@_);
528 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
529 return $config{$topdir}{$subdir}{$rcs."_".$action};
537 my ($topdir, $subdir) = @_;
538 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
542 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
543 my $fulldir=fulldir($topdir, $subdir);
545 $ENV{MR_CONFIG}=$configfiles{$topdir};
546 my $lib=exists $config{$topdir}{$subdir}{lib} ?
547 $config{$topdir}{$subdir}{lib}."\n" : "";
548 my $is_checkout=($action eq 'checkout');
549 my $is_update=($action =~ /update/);
554 if (! $force_checkout) {
556 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
560 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
565 return action("checkout", $dir, $topdir, $subdir);
569 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
570 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
572 if (defined $skiptest) {
573 my $test="set -e;".$lib.
574 "my_action(){ $skiptest\n }; my_action '$action'";
575 print "mr $action: running skip test >>$test<<\n" if $verbose;
576 my $ret=system($test);
578 if (($? & 127) == 2) {
579 print STDERR "mr $action: interrupted\n";
583 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
587 if ($ret >> 8 == 0) {
588 print "mr $action: $dir skipped per config file\n" if $verbose;
593 if ($is_checkout && ! -d $dir) {
594 print "mr $action: creating parent directory $dir\n" if $verbose;
595 system("mkdir", "-p", $dir);
598 if (! $no_chdir && ! chdir($dir)) {
599 print STDERR "mr $action: failed to chdir to $dir: $!\n";
602 elsif (! defined $command) {
603 my $rcs=rcs_test(@_);
604 if (! defined $rcs) {
605 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
609 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
615 print "mr $action: $fulldir\n" unless $quiet;
619 $s=~s/^\Q$fulldir\E\/?//;
620 print "mr $action: $fulldir (in subdir $s)\n" unless $quiet;
623 my $hookret=hook("pre_$action", $topdir, $subdir);
624 return $hookret if $hookret != OK;
626 $command="set -e; ".$lib.
627 "my_action(){ $command\n }; my_action ".
628 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
629 print "mr $action: running >>$command<<\n" if $verbose;
630 my $ret=system($command);
632 if (($? & 127) == 2) {
633 print STDERR "mr $action: interrupted\n";
637 print STDERR "mr $action: received signal ".($? & 127)."\n";
640 print STDERR "mr $action: failed ($ret)\n" if $verbose;
641 if ($ret >> 8 != 0) {
642 print STDERR "mr $action: command failed\n";
643 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
644 # recreate original command line to
645 # remember, and avoid recursing
647 @ARGV=('-n', $action, @orig);
648 action("remember", $dir, $topdir, $subdir);
653 print STDERR "mr $action: command died ($ret)\n";
658 if ($is_checkout && ! -d $dir) {
659 print STDERR "mr $action: $dir missing after checkout\n";;
663 my $ret=hook("post_$action", $topdir, $subdir);
664 return $ret if $ret != OK;
666 if (($is_checkout || $is_update)) {
667 my $ret=hook("fixups", $topdir, $subdir);
668 return $ret if $ret != OK;
677 my ($hook, $topdir, $subdir) = @_;
679 my $command=$config{$topdir}{$subdir}{$hook};
680 return OK unless defined $command;
681 my $lib=exists $config{$topdir}{$subdir}{lib} ?
682 $config{$topdir}{$subdir}{lib}."\n" : "";
683 my $shell="set -e;".$lib.
684 "my_hook(){ $command\n }; my_hook";
685 print "mr $hook: running >>$shell<<\n" if $verbose;
686 my $ret=system($shell);
688 if (($? & 127) == 2) {
689 print STDERR "mr $hook: interrupted\n";
693 print STDERR "mr $hook: received signal ".($? & 127)."\n";
701 # run actions on multiple repos, in parallel
711 while (@fhs or @repos) {
712 while ((!$jobs || $running < $jobs) && @repos) {
714 my $repo = shift @repos;
715 pipe(my $outfh, CHILD_STDOUT);
716 pipe(my $errfh, CHILD_STDERR);
718 unless ($pid = fork) {
719 die "mr $action: cannot fork: $!" unless defined $pid;
720 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
721 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
726 exit action($action, @$repo);
730 push @active, [$pid, $repo];
731 push @fhs, [$outfh, $errfh];
734 my ($rin, $rout) = ('','');
736 foreach my $fh (@fhs) {
737 next unless defined $fh;
738 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
739 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
741 $nfound = select($rout=$rin, undef, undef, 1);
742 foreach my $channel (0, 1) {
743 foreach my $i (0..$#fhs) {
744 next unless defined $fhs[$i];
745 my $fh = $fhs[$i][$channel];
746 next unless defined $fh;
747 if (vec($rout, fileno($fh), 1) == 1) {
749 if (sysread($fh, $r, 1024) == 0) {
751 $fhs[$i][$channel] = undef;
752 if (! defined $fhs[$i][0] &&
753 ! defined $fhs[$i][1]) {
754 waitpid($active[$i][0], 0);
755 print STDOUT $out[$i][0];
756 print STDERR $out[$i][1];
757 record($active[$i][1], $? >> 8);
759 splice(@active, $i, 1);
764 $out[$i][$channel] .= $r;
772 my $dir=shift()->[0];
777 print "\n" unless $quiet;
779 elsif ($ret == FAILED) {
781 chdir($dir) unless $no_chdir;
782 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
783 system((getpwuid($<))[8], "-i");
786 print "\n" unless $quiet;
788 elsif ($ret == SKIPPED) {
791 elsif ($ret == ABORT) {
795 die "unknown exit status $ret";
801 if (! @ok && ! @failed && ! @skipped) {
802 die "mr $action: no repositories found to work on\n";
804 print "mr $action: finished (".join("; ",
805 showstat($#ok+1, "ok", "ok"),
806 showstat($#failed+1, "failed", "failed"),
807 showstat($#skipped+1, "skipped", "skipped"),
808 ).")\n" unless $quiet;
811 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
814 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
824 return "$count ".($count > 1 ? $plural : $singular);
829 # an ordered list of repos
832 foreach my $topdir (sort keys %config) {
833 foreach my $subdir (sort keys %{$config{$topdir}}) {
837 order => $config{$topdir}{$subdir}{order},
842 $a->{order} <=> $b->{order}
844 $a->{topdir} cmp $b->{topdir}
846 $a->{subdir} cmp $b->{subdir}
852 my $topdir=$repo->{topdir};
853 my $subdir=$repo->{subdir};
854 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
859 # figure out which repos to act on
862 foreach my $repo (repolist()) {
863 my $topdir=$repo->{topdir};
864 my $subdir=$repo->{subdir};
866 next if $subdir eq 'DEFAULT';
867 my $dir=repodir($repo);
869 $dir.="/" unless $dir=~/\/$/;
870 $d.="/" unless $d=~/\/$/;
871 next if $dir ne $d && $dir !~ /^\Q$d\E/;
872 if (defined $max_depth) {
873 my @a=split('/', $dir);
874 my @b=split('/', $d);
875 do { } while (@a && @b && shift(@a) eq shift(@b));
876 next if @a > $max_depth || @b > $max_depth;
878 push @repos, [$dir, $topdir, $subdir];
881 # fallback to find a leaf repo
882 foreach my $repo (reverse repolist()) {
883 my $topdir=$repo->{topdir};
884 my $subdir=$repo->{subdir};
886 next if $subdir eq 'DEFAULT';
887 my $dir=repodir($repo);
889 $dir.="/" unless $dir=~/\/$/;
890 $d.="/" unless $d=~/\/$/;
891 if ($d=~/^\Q$dir\E/) {
892 push @repos, [$dir, $topdir, $subdir];
914 sub is_trusted_config {
915 my $config=shift; # must be abs_pathed already
917 # We always trust ~/.mrconfig.
918 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
920 return 1 if $trust_all;
922 my $trustfile=$ENV{HOME}."/.mrtrust";
924 if (! -e $trustfile) {
925 print "mr: Assuming $config is trusted.\n";
926 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
927 print "mr: and list all trusted mrconfig files in it.\n";
932 $trusted{"$ENV{HOME}/.mrconfig"}=1;
933 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
936 s/^~\//$ENV{HOME}\//;
937 $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]) {
1032 if (ref $f eq 'GLOB') {
1042 my $absf=abs_path($f);
1043 if ($loaded{$absf}) {
1048 $trusted=is_trusted_config($absf);
1050 if (! defined $dir) {
1051 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1052 if (! defined $dir) {
1057 $dir=abs_path($dir)."/";
1059 if (! exists $configfiles{$dir}) {
1060 $configfiles{$dir}=$f;
1063 # copy in defaults from first parent
1065 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1066 if ($parent eq '/') {
1069 if (exists $config{$parent} &&
1070 exists $config{$parent}{DEFAULT}) {
1071 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1076 print "mr: loading config $f\n" if $verbose;
1077 open($in, "<", $f) || die "mr: open $f: $!\n";
1080 close $in unless ref $f eq 'GLOB';
1088 next if /^\s*\#/ || /^\s*$/;
1089 if (/^\[([^\]]*)\]\s*$/) {
1093 if (! is_trusted_repo($section) ||
1094 $section eq 'ALIAS' ||
1095 $section eq 'DEFAULT') {
1096 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1099 $section=expandenv($section) if $trusted;
1100 if ($section ne 'ALIAS' &&
1101 ! exists $config{$dir}{$section} &&
1102 exists $config{$dir}{DEFAULT}) {
1104 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1107 elsif (/^(\w+)\s*=\s*(.*)/) {
1112 while (@lines && $lines[0]=~/^\s(.+)/) {
1120 # Untrusted files can only contain checkout
1122 if ($parameter ne 'checkout') {
1123 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1125 if (! is_trusted_checkout($value)) {
1126 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1130 if ($parameter eq "include") {
1131 print "mr: including output of \"$value\"\n" if $verbose;
1132 unshift @lines, `$value`;
1134 print STDERR "mr: include command exited nonzero ($?)\n";
1139 if (! defined $section) {
1140 die "$f line $.: parameter ($parameter) not in section\n";
1142 if ($section eq 'ALIAS') {
1143 $alias{$parameter}=$value;
1145 elsif ($parameter eq 'lib') {
1146 $config{$dir}{$section}{lib}.=$value."\n";
1149 $config{$dir}{$section}{$parameter}=$value;
1150 if ($parameter =~ /.*_(.*)/) {
1151 $knownactions{$1}=1;
1154 $knownactions{$parameter}=1;
1156 if ($parameter eq 'chain' &&
1157 length $dir && $section ne "DEFAULT" &&
1158 -e $dir.$section."/.mrconfig") {
1159 my $ret=system($value);
1161 if (($? & 127) == 2) {
1162 print STDERR "mr: chain test interrupted\n";
1166 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1170 push @toload, $dir.$section."/.mrconfig";
1176 die "$f line $line: parse error\n";
1185 sub startingconfig {
1186 %alias=%config=%configfiles=%knownactions=%loaded=();
1187 my $datapos=tell(DATA);
1189 seek(DATA,$datapos,0); # rewind
1194 # the section to modify or add
1195 my $targetsection=shift;
1196 # fields to change in the section
1197 # To remove a field, set its value to "".
1198 my %changefields=@_;
1204 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1209 my $formatfield=sub {
1211 my @value=split(/\n/, shift);
1213 return "$field = ".shift(@value)."\n".
1214 join("", map { "\t$_\n" } @value);
1218 while ($out[$#out] =~ /^\s*$/) {
1219 unshift @blanks, pop @out;
1221 foreach my $field (sort keys %changefields) {
1222 if (length $changefields{$field}) {
1223 push @out, "$field = $changefields{$field}\n";
1224 delete $changefields{$field};
1234 if (/^\s*\#/ || /^\s*$/) {
1237 elsif (/^\[([^\]]*)\]\s*$/) {
1238 if (defined $section &&
1239 $section eq $targetsection) {
1243 $section=expandenv($1);
1247 elsif (/^(\w+)\s*=\s(.*)/) {
1252 while (@lines && $lines[0]=~/^\s(.+)/) {
1258 if ($section eq $targetsection) {
1259 if (exists $changefields{$parameter}) {
1260 if (length $changefields{$parameter}) {
1261 $value=$changefields{$parameter};
1263 delete $changefields{$parameter};
1267 push @out, $formatfield->($parameter, $value);
1271 if (defined $section &&
1272 $section eq $targetsection) {
1275 elsif (%changefields) {
1276 push @out, "\n[$targetsection]\n";
1277 foreach my $field (sort keys %changefields) {
1278 if (length $changefields{$field}) {
1279 push @out, $formatfield->($field, $changefields{$field});
1284 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1292 # actions that do not operate on all repos
1293 if ($action eq 'help') {
1296 elsif ($action eq 'config') {
1299 elsif ($action eq 'register') {
1302 elsif ($action eq 'bootstrap') {
1305 elsif ($action eq 'remember' ||
1306 $action eq 'offline' ||
1307 $action eq 'online') {
1308 my @repos=selectrepos;
1309 action($action, @{$repos[0]}) if @repos;
1313 if (!$jobs || $jobs > 1) {
1314 mrs($action, selectrepos());
1317 foreach my $repo (selectrepos()) {
1318 record($repo, action($action, @$repo));
1324 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1329 die "mr config: not enough parameters\n";
1332 if ($section=~/^\//) {
1333 # try to convert to a path relative to the config file
1334 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1335 $dir=abs_path($dir);
1336 $dir.="/" unless $dir=~/\/$/;
1337 if ($section=~/^\Q$dir\E(.*)/) {
1343 if (/^([^=]+)=(.*)$/) {
1344 $changefields{$1}=$2;
1348 foreach my $topdir (sort keys %config) {
1349 if (exists $config{$topdir}{$section} &&
1350 exists $config{$topdir}{$section}{$_}) {
1351 print $config{$topdir}{$section}{$_}."\n";
1353 last if $section eq 'DEFAULT';
1357 die "mr config: $section $_ not set\n";
1361 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1366 if ($config_overridden) {
1367 # Find the directory that the specified config file is
1369 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1372 # Find the closest known mrconfig file to the current
1374 $directory.="/" unless $directory=~/\/$/;
1376 foreach my $topdir (reverse sort keys %config) {
1377 next unless length $topdir;
1378 if ($directory=~/^\Q$topdir\E/) {
1379 $ENV{MR_CONFIG}=$configfiles{$topdir};
1385 if (! $foundconfig) {
1386 $directory=""; # no config file, use builtin
1390 my $subdir=shift @ARGV;
1391 if (! chdir($subdir)) {
1392 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1396 $ENV{MR_REPO}=getcwd();
1397 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1398 if (! defined $command) {
1399 die "mr register: unknown repository type\n";
1402 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1403 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1404 "my_action(){ $command\n }; my_action ".
1405 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1406 print "mr register: running >>$command<<\n" if $verbose;
1407 exec($command) || die "exec: $!";
1411 my $url=shift @ARGV;
1412 my $dir=shift @ARGV || ".";
1414 if (! defined $url || ! length $url) {
1415 die "mr: bootstrap requires url\n";
1418 # Download the config file to a temporary location.
1419 eval q{use File::Temp};
1421 my $tmpconfig=File::Temp->new();
1422 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1423 push(@curlargs, "-k") if $insecure;
1424 my $curlstatus = system(@curlargs);
1425 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1426 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1429 system("mkdir", "-p", $dir);
1431 chdir($dir) || die "chdir $dir: $!";
1433 # Special case to handle checkout of the "." repo, which
1434 # would normally be skipped.
1435 my $topdir=abs_path(".")."/";
1436 my @repo=($topdir, $topdir, ".");
1437 loadconfig($tmpconfig, $topdir);
1438 record(\@repo, action("checkout", @repo, 1))
1439 if exists $config{$topdir}{"."}{"checkout"};
1441 if (-e ".mrconfig") {
1442 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1445 eval q{use File::Copy};
1447 move($tmpconfig, ".mrconfig") || die "rename: $!";
1450 # Reload the config file (in case we got a different version)
1451 # and checkout everything else.
1453 loadconfig(".mrconfig");
1454 dispatch("checkout");
1455 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1456 showstats("bootstrap");
1460 # alias expansion and command stemming
1463 if (exists $alias{$action}) {
1464 $action=$alias{$action};
1466 if (! exists $knownactions{$action}) {
1467 my @matches = grep { /^\Q$action\E/ }
1468 keys %knownactions, keys %alias;
1469 if (@matches == 1) {
1470 $action=$matches[0];
1472 elsif (@matches == 0) {
1473 die "mr: unknown action \"$action\" (known actions: ".
1474 join(", ", sort keys %knownactions).")\n";
1477 die "mr: ambiguous action \"$action\" (matches: ".
1478 join(", ", @matches).")\n";
1484 sub find_nearest_mrconfig {
1486 while (length $dir) {
1487 if (-e "$dir/.mrconfig") {
1488 return "$dir/.mrconfig";
1490 $dir=~s/\/[^\/]*$//;
1492 die "no .mrconfig found in path\n";
1497 Getopt::Long::Configure("bundling", "no_permute");
1498 my $result=GetOptions(
1499 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1500 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1501 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1502 "v|verbose" => \$verbose,
1503 "q|quiet" => \$quiet,
1504 "s|stats" => \$stats,
1505 "k|insecure" => \$insecure,
1506 "i|interactive" => \$interactive,
1507 "n|no-recurse:i" => \$max_depth,
1508 "j|jobs:i" => \$jobs,
1509 "t|trust-all" => \$trust_all,
1511 if (! $result || @ARGV < 1) {
1512 die("Usage: mr [options] action [params ...]\n".
1513 "(Use mr help for man page.)\n");
1516 $ENV{MR_SWITCHES}="";
1517 foreach my $option (@saved) {
1518 last if $option eq $ARGV[0];
1519 $ENV{MR_SWITCHES}.="$option ";
1525 print STDERR "mr: interrupted\n";
1529 # This can happen if it's run in a directory that was removed
1530 # or other strangeness.
1531 if (! defined $directory) {
1532 die("mr: failed to determine working directory\n");
1534 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1535 # the config file might be a symlink to elsewhere, and the directory it's
1536 # in is significant.
1537 if ($ENV{MR_CONFIG} !~ /^\//) {
1538 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1540 # Try to set MR_PATH to the path to the program.
1542 use FindBin qw($Bin $Script);
1543 $ENV{MR_PATH}=$Bin."/".$Script;
1551 elsif (! @ok && @skipped) {
1564 loadconfig($ENV{MR_CONFIG});
1565 #use Data::Dumper; print Dumper(\%config);
1567 my $action=expandaction(shift @ARGV);
1574 # Finally, some useful actions that mr knows about by default.
1575 # These can be overridden in ~/.mrconfig.
1590 echo "mr (warning): $@" >&2
1596 if [ -z "$1" ] || [ -z "$2" ]; then
1597 error "mr: usage: hours_since action num"
1599 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1600 if [ -e "$MR_REPO/$dir" ]; then
1601 flagfile="$MR_REPO/$dir/.mr_last$1"
1605 if [ -z "$flagfile" ]; then
1606 error "cannot determine flag filename"
1608 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1609 if [ "$delta" -lt "$2" ]; then
1617 svn_test = test -d "$MR_REPO"/.svn
1618 git_test = test -d "$MR_REPO"/.git
1619 bzr_test = test -d "$MR_REPO"/.bzr
1620 cvs_test = test -d "$MR_REPO"/CVS
1621 hg_test = test -d "$MR_REPO"/.hg
1622 darcs_test = test -d "$MR_REPO"/_darcs
1623 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1625 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1626 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1627 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1629 svn_update = svn update "$@"
1630 git_update = git pull "$@"
1631 bzr_update = bzr merge --pull "$@"
1632 cvs_update = cvs update "$@"
1633 hg_update = hg pull "$@" && hg update "$@"
1634 darcs_update = darcs pull -a "$@"
1635 fossil_update = fossil pull "$@"
1637 svn_status = svn status "$@"
1638 git_status = git status -s "$@" || true
1639 bzr_status = bzr status --short "$@"
1640 cvs_status = cvs status "$@"
1641 hg_status = hg status "$@"
1642 darcs_status = darcs whatsnew -ls "$@" || true
1643 fossil_status = fossil changes "$@"
1645 svn_commit = svn commit "$@"
1646 git_commit = git commit -a "$@" && git push --all
1647 bzr_commit = bzr commit "$@" && bzr push
1648 cvs_commit = cvs commit "$@"
1649 hg_commit = hg commit -m "$@" && hg push
1650 darcs_commit = darcs record -a -m "$@" && darcs push -a
1651 fossil_commit = fossil commit "$@"
1653 git_record = git commit -a "$@"
1654 bzr_record = bzr commit "$@"
1655 hg_record = hg commit -m "$@"
1656 darcs_record = darcs record -a -m "$@"
1657 fossil_record = fossil commit "$@"
1660 git_push = git push "$@"
1661 bzr_push = bzr push "$@"
1663 hg_push = hg push "$@"
1664 darcs_push = darcs push -a "$@"
1665 fossil_push = fossil push "$@"
1667 svn_diff = svn diff "$@"
1668 git_diff = git diff "$@"
1669 bzr_diff = bzr diff "$@"
1670 cvs_diff = cvs diff "$@"
1671 hg_diff = hg diff "$@"
1672 darcs_diff = darcs diff -u "$@"
1673 fossil_diff = fossil diff "$@"
1675 svn_log = svn log "$@"
1676 git_log = git log "$@"
1677 bzr_log = bzr log "$@"
1678 cvs_log = cvs log "$@"
1679 hg_log = hg log "$@"
1680 darcs_log = darcs changes "$@"
1681 git_bare_log = git log "$@"
1682 fossil_log = fossil timeline "$@"
1685 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1686 if [ -z "$url" ]; then
1687 error "cannot determine svn url"
1689 echo "Registering svn url: $url in $MR_CONFIG"
1690 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1692 url="`LC_ALL=C git config --get remote.origin.url`" || true
1693 if [ -z "$url" ]; then
1694 error "cannot determine git url"
1696 echo "Registering git url: $url in $MR_CONFIG"
1697 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1699 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1700 if [ -z "$url" ]; then
1701 error "cannot determine bzr url"
1703 echo "Registering bzr url: $url in $MR_CONFIG"
1704 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1706 repo=`cat CVS/Repository`
1708 if [ -z "$root" ]; then
1709 error "cannot determine cvs root"
1711 echo "Registering cvs repository $repo at root $root"
1712 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1714 url=`hg showconfig paths.default`
1715 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1716 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1718 url=`cat _darcs/prefs/defaultrepo`
1719 echo "Registering darcs repository $url in $MR_CONFIG"
1720 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1722 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1723 if [ -z "$url" ]; then
1724 error "cannot determine git url"
1726 echo "Registering git url: $url in $MR_CONFIG"
1727 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1729 url=`fossil remote-url`
1730 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1731 echo "Registering fossil repository $url in $MR_CONFIG"
1732 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1734 svn_trusted_checkout = svn co $url $repo
1735 svn_alt_trusted_checkout = svn checkout $url $repo
1736 git_trusted_checkout = git clone $url $repo
1737 bzr_trusted_checkout = bzr clone $url $repo
1739 hg_trusted_checkout = hg clone $url $repo
1740 darcs_trusted_checkout = darcs get $url $repo
1741 git_bare_trusted_checkout = git clone --bare $url $repo
1742 # fossil: messy to do
1748 SHOWMANFILE="man -f"
1754 SHOWMANFILE="man -l"
1757 if [ ! -e "$MR_PATH" ]; then
1758 error "cannot find program path"
1760 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1761 trap "rm -f $tmp" exit
1762 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1763 $SHOWMANFILE "$tmp" || error "man failed"
1769 if [ -s ~/.mrlog ]; then
1770 info "running offline commands"
1771 mv -f ~/.mrlog ~/.mrlog.old
1772 if ! sh -e ~/.mrlog.old; then
1773 error "offline command failed; left in ~/.mrlog.old"
1777 info "no offline commands to run"
1782 info "offline mode enabled"
1784 info "remembering command: 'mr $@'"
1785 command="mr -d '$(pwd)' $MR_SWITCHES"
1787 command="$command '$w'"
1789 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1790 echo "$command" >> ~/.mrlog
1793 ed = echo "A horse is a horse, of course, of course.."
1794 T = echo "I pity the fool."
1795 right = echo "Not found."
1797 # vim:sw=8:sts=0:ts=8:noet