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 and darcs repositories, and support for other revision control systems can
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 When looking for a command to run for a given action, mr first looks for
376 a parameter with the same name as the action. If that is not found, it
377 looks for a parameter named "rcs_action" (substituting in the name of the
378 revision control system and the action). The name of the revision control
379 system is itself determined by running each defined "rcs_test" action,
382 Internally, mr has settings for "git_update", "svn_update", etc. To change
383 the action that is performed for a given revision control system, you can
384 override these rcs specific actions. To add a new revision control system,
385 you can just add rcs specific actions for it.
387 The ~/.mrlog file contains commands that mr has remembered to run later,
388 due to being offline. You can delete or edit this file to remove commands,
389 or even to add other commands for 'mr online' to run. If the file is
390 present, mr assumes it is in offline mode.
392 =head1 "UNTRUSTED MRCONFIG FILES"
394 Since mrconfig files can contain arbitrary shell commands, they can do
395 anything. This flexability is good, but it also allows a malicious mrconfig
396 file to delete your whole home directory. Such a file might be contained
397 inside a repository that your main ~/.mrconfig checks out and chains to. To
398 avoid worries about evil commands in a mrconfig file, mr
399 has the ability to read mrconfig files in untrusted mode. Such files are
400 limited to running only known safe commands (like "git clone") in a
401 carefully checked manner.
403 By default, mr trusts all mrconfig files. (This default will change in a
404 future release!) But if you have a ~/.mrtrust file, mr will only trust
405 mrconfig files that are listed within it. (One file per line.) All other
406 files will be treated as untrusted.
410 mr can be extended to support things such as unison and git-svn. Some
411 files providing such extensions are available in /usr/share/mr/. See
412 the documentation in the files for details about using them.
416 Copyright 2007-2009 Joey Hess <joey@kitenet.net>
418 Licensed under the GNU GPL version 2 or higher.
420 http://kitenet.net/~joey/code/mr/
427 use Cwd qw(getcwd abs_path);
429 # things that can happen when mr runs a command
438 my $config_overridden=0;
448 my $directory=getcwd();
449 $ENV{MR_CONFIG}="$ENV{HOME}/.mrconfig";
456 my (@ok, @failed, @skipped);
462 my ($action, $dir, $topdir, $subdir) = @_;
464 if (exists $rcs{$dir}) {
469 foreach my $rcs_test (
471 length $a <=> length $b
474 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
475 my ($rcs)=$rcs_test=~/(.*)_test/;
476 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
477 $test.="if my_$rcs_test; then echo $rcs; fi\n";
479 $test=$config{$topdir}{$subdir}{lib}."\n".$test
480 if exists $config{$topdir}{$subdir}{lib};
482 print "mr $action: running rcs test >>$test<<\n" if $verbose;
487 print STDERR "mr $action: found multiple possible repository types ($rcs) for $topdir$subdir\n";
491 return $rcs{$dir}=undef;
494 return $rcs{$dir}=$rcs;
499 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
501 if (exists $config{$topdir}{$subdir}{$action}) {
502 return $config{$topdir}{$subdir}{$action};
509 my $rcs=rcs_test(@_);
512 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
513 return $config{$topdir}{$subdir}{$rcs."_".$action};
521 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
523 $ENV{MR_CONFIG}=$configfiles{$topdir};
524 my $lib=exists $config{$topdir}{$subdir}{lib} ?
525 $config{$topdir}{$subdir}{lib}."\n" : "";
526 my $is_checkout=($action eq 'checkout');
531 if (! $force_checkout) {
533 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
537 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
540 elsif ($action =~ /update/) {
542 return action("checkout", $dir, $topdir, $subdir);
546 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
547 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
549 if (defined $skiptest) {
550 my $test="set -e;".$lib.
551 "my_action(){ $skiptest\n }; my_action '$action'";
552 print "mr $action: running skip test >>$test<<\n" if $verbose;
553 my $ret=system($test);
555 if (($? & 127) == 2) {
556 print STDERR "mr $action: interrupted\n";
560 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
564 if ($ret >> 8 == 0) {
565 print "mr $action: $dir skipped per config file\n" if $verbose;
570 if ($is_checkout && ! -d $dir) {
571 print "mr $action: creating parent directory $dir\n" if $verbose;
572 system("mkdir", "-p", $dir);
575 if (! $no_chdir && ! chdir($dir)) {
576 print STDERR "mr $action: failed to chdir to $dir: $!\n";
579 elsif (! defined $command) {
580 my $rcs=rcs_test(@_);
581 if (! defined $rcs) {
582 print STDERR "mr $action: unknown repository type and no defined $action command for $topdir$subdir\n";
586 print STDERR "mr $action: no defined action for $rcs repository $topdir$subdir, skipping\n";
592 print "mr $action: $topdir$subdir\n" unless $quiet;
596 $s=~s/^\Q$topdir$subdir\E\/?//;
597 print "mr $action: $topdir$subdir (in subdir $s)\n" unless $quiet;
599 $command="set -e; ".$lib.
600 "my_action(){ $command\n }; my_action ".
601 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
602 print "mr $action: running >>$command<<\n" if $verbose;
603 my $ret=system($command);
605 if (($? & 127) == 2) {
606 print STDERR "mr $action: interrupted\n";
610 print STDERR "mr $action: received signal ".($? & 127)."\n";
613 print STDERR "mr $action: failed ($ret)\n" if $verbose;
614 if ($ret >> 8 != 0) {
615 print STDERR "mr $action: command failed\n";
616 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
617 # recreate original command line to
618 # remember, and avoid recursing
620 @ARGV=('-n', $action, @orig);
621 action("remember", $dir, $topdir, $subdir);
626 print STDERR "mr $action: command died ($ret)\n";
631 if ($action eq 'checkout' && ! -d $dir) {
632 print STDERR "mr $action: $dir missing after checkout\n";;
641 # run actions on multiple repos, in parallel
651 while (@fhs or @repos) {
652 while ((!$jobs || $running < $jobs) && @repos) {
654 my $repo = shift @repos;
655 pipe(my $outfh, CHILD_STDOUT);
656 pipe(my $errfh, CHILD_STDERR);
658 unless ($pid = fork) {
659 die "mr $action: cannot fork: $!" unless defined $pid;
660 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
661 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
666 exit action($action, @$repo);
670 push @active, [$pid, $repo];
671 push @fhs, [$outfh, $errfh];
674 my ($rin, $rout) = ('','');
676 foreach my $fh (@fhs) {
677 next unless defined $fh;
678 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
679 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
681 $nfound = select($rout=$rin, undef, undef, 1);
682 foreach my $channel (0, 1) {
683 foreach my $i (0..$#fhs) {
684 next unless defined $fhs[$i];
685 my $fh = $fhs[$i][$channel];
686 next unless defined $fh;
687 if (vec($rout, fileno($fh), 1) == 1) {
689 if (sysread($fh, $r, 1024) == 0) {
691 $fhs[$i][$channel] = undef;
692 if (! defined $fhs[$i][0] &&
693 ! defined $fhs[$i][1]) {
694 waitpid($active[$i][0], 0);
695 print STDOUT $out[$i][0];
696 print STDERR $out[$i][1];
697 record($active[$i][1], $? >> 8);
699 splice(@active, $i, 1);
704 $out[$i][$channel] .= $r;
712 my $dir=shift()->[0];
717 print "\n" unless $quiet;
719 elsif ($ret == FAILED) {
721 chdir($dir) unless $no_chdir;
722 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
723 system((getpwuid($<))[8], "-i");
726 print "\n" unless $quiet;
728 elsif ($ret == SKIPPED) {
731 elsif ($ret == ABORT) {
735 die "unknown exit status $ret";
741 if (! @ok && ! @failed && ! @skipped) {
742 die "mr $action: no repositories found to work on\n";
744 print "mr $action: finished (".join("; ",
745 showstat($#ok+1, "ok", "ok"),
746 showstat($#failed+1, "failed", "failed"),
747 showstat($#skipped+1, "skipped", "skipped"),
748 ).")\n" unless $quiet;
751 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
754 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
764 return "$count ".($count > 1 ? $plural : $singular);
769 # an ordered list of repos
772 foreach my $topdir (sort keys %config) {
773 foreach my $subdir (sort keys %{$config{$topdir}}) {
777 order => $config{$topdir}{$subdir}{order},
782 $a->{order} <=> $b->{order}
784 $a->{topdir} cmp $b->{topdir}
786 $a->{subdir} cmp $b->{subdir}
792 my $topdir=$repo->{topdir};
793 my $subdir=$repo->{subdir};
794 return abs_path(($subdir =~/^\//) ? $subdir : $topdir.$subdir);
797 # figure out which repos to act on
800 foreach my $repo (repolist()) {
801 my $topdir=$repo->{topdir};
802 my $subdir=$repo->{subdir};
804 next if $subdir eq 'DEFAULT';
805 my $dir=repodir($repo);
807 $dir.="/" unless $dir=~/\/$/;
808 $d.="/" unless $d=~/\/$/;
809 next if $dir ne $d && $dir !~ /^\Q$d\E/;
810 if (defined $max_depth) {
811 my @a=split('/', $dir);
812 my @b=split('/', $d);
813 do { } while (@a && @b && shift(@a) eq shift(@b));
814 next if @a > $max_depth || @b > $max_depth;
816 push @repos, [$dir, $topdir, $subdir];
819 # fallback to find a leaf repo
820 foreach my $repo (reverse repolist()) {
821 my $topdir=$repo->{topdir};
822 my $subdir=$repo->{subdir};
824 next if $subdir eq 'DEFAULT';
825 my $dir=repodir($repo);
827 $dir.="/" unless $dir=~/\/$/;
828 $d.="/" unless $d=~/\/$/;
829 if ($d=~/^\Q$dir\E/) {
830 push @repos, [$dir, $topdir, $subdir];
852 sub is_trusted_config {
853 my $config=shift; # must be abs_pathed already
855 # We always trust ~/.mrconfig.
856 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
858 return 1 if $trust_all;
860 my $trustfile=$ENV{HOME}."/.mrtrust";
862 if (! -e $trustfile) {
863 print "mr: Assuming $config is trusted.\n";
864 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
865 print "mr: and list all trusted mrconfig files in it.\n";
870 $trusted{"$ENV{HOME}/.mrconfig"}=1;
871 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
874 s/^~\//$ENV{HOME}\//;
875 $trusted{abs_path($_)}=1;
880 return $trusted{$config};
884 sub is_trusted_repo {
887 # Tightly limit what is allowed in a repo name.
888 # No ../, no absolute paths, and no unusual filenames
889 # that might try to escape to the shell.
890 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
891 $repo !~ /\.\./ && $repo !~ /^\//;
894 sub is_trusted_checkout {
897 # To determine if the command is safe, compare it with the
898 # *_trusted_checkout config settings. Those settings are
899 # templates for allowed commands, so make sure that each word
900 # of the command matches the corresponding word of the template.
903 foreach my $word (split(' ', $command)) {
905 if ($word=~/^'(.*)'$/) {
908 elsif ($word=~/^"(.*)"$/) {
915 foreach my $key (grep { /_trusted_checkout$/ }
916 keys %{$config{''}{DEFAULT}}) {
917 my @twords=split(' ', $config{''}{DEFAULT}{$key});
918 next if @words > @twords;
922 for (my $c=0; $c < @twords && $match; $c++) {
923 if ($twords[$c] eq '$url') {
924 # Match all the typical characters found in
925 # urls, plus @ which svn can use. Note
926 # that the "url" might also be a local
929 defined $words[$c] &&
930 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
934 elsif ($twords[$c] eq '$repo') {
935 # If a repo is not specified, assume it
936 # will be the last path component of the
937 # url, or something derived from it, and
939 if (! defined $words[$c] && defined $url) {
940 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
944 defined $words[$c] &&
945 is_trusted_repo($words[$c])
948 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
970 if (ref $f eq 'GLOB') {
980 my $absf=abs_path($f);
981 if ($loaded{$absf}) {
986 $trusted=is_trusted_config($absf);
988 if (! defined $dir) {
989 ($dir)=$f=~/^(.*\/)[^\/]+$/;
990 if (! defined $dir) {
995 $dir=abs_path($dir)."/";
997 if (! exists $configfiles{$dir}) {
998 $configfiles{$dir}=$f;
1001 # copy in defaults from first parent
1003 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1004 if ($parent eq '/') {
1007 if (exists $config{$parent} &&
1008 exists $config{$parent}{DEFAULT}) {
1009 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1014 print "mr: loading config $f\n" if $verbose;
1015 open($in, "<", $f) || die "mr: open $f: $!\n";
1018 close $in unless ref $f eq 'GLOB';
1026 next if /^\s*\#/ || /^\s*$/;
1027 if (/^\[([^\]]*)\]\s*$/) {
1031 if (! is_trusted_repo($section) ||
1032 $section eq 'ALIAS' ||
1033 $section eq 'DEFAULT') {
1034 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1037 $section=expandenv($section) if $trusted;
1039 elsif (/^(\w+)\s*=\s*(.*)/) {
1044 while (@lines && $lines[0]=~/^\s(.+)/) {
1052 # Untrusted files can only contain checkout
1054 if ($parameter ne 'checkout') {
1055 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1057 if (! is_trusted_checkout($value)) {
1058 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1062 if ($parameter eq "include") {
1063 print "mr: including output of \"$value\"\n" if $verbose;
1064 unshift @lines, `$value`;
1066 print STDERR "mr: include command exited nonzero ($?)\n";
1071 if (! defined $section) {
1072 die "$f line $.: parameter ($parameter) not in section\n";
1074 if ($section ne 'ALIAS' &&
1075 ! exists $config{$dir}{$section} &&
1076 exists $config{$dir}{DEFAULT}) {
1078 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1080 if ($section eq 'ALIAS') {
1081 $alias{$parameter}=$value;
1083 elsif ($parameter eq 'lib') {
1084 $config{$dir}{$section}{lib}.=$value."\n";
1087 $config{$dir}{$section}{$parameter}=$value;
1088 if ($parameter =~ /.*_(.*)/) {
1089 $knownactions{$1}=1;
1092 $knownactions{$parameter}=1;
1094 if ($parameter eq 'chain' &&
1095 length $dir && $section ne "DEFAULT" &&
1096 -e $dir.$section."/.mrconfig") {
1097 my $ret=system($value);
1099 if (($? & 127) == 2) {
1100 print STDERR "mr: chain test interrupted\n";
1104 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1108 push @toload, $dir.$section."/.mrconfig";
1114 die "$f line $line: parse error\n";
1123 sub startingconfig {
1124 %alias=%config=%configfiles=%knownactions=%loaded=();
1125 my $datapos=tell(DATA);
1127 seek(DATA,$datapos,0); # rewind
1132 # the section to modify or add
1133 my $targetsection=shift;
1134 # fields to change in the section
1135 # To remove a field, set its value to "".
1136 my %changefields=@_;
1142 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1147 my $formatfield=sub {
1149 my @value=split(/\n/, shift);
1151 return "$field = ".shift(@value)."\n".
1152 join("", map { "\t$_\n" } @value);
1156 while ($out[$#out] =~ /^\s*$/) {
1157 unshift @blanks, pop @out;
1159 foreach my $field (sort keys %changefields) {
1160 if (length $changefields{$field}) {
1161 push @out, "$field = $changefields{$field}\n";
1162 delete $changefields{$field};
1172 if (/^\s*\#/ || /^\s*$/) {
1175 elsif (/^\[([^\]]*)\]\s*$/) {
1176 if (defined $section &&
1177 $section eq $targetsection) {
1181 $section=expandenv($1);
1185 elsif (/^(\w+)\s*=\s(.*)/) {
1190 while (@lines && $lines[0]=~/^\s(.+)/) {
1196 if ($section eq $targetsection) {
1197 if (exists $changefields{$parameter}) {
1198 if (length $changefields{$parameter}) {
1199 $value=$changefields{$parameter};
1201 delete $changefields{$parameter};
1205 push @out, $formatfield->($parameter, $value);
1209 if (defined $section &&
1210 $section eq $targetsection) {
1213 elsif (%changefields) {
1214 push @out, "\n[$targetsection]\n";
1215 foreach my $field (sort keys %changefields) {
1216 if (length $changefields{$field}) {
1217 push @out, $formatfield->($field, $changefields{$field});
1222 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1230 # actions that do not operate on all repos
1231 if ($action eq 'help') {
1234 elsif ($action eq 'config') {
1237 elsif ($action eq 'register') {
1240 elsif ($action eq 'bootstrap') {
1243 elsif ($action eq 'remember' ||
1244 $action eq 'offline' ||
1245 $action eq 'online') {
1246 my @repos=selectrepos;
1247 action($action, @{$repos[0]}) if @repos;
1251 if (!$jobs || $jobs > 1) {
1252 mrs($action, selectrepos());
1255 foreach my $repo (selectrepos()) {
1256 record($repo, action($action, @$repo));
1262 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1267 die "mr config: not enough parameters\n";
1270 if ($section=~/^\//) {
1271 # try to convert to a path relative to the config file
1272 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1273 $dir=abs_path($dir);
1274 $dir.="/" unless $dir=~/\/$/;
1275 if ($section=~/^\Q$dir\E(.*)/) {
1281 if (/^([^=]+)=(.*)$/) {
1282 $changefields{$1}=$2;
1286 foreach my $topdir (sort keys %config) {
1287 if (exists $config{$topdir}{$section} &&
1288 exists $config{$topdir}{$section}{$_}) {
1289 print $config{$topdir}{$section}{$_}."\n";
1291 last if $section eq 'DEFAULT';
1295 die "mr config: $section $_ not set\n";
1299 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1304 if ($config_overridden) {
1305 # Find the directory that the specified config file is
1307 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1310 # Find the closest known mrconfig file to the current
1312 $directory.="/" unless $directory=~/\/$/;
1314 foreach my $topdir (reverse sort keys %config) {
1315 next unless length $topdir;
1316 if ($directory=~/^\Q$topdir\E/) {
1317 $ENV{MR_CONFIG}=$configfiles{$topdir};
1323 if (! $foundconfig) {
1324 $directory=""; # no config file, use builtin
1328 my $subdir=shift @ARGV;
1329 if (! chdir($subdir)) {
1330 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1334 $ENV{MR_REPO}=getcwd();
1335 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1336 if (! defined $command) {
1337 die "mr register: unknown repository type\n";
1340 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1341 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1342 "my_action(){ $command\n }; my_action ".
1343 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1344 print "mr register: running >>$command<<\n" if $verbose;
1345 exec($command) || die "exec: $!";
1349 my $url=shift @ARGV;
1350 my $dir=shift @ARGV || ".";
1352 if (! defined $url || ! length $url) {
1353 die "mr: bootstrap requires url\n";
1356 # Download the config file to a temporary location.
1357 eval q{use File::Temp};
1359 my $tmpconfig=File::Temp->new();
1360 my @curlargs = ("curl", "-A", "mr", "-s", $url, "-o", $tmpconfig);
1361 push(@curlargs, "-k") if $insecure;
1362 my $curlstatus = system(@curlargs);
1363 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1364 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1367 system("mkdir", "-p", $dir);
1369 chdir($dir) || die "chdir $dir: $!";
1371 # Special case to handle checkout of the "." repo, which
1372 # would normally be skipped.
1373 my $topdir=abs_path(".")."/";
1374 my @repo=($topdir, $topdir, ".");
1375 loadconfig($tmpconfig, $topdir);
1376 record(\@repo, action("checkout", @repo, 1))
1377 if exists $config{$topdir}{"."}{"checkout"};
1379 if (-e ".mrconfig") {
1380 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1383 eval q{use File::Copy};
1385 move($tmpconfig, ".mrconfig") || die "rename: $!";
1388 # Reload the config file (in case we got a different version)
1389 # and checkout everything else.
1391 loadconfig(".mrconfig");
1392 dispatch("checkout");
1393 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1394 showstats("bootstrap");
1398 # alias expansion and command stemming
1401 if (exists $alias{$action}) {
1402 $action=$alias{$action};
1404 if (! exists $knownactions{$action}) {
1405 my @matches = grep { /^\Q$action\E/ }
1406 keys %knownactions, keys %alias;
1407 if (@matches == 1) {
1408 $action=$matches[0];
1410 elsif (@matches == 0) {
1411 die "mr: unknown action \"$action\" (known actions: ".
1412 join(", ", sort keys %knownactions).")\n";
1415 die "mr: ambiguous action \"$action\" (matches: ".
1416 join(", ", @matches).")\n";
1422 sub find_nearest_mrconfig {
1424 while (length $dir) {
1425 if (-e "$dir/.mrconfig") {
1426 return "$dir/.mrconfig";
1428 $dir=~s/\/[^\/]*$//;
1430 die "no .mrconfig found in path\n";
1435 Getopt::Long::Configure("bundling", "no_permute");
1436 my $result=GetOptions(
1437 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1438 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1439 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1440 "v|verbose" => \$verbose,
1441 "q|quiet" => \$quiet,
1442 "s|stats" => \$stats,
1443 "k|insecure" => \$insecure,
1444 "i|interactive" => \$interactive,
1445 "n|no-recurse:i" => \$max_depth,
1446 "j|jobs:i" => \$jobs,
1447 "t|trust-all" => \$trust_all,
1449 if (! $result || @ARGV < 1) {
1450 die("Usage: mr [options] action [params ...]\n".
1451 "(Use mr help for man page.)\n");
1454 $ENV{MR_SWITCHES}="";
1455 foreach my $option (@saved) {
1456 last if $option eq $ARGV[0];
1457 $ENV{MR_SWITCHES}.="$option ";
1463 print STDERR "mr: interrupted\n";
1467 # This can happen if it's run in a directory that was removed
1468 # or other strangeness.
1469 if (! defined $directory) {
1470 die("mr: failed to determine working directory\n");
1472 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1473 # the config file might be a symlink to elsewhere, and the directory it's
1474 # in is significant.
1475 if ($ENV{MR_CONFIG} !~ /^\//) {
1476 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1478 # Try to set MR_PATH to the path to the program.
1480 use FindBin qw($Bin $Script);
1481 $ENV{MR_PATH}=$Bin."/".$Script;
1489 elsif (! @ok && @skipped) {
1502 loadconfig($ENV{MR_CONFIG});
1503 #use Data::Dumper; print Dumper(\%config);
1505 my $action=expandaction(shift @ARGV);
1512 # Finally, some useful actions that mr knows about by default.
1513 # These can be overridden in ~/.mrconfig.
1528 echo "mr (warning): $@" >&2
1534 if [ -z "$1" ] || [ -z "$2" ]; then
1535 error "mr: usage: hours_since action num"
1537 for dir in .git .svn .bzr CVS .hg _darcs; do
1538 if [ -e "$MR_REPO/$dir" ]; then
1539 flagfile="$MR_REPO/$dir/.mr_last$1"
1543 if [ -z "$flagfile" ]; then
1544 error "cannot determine flag filename"
1546 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1547 if [ "$delta" -lt "$2" ]; then
1555 svn_test = test -d "$MR_REPO"/.svn
1556 git_test = test -d "$MR_REPO"/.git
1557 bzr_test = test -d "$MR_REPO"/.bzr
1558 cvs_test = test -d "$MR_REPO"/CVS
1559 hg_test = test -d "$MR_REPO"/.hg
1560 darcs_test = test -d "$MR_REPO"/_darcs
1562 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1563 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1564 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1566 svn_update = svn update "$@"
1567 git_update = git pull "$@"
1568 bzr_update = bzr merge --pull "$@"
1569 cvs_update = cvs update "$@"
1570 hg_update = hg pull "$@" && hg update "$@"
1571 darcs_update = darcs pull -a "$@"
1573 svn_status = svn status "$@"
1574 git_status = git status "$@" || true
1575 bzr_status = bzr status "$@"
1576 cvs_status = cvs status "$@"
1577 hg_status = hg status "$@"
1578 darcs_status = darcs whatsnew -ls "$@" || true
1580 svn_commit = svn commit "$@"
1581 git_commit = git commit -a "$@" && git push --all
1582 bzr_commit = bzr commit "$@" && bzr push
1583 cvs_commit = cvs commit "$@"
1584 hg_commit = hg commit -m "$@" && hg push
1585 darcs_commit = darcs record -a -m "$@" && darcs push -a
1587 git_record = git commit -a "$@"
1588 bzr_record = bzr commit "$@"
1589 hg_record = hg commit -m "$@"
1590 darcs_record = darcs record -a -m "$@"
1593 git_push = git push "$@"
1594 bzr_push = bzr push "$@"
1596 hg_push = hg push "$@"
1597 darcs_push = darcs push -a "$@"
1599 svn_diff = svn diff "$@"
1600 git_diff = git diff "$@"
1601 bzr_diff = bzr diff "$@"
1602 cvs_diff = cvs diff "$@"
1603 hg_diff = hg diff "$@"
1604 darcs_diff = darcs diff -u "$@"
1606 svn_log = svn log "$@"
1607 git_log = git log "$@"
1608 bzr_log = bzr log "$@"
1609 cvs_log = cvs log "$@"
1610 hg_log = hg log "$@"
1611 darcs_log = darcs changes "$@"
1612 git_bare_log = git log "$@"
1615 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1616 if [ -z "$url" ]; then
1617 error "cannot determine svn url"
1619 echo "Registering svn url: $url in $MR_CONFIG"
1620 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1622 url="`LC_ALL=C git config --get remote.origin.url`" || true
1623 if [ -z "$url" ]; then
1624 error "cannot determine git url"
1626 echo "Registering git url: $url in $MR_CONFIG"
1627 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1629 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1630 if [ -z "$url" ]; then
1631 error "cannot determine bzr url"
1633 echo "Registering bzr url: $url in $MR_CONFIG"
1634 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1636 repo=`cat CVS/Repository`
1638 if [ -z "$root" ]; then
1639 error "cannot determine cvs root"
1641 echo "Registering cvs repository $repo at root $root"
1642 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1644 url=`hg showconfig paths.default`
1645 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1646 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1648 url=`cat _darcs/prefs/defaultrepo`
1649 echo "Registering darcs repository $url in $MR_CONFIG"
1650 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1652 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1653 if [ -z "$url" ]; then
1654 error "cannot determine git url"
1656 echo "Registering git url: $url in $MR_CONFIG"
1657 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1659 svn_trusted_checkout = svn co $url $repo
1660 svn_alt_trusted_checkout = svn checkout $url $repo
1661 git_trusted_checkout = git clone $url $repo
1662 bzr_trusted_checkout = bzr clone $url $repo
1664 hg_trusted_checkout = hg clone $url $repo
1665 darcs_trusted_checkout = darcs get $url $repo
1666 git_bare_trusted_checkout = git clone --bare $url $repo
1672 SHOWMANFILE="man -f"
1678 SHOWMANFILE="man -l"
1681 if [ ! -e "$MR_PATH" ]; then
1682 error "cannot find program path"
1684 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1685 trap "rm -f $tmp" exit
1686 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1687 $SHOWMANFILE "$tmp" || error "man failed"
1693 if [ -s ~/.mrlog ]; then
1694 info "running offline commands"
1695 mv -f ~/.mrlog ~/.mrlog.old
1696 if ! sh -e ~/.mrlog.old; then
1697 error "offline command failed; left in ~/.mrlog.old"
1701 info "no offline commands to run"
1706 info "offline mode enabled"
1708 info "remembering command: 'mr $@'"
1709 command="mr -d '$(pwd)' $MR_SWITCHES"
1711 command="$command '$w'"
1713 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1714 echo "$command" >> ~/.mrlog
1717 ed = echo "A horse is a horse, of course, of course.."
1718 T = echo "I pity the fool."
1719 right = echo "Not found."
1721 # vim:sw=8:sts=0:ts=8:noet