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"]
23 B<mr> [options] bootstrap url [directory]
25 B<mr> [options] register [repository]
27 B<mr> [options] config section ["parameter=[value]" ...]
29 B<mr> [options] action [params ...]
31 B<mr> [options] [online|offline]
33 B<mr> [options] remember action [params ...]
37 B<mr> is a Multiple Repository management tool. It can checkout, update, or
38 perform other actions on a set of repositories as if they were one combined
39 repository. It supports any combination of subversion, git, cvs, mercurial,
40 bzr, darcs and fossil repositories, and support for other revision
41 control systems can easily be added.
43 B<mr> cds into and operates on all registered repositories at or below your
44 working directory. Or, if you are in a subdirectory of a repository that
45 contains no other registered repositories, it will stay in that directory,
46 and work on only that repository,
48 B<mr> is configured by .mrconfig files, which list the repositories. It
49 starts by reading the .mrconfig file in your home directory, and this can
50 in turn chain load .mrconfig files from repositories.
52 These predefined commands should be fairly familiar to users of any revision
57 =item checkout (or co)
59 Checks out any repositories that are not already checked out.
63 Updates each repository from its configured remote repository.
65 If a repository isn't checked out yet, it will first check it out.
69 Displays a status report for each repository, showing what
70 uncommitted changes are present in the repository.
74 Commits changes to each repository. (By default, changes are pushed to the
75 remote repository too, when using distributed systems like git. If you
76 don't like this default, you can change it in your .mrconfig, or use record
79 The optional -m parameter allows specifying a commit message.
83 Records changes to the local repository, but does not push them to the
84 remote repository. Only supported for distributed revision control systems.
86 The optional -m parameter allows specifying a commit message.
90 Pushes committed local changes to the remote repository. A no-op for
91 centralized revision control systems.
95 Show a diff of uncommitted changes.
103 These commands are also available:
107 =item bootstrap url [directory]
109 Causes mr to download the url, and use it as a .mrconfig file
110 to checkout the repositories listed in it, into the specified directory.
112 The directory will be created if it does not exist. If no directory is
113 specified, the current directory will be used.
115 If the .mrconfig file includes a repository named ".", that
116 is checked out into the top of the specified directory.
120 List the repositories that mr will act on.
124 Register an existing repository in a mrconfig file. By default, the
125 repository in the current directory is registered, or you can specify a
126 directory to register.
128 The mrconfig file that is modified is chosen by either the -c option, or by
129 looking for the closest known one at or below the current directory.
133 Adds, modifies, removes, or prints a value from a mrconfig file. The next
134 parameter is the name of the section the value is in. To add or modify
135 values, use one or more instances of "parameter=value". Use "parameter=" to
136 remove a parameter. Use just "parameter" to get the value of a parameter.
138 For example, to add (or edit) a repository in src/foo:
140 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
142 To show the command that mr uses to update the repository in src/foo:
144 mr config src/foo update
146 To see the built-in library of shell functions contained in mr:
148 mr config DEFAULT lib
150 The ~/.mrconfig file is used by default. To use a different config file,
155 Advises mr that it is in offline mode. Any commands that fail in
156 offline mode will be remembered, and retried when mr is told it's online.
160 Advices mr that it is in online mode again. Commands that failed while in
161 offline mode will be re-run.
165 Remember a command, to be run later when mr re-enters online mode. This
166 implicitly puts mr into offline mode. The command can be any regular mr
167 command. This is useful when you know that a command will fail due to being
168 offline, and so don't want to run it right now at all, but just remember
169 to run it when you go back online.
177 Actions can be abbreviated to any unambiguous substring, so
178 "mr st" is equivalent to "mr status", and "mr up" is equivalent to "mr
181 Additional parameters can be passed to most commands, and are passed on
182 unchanged to the underlying revision control system. This is mostly useful
183 if the repositories mr will act on all use the same revision control
192 =item --directory directory
194 Specifies the topmost directory that B<mr> should work in. The default is
195 the current working directory.
199 =item --config mrconfig
201 Use the specified mrconfig file. The default is B<~/.mrconfig>
207 Search in the current directory, and its parent directories and use
208 the first B<.mrconfig> found, instead of the default B<~/.mrconfig>.
226 Accept untrusted SSL certificates when bootstrapping.
232 Expand the statistics line displayed at the end to include information
233 about exactly which repositories failed and were skipped, if any.
239 Interactive mode. If a repository fails to be processed, a subshell will be
240 started which you can use to resolve or investigate the problem. Exit the
241 subshell to continue the mr run.
245 =item --no-recurse [number]
247 If no number if specified, just operate on the repository for the current
248 directory, do not recurse into deeper repositories.
250 If a number is specified, will recurse into repositories at most that many
251 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
252 but not ./src/packages/bar.
256 =item --jobs [number]
258 Run the specified number of jobs in parallel, or an unlimited number of jobs
259 with no number specified. This can greatly speed up operations such as updates.
260 It is not recommended for interactive operations.
262 Note that running more than 10 jobs at a time is likely to run afoul of
263 ssh connection limits. Running between 3 and 5 jobs at a time will yield
264 a good speedup in updates without loading the machine too much.
270 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
275 =head1 MRCONFIG FILES
277 Here is an example .mrconfig file:
280 checkout = svn co svn://svn.example.com/src/trunk src
284 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
286 git checkout -b mybranch origin/master
288 The .mrconfig file uses a variant of the INI file format. Lines starting with
289 "#" are comments. Values can be continued to the following line by
290 indenting the line with whitespace.
292 The "DEFAULT" section allows setting default values for the sections that
295 The "ALIAS" section allows adding aliases for actions. Each parameter
296 is an alias, and its value is the action to use.
298 All other sections add repositories. The section header specifies the
299 directory where the repository is located. This is relative to the directory
300 that contains the mrconfig file, but you can also choose to use absolute
301 paths. (Note that you can use environment variables in section names; they
302 will be passed through the shell for expansion. For example,
303 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
305 Within a section, each parameter defines a shell command to run to handle a
306 given action. mr contains default handlers for "update", "status",
307 "commit", and other standard actions. Normally you only need to specify what
308 to do for "checkout".
310 Note that these shell commands are run in a "set -e" shell
311 environment, where any additional parameters you pass are available in
312 "$@". The "checkout" command is run in the parent of the repository
313 directory, since the repository isn't checked out yet. All other commands
314 are run inside the repository, though not necessarily at the top of it.
316 The "MR_REPO" environment variable is set to the path to the top of the
317 repository. (For the "register" action, "MR_REPO" is instead set to the
318 basename of the directory that should be created when checking the
321 The "MR_CONFIG" environment variable is set to the .mrconfig file
322 that defines the repo being acted on, or, if the repo is not yet in a config
323 file, the .mrconfig file that should be modified to register the repo.
325 A few parameters have special meanings:
331 If the "skip" parameter is set and its command returns true, then B<mr>
332 will skip acting on that repository. The command is passed the action
335 Here are two examples. The first skips the repo unless
336 mr is run by joey. The second uses the hours_since function
337 (included in mr's built-in library) to skip updating the repo unless it's
338 been at least 12 hours since the last update.
340 skip = test `whoami` != joey
341 skip = [ "$1" = update ] && ! hours_since "$1" 12
345 The "order" parameter can be used to override the default ordering of
346 repositories. The default order value is 10. Use smaller values to make
347 repositories be processed earlier, and larger values to make repositories
350 Note that if a repository is located in a subdirectory of another
351 repository, ordering it to be processed earlier is not recommended.
355 If the "chain" parameter is set and its command returns true, then B<mr>
356 will try to load a .mrconfig file from the root of the repository.
360 If the "include" parameter is set, its command is ran, and should output
361 additional mrconfig file content. The content is included as if it were
362 part of the including file.
364 Unlike all other parameters, this parameter does not need to be placed
369 The "lib" parameter can specify some shell code that will be run before each
370 command, this can be a useful way to define shell functions for other commands
375 If the "fixups" parameter is set, its command is run whenever a repository
376 is checked out, or updated. This provides an easy way to do things
377 like permissions fixups, or other tweaks to the repository content,
378 whenever the repository is changed.
382 If a "pre_action" parameter is set, its command is run before mr performs the
383 specified action. Similarly, "post_action" parameters are run after mr
384 successfully performs the specified action. For example, "pre_commit" is
385 run before committing; "post_update" is run after updating.
389 When looking for a command to run for a given action, mr first looks for
390 a parameter with the same name as the action. If that is not found, it
391 looks for a parameter named "rcs_action" (substituting in the name of the
392 revision control system and the action). The name of the revision control
393 system is itself determined by running each defined "rcs_test" action,
396 Internally, mr has settings for "git_update", "svn_update", etc. To change
397 the action that is performed for a given revision control system, you can
398 override these rcs specific actions. To add a new revision control system,
399 you can just add rcs specific actions for it.
401 The ~/.mrlog file contains commands that mr has remembered to run later,
402 due to being offline. You can delete or edit this file to remove commands,
403 or even to add other commands for 'mr online' to run. If the file is
404 present, mr assumes it is in offline mode.
406 =head1 UNTRUSTED MRCONFIG FILES
408 Since mrconfig files can contain arbitrary shell commands, they can do
409 anything. This flexibility is good, but it also allows a malicious mrconfig
410 file to delete your whole home directory. Such a file might be contained
411 inside a repository that your main ~/.mrconfig checks out and chains to. To
412 avoid worries about evil commands in a mrconfig file, mr
413 has the ability to read mrconfig files in untrusted mode. Such files are
414 limited to running only known safe commands (like "git clone") in a
415 carefully checked manner.
417 By default, mr trusts all mrconfig files. (This default will change in a
418 future release!) But if you have a ~/.mrtrust file, mr will only trust
419 mrconfig files that are listed within it. (One file per line.) All other
420 files will be treated as untrusted.
424 mr can be extended to support things such as unison and git-svn. Some
425 files providing such extensions are available in /usr/share/mr/. See
426 the documentation in the files for details about using them.
430 Copyright 2007-2010 Joey Hess <joey@kitenet.net>
432 Licensed under the GNU GPL version 2 or higher.
434 http://kitenet.net/~joey/code/mr/
441 use Cwd qw(getcwd abs_path);
443 # things that can happen when mr runs a command
452 my $config_overridden=0;
462 my $directory=getcwd();
463 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
470 my (@ok, @failed, @skipped);
476 my ($action, $dir, $topdir, $subdir) = @_;
478 if (exists $rcs{$dir}) {
483 foreach my $rcs_test (
485 length $a <=> length $b
488 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
489 my ($rcs)=$rcs_test=~/(.*)_test/;
490 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
491 $test.="if my_$rcs_test; then echo $rcs; fi\n";
493 $test=$config{$topdir}{$subdir}{lib}."\n".$test
494 if exists $config{$topdir}{$subdir}{lib};
496 print "mr $action: running rcs test >>$test<<\n" if $verbose;
501 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
505 return $rcs{$dir}=undef;
508 return $rcs{$dir}=$rcs;
513 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
515 if (exists $config{$topdir}{$subdir}{$action}) {
516 return $config{$topdir}{$subdir}{$action};
523 my $rcs=rcs_test(@_);
526 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
527 return $config{$topdir}{$subdir}{$rcs."_".$action};
535 my ($topdir, $subdir) = @_;
536 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
540 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
541 my $fulldir=fulldir($topdir, $subdir);
543 $ENV{MR_CONFIG}=$configfiles{$topdir};
544 my $lib=exists $config{$topdir}{$subdir}{lib} ?
545 $config{$topdir}{$subdir}{lib}."\n" : "";
546 my $is_checkout=($action eq 'checkout');
547 my $is_update=($action =~ /update/);
552 if (! $force_checkout) {
554 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
558 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
563 return action("checkout", $dir, $topdir, $subdir);
567 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
568 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
570 if (defined $skiptest) {
571 my $test="set -e;".$lib.
572 "my_action(){ $skiptest\n }; my_action '$action'";
573 print "mr $action: running skip test >>$test<<\n" if $verbose;
574 my $ret=system($test);
576 if (($? & 127) == 2) {
577 print STDERR "mr $action: interrupted\n";
581 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
585 if ($ret >> 8 == 0) {
586 print "mr $action: $dir skipped per config file\n" if $verbose;
591 if ($is_checkout && ! -d $dir) {
592 print "mr $action: creating parent directory $dir\n" if $verbose;
593 system("mkdir", "-p", $dir);
596 if (! $no_chdir && ! chdir($dir)) {
597 print STDERR "mr $action: failed to chdir to $dir: $!\n";
600 elsif (! defined $command) {
601 my $rcs=rcs_test(@_);
602 if (! defined $rcs) {
603 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
607 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
613 print "mr $action: $fulldir\n" unless $quiet;
617 $s=~s/^\Q$fulldir\E\/?//;
618 print "mr $action: $fulldir (in subdir $s)\n" unless $quiet;
621 my $hookret=hook("pre_$action", $topdir, $subdir);
622 return $hookret if $hookret != OK;
624 $command="set -e; ".$lib.
625 "my_action(){ $command\n }; my_action ".
626 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
627 print "mr $action: running >>$command<<\n" if $verbose;
628 my $ret=system($command);
630 if (($? & 127) == 2) {
631 print STDERR "mr $action: interrupted\n";
635 print STDERR "mr $action: received signal ".($? & 127)."\n";
638 print STDERR "mr $action: failed ($ret)\n" if $verbose;
639 if ($ret >> 8 != 0) {
640 print STDERR "mr $action: command failed\n";
641 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
642 # recreate original command line to
643 # remember, and avoid recursing
645 @ARGV=('-n', $action, @orig);
646 action("remember", $dir, $topdir, $subdir);
651 print STDERR "mr $action: command died ($ret)\n";
656 if ($is_checkout && ! -d $dir) {
657 print STDERR "mr $action: $dir missing after checkout\n";;
661 my $ret=hook("post_$action", $topdir, $subdir);
662 return $ret if $ret != OK;
664 if (($is_checkout || $is_update)) {
665 my $ret=hook("fixups", $topdir, $subdir);
666 return $ret if $ret != OK;
675 my ($hook, $topdir, $subdir) = @_;
677 my $command=$config{$topdir}{$subdir}{$hook};
678 return OK unless defined $command;
679 my $lib=exists $config{$topdir}{$subdir}{lib} ?
680 $config{$topdir}{$subdir}{lib}."\n" : "";
681 my $shell="set -e;".$lib.
682 "my_hook(){ $command\n }; my_hook";
683 print "mr $hook: running >>$shell<<\n" if $verbose;
684 my $ret=system($shell);
686 if (($? & 127) == 2) {
687 print STDERR "mr $hook: interrupted\n";
691 print STDERR "mr $hook: received signal ".($? & 127)."\n";
699 # run actions on multiple repos, in parallel
709 while (@fhs or @repos) {
710 while ((!$jobs || $running < $jobs) && @repos) {
712 my $repo = shift @repos;
713 pipe(my $outfh, CHILD_STDOUT);
714 pipe(my $errfh, CHILD_STDERR);
716 unless ($pid = fork) {
717 die "mr $action: cannot fork: $!" unless defined $pid;
718 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
719 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
724 exit action($action, @$repo);
728 push @active, [$pid, $repo];
729 push @fhs, [$outfh, $errfh];
732 my ($rin, $rout) = ('','');
734 foreach my $fh (@fhs) {
735 next unless defined $fh;
736 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
737 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
739 $nfound = select($rout=$rin, undef, undef, 1);
740 foreach my $channel (0, 1) {
741 foreach my $i (0..$#fhs) {
742 next unless defined $fhs[$i];
743 my $fh = $fhs[$i][$channel];
744 next unless defined $fh;
745 if (vec($rout, fileno($fh), 1) == 1) {
747 if (sysread($fh, $r, 1024) == 0) {
749 $fhs[$i][$channel] = undef;
750 if (! defined $fhs[$i][0] &&
751 ! defined $fhs[$i][1]) {
752 waitpid($active[$i][0], 0);
753 print STDOUT $out[$i][0];
754 print STDERR $out[$i][1];
755 record($active[$i][1], $? >> 8);
757 splice(@active, $i, 1);
762 $out[$i][$channel] .= $r;
770 my $dir=shift()->[0];
775 print "\n" unless $quiet;
777 elsif ($ret == FAILED) {
779 chdir($dir) unless $no_chdir;
780 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
781 system((getpwuid($<))[8], "-i");
784 print "\n" unless $quiet;
786 elsif ($ret == SKIPPED) {
789 elsif ($ret == ABORT) {
793 die "unknown exit status $ret";
799 if (! @ok && ! @failed && ! @skipped) {
800 die "mr $action: no repositories found to work on\n";
802 print "mr $action: finished (".join("; ",
803 showstat($#ok+1, "ok", "ok"),
804 showstat($#failed+1, "failed", "failed"),
805 showstat($#skipped+1, "skipped", "skipped"),
806 ).")\n" unless $quiet;
809 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
812 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
822 return "$count ".($count > 1 ? $plural : $singular);
827 # an ordered list of repos
830 foreach my $topdir (sort keys %config) {
831 foreach my $subdir (sort keys %{$config{$topdir}}) {
835 order => $config{$topdir}{$subdir}{order},
840 $a->{order} <=> $b->{order}
842 $a->{topdir} cmp $b->{topdir}
844 $a->{subdir} cmp $b->{subdir}
850 my $topdir=$repo->{topdir};
851 my $subdir=$repo->{subdir};
852 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
857 # figure out which repos to act on
860 foreach my $repo (repolist()) {
861 my $topdir=$repo->{topdir};
862 my $subdir=$repo->{subdir};
864 next if $subdir eq 'DEFAULT';
865 my $dir=repodir($repo);
867 $dir.="/" unless $dir=~/\/$/;
868 $d.="/" unless $d=~/\/$/;
869 next if $dir ne $d && $dir !~ /^\Q$d\E/;
870 if (defined $max_depth) {
871 my @a=split('/', $dir);
872 my @b=split('/', $d);
873 do { } while (@a && @b && shift(@a) eq shift(@b));
874 next if @a > $max_depth || @b > $max_depth;
876 push @repos, [$dir, $topdir, $subdir];
879 # fallback to find a leaf repo
880 foreach my $repo (reverse repolist()) {
881 my $topdir=$repo->{topdir};
882 my $subdir=$repo->{subdir};
884 next if $subdir eq 'DEFAULT';
885 my $dir=repodir($repo);
887 $dir.="/" unless $dir=~/\/$/;
888 $d.="/" unless $d=~/\/$/;
889 if ($d=~/^\Q$dir\E/) {
890 push @repos, [$dir, $topdir, $subdir];
912 sub is_trusted_config {
913 my $config=shift; # must be abs_pathed already
915 # We always trust ~/.mrconfig.
916 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
918 return 1 if $trust_all;
920 my $trustfile=$ENV{HOME}."/.mrtrust";
922 if (! -e $trustfile) {
923 print "mr: Assuming $config is trusted.\n";
924 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
925 print "mr: and list all trusted mrconfig files in it.\n";
930 $trusted{"$ENV{HOME}/.mrconfig"}=1;
931 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
934 s/^~\//$ENV{HOME}\//;
935 $trusted{abs_path($_)}=1;
940 return $trusted{$config};
944 sub is_trusted_repo {
947 # Tightly limit what is allowed in a repo name.
948 # No ../, no absolute paths, and no unusual filenames
949 # that might try to escape to the shell.
950 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
951 $repo !~ /\.\./ && $repo !~ /^\//;
954 sub is_trusted_checkout {
957 # To determine if the command is safe, compare it with the
958 # *_trusted_checkout config settings. Those settings are
959 # templates for allowed commands, so make sure that each word
960 # of the command matches the corresponding word of the template.
963 foreach my $word (split(' ', $command)) {
965 if ($word=~/^'(.*)'$/) {
968 elsif ($word=~/^"(.*)"$/) {
975 foreach my $key (grep { /_trusted_checkout$/ }
976 keys %{$config{''}{DEFAULT}}) {
977 my @twords=split(' ', $config{''}{DEFAULT}{$key});
978 next if @words > @twords;
982 for (my $c=0; $c < @twords && $match; $c++) {
983 if ($twords[$c] eq '$url') {
984 # Match all the typical characters found in
985 # urls, plus @ which svn can use. Note
986 # that the "url" might also be a local
989 defined $words[$c] &&
990 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
994 elsif ($twords[$c] eq '$repo') {
995 # If a repo is not specified, assume it
996 # will be the last path component of the
997 # url, or something derived from it, and
999 if (! defined $words[$c] && defined $url) {
1000 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1004 defined $words[$c] &&
1005 is_trusted_repo($words[$c])
1008 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1030 if (ref $f eq 'GLOB') {
1040 my $absf=abs_path($f);
1041 if ($loaded{$absf}) {
1046 $trusted=is_trusted_config($absf);
1048 if (! defined $dir) {
1049 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1050 if (! defined $dir) {
1055 $dir=abs_path($dir)."/";
1057 if (! exists $configfiles{$dir}) {
1058 $configfiles{$dir}=$f;
1061 # copy in defaults from first parent
1063 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1064 if ($parent eq '/') {
1067 if (exists $config{$parent} &&
1068 exists $config{$parent}{DEFAULT}) {
1069 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1074 print "mr: loading config $f\n" if $verbose;
1075 open($in, "<", $f) || die "mr: open $f: $!\n";
1078 close $in unless ref $f eq 'GLOB';
1086 next if /^\s*\#/ || /^\s*$/;
1087 if (/^\[([^\]]*)\]\s*$/) {
1091 if (! is_trusted_repo($section) ||
1092 $section eq 'ALIAS' ||
1093 $section eq 'DEFAULT') {
1094 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1097 $section=expandenv($section) if $trusted;
1098 if ($section ne 'ALIAS' &&
1099 ! exists $config{$dir}{$section} &&
1100 exists $config{$dir}{DEFAULT}) {
1102 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1105 elsif (/^(\w+)\s*=\s*(.*)/) {
1110 while (@lines && $lines[0]=~/^\s(.+)/) {
1118 # Untrusted files can only contain checkout
1120 if ($parameter ne 'checkout') {
1121 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1123 if (! is_trusted_checkout($value)) {
1124 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1128 if ($parameter eq "include") {
1129 print "mr: including output of \"$value\"\n" if $verbose;
1130 unshift @lines, `$value`;
1132 print STDERR "mr: include command exited nonzero ($?)\n";
1137 if (! defined $section) {
1138 die "$f line $.: parameter ($parameter) not in section\n";
1140 if ($section eq 'ALIAS') {
1141 $alias{$parameter}=$value;
1143 elsif ($parameter eq 'lib') {
1144 $config{$dir}{$section}{lib}.=$value."\n";
1147 $config{$dir}{$section}{$parameter}=$value;
1148 if ($parameter =~ /.*_(.*)/) {
1149 $knownactions{$1}=1;
1152 $knownactions{$parameter}=1;
1154 if ($parameter eq 'chain' &&
1155 length $dir && $section ne "DEFAULT" &&
1156 -e $dir.$section."/.mrconfig") {
1157 my $ret=system($value);
1159 if (($? & 127) == 2) {
1160 print STDERR "mr: chain test interrupted\n";
1164 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1168 push @toload, $dir.$section."/.mrconfig";
1174 die "$f line $line: parse error\n";
1183 sub startingconfig {
1184 %alias=%config=%configfiles=%knownactions=%loaded=();
1185 my $datapos=tell(DATA);
1187 seek(DATA,$datapos,0); # rewind
1192 # the section to modify or add
1193 my $targetsection=shift;
1194 # fields to change in the section
1195 # To remove a field, set its value to "".
1196 my %changefields=@_;
1202 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1207 my $formatfield=sub {
1209 my @value=split(/\n/, shift);
1211 return "$field = ".shift(@value)."\n".
1212 join("", map { "\t$_\n" } @value);
1216 while ($out[$#out] =~ /^\s*$/) {
1217 unshift @blanks, pop @out;
1219 foreach my $field (sort keys %changefields) {
1220 if (length $changefields{$field}) {
1221 push @out, "$field = $changefields{$field}\n";
1222 delete $changefields{$field};
1232 if (/^\s*\#/ || /^\s*$/) {
1235 elsif (/^\[([^\]]*)\]\s*$/) {
1236 if (defined $section &&
1237 $section eq $targetsection) {
1241 $section=expandenv($1);
1245 elsif (/^(\w+)\s*=\s(.*)/) {
1250 while (@lines && $lines[0]=~/^\s(.+)/) {
1256 if ($section eq $targetsection) {
1257 if (exists $changefields{$parameter}) {
1258 if (length $changefields{$parameter}) {
1259 $value=$changefields{$parameter};
1261 delete $changefields{$parameter};
1265 push @out, $formatfield->($parameter, $value);
1269 if (defined $section &&
1270 $section eq $targetsection) {
1273 elsif (%changefields) {
1274 push @out, "\n[$targetsection]\n";
1275 foreach my $field (sort keys %changefields) {
1276 if (length $changefields{$field}) {
1277 push @out, $formatfield->($field, $changefields{$field});
1282 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1290 # actions that do not operate on all repos
1291 if ($action eq 'help') {
1294 elsif ($action eq 'config') {
1297 elsif ($action eq 'register') {
1300 elsif ($action eq 'bootstrap') {
1303 elsif ($action eq 'remember' ||
1304 $action eq 'offline' ||
1305 $action eq 'online') {
1306 my @repos=selectrepos;
1307 action($action, @{$repos[0]}) if @repos;
1311 if (!$jobs || $jobs > 1) {
1312 mrs($action, selectrepos());
1315 foreach my $repo (selectrepos()) {
1316 record($repo, action($action, @$repo));
1322 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1327 die "mr config: not enough parameters\n";
1330 if ($section=~/^\//) {
1331 # try to convert to a path relative to the config file
1332 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1333 $dir=abs_path($dir);
1334 $dir.="/" unless $dir=~/\/$/;
1335 if ($section=~/^\Q$dir\E(.*)/) {
1341 if (/^([^=]+)=(.*)$/) {
1342 $changefields{$1}=$2;
1346 foreach my $topdir (sort keys %config) {
1347 if (exists $config{$topdir}{$section} &&
1348 exists $config{$topdir}{$section}{$_}) {
1349 print $config{$topdir}{$section}{$_}."\n";
1351 last if $section eq 'DEFAULT';
1355 die "mr config: $section $_ not set\n";
1359 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1364 if ($config_overridden) {
1365 # Find the directory that the specified config file is
1367 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1370 # Find the closest known mrconfig file to the current
1372 $directory.="/" unless $directory=~/\/$/;
1374 foreach my $topdir (reverse sort keys %config) {
1375 next unless length $topdir;
1376 if ($directory=~/^\Q$topdir\E/) {
1377 $ENV{MR_CONFIG}=$configfiles{$topdir};
1383 if (! $foundconfig) {
1384 $directory=""; # no config file, use builtin
1388 my $subdir=shift @ARGV;
1389 if (! chdir($subdir)) {
1390 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1394 $ENV{MR_REPO}=getcwd();
1395 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1396 if (! defined $command) {
1397 die "mr register: unknown repository type\n";
1400 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1401 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1402 "my_action(){ $command\n }; my_action ".
1403 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1404 print "mr register: running >>$command<<\n" if $verbose;
1405 exec($command) || die "exec: $!";
1409 my $url=shift @ARGV;
1410 my $dir=shift @ARGV || ".";
1412 if (! defined $url || ! length $url) {
1413 die "mr: bootstrap requires url\n";
1416 # Download the config file to a temporary location.
1417 eval q{use File::Temp};
1419 my $tmpconfig=File::Temp->new();
1420 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1421 push(@curlargs, "-k") if $insecure;
1422 my $curlstatus = system(@curlargs);
1423 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1424 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1427 system("mkdir", "-p", $dir);
1429 chdir($dir) || die "chdir $dir: $!";
1431 # Special case to handle checkout of the "." repo, which
1432 # would normally be skipped.
1433 my $topdir=abs_path(".")."/";
1434 my @repo=($topdir, $topdir, ".");
1435 loadconfig($tmpconfig, $topdir);
1436 record(\@repo, action("checkout", @repo, 1))
1437 if exists $config{$topdir}{"."}{"checkout"};
1439 if (-e ".mrconfig") {
1440 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1443 eval q{use File::Copy};
1445 move($tmpconfig, ".mrconfig") || die "rename: $!";
1448 # Reload the config file (in case we got a different version)
1449 # and checkout everything else.
1451 loadconfig(".mrconfig");
1452 dispatch("checkout");
1453 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1454 showstats("bootstrap");
1458 # alias expansion and command stemming
1461 if (exists $alias{$action}) {
1462 $action=$alias{$action};
1464 if (! exists $knownactions{$action}) {
1465 my @matches = grep { /^\Q$action\E/ }
1466 keys %knownactions, keys %alias;
1467 if (@matches == 1) {
1468 $action=$matches[0];
1470 elsif (@matches == 0) {
1471 die "mr: unknown action \"$action\" (known actions: ".
1472 join(", ", sort keys %knownactions).")\n";
1475 die "mr: ambiguous action \"$action\" (matches: ".
1476 join(", ", @matches).")\n";
1482 sub find_nearest_mrconfig {
1484 while (length $dir) {
1485 if (-e "$dir/.mrconfig") {
1486 return "$dir/.mrconfig";
1488 $dir=~s/\/[^\/]*$//;
1490 die "no .mrconfig found in path\n";
1495 Getopt::Long::Configure("bundling", "no_permute");
1496 my $result=GetOptions(
1497 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1498 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1499 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1500 "v|verbose" => \$verbose,
1501 "q|quiet" => \$quiet,
1502 "s|stats" => \$stats,
1503 "k|insecure" => \$insecure,
1504 "i|interactive" => \$interactive,
1505 "n|no-recurse:i" => \$max_depth,
1506 "j|jobs:i" => \$jobs,
1507 "t|trust-all" => \$trust_all,
1509 if (! $result || @ARGV < 1) {
1510 die("Usage: mr [options] action [params ...]\n".
1511 "(Use mr help for man page.)\n");
1514 $ENV{MR_SWITCHES}="";
1515 foreach my $option (@saved) {
1516 last if $option eq $ARGV[0];
1517 $ENV{MR_SWITCHES}.="$option ";
1523 print STDERR "mr: interrupted\n";
1527 # This can happen if it's run in a directory that was removed
1528 # or other strangeness.
1529 if (! defined $directory) {
1530 die("mr: failed to determine working directory\n");
1532 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1533 # the config file might be a symlink to elsewhere, and the directory it's
1534 # in is significant.
1535 if ($ENV{MR_CONFIG} !~ /^\//) {
1536 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1538 # Try to set MR_PATH to the path to the program.
1540 use FindBin qw($Bin $Script);
1541 $ENV{MR_PATH}=$Bin."/".$Script;
1549 elsif (! @ok && @skipped) {
1562 loadconfig($ENV{MR_CONFIG});
1563 #use Data::Dumper; print Dumper(\%config);
1565 my $action=expandaction(shift @ARGV);
1572 # Finally, some useful actions that mr knows about by default.
1573 # These can be overridden in ~/.mrconfig.
1588 echo "mr (warning): $@" >&2
1594 if [ -z "$1" ] || [ -z "$2" ]; then
1595 error "mr: usage: hours_since action num"
1597 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1598 if [ -e "$MR_REPO/$dir" ]; then
1599 flagfile="$MR_REPO/$dir/.mr_last$1"
1603 if [ -z "$flagfile" ]; then
1604 error "cannot determine flag filename"
1606 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1607 if [ "$delta" -lt "$2" ]; then
1615 svn_test = test -d "$MR_REPO"/.svn
1616 git_test = test -d "$MR_REPO"/.git
1617 bzr_test = test -d "$MR_REPO"/.bzr
1618 cvs_test = test -d "$MR_REPO"/CVS
1619 hg_test = test -d "$MR_REPO"/.hg
1620 darcs_test = test -d "$MR_REPO"/_darcs
1621 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1623 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1624 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1625 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1627 svn_update = svn update "$@"
1628 git_update = git pull "$@"
1629 bzr_update = bzr merge --pull "$@"
1630 cvs_update = cvs update "$@"
1631 hg_update = hg pull "$@" && hg update "$@"
1632 darcs_update = darcs pull -a "$@"
1633 fossil_update = fossil pull "$@"
1635 svn_status = svn status "$@"
1636 git_status = git status -s "$@" || true
1637 bzr_status = bzr status --short "$@"
1638 cvs_status = cvs status "$@"
1639 hg_status = hg status "$@"
1640 darcs_status = darcs whatsnew -ls "$@" || true
1641 fossil_status = fossil changes "$@"
1643 svn_commit = svn commit "$@"
1644 git_commit = git commit -a "$@" && git push --all
1645 bzr_commit = bzr commit "$@" && bzr push
1646 cvs_commit = cvs commit "$@"
1647 hg_commit = hg commit -m "$@" && hg push
1648 darcs_commit = darcs record -a -m "$@" && darcs push -a
1649 fossil_commit = fossil commit "$@"
1651 git_record = git commit -a "$@"
1652 bzr_record = bzr commit "$@"
1653 hg_record = hg commit -m "$@"
1654 darcs_record = darcs record -a -m "$@"
1655 fossil_record = fossil commit "$@"
1658 git_push = git push "$@"
1659 bzr_push = bzr push "$@"
1661 hg_push = hg push "$@"
1662 darcs_push = darcs push -a "$@"
1663 fossil_push = fossil push "$@"
1665 svn_diff = svn diff "$@"
1666 git_diff = git diff "$@"
1667 bzr_diff = bzr diff "$@"
1668 cvs_diff = cvs diff "$@"
1669 hg_diff = hg diff "$@"
1670 darcs_diff = darcs diff -u "$@"
1671 fossil_diff = fossil diff "$@"
1673 svn_log = svn log "$@"
1674 git_log = git log "$@"
1675 bzr_log = bzr log "$@"
1676 cvs_log = cvs log "$@"
1677 hg_log = hg log "$@"
1678 darcs_log = darcs changes "$@"
1679 git_bare_log = git log "$@"
1680 fossil_log = fossil timeline "$@"
1683 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1684 if [ -z "$url" ]; then
1685 error "cannot determine svn url"
1687 echo "Registering svn url: $url in $MR_CONFIG"
1688 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1690 url="`LC_ALL=C git config --get remote.origin.url`" || true
1691 if [ -z "$url" ]; then
1692 error "cannot determine git url"
1694 echo "Registering git url: $url in $MR_CONFIG"
1695 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1697 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1698 if [ -z "$url" ]; then
1699 error "cannot determine bzr url"
1701 echo "Registering bzr url: $url in $MR_CONFIG"
1702 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1704 repo=`cat CVS/Repository`
1706 if [ -z "$root" ]; then
1707 error "cannot determine cvs root"
1709 echo "Registering cvs repository $repo at root $root"
1710 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1712 url=`hg showconfig paths.default`
1713 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1714 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1716 url=`cat _darcs/prefs/defaultrepo`
1717 echo "Registering darcs repository $url in $MR_CONFIG"
1718 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1720 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1721 if [ -z "$url" ]; then
1722 error "cannot determine git url"
1724 echo "Registering git url: $url in $MR_CONFIG"
1725 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1727 url=`fossil remote-url`
1728 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1729 echo "Registering fossil repository $url in $MR_CONFIG"
1730 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1732 svn_trusted_checkout = svn co $url $repo
1733 svn_alt_trusted_checkout = svn checkout $url $repo
1734 git_trusted_checkout = git clone $url $repo
1735 bzr_trusted_checkout = bzr clone $url $repo
1737 hg_trusted_checkout = hg clone $url $repo
1738 darcs_trusted_checkout = darcs get $url $repo
1739 git_bare_trusted_checkout = git clone --bare $url $repo
1740 # fossil: messy to do
1746 SHOWMANFILE="man -f"
1752 SHOWMANFILE="man -l"
1755 if [ ! -e "$MR_PATH" ]; then
1756 error "cannot find program path"
1758 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1759 trap "rm -f $tmp" exit
1760 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1761 $SHOWMANFILE "$tmp" || error "man failed"
1767 if [ -s ~/.mrlog ]; then
1768 info "running offline commands"
1769 mv -f ~/.mrlog ~/.mrlog.old
1770 if ! sh -e ~/.mrlog.old; then
1771 error "offline command failed; left in ~/.mrlog.old"
1775 info "no offline commands to run"
1780 info "offline mode enabled"
1782 info "remembering command: 'mr $@'"
1783 command="mr -d '$(pwd)' $MR_SWITCHES"
1785 command="$command '$w'"
1787 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1788 echo "$command" >> ~/.mrlog
1791 ed = echo "A horse is a horse, of course, of course.."
1792 T = echo "I pity the fool."
1793 right = echo "Not found."
1795 # vim:sw=8:sts=0:ts=8:noet