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 $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 ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
537 $ENV{MR_CONFIG}=$configfiles{$topdir};
538 my $lib=exists $config{$topdir}{$subdir}{lib} ?
539 $config{$topdir}{$subdir}{lib}."\n" : "";
540 my $is_checkout=($action eq 'checkout');
541 my $is_update=($action =~ /update/);
546 if (! $force_checkout) {
548 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
552 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
557 return action("checkout", $dir, $topdir, $subdir);
561 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
562 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
564 if (defined $skiptest) {
565 my $test="set -e;".$lib.
566 "my_action(){ $skiptest\n }; my_action '$action'";
567 print "mr $action: running skip test >>$test<<\n" if $verbose;
568 my $ret=system($test);
570 if (($? & 127) == 2) {
571 print STDERR "mr $action: interrupted\n";
575 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
579 if ($ret >> 8 == 0) {
580 print "mr $action: $dir skipped per config file\n" if $verbose;
585 if ($is_checkout && ! -d $dir) {
586 print "mr $action: creating parent directory $dir\n" if $verbose;
587 system("mkdir", "-p", $dir);
590 if (! $no_chdir && ! chdir($dir)) {
591 print STDERR "mr $action: failed to chdir to $dir: $!\n";
594 elsif (! defined $command) {
595 my $rcs=rcs_test(@_);
596 if (! defined $rcs) {
597 print STDERR "mr $action: unknown repository type and no defined $action command for $topdir$subdir\n";
601 print STDERR "mr $action: no defined action for $rcs repository $topdir$subdir, skipping\n";
607 print "mr $action: $topdir$subdir\n" unless $quiet;
611 $s=~s/^\Q$topdir$subdir\E\/?//;
612 print "mr $action: $topdir$subdir (in subdir $s)\n" unless $quiet;
615 my $hookret=hook("pre_$action", $topdir, $subdir);
616 return $hookret if $hookret != OK;
618 $command="set -e; ".$lib.
619 "my_action(){ $command\n }; my_action ".
620 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
621 print "mr $action: running >>$command<<\n" if $verbose;
622 my $ret=system($command);
624 if (($? & 127) == 2) {
625 print STDERR "mr $action: interrupted\n";
629 print STDERR "mr $action: received signal ".($? & 127)."\n";
632 print STDERR "mr $action: failed ($ret)\n" if $verbose;
633 if ($ret >> 8 != 0) {
634 print STDERR "mr $action: command failed\n";
635 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
636 # recreate original command line to
637 # remember, and avoid recursing
639 @ARGV=('-n', $action, @orig);
640 action("remember", $dir, $topdir, $subdir);
645 print STDERR "mr $action: command died ($ret)\n";
650 if ($is_checkout && ! -d $dir) {
651 print STDERR "mr $action: $dir missing after checkout\n";;
655 my $ret=hook("post_$action", $topdir, $subdir);
656 return $ret if $ret != OK;
658 if (($is_checkout || $is_update)) {
659 my $ret=hook("fixups", $topdir, $subdir);
660 return $ret if $ret != OK;
669 my ($hook, $topdir, $subdir) = @_;
671 my $command=$config{$topdir}{$subdir}{$hook};
672 return OK unless defined $command;
673 my $lib=exists $config{$topdir}{$subdir}{lib} ?
674 $config{$topdir}{$subdir}{lib}."\n" : "";
675 my $shell="set -e;".$lib.
676 "my_hook(){ $command\n }; my_hook";
677 print "mr $hook: running >>$shell<<\n" if $verbose;
678 my $ret=system($shell);
680 if (($? & 127) == 2) {
681 print STDERR "mr $hook: interrupted\n";
685 print STDERR "mr $hook: received signal ".($? & 127)."\n";
693 # run actions on multiple repos, in parallel
703 while (@fhs or @repos) {
704 while ((!$jobs || $running < $jobs) && @repos) {
706 my $repo = shift @repos;
707 pipe(my $outfh, CHILD_STDOUT);
708 pipe(my $errfh, CHILD_STDERR);
710 unless ($pid = fork) {
711 die "mr $action: cannot fork: $!" unless defined $pid;
712 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
713 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
718 exit action($action, @$repo);
722 push @active, [$pid, $repo];
723 push @fhs, [$outfh, $errfh];
726 my ($rin, $rout) = ('','');
728 foreach my $fh (@fhs) {
729 next unless defined $fh;
730 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
731 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
733 $nfound = select($rout=$rin, undef, undef, 1);
734 foreach my $channel (0, 1) {
735 foreach my $i (0..$#fhs) {
736 next unless defined $fhs[$i];
737 my $fh = $fhs[$i][$channel];
738 next unless defined $fh;
739 if (vec($rout, fileno($fh), 1) == 1) {
741 if (sysread($fh, $r, 1024) == 0) {
743 $fhs[$i][$channel] = undef;
744 if (! defined $fhs[$i][0] &&
745 ! defined $fhs[$i][1]) {
746 waitpid($active[$i][0], 0);
747 print STDOUT $out[$i][0];
748 print STDERR $out[$i][1];
749 record($active[$i][1], $? >> 8);
751 splice(@active, $i, 1);
756 $out[$i][$channel] .= $r;
764 my $dir=shift()->[0];
769 print "\n" unless $quiet;
771 elsif ($ret == FAILED) {
773 chdir($dir) unless $no_chdir;
774 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
775 system((getpwuid($<))[8], "-i");
778 print "\n" unless $quiet;
780 elsif ($ret == SKIPPED) {
783 elsif ($ret == ABORT) {
787 die "unknown exit status $ret";
793 if (! @ok && ! @failed && ! @skipped) {
794 die "mr $action: no repositories found to work on\n";
796 print "mr $action: finished (".join("; ",
797 showstat($#ok+1, "ok", "ok"),
798 showstat($#failed+1, "failed", "failed"),
799 showstat($#skipped+1, "skipped", "skipped"),
800 ).")\n" unless $quiet;
803 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
806 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
816 return "$count ".($count > 1 ? $plural : $singular);
821 # an ordered list of repos
824 foreach my $topdir (sort keys %config) {
825 foreach my $subdir (sort keys %{$config{$topdir}}) {
829 order => $config{$topdir}{$subdir}{order},
834 $a->{order} <=> $b->{order}
836 $a->{topdir} cmp $b->{topdir}
838 $a->{subdir} cmp $b->{subdir}
844 my $topdir=$repo->{topdir};
845 my $subdir=$repo->{subdir};
846 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
851 # figure out which repos to act on
854 foreach my $repo (repolist()) {
855 my $topdir=$repo->{topdir};
856 my $subdir=$repo->{subdir};
858 next if $subdir eq 'DEFAULT';
859 my $dir=repodir($repo);
861 $dir.="/" unless $dir=~/\/$/;
862 $d.="/" unless $d=~/\/$/;
863 next if $dir ne $d && $dir !~ /^\Q$d\E/;
864 if (defined $max_depth) {
865 my @a=split('/', $dir);
866 my @b=split('/', $d);
867 do { } while (@a && @b && shift(@a) eq shift(@b));
868 next if @a > $max_depth || @b > $max_depth;
870 push @repos, [$dir, $topdir, $subdir];
873 # fallback to find a leaf repo
874 foreach my $repo (reverse repolist()) {
875 my $topdir=$repo->{topdir};
876 my $subdir=$repo->{subdir};
878 next if $subdir eq 'DEFAULT';
879 my $dir=repodir($repo);
881 $dir.="/" unless $dir=~/\/$/;
882 $d.="/" unless $d=~/\/$/;
883 if ($d=~/^\Q$dir\E/) {
884 push @repos, [$dir, $topdir, $subdir];
906 sub is_trusted_config {
907 my $config=shift; # must be abs_pathed already
909 # We always trust ~/.mrconfig.
910 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
912 return 1 if $trust_all;
914 my $trustfile=$ENV{HOME}."/.mrtrust";
916 if (! -e $trustfile) {
917 print "mr: Assuming $config is trusted.\n";
918 print "mr: For better security, you are encouraged to create ~/.mrtrust\n";
919 print "mr: and list all trusted mrconfig files in it.\n";
924 $trusted{"$ENV{HOME}/.mrconfig"}=1;
925 open (TRUST, "<", $trustfile) || die "$trustfile: $!";
928 s/^~\//$ENV{HOME}\//;
929 $trusted{abs_path($_)}=1;
934 return $trusted{$config};
938 sub is_trusted_repo {
941 # Tightly limit what is allowed in a repo name.
942 # No ../, no absolute paths, and no unusual filenames
943 # that might try to escape to the shell.
944 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
945 $repo !~ /\.\./ && $repo !~ /^\//;
948 sub is_trusted_checkout {
951 # To determine if the command is safe, compare it with the
952 # *_trusted_checkout config settings. Those settings are
953 # templates for allowed commands, so make sure that each word
954 # of the command matches the corresponding word of the template.
957 foreach my $word (split(' ', $command)) {
959 if ($word=~/^'(.*)'$/) {
962 elsif ($word=~/^"(.*)"$/) {
969 foreach my $key (grep { /_trusted_checkout$/ }
970 keys %{$config{''}{DEFAULT}}) {
971 my @twords=split(' ', $config{''}{DEFAULT}{$key});
972 next if @words > @twords;
976 for (my $c=0; $c < @twords && $match; $c++) {
977 if ($twords[$c] eq '$url') {
978 # Match all the typical characters found in
979 # urls, plus @ which svn can use. Note
980 # that the "url" might also be a local
983 defined $words[$c] &&
984 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
988 elsif ($twords[$c] eq '$repo') {
989 # If a repo is not specified, assume it
990 # will be the last path component of the
991 # url, or something derived from it, and
993 if (! defined $words[$c] && defined $url) {
994 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
998 defined $words[$c] &&
999 is_trusted_repo($words[$c])
1002 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1024 if (ref $f eq 'GLOB') {
1034 my $absf=abs_path($f);
1035 if ($loaded{$absf}) {
1040 $trusted=is_trusted_config($absf);
1042 if (! defined $dir) {
1043 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1044 if (! defined $dir) {
1049 $dir=abs_path($dir)."/";
1051 if (! exists $configfiles{$dir}) {
1052 $configfiles{$dir}=$f;
1055 # copy in defaults from first parent
1057 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1058 if ($parent eq '/') {
1061 if (exists $config{$parent} &&
1062 exists $config{$parent}{DEFAULT}) {
1063 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1068 print "mr: loading config $f\n" if $verbose;
1069 open($in, "<", $f) || die "mr: open $f: $!\n";
1072 close $in unless ref $f eq 'GLOB';
1080 next if /^\s*\#/ || /^\s*$/;
1081 if (/^\[([^\]]*)\]\s*$/) {
1085 if (! is_trusted_repo($section) ||
1086 $section eq 'ALIAS' ||
1087 $section eq 'DEFAULT') {
1088 die "mr: illegal section \"[$section]\" in untrusted $f line $line\n";
1091 $section=expandenv($section) if $trusted;
1092 if ($section ne 'ALIAS' &&
1093 ! exists $config{$dir}{$section} &&
1094 exists $config{$dir}{DEFAULT}) {
1096 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1099 elsif (/^(\w+)\s*=\s*(.*)/) {
1104 while (@lines && $lines[0]=~/^\s(.+)/) {
1112 # Untrusted files can only contain checkout
1114 if ($parameter ne 'checkout') {
1115 die "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line\n";
1117 if (! is_trusted_checkout($value)) {
1118 die "mr: illegal checkout command \"$value\" in untrusted $f line $line\n";
1122 if ($parameter eq "include") {
1123 print "mr: including output of \"$value\"\n" if $verbose;
1124 unshift @lines, `$value`;
1126 print STDERR "mr: include command exited nonzero ($?)\n";
1131 if (! defined $section) {
1132 die "$f line $.: parameter ($parameter) not in section\n";
1134 if ($section eq 'ALIAS') {
1135 $alias{$parameter}=$value;
1137 elsif ($parameter eq 'lib') {
1138 $config{$dir}{$section}{lib}.=$value."\n";
1141 $config{$dir}{$section}{$parameter}=$value;
1142 if ($parameter =~ /.*_(.*)/) {
1143 $knownactions{$1}=1;
1146 $knownactions{$parameter}=1;
1148 if ($parameter eq 'chain' &&
1149 length $dir && $section ne "DEFAULT" &&
1150 -e $dir.$section."/.mrconfig") {
1151 my $ret=system($value);
1153 if (($? & 127) == 2) {
1154 print STDERR "mr: chain test interrupted\n";
1158 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1162 push @toload, $dir.$section."/.mrconfig";
1168 die "$f line $line: parse error\n";
1177 sub startingconfig {
1178 %alias=%config=%configfiles=%knownactions=%loaded=();
1179 my $datapos=tell(DATA);
1181 seek(DATA,$datapos,0); # rewind
1186 # the section to modify or add
1187 my $targetsection=shift;
1188 # fields to change in the section
1189 # To remove a field, set its value to "".
1190 my %changefields=@_;
1196 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1201 my $formatfield=sub {
1203 my @value=split(/\n/, shift);
1205 return "$field = ".shift(@value)."\n".
1206 join("", map { "\t$_\n" } @value);
1210 while ($out[$#out] =~ /^\s*$/) {
1211 unshift @blanks, pop @out;
1213 foreach my $field (sort keys %changefields) {
1214 if (length $changefields{$field}) {
1215 push @out, "$field = $changefields{$field}\n";
1216 delete $changefields{$field};
1226 if (/^\s*\#/ || /^\s*$/) {
1229 elsif (/^\[([^\]]*)\]\s*$/) {
1230 if (defined $section &&
1231 $section eq $targetsection) {
1235 $section=expandenv($1);
1239 elsif (/^(\w+)\s*=\s(.*)/) {
1244 while (@lines && $lines[0]=~/^\s(.+)/) {
1250 if ($section eq $targetsection) {
1251 if (exists $changefields{$parameter}) {
1252 if (length $changefields{$parameter}) {
1253 $value=$changefields{$parameter};
1255 delete $changefields{$parameter};
1259 push @out, $formatfield->($parameter, $value);
1263 if (defined $section &&
1264 $section eq $targetsection) {
1267 elsif (%changefields) {
1268 push @out, "\n[$targetsection]\n";
1269 foreach my $field (sort keys %changefields) {
1270 if (length $changefields{$field}) {
1271 push @out, $formatfield->($field, $changefields{$field});
1276 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1284 # actions that do not operate on all repos
1285 if ($action eq 'help') {
1288 elsif ($action eq 'config') {
1291 elsif ($action eq 'register') {
1294 elsif ($action eq 'bootstrap') {
1297 elsif ($action eq 'remember' ||
1298 $action eq 'offline' ||
1299 $action eq 'online') {
1300 my @repos=selectrepos;
1301 action($action, @{$repos[0]}) if @repos;
1305 if (!$jobs || $jobs > 1) {
1306 mrs($action, selectrepos());
1309 foreach my $repo (selectrepos()) {
1310 record($repo, action($action, @$repo));
1316 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1321 die "mr config: not enough parameters\n";
1324 if ($section=~/^\//) {
1325 # try to convert to a path relative to the config file
1326 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1327 $dir=abs_path($dir);
1328 $dir.="/" unless $dir=~/\/$/;
1329 if ($section=~/^\Q$dir\E(.*)/) {
1335 if (/^([^=]+)=(.*)$/) {
1336 $changefields{$1}=$2;
1340 foreach my $topdir (sort keys %config) {
1341 if (exists $config{$topdir}{$section} &&
1342 exists $config{$topdir}{$section}{$_}) {
1343 print $config{$topdir}{$section}{$_}."\n";
1345 last if $section eq 'DEFAULT';
1349 die "mr config: $section $_ not set\n";
1353 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1358 if ($config_overridden) {
1359 # Find the directory that the specified config file is
1361 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1364 # Find the closest known mrconfig file to the current
1366 $directory.="/" unless $directory=~/\/$/;
1368 foreach my $topdir (reverse sort keys %config) {
1369 next unless length $topdir;
1370 if ($directory=~/^\Q$topdir\E/) {
1371 $ENV{MR_CONFIG}=$configfiles{$topdir};
1377 if (! $foundconfig) {
1378 $directory=""; # no config file, use builtin
1382 my $subdir=shift @ARGV;
1383 if (! chdir($subdir)) {
1384 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1388 $ENV{MR_REPO}=getcwd();
1389 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1390 if (! defined $command) {
1391 die "mr register: unknown repository type\n";
1394 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1395 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1396 "my_action(){ $command\n }; my_action ".
1397 join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1398 print "mr register: running >>$command<<\n" if $verbose;
1399 exec($command) || die "exec: $!";
1403 my $url=shift @ARGV;
1404 my $dir=shift @ARGV || ".";
1406 if (! defined $url || ! length $url) {
1407 die "mr: bootstrap requires url\n";
1410 # Download the config file to a temporary location.
1411 eval q{use File::Temp};
1413 my $tmpconfig=File::Temp->new();
1414 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1415 push(@curlargs, "-k") if $insecure;
1416 my $curlstatus = system(@curlargs);
1417 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1418 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1421 system("mkdir", "-p", $dir);
1423 chdir($dir) || die "chdir $dir: $!";
1425 # Special case to handle checkout of the "." repo, which
1426 # would normally be skipped.
1427 my $topdir=abs_path(".")."/";
1428 my @repo=($topdir, $topdir, ".");
1429 loadconfig($tmpconfig, $topdir);
1430 record(\@repo, action("checkout", @repo, 1))
1431 if exists $config{$topdir}{"."}{"checkout"};
1433 if (-e ".mrconfig") {
1434 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1437 eval q{use File::Copy};
1439 move($tmpconfig, ".mrconfig") || die "rename: $!";
1442 # Reload the config file (in case we got a different version)
1443 # and checkout everything else.
1445 loadconfig(".mrconfig");
1446 dispatch("checkout");
1447 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1448 showstats("bootstrap");
1452 # alias expansion and command stemming
1455 if (exists $alias{$action}) {
1456 $action=$alias{$action};
1458 if (! exists $knownactions{$action}) {
1459 my @matches = grep { /^\Q$action\E/ }
1460 keys %knownactions, keys %alias;
1461 if (@matches == 1) {
1462 $action=$matches[0];
1464 elsif (@matches == 0) {
1465 die "mr: unknown action \"$action\" (known actions: ".
1466 join(", ", sort keys %knownactions).")\n";
1469 die "mr: ambiguous action \"$action\" (matches: ".
1470 join(", ", @matches).")\n";
1476 sub find_nearest_mrconfig {
1478 while (length $dir) {
1479 if (-e "$dir/.mrconfig") {
1480 return "$dir/.mrconfig";
1482 $dir=~s/\/[^\/]*$//;
1484 die "no .mrconfig found in path\n";
1489 Getopt::Long::Configure("bundling", "no_permute");
1490 my $result=GetOptions(
1491 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1492 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1493 "p|path" => sub { $ENV{MR_CONFIG}=find_nearest_mrconfig(); $config_overridden=1 },
1494 "v|verbose" => \$verbose,
1495 "q|quiet" => \$quiet,
1496 "s|stats" => \$stats,
1497 "k|insecure" => \$insecure,
1498 "i|interactive" => \$interactive,
1499 "n|no-recurse:i" => \$max_depth,
1500 "j|jobs:i" => \$jobs,
1501 "t|trust-all" => \$trust_all,
1503 if (! $result || @ARGV < 1) {
1504 die("Usage: mr [options] action [params ...]\n".
1505 "(Use mr help for man page.)\n");
1508 $ENV{MR_SWITCHES}="";
1509 foreach my $option (@saved) {
1510 last if $option eq $ARGV[0];
1511 $ENV{MR_SWITCHES}.="$option ";
1517 print STDERR "mr: interrupted\n";
1521 # This can happen if it's run in a directory that was removed
1522 # or other strangeness.
1523 if (! defined $directory) {
1524 die("mr: failed to determine working directory\n");
1526 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1527 # the config file might be a symlink to elsewhere, and the directory it's
1528 # in is significant.
1529 if ($ENV{MR_CONFIG} !~ /^\//) {
1530 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1532 # Try to set MR_PATH to the path to the program.
1534 use FindBin qw($Bin $Script);
1535 $ENV{MR_PATH}=$Bin."/".$Script;
1543 elsif (! @ok && @skipped) {
1556 loadconfig($ENV{MR_CONFIG});
1557 #use Data::Dumper; print Dumper(\%config);
1559 my $action=expandaction(shift @ARGV);
1566 # Finally, some useful actions that mr knows about by default.
1567 # These can be overridden in ~/.mrconfig.
1582 echo "mr (warning): $@" >&2
1588 if [ -z "$1" ] || [ -z "$2" ]; then
1589 error "mr: usage: hours_since action num"
1591 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1592 if [ -e "$MR_REPO/$dir" ]; then
1593 flagfile="$MR_REPO/$dir/.mr_last$1"
1597 if [ -z "$flagfile" ]; then
1598 error "cannot determine flag filename"
1600 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1601 if [ "$delta" -lt "$2" ]; then
1609 svn_test = test -d "$MR_REPO"/.svn
1610 git_test = test -d "$MR_REPO"/.git
1611 bzr_test = test -d "$MR_REPO"/.bzr
1612 cvs_test = test -d "$MR_REPO"/CVS
1613 hg_test = test -d "$MR_REPO"/.hg
1614 darcs_test = test -d "$MR_REPO"/_darcs
1615 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1617 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1618 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1619 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1621 svn_update = svn update "$@"
1622 git_update = git pull "$@"
1623 bzr_update = bzr merge --pull "$@"
1624 cvs_update = cvs update "$@"
1625 hg_update = hg pull "$@" && hg update "$@"
1626 darcs_update = darcs pull -a "$@"
1627 fossil_update = fossil pull "$@"
1629 svn_status = svn status "$@"
1630 git_status = git status -s "$@" || true
1631 bzr_status = bzr status --short "$@"
1632 cvs_status = cvs status "$@"
1633 hg_status = hg status "$@"
1634 darcs_status = darcs whatsnew -ls "$@" || true
1635 fossil_status = fossil changes "$@"
1637 svn_commit = svn commit "$@"
1638 git_commit = git commit -a "$@" && git push --all
1639 bzr_commit = bzr commit "$@" && bzr push
1640 cvs_commit = cvs commit "$@"
1641 hg_commit = hg commit -m "$@" && hg push
1642 darcs_commit = darcs record -a -m "$@" && darcs push -a
1643 fossil_commit = fossil commit "$@"
1645 git_record = git commit -a "$@"
1646 bzr_record = bzr commit "$@"
1647 hg_record = hg commit -m "$@"
1648 darcs_record = darcs record -a -m "$@"
1649 fossil_record = fossil commit "$@"
1652 git_push = git push "$@"
1653 bzr_push = bzr push "$@"
1655 hg_push = hg push "$@"
1656 darcs_push = darcs push -a "$@"
1657 fossil_push = fossil push "$@"
1659 svn_diff = svn diff "$@"
1660 git_diff = git diff "$@"
1661 bzr_diff = bzr diff "$@"
1662 cvs_diff = cvs diff "$@"
1663 hg_diff = hg diff "$@"
1664 darcs_diff = darcs diff -u "$@"
1665 fossil_diff = fossil diff "$@"
1667 svn_log = svn log "$@"
1668 git_log = git log "$@"
1669 bzr_log = bzr log "$@"
1670 cvs_log = cvs log "$@"
1671 hg_log = hg log "$@"
1672 darcs_log = darcs changes "$@"
1673 git_bare_log = git log "$@"
1674 fossil_log = fossil timeline "$@"
1677 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1678 if [ -z "$url" ]; then
1679 error "cannot determine svn url"
1681 echo "Registering svn url: $url in $MR_CONFIG"
1682 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1684 url="`LC_ALL=C git config --get remote.origin.url`" || true
1685 if [ -z "$url" ]; then
1686 error "cannot determine git url"
1688 echo "Registering git url: $url in $MR_CONFIG"
1689 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1691 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1692 if [ -z "$url" ]; then
1693 error "cannot determine bzr url"
1695 echo "Registering bzr url: $url in $MR_CONFIG"
1696 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1698 repo=`cat CVS/Repository`
1700 if [ -z "$root" ]; then
1701 error "cannot determine cvs root"
1703 echo "Registering cvs repository $repo at root $root"
1704 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1706 url=`hg showconfig paths.default`
1707 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1708 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1710 url=`cat _darcs/prefs/defaultrepo`
1711 echo "Registering darcs repository $url in $MR_CONFIG"
1712 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1714 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1715 if [ -z "$url" ]; then
1716 error "cannot determine git url"
1718 echo "Registering git url: $url in $MR_CONFIG"
1719 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1721 url=`fossil remote-url`
1722 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1723 echo "Registering fossil repository $url in $MR_CONFIG"
1724 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1726 svn_trusted_checkout = svn co $url $repo
1727 svn_alt_trusted_checkout = svn checkout $url $repo
1728 git_trusted_checkout = git clone $url $repo
1729 bzr_trusted_checkout = bzr clone $url $repo
1731 hg_trusted_checkout = hg clone $url $repo
1732 darcs_trusted_checkout = darcs get $url $repo
1733 git_bare_trusted_checkout = git clone --bare $url $repo
1734 # fossil: messy to do
1740 SHOWMANFILE="man -f"
1746 SHOWMANFILE="man -l"
1749 if [ ! -e "$MR_PATH" ]; then
1750 error "cannot find program path"
1752 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1753 trap "rm -f $tmp" exit
1754 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1755 $SHOWMANFILE "$tmp" || error "man failed"
1761 if [ -s ~/.mrlog ]; then
1762 info "running offline commands"
1763 mv -f ~/.mrlog ~/.mrlog.old
1764 if ! sh -e ~/.mrlog.old; then
1765 error "offline command failed; left in ~/.mrlog.old"
1769 info "no offline commands to run"
1774 info "offline mode enabled"
1776 info "remembering command: 'mr $@'"
1777 command="mr -d '$(pwd)' $MR_SWITCHES"
1779 command="$command '$w'"
1781 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1782 echo "$command" >> ~/.mrlog
1785 ed = echo "A horse is a horse, of course, of course.."
1786 T = echo "I pity the fool."
1787 right = echo "Not found."
1789 # vim:sw=8:sts=0:ts=8:noet