All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
5 mr - a Multiple Repository management tool
9 B<mr> [options] checkout
11 B<mr> [options] update
13 B<mr> [options] status
15 B<mr> [options] commit [-m "message"]
17 B<mr> [options] record [-m "message"]
25 B<mr> [options] run command [param ...]
27 B<mr> [options] bootstrap url [directory]
29 B<mr> [options] register [repository]
31 B<mr> [options] config section ["parameter=[value]" ...]
33 B<mr> [options] action [params ...]
35 B<mr> [options] [online|offline]
37 B<mr> [options] remember action [params ...]
41 B<mr> is a Multiple Repository management tool. It can checkout, update, or
42 perform other actions on a set of repositories as if they were one combined
43 repository. It supports any combination of subversion, git, cvs, mercurial,
44 bzr, darcs and fossil repositories, and support for other revision
45 control systems can easily be added.
47 B<mr> cds into and operates on all registered repositories at or below your
48 working directory. Or, if you are in a subdirectory of a repository that
49 contains no other registered repositories, it will stay in that directory,
50 and work on only that repository,
52 B<mr> is configured by .mrconfig files, which list the repositories. It
53 starts by reading the .mrconfig file in your home directory, and this can
54 in turn chain load .mrconfig files from repositories. It also automatically
55 looks for a .mrconfig file in the current directory, or in one of its
58 These predefined commands should be fairly familiar to users of any revision
63 =item checkout (or co)
65 Checks out any repositories that are not already checked out.
69 Updates each repository from its configured remote repository.
71 If a repository isn't checked out yet, it will first check it out.
75 Displays a status report for each repository, showing what
76 uncommitted changes are present in the repository.
80 Commits changes to each repository. (By default, changes are pushed to the
81 remote repository too, when using distributed systems like git. If you
82 don't like this default, you can change it in your .mrconfig, or use record
85 The optional -m parameter allows specifying a commit message.
89 Records changes to the local repository, but does not push them to the
90 remote repository. Only supported for distributed revision control systems.
92 The optional -m parameter allows specifying a commit message.
96 Pushes committed local changes to the remote repository. A no-op for
97 centralized revision control systems.
101 Show a diff of uncommitted changes.
107 =item run command [param ...]
109 Runs the specified command in each repository.
113 These commands are also available:
117 =item bootstrap url [directory]
119 Causes mr to download the url, and use it as a .mrconfig file
120 to checkout the repositories listed in it, into the specified directory.
122 The directory will be created if it does not exist. If no directory is
123 specified, the current directory will be used.
125 If the .mrconfig file includes a repository named ".", that
126 is checked out into the top of the specified directory.
130 List the repositories that mr will act on.
134 Register an existing repository in a mrconfig file. By default, the
135 repository in the current directory is registered, or you can specify a
136 directory to register.
138 The mrconfig file that is modified is chosen by either the -c option, or by
139 looking for the closest known one at or in a parent of the current directory.
143 Adds, modifies, removes, or prints a value from a mrconfig file. The next
144 parameter is the name of the section the value is in. To add or modify
145 values, use one or more instances of "parameter=value". Use "parameter=" to
146 remove a parameter. Use just "parameter" to get the value of a parameter.
148 For example, to add (or edit) a repository in src/foo:
150 mr config src/foo checkout="svn co svn://example.com/foo/trunk foo"
152 To show the command that mr uses to update the repository in src/foo:
154 mr config src/foo update
156 To see the built-in library of shell functions contained in mr:
158 mr config DEFAULT lib
160 The mrconfig file that is used is chosen by either the -c option, or by
161 looking for the closest known one at or in a parent of the current directory.
165 Advises mr that it is in offline mode. Any commands that fail in
166 offline mode will be remembered, and retried when mr is told it's online.
170 Advices mr that it is in online mode again. Commands that failed while in
171 offline mode will be re-run.
175 Remember a command, to be run later when mr re-enters online mode. This
176 implicitly puts mr into offline mode. The command can be any regular mr
177 command. This is useful when you know that a command will fail due to being
178 offline, and so don't want to run it right now at all, but just remember
179 to run it when you go back online.
187 Actions can be abbreviated to any unambiguous substring, so
188 "mr st" is equivalent to "mr status", and "mr up" is equivalent to "mr
191 Additional parameters can be passed to most commands, and are passed on
192 unchanged to the underlying revision control system. This is mostly useful
193 if the repositories mr will act on all use the same revision control
202 =item --directory directory
204 Specifies the topmost directory that B<mr> should work in. The default is
205 the current working directory.
209 =item --config mrconfig
211 Use the specified mrconfig file. The default is to use both B<~/.mrconfig>
212 as well as look for a .mrconfig file in the current directory, or in one
213 of its parent directories.
225 Be quiet. This supresses mr's usual output. Any output from the
226 commands that mr runs will still be displayed.
228 For example, to see only uncommitted changes, use "mr -q status"
234 Accept untrusted SSL certificates when bootstrapping.
240 Expand the statistics line displayed at the end to include information
241 about exactly which repositories failed and were skipped, if any.
247 Interactive mode. If a repository fails to be processed, a subshell will be
248 started which you can use to resolve or investigate the problem. Exit the
249 subshell to continue the mr run.
253 =item --no-recurse [number]
255 If no number if specified, just operate on the repository for the current
256 directory, do not recurse into deeper repositories.
258 If a number is specified, will recurse into repositories at most that many
259 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
260 but not ./src/packages/bar.
264 =item --jobs [number]
266 Run the specified number of jobs in parallel, or an unlimited number of jobs
267 with no number specified. This can greatly speed up operations such as updates.
268 It is not recommended for interactive operations.
270 Note that running more than 10 jobs at a time is likely to run afoul of
271 ssh connection limits. Running between 3 and 5 jobs at a time will yield
272 a good speedup in updates without loading the machine too much.
278 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
285 This obsolete flag is ignored.
289 =head1 MRCONFIG FILES
291 Here is an example .mrconfig file:
294 checkout = svn co svn://svn.example.com/src/trunk src
298 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
300 git checkout -b mybranch origin/master
302 The .mrconfig file uses a variant of the INI file format. Lines starting with
303 "#" are comments. Values can be continued to the following line by
304 indenting the line with whitespace.
306 The "DEFAULT" section allows setting default values for the sections that
309 The "ALIAS" section allows adding aliases for actions. Each parameter
310 is an alias, and its value is the action to use.
312 All other sections add repositories. The section header specifies the
313 directory where the repository is located. This is relative to the directory
314 that contains the mrconfig file, but you can also choose to use absolute
315 paths. (Note that you can use environment variables in section names; they
316 will be passed through the shell for expansion. For example,
317 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
319 Within a section, each parameter defines a shell command to run to handle a
320 given action. mr contains default handlers for "update", "status",
321 "commit", and other standard actions. Normally you only need to specify what
322 to do for "checkout".
324 Note that these shell commands are run in a "set -e" shell
325 environment, where any additional parameters you pass are available in
326 "$@". The "checkout" command is run in the parent of the repository
327 directory, since the repository isn't checked out yet. All other commands
328 are run inside the repository, though not necessarily at the top of it.
330 The "MR_REPO" environment variable is set to the path to the top of the
331 repository. (For the "register" action, "MR_REPO" is instead set to the
332 basename of the directory that should be created when checking the
335 The "MR_CONFIG" environment variable is set to the .mrconfig file
336 that defines the repo being acted on, or, if the repo is not yet in a config
337 file, the .mrconfig file that should be modified to register the repo.
339 A few parameters have special meanings:
345 If the "skip" parameter is set and its command returns true, then B<mr>
346 will skip acting on that repository. The command is passed the action
349 Here are two examples. The first skips the repo unless
350 mr is run by joey. The second uses the hours_since function
351 (included in mr's built-in library) to skip updating the repo unless it's
352 been at least 12 hours since the last update.
354 skip = test `whoami` != joey
355 skip = [ "$1" = update ] && ! hours_since "$1" 12
359 The "order" parameter can be used to override the default ordering of
360 repositories. The default order value is 10. Use smaller values to make
361 repositories be processed earlier, and larger values to make repositories
364 Note that if a repository is located in a subdirectory of another
365 repository, ordering it to be processed earlier is not recommended.
369 If the "chain" parameter is set and its command returns true, then B<mr>
370 will try to load a .mrconfig file from the root of the repository.
374 If the "include" parameter is set, its command is ran, and should output
375 additional mrconfig file content. The content is included as if it were
376 part of the including file.
378 Unlike all other parameters, this parameter does not need to be placed
383 The "lib" parameter can specify some shell code that will be run before each
384 command, this can be a useful way to define shell functions for other commands
389 If the "fixups" parameter is set, its command is run whenever a repository
390 is checked out, or updated. This provides an easy way to do things
391 like permissions fixups, or other tweaks to the repository content,
392 whenever the repository is changed.
396 If a "pre_action" parameter is set, its command is run before mr performs the
397 specified action. Similarly, "post_action" parameters are run after mr
398 successfully performs the specified action. For example, "pre_commit" is
399 run before committing; "post_update" is run after updating.
403 When looking for a command to run for a given action, mr first looks for
404 a parameter with the same name as the action. If that is not found, it
405 looks for a parameter named "rcs_action" (substituting in the name of the
406 revision control system and the action). The name of the revision control
407 system is itself determined by running each defined "rcs_test" action,
410 Internally, mr has settings for "git_update", "svn_update", etc. To change
411 the action that is performed for a given revision control system, you can
412 override these rcs specific actions. To add a new revision control system,
413 you can just add rcs specific actions for it.
415 =head1 UNTRUSTED MRCONFIG FILES
417 Since mrconfig files can contain arbitrary shell commands, they can do
418 anything. This flexibility is good, but it also allows a malicious mrconfig
419 file to delete your whole home directory. Such a file might be contained
420 inside a repository that your main ~/.mrconfig checks out. To
421 avoid worries about evil commands in a mrconfig file, mr defaults to
422 reading all mrconfig files other than the main ~/.mrconfig in untrusted
423 mode. In untrusted mode, mrconfig files are limited to running only known
424 safe commands (like "git clone") in a carefully checked manner.
426 To configure mr to trust other mrconfig files, list them in ~/.mrtrust.
427 One mrconfig file should be listed per line. Either the full pathname
428 should be listed, or the pathname can start with "~/" to specify a file
429 relative to your home directory.
431 =head1 OFFLINE LOG FILE
433 The ~/.mrlog file contains commands that mr has remembered to run later,
434 due to being offline. You can delete or edit this file to remove commands,
435 or even to add other commands for 'mr online' to run. If the file is
436 present, mr assumes it is in offline mode.
440 mr can be extended to support things such as unison and git-svn. Some
441 files providing such extensions are available in /usr/share/mr/. See
442 the documentation in the files for details about using them.
446 mr returns nonzero if a command failed in any of the repositories.
450 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
452 Licensed under the GNU GPL version 2 or higher.
454 http://kitenet.net/~joey/code/mr/
461 use Cwd qw(getcwd abs_path);
463 # things that can happen when mr runs a command
472 my $config_overridden=0;
482 my $directory=getcwd();
484 $ENV{MR_CONFIG}=find_mrconfig();
491 my (@ok, @failed, @skipped);
497 my ($action, $dir, $topdir, $subdir) = @_;
499 if (exists $rcs{$dir}) {
504 foreach my $rcs_test (
506 length $a <=> length $b
509 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
510 my ($rcs)=$rcs_test=~/(.*)_test/;
511 $test="my_$rcs_test() {\n$config{$topdir}{$subdir}{$rcs_test}\n}\n".$test;
512 $test.="if my_$rcs_test; then echo $rcs; fi\n";
514 $test=$config{$topdir}{$subdir}{lib}."\n".$test
515 if exists $config{$topdir}{$subdir}{lib};
517 print "mr $action: running rcs test >>$test<<\n" if $verbose;
522 print STDERR "mr $action: found multiple possible repository types ($rcs) for ".fulldir($topdir, $subdir)."\n";
526 return $rcs{$dir}=undef;
529 return $rcs{$dir}=$rcs;
534 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
536 if (exists $config{$topdir}{$subdir}{$action}) {
537 return $config{$topdir}{$subdir}{$action};
544 my $rcs=rcs_test(@_);
547 exists $config{$topdir}{$subdir}{$rcs."_".$action}) {
548 return $config{$topdir}{$subdir}{$rcs."_".$action};
556 my ($topdir, $subdir) = @_;
557 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
561 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
562 my $fulldir=fulldir($topdir, $subdir);
564 $ENV{MR_CONFIG}=$configfiles{$topdir};
565 my $lib=exists $config{$topdir}{$subdir}{lib} ?
566 $config{$topdir}{$subdir}{lib}."\n" : "";
567 my $is_checkout=($action eq 'checkout');
568 my $is_update=($action =~ /update/);
573 if (! $force_checkout) {
575 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
579 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
584 return action("checkout", $dir, $topdir, $subdir);
588 my $skiptest=findcommand("skip", $dir, $topdir, $subdir, $is_checkout);
589 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
591 if (defined $skiptest) {
592 my $test="set -e;".$lib.
593 "my_action(){ $skiptest\n }; my_action '$action'";
594 print "mr $action: running skip test >>$test<<\n" if $verbose;
595 my $ret=system($test);
597 if (($? & 127) == 2) {
598 print STDERR "mr $action: interrupted\n";
602 print STDERR "mr $action: skip test received signal ".($? & 127)."\n";
606 if ($ret >> 8 == 0) {
607 print "mr $action: $dir skipped per config file\n" if $verbose;
612 if ($is_checkout && ! -d $dir) {
613 print "mr $action: creating parent directory $dir\n" if $verbose;
614 system("mkdir", "-p", $dir);
617 if (! $no_chdir && ! chdir($dir)) {
618 print STDERR "mr $action: failed to chdir to $dir: $!\n";
621 elsif (! defined $command) {
622 my $rcs=rcs_test(@_);
623 if (! defined $rcs) {
624 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
628 print STDERR "mr $action: no defined action for $rcs repository $fulldir, skipping\n";
634 print "mr $action: $fulldir\n" unless $quiet;
638 $s=~s/^\Q$fulldir\E\/?//;
639 print "mr $action: $fulldir (in subdir $s)\n" unless $quiet;
642 my $hookret=hook("pre_$action", $topdir, $subdir);
643 return $hookret if $hookret != OK;
645 $command="set -e; ".$lib.
646 "my_action(){ $command\n }; my_action ".
647 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
648 print "mr $action: running >>$command<<\n" if $verbose;
649 my $ret=system($command);
651 if (($? & 127) == 2) {
652 print STDERR "mr $action: interrupted\n";
656 print STDERR "mr $action: received signal ".($? & 127)."\n";
659 print STDERR "mr $action: failed ($ret)\n" if $verbose;
660 if ($ret >> 8 != 0) {
661 print STDERR "mr $action: command failed\n";
662 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
663 # recreate original command line to
664 # remember, and avoid recursing
666 @ARGV=('-n', $action, @orig);
667 action("remember", $dir, $topdir, $subdir);
672 print STDERR "mr $action: command died ($ret)\n";
677 if ($is_checkout && ! -d $dir) {
678 print STDERR "mr $action: $dir missing after checkout\n";;
682 my $ret=hook("post_$action", $topdir, $subdir);
683 return $ret if $ret != OK;
685 if (($is_checkout || $is_update)) {
686 my $ret=hook("fixups", $topdir, $subdir);
687 return $ret if $ret != OK;
696 my ($hook, $topdir, $subdir) = @_;
698 my $command=$config{$topdir}{$subdir}{$hook};
699 return OK unless defined $command;
700 my $lib=exists $config{$topdir}{$subdir}{lib} ?
701 $config{$topdir}{$subdir}{lib}."\n" : "";
702 my $shell="set -e;".$lib.
703 "my_hook(){ $command\n }; my_hook";
704 print "mr $hook: running >>$shell<<\n" if $verbose;
705 my $ret=system($shell);
707 if (($? & 127) == 2) {
708 print STDERR "mr $hook: interrupted\n";
712 print STDERR "mr $hook: received signal ".($? & 127)."\n";
720 # run actions on multiple repos, in parallel
730 while (@fhs or @repos) {
731 while ((!$jobs || $running < $jobs) && @repos) {
733 my $repo = shift @repos;
734 pipe(my $outfh, CHILD_STDOUT);
735 pipe(my $errfh, CHILD_STDERR);
737 unless ($pid = fork) {
738 die "mr $action: cannot fork: $!" unless defined $pid;
739 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
740 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
745 exit action($action, @$repo);
749 push @active, [$pid, $repo];
750 push @fhs, [$outfh, $errfh];
753 my ($rin, $rout) = ('','');
755 foreach my $fh (@fhs) {
756 next unless defined $fh;
757 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
758 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
760 $nfound = select($rout=$rin, undef, undef, 1);
761 foreach my $channel (0, 1) {
762 foreach my $i (0..$#fhs) {
763 next unless defined $fhs[$i];
764 my $fh = $fhs[$i][$channel];
765 next unless defined $fh;
766 if (vec($rout, fileno($fh), 1) == 1) {
768 if (sysread($fh, $r, 1024) == 0) {
770 $fhs[$i][$channel] = undef;
771 if (! defined $fhs[$i][0] &&
772 ! defined $fhs[$i][1]) {
773 waitpid($active[$i][0], 0);
774 print STDOUT $out[$i][0];
775 print STDERR $out[$i][1];
776 record($active[$i][1], $? >> 8);
778 splice(@active, $i, 1);
783 $out[$i][$channel] .= $r;
791 my $dir=shift()->[0];
796 print "\n" unless $quiet;
798 elsif ($ret == FAILED) {
800 chdir($dir) unless $no_chdir;
801 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
802 system((getpwuid($<))[8], "-i");
805 print "\n" unless $quiet;
807 elsif ($ret == SKIPPED) {
810 elsif ($ret == ABORT) {
814 die "unknown exit status $ret";
820 if (! @ok && ! @failed && ! @skipped) {
821 die "mr $action: no repositories found to work on\n";
823 print "mr $action: finished (".join("; ",
824 showstat($#ok+1, "ok", "ok"),
825 showstat($#failed+1, "failed", "failed"),
826 showstat($#skipped+1, "skipped", "skipped"),
827 ).")\n" unless $quiet;
830 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
833 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
843 return "$count ".($count > 1 ? $plural : $singular);
848 # an ordered list of repos
851 foreach my $topdir (sort keys %config) {
852 foreach my $subdir (sort keys %{$config{$topdir}}) {
856 order => $config{$topdir}{$subdir}{order},
861 $a->{order} <=> $b->{order}
863 $a->{topdir} cmp $b->{topdir}
865 $a->{subdir} cmp $b->{subdir}
871 my $topdir=$repo->{topdir};
872 my $subdir=$repo->{subdir};
873 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
878 # figure out which repos to act on
881 foreach my $repo (repolist()) {
882 my $topdir=$repo->{topdir};
883 my $subdir=$repo->{subdir};
885 next if $subdir eq 'DEFAULT';
886 my $dir=repodir($repo);
888 $dir.="/" unless $dir=~/\/$/;
889 $d.="/" unless $d=~/\/$/;
890 next if $dir ne $d && $dir !~ /^\Q$d\E/;
891 if (defined $max_depth) {
892 my @a=split('/', $dir);
893 my @b=split('/', $d);
894 do { } while (@a && @b && shift(@a) eq shift(@b));
895 next if @a > $max_depth || @b > $max_depth;
897 push @repos, [$dir, $topdir, $subdir];
900 # fallback to find a leaf repo
901 foreach my $repo (reverse repolist()) {
902 my $topdir=$repo->{topdir};
903 my $subdir=$repo->{subdir};
905 next if $subdir eq 'DEFAULT';
906 my $dir=repodir($repo);
908 $dir.="/" unless $dir=~/\/$/;
909 $d.="/" unless $d=~/\/$/;
910 if ($d=~/^\Q$dir\E/) {
911 push @repos, [$dir, $topdir, $subdir];
933 sub is_trusted_config {
934 my $config=shift; # must be abs_pathed already
936 # We always trust ~/.mrconfig.
937 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
939 return 1 if $trust_all;
941 my $trustfile=$ENV{HOME}."/.mrtrust";
944 $trusted{"$ENV{HOME}/.mrconfig"}=1;
945 if (open (TRUST, "<", $trustfile)) {
948 s/^~\//$ENV{HOME}\//;
949 $trusted{abs_path($_)}=1;
955 return $trusted{$config};
959 sub is_trusted_repo {
962 # Tightly limit what is allowed in a repo name.
963 # No ../, no absolute paths, and no unusual filenames
964 # that might try to escape to the shell.
965 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
966 $repo !~ /\.\./ && $repo !~ /^\//;
969 sub is_trusted_checkout {
972 # To determine if the command is safe, compare it with the
973 # *_trusted_checkout config settings. Those settings are
974 # templates for allowed commands, so make sure that each word
975 # of the command matches the corresponding word of the template.
978 foreach my $word (split(' ', $command)) {
980 if ($word=~/^'(.*)'$/) {
983 elsif ($word=~/^"(.*)"$/) {
990 foreach my $key (grep { /_trusted_checkout$/ }
991 keys %{$config{''}{DEFAULT}}) {
992 my @twords=split(' ', $config{''}{DEFAULT}{$key});
993 next if @words > @twords;
997 for (my $c=0; $c < @twords && $match; $c++) {
998 if ($twords[$c] eq '$url') {
999 # Match all the typical characters found in
1000 # urls, plus @ which svn can use. Note
1001 # that the "url" might also be a local
1004 defined $words[$c] &&
1005 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1009 elsif ($twords[$c] eq '$repo') {
1010 # If a repo is not specified, assume it
1011 # will be the last path component of the
1012 # url, or something derived from it, and
1014 if (! defined $words[$c] && defined $url) {
1015 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1019 defined $words[$c] &&
1020 is_trusted_repo($words[$c])
1023 elsif (defined $words[$c] && $twords[$c] eq $words[$c]) {
1038 "(To trust this file, list it in ~/.mrtrust.)\n";
1050 if (ref $f eq 'GLOB') {
1060 my $absf=abs_path($f);
1061 if ($loaded{$absf}) {
1066 $trusted=is_trusted_config($absf);
1068 if (! defined $dir) {
1069 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1070 if (! defined $dir) {
1075 $dir=abs_path($dir)."/";
1077 if (! exists $configfiles{$dir}) {
1078 $configfiles{$dir}=$f;
1081 # copy in defaults from first parent
1083 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1084 if ($parent eq '/') {
1087 if (exists $config{$parent} &&
1088 exists $config{$parent}{DEFAULT}) {
1089 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1094 print "mr: loading config $f\n" if $verbose;
1095 open($in, "<", $f) || die "mr: open $f: $!\n";
1098 close $in unless ref $f eq 'GLOB';
1106 next if /^\s*\#/ || /^\s*$/;
1107 if (/^\[([^\]]*)\]\s*$/) {
1111 if (! is_trusted_repo($section) ||
1112 $section eq 'ALIAS' ||
1113 $section eq 'DEFAULT') {
1114 trusterror "mr: illegal section \"[$section]\" in untrusted $f line $line";
1117 $section=expandenv($section) if $trusted;
1118 if ($section ne 'ALIAS' &&
1119 ! exists $config{$dir}{$section} &&
1120 exists $config{$dir}{DEFAULT}) {
1122 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1125 elsif (/^(\w+)\s*=\s*(.*)/) {
1130 while (@lines && $lines[0]=~/^\s(.+)/) {
1138 # Untrusted files can only contain checkout
1140 if ($parameter ne 'checkout') {
1141 trusterror "mr: illegal setting \"$parameter=$value\" in untrusted $f line $line";
1143 if (! is_trusted_checkout($value)) {
1144 trusterror "mr: illegal checkout command \"$value\" in untrusted $f line $line";
1148 if ($parameter eq "include") {
1149 print "mr: including output of \"$value\"\n" if $verbose;
1150 unshift @lines, `$value`;
1152 print STDERR "mr: include command exited nonzero ($?)\n";
1157 if (! defined $section) {
1158 die "$f line $.: parameter ($parameter) not in section\n";
1160 if ($section eq 'ALIAS') {
1161 $alias{$parameter}=$value;
1163 elsif ($parameter eq 'lib') {
1164 $config{$dir}{$section}{lib}.=$value."\n";
1167 $config{$dir}{$section}{$parameter}=$value;
1168 if ($parameter =~ /.*_(.*)/) {
1169 $knownactions{$1}=1;
1172 $knownactions{$parameter}=1;
1174 if ($parameter eq 'chain' &&
1175 length $dir && $section ne "DEFAULT" &&
1176 -e $dir.$section."/.mrconfig") {
1177 my $ret=system($value);
1179 if (($? & 127) == 2) {
1180 print STDERR "mr: chain test interrupted\n";
1184 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1188 push @toload, $dir.$section."/.mrconfig";
1194 die "$f line $line: parse error\n";
1203 sub startingconfig {
1204 %alias=%config=%configfiles=%knownactions=%loaded=();
1205 my $datapos=tell(DATA);
1207 seek(DATA,$datapos,0); # rewind
1212 # the section to modify or add
1213 my $targetsection=shift;
1214 # fields to change in the section
1215 # To remove a field, set its value to "".
1216 my %changefields=@_;
1222 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1227 my $formatfield=sub {
1229 my @value=split(/\n/, shift);
1231 return "$field = ".shift(@value)."\n".
1232 join("", map { "\t$_\n" } @value);
1236 while ($out[$#out] =~ /^\s*$/) {
1237 unshift @blanks, pop @out;
1239 foreach my $field (sort keys %changefields) {
1240 if (length $changefields{$field}) {
1241 push @out, "$field = $changefields{$field}\n";
1242 delete $changefields{$field};
1252 if (/^\s*\#/ || /^\s*$/) {
1255 elsif (/^\[([^\]]*)\]\s*$/) {
1256 if (defined $section &&
1257 $section eq $targetsection) {
1261 $section=expandenv($1);
1265 elsif (/^(\w+)\s*=\s(.*)/) {
1270 while (@lines && $lines[0]=~/^\s(.+)/) {
1276 if ($section eq $targetsection) {
1277 if (exists $changefields{$parameter}) {
1278 if (length $changefields{$parameter}) {
1279 $value=$changefields{$parameter};
1281 delete $changefields{$parameter};
1285 push @out, $formatfield->($parameter, $value);
1289 if (defined $section &&
1290 $section eq $targetsection) {
1293 elsif (%changefields) {
1294 push @out, "\n[$targetsection]\n";
1295 foreach my $field (sort keys %changefields) {
1296 if (length $changefields{$field}) {
1297 push @out, $formatfield->($field, $changefields{$field});
1302 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1310 # actions that do not operate on all repos
1311 if ($action eq 'help') {
1314 elsif ($action eq 'config') {
1317 elsif ($action eq 'register') {
1320 elsif ($action eq 'bootstrap') {
1323 elsif ($action eq 'remember' ||
1324 $action eq 'offline' ||
1325 $action eq 'online') {
1326 my @repos=selectrepos;
1327 action($action, @{$repos[0]}) if @repos;
1331 if (!$jobs || $jobs > 1) {
1332 mrs($action, selectrepos());
1335 foreach my $repo (selectrepos()) {
1336 record($repo, action($action, @$repo));
1342 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1347 die "mr config: not enough parameters\n";
1350 if ($section=~/^\//) {
1351 # try to convert to a path relative to the config file
1352 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1353 $dir=abs_path($dir);
1354 $dir.="/" unless $dir=~/\/$/;
1355 if ($section=~/^\Q$dir\E(.*)/) {
1361 if (/^([^=]+)=(.*)$/) {
1362 $changefields{$1}=$2;
1366 foreach my $topdir (sort keys %config) {
1367 if (exists $config{$topdir}{$section} &&
1368 exists $config{$topdir}{$section}{$_}) {
1369 print $config{$topdir}{$section}{$_}."\n";
1371 last if $section eq 'DEFAULT';
1375 die "mr config: $section $_ not set\n";
1379 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1384 if ($config_overridden) {
1385 # Find the directory that the specified config file is
1387 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1390 # Find the closest known mrconfig file to the current
1392 $directory.="/" unless $directory=~/\/$/;
1394 foreach my $topdir (reverse sort keys %config) {
1395 next unless length $topdir;
1396 if ($directory=~/^\Q$topdir\E/) {
1397 $ENV{MR_CONFIG}=$configfiles{$topdir};
1403 if (! $foundconfig) {
1404 $directory=""; # no config file, use builtin
1408 my $subdir=shift @ARGV;
1409 if (! chdir($subdir)) {
1410 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1414 $ENV{MR_REPO}=getcwd();
1415 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1416 if (! defined $command) {
1417 die "mr register: unknown repository type\n";
1420 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1421 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1422 "my_action(){ $command\n }; my_action ".
1423 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1424 print "mr register: running >>$command<<\n" if $verbose;
1425 exec($command) || die "exec: $!";
1429 my $url=shift @ARGV;
1430 my $dir=shift @ARGV || ".";
1432 if (! defined $url || ! length $url) {
1433 die "mr: bootstrap requires url\n";
1436 # Download the config file to a temporary location.
1437 eval q{use File::Temp};
1439 my $tmpconfig=File::Temp->new();
1440 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1441 push(@curlargs, "-k") if $insecure;
1442 my $curlstatus = system(@curlargs);
1443 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1444 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1447 system("mkdir", "-p", $dir);
1449 chdir($dir) || die "chdir $dir: $!";
1451 # Special case to handle checkout of the "." repo, which
1452 # would normally be skipped.
1453 my $topdir=abs_path(".")."/";
1454 my @repo=($topdir, $topdir, ".");
1455 loadconfig($tmpconfig, $topdir);
1456 record(\@repo, action("checkout", @repo, 1))
1457 if exists $config{$topdir}{"."}{"checkout"};
1459 if (-e ".mrconfig") {
1460 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1463 eval q{use File::Copy};
1465 move($tmpconfig, ".mrconfig") || die "rename: $!";
1468 # Reload the config file (in case we got a different version)
1469 # and checkout everything else.
1471 loadconfig(".mrconfig");
1472 dispatch("checkout");
1473 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1474 showstats("bootstrap");
1478 # alias expansion and command stemming
1481 if (exists $alias{$action}) {
1482 $action=$alias{$action};
1484 if (! exists $knownactions{$action}) {
1485 my @matches = grep { /^\Q$action\E/ }
1486 keys %knownactions, keys %alias;
1487 if (@matches == 1) {
1488 $action=$matches[0];
1490 elsif (@matches == 0) {
1491 die "mr: unknown action \"$action\" (known actions: ".
1492 join(", ", sort keys %knownactions).")\n";
1495 die "mr: ambiguous action \"$action\" (matches: ".
1496 join(", ", @matches).")\n";
1504 while (length $dir) {
1505 if (-e "$dir/.mrconfig") {
1506 return "$dir/.mrconfig";
1508 $dir=~s/\/[^\/]*$//;
1510 return "$ENV{HOME}/.mrconfig";
1515 Getopt::Long::Configure("bundling", "no_permute");
1516 my $result=GetOptions(
1517 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1518 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1519 "p|path" => sub { }, # now default, ignore
1520 "v|verbose" => \$verbose,
1521 "q|quiet" => \$quiet,
1522 "s|stats" => \$stats,
1523 "k|insecure" => \$insecure,
1524 "i|interactive" => \$interactive,
1525 "n|no-recurse:i" => \$max_depth,
1526 "j|jobs:i" => \$jobs,
1527 "t|trust-all" => \$trust_all,
1529 if (! $result || @ARGV < 1) {
1530 die("Usage: mr [options] action [params ...]\n".
1531 "(Use mr help for man page.)\n");
1534 $ENV{MR_SWITCHES}="";
1535 foreach my $option (@saved) {
1536 last if $option eq $ARGV[0];
1537 $ENV{MR_SWITCHES}.="$option ";
1543 print STDERR "mr: interrupted\n";
1547 # This can happen if it's run in a directory that was removed
1548 # or other strangeness.
1549 if (! defined $directory) {
1550 die("mr: failed to determine working directory\n");
1552 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1553 # the config file might be a symlink to elsewhere, and the directory it's
1554 # in is significant.
1555 if ($ENV{MR_CONFIG} !~ /^\//) {
1556 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1558 # Try to set MR_PATH to the path to the program.
1560 use FindBin qw($Bin $Script);
1561 $ENV{MR_PATH}=$Bin."/".$Script;
1579 loadconfig("$ENV{HOME}/.mrconfig");
1580 loadconfig($ENV{MR_CONFIG});
1581 #use Data::Dumper; print Dumper(\%config);
1583 my $action=expandaction(shift @ARGV);
1590 # Finally, some useful actions that mr knows about by default.
1591 # These can be overridden in ~/.mrconfig.
1606 echo "mr (warning): $@" >&2
1612 if [ -z "$1" ] || [ -z "$2" ]; then
1613 error "mr: usage: hours_since action num"
1615 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1616 if [ -e "$MR_REPO/$dir" ]; then
1617 flagfile="$MR_REPO/$dir/.mr_last$1"
1621 if [ -z "$flagfile" ]; then
1622 error "cannot determine flag filename"
1624 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1625 if [ "$delta" -lt "$2" ]; then
1633 svn_test = test -d "$MR_REPO"/.svn
1634 git_test = test -d "$MR_REPO"/.git
1635 bzr_test = test -d "$MR_REPO"/.bzr
1636 cvs_test = test -d "$MR_REPO"/CVS
1637 hg_test = test -d "$MR_REPO"/.hg
1638 darcs_test = test -d "$MR_REPO"/_darcs
1639 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1641 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1642 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1643 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1645 svn_update = svn update "$@"
1646 git_update = git pull "$@"
1647 bzr_update = bzr merge --pull "$@"
1648 cvs_update = cvs update "$@"
1649 hg_update = hg pull "$@" && hg update "$@"
1650 darcs_update = darcs pull -a "$@"
1651 fossil_update = fossil pull "$@"
1653 svn_status = svn status "$@"
1654 git_status = git status -s "$@" || true
1655 bzr_status = bzr status --short "$@"
1656 cvs_status = cvs status "$@"
1657 hg_status = hg status "$@"
1658 darcs_status = darcs whatsnew -ls "$@" || true
1659 fossil_status = fossil changes "$@"
1661 svn_commit = svn commit "$@"
1662 git_commit = git commit -a "$@" && git push --all
1663 bzr_commit = bzr commit "$@" && bzr push
1664 cvs_commit = cvs commit "$@"
1665 hg_commit = hg commit -m "$@" && hg push
1666 darcs_commit = darcs record -a -m "$@" && darcs push -a
1667 fossil_commit = fossil commit "$@"
1669 git_record = git commit -a "$@"
1670 bzr_record = bzr commit "$@"
1671 hg_record = hg commit -m "$@"
1672 darcs_record = darcs record -a -m "$@"
1673 fossil_record = fossil commit "$@"
1676 git_push = git push "$@"
1677 bzr_push = bzr push "$@"
1679 hg_push = hg push "$@"
1680 darcs_push = darcs push -a "$@"
1681 fossil_push = fossil push "$@"
1683 svn_diff = svn diff "$@"
1684 git_diff = git diff "$@"
1685 bzr_diff = bzr diff "$@"
1686 cvs_diff = cvs diff "$@"
1687 hg_diff = hg diff "$@"
1688 darcs_diff = darcs diff -u "$@"
1689 fossil_diff = fossil diff "$@"
1691 svn_log = svn log "$@"
1692 git_log = git log "$@"
1693 bzr_log = bzr log "$@"
1694 cvs_log = cvs log "$@"
1695 hg_log = hg log "$@"
1696 darcs_log = darcs changes "$@"
1697 git_bare_log = git log "$@"
1698 fossil_log = fossil timeline "$@"
1703 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1704 if [ -z "$url" ]; then
1705 error "cannot determine svn url"
1707 echo "Registering svn url: $url in $MR_CONFIG"
1708 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1710 url="`LC_ALL=C git config --get remote.origin.url`" || true
1711 if [ -z "$url" ]; then
1712 error "cannot determine git url"
1714 echo "Registering git url: $url in $MR_CONFIG"
1715 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1717 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1718 if [ -z "$url" ]; then
1719 error "cannot determine bzr url"
1721 echo "Registering bzr url: $url in $MR_CONFIG"
1722 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'"
1724 repo=`cat CVS/Repository`
1726 if [ -z "$root" ]; then
1727 error "cannot determine cvs root"
1729 echo "Registering cvs repository $repo at root $root"
1730 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1732 url=`hg showconfig paths.default`
1733 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1734 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1736 url=`cat _darcs/prefs/defaultrepo`
1737 echo "Registering darcs repository $url in $MR_CONFIG"
1738 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1740 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1741 if [ -z "$url" ]; then
1742 error "cannot determine git url"
1744 echo "Registering git url: $url in $MR_CONFIG"
1745 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1747 url=`fossil remote-url`
1748 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1749 echo "Registering fossil repository $url in $MR_CONFIG"
1750 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1752 svn_trusted_checkout = svn co $url $repo
1753 svn_alt_trusted_checkout = svn checkout $url $repo
1754 git_trusted_checkout = git clone $url $repo
1755 bzr_trusted_checkout = bzr clone $url $repo
1757 hg_trusted_checkout = hg clone $url $repo
1758 darcs_trusted_checkout = darcs get $url $repo
1759 git_bare_trusted_checkout = git clone --bare $url $repo
1760 # fossil: messy to do
1766 SHOWMANFILE="man -f"
1772 SHOWMANFILE="man -l"
1775 if [ ! -e "$MR_PATH" ]; then
1776 error "cannot find program path"
1778 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1779 trap "rm -f $tmp" exit
1780 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1781 $SHOWMANFILE "$tmp" || error "man failed"
1787 if [ -s ~/.mrlog ]; then
1788 info "running offline commands"
1789 mv -f ~/.mrlog ~/.mrlog.old
1790 if ! sh -e ~/.mrlog.old; then
1791 error "offline command failed; left in ~/.mrlog.old"
1795 info "no offline commands to run"
1800 info "offline mode enabled"
1802 info "remembering command: 'mr $@'"
1803 command="mr -d '$(pwd)' $MR_SWITCHES"
1805 command="$command '$w'"
1807 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1808 echo "$command" >> ~/.mrlog
1811 ed = echo "A horse is a horse, of course, of course.."
1812 T = echo "I pity the fool."
1813 right = echo "Not found."
1815 # vim:sw=8:sts=0:ts=8:noet