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 version
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 version
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 version 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 version 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 version control system. This is mostly useful
193 if the repositories mr will act on all use the same version 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, as well as any output from
226 commands that are run (including stderr output). If a command fails,
227 the output will be shown.
233 Accept untrusted SSL certificates when bootstrapping.
239 Expand the statistics line displayed at the end to include information
240 about exactly which repositories failed and were skipped, if any.
246 Interactive mode. If a repository fails to be processed, a subshell will be
247 started which you can use to resolve or investigate the problem. Exit the
248 subshell to continue the mr run.
252 =item --no-recurse [number]
254 If no number if specified, just operate on the repository for the current
255 directory, do not recurse into deeper repositories.
257 If a number is specified, will recurse into repositories at most that many
258 subdirectories deep. For example, with -n 2 it would recurse into ./src/foo,
259 but not ./src/packages/bar.
263 =item --jobs [number]
265 Run the specified number of jobs in parallel, or an unlimited number of jobs
266 with no number specified. This can greatly speed up operations such as updates.
267 It is not recommended for interactive operations.
269 Note that running more than 10 jobs at a time is likely to run afoul of
270 ssh connection limits. Running between 3 and 5 jobs at a time will yield
271 a good speedup in updates without loading the machine too much.
277 Trust all mrconfig files even if they are not listed in ~/.mrtrust.
284 This obsolete flag is ignored.
288 =head1 MRCONFIG FILES
290 Here is an example .mrconfig file:
293 checkout = svn checkout svn://svn.example.com/src/trunk src
297 checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git &&
299 git checkout -b mybranch origin/master
301 The .mrconfig file uses a variant of the INI file format. Lines starting with
302 "#" are comments. Values can be continued to the following line by
303 indenting the line with whitespace.
305 The "DEFAULT" section allows setting default values for the sections that
308 The "ALIAS" section allows adding aliases for actions. Each parameter
309 is an alias, and its value is the action to use.
311 All other sections add repositories. The section header specifies the
312 directory where the repository is located. This is relative to the directory
313 that contains the mrconfig file, but you can also choose to use absolute
314 paths. (Note that you can use environment variables in section names; they
315 will be passed through the shell for expansion. For example,
316 "[$HOSTNAME]", or "[${HOSTNAME}foo]")
318 Within a section, each parameter defines a shell command to run to handle a
319 given action. mr contains default handlers for "update", "status",
320 "commit", and other standard actions.
322 Normally you only need to specify what to do for "checkout". Here you
323 specify the command to run in order to create a checkout of the repository.
324 The command will be run in the parent directory, and must create the
325 repository's directory. So use "git clone", "svn checkout", "bzr branch"
326 or "bzr checkout" (for a bound branch), etc.
328 Note that these shell commands are run in a "set -e" shell
329 environment, where any additional parameters you pass are available in
330 "$@". All commands other than "checkout" are run inside the repository,
331 though not necessarily at the top of it.
333 The "MR_REPO" environment variable is set to the path to the top of the
334 repository. (For the "register" action, "MR_REPO" is instead set to the
335 basename of the directory that should be created when checking the
338 The "MR_CONFIG" environment variable is set to the .mrconfig file
339 that defines the repo being acted on, or, if the repo is not yet in a config
340 file, the .mrconfig file that should be modified to register the repo.
342 The "MR_ACTION" environment variable is set to the command being run
343 (update, checkout, etc).
345 A few parameters have special meanings:
351 If the "skip" parameter is set and its command returns true, then B<mr>
352 will skip acting on that repository. The command is passed the action
355 Here are two examples. The first skips the repo unless
356 mr is run by joey. The second uses the hours_since function
357 (included in mr's built-in library) to skip updating the repo unless it's
358 been at least 12 hours since the last update.
362 skip = test `whoami` != joey
366 skip = [ "$1" = update ] && ! hours_since "$1" 12
368 Another way to use skip is for a lazy checkout. This makes mr skip
369 operating on a repo unless it already exists. To enable the
370 repo, you have to explicitly check it out (using "mr -d foo checkout").
378 The "order" parameter can be used to override the default ordering of
379 repositories. The default order value is 10. Use smaller values to make
380 repositories be processed earlier, and larger values to make repositories
383 Note that if a repository is located in a subdirectory of another
384 repository, ordering it to be processed earlier is not recommended.
388 If the "chain" parameter is set and its command returns true, then B<mr>
389 will try to load a .mrconfig file from the root of the repository.
393 If the "include" parameter is set, its command is ran, and should output
394 additional mrconfig file content. The content is included as if it were
395 part of the including file.
397 Unlike all other parameters, this parameter does not need to be placed
400 B<mr> ships several libraries that can be included to add support for
401 additional version control type things (unison, git-svn, vcsh, git-fake-bare,
402 git-subtree). To include them all, you could use:
404 include = cat /usr/share/mr/*
406 See the individual files for details.
410 If the "deleted" parameter is set and its command returns true, then
411 B<mr> will treat the repository as deleted. It won't ever actually delete
412 the repository, but it will warn if it sees the repository's directory.
413 This is useful when one mrconfig file is shared amoung multiple machines,
414 to keep track of and remember to delete old repositories.
418 The "lib" parameter can specify some shell code that will be run before each
419 command, this can be a useful way to define shell functions for other commands
424 If the "fixups" parameter is set, its command is run whenever a repository
425 is checked out, or updated. This provides an easy way to do things
426 like permissions fixups, or other tweaks to the repository content,
427 whenever the repository is changed.
431 If a "pre_action" parameter is set, its command is run before mr performs the
432 specified action. Similarly, "post_action" parameters are run after mr
433 successfully performs the specified action. For example, "pre_commit" is
434 run before committing; "post_update" is run after updating.
438 When looking for a command to run for a given action, mr first looks for
439 a parameter with the same name as the action. If that is not found, it
440 looks for a parameter named "VCS_action" (substituting in the name of the
441 version control system and the action). The name of the version control
442 system is itself determined by running each defined "VCS_test" action,
445 Internally, mr has settings for "git_update", "svn_update", etc. To change
446 the action that is performed for a given version control system, you can
447 override these VCS specific actions. To add a new version control system,
448 you can just add VCS specific actions for it.
450 =head1 UNTRUSTED MRCONFIG FILES
452 Since mrconfig files can contain arbitrary shell commands, they can do
453 anything. This flexibility is good, but it also allows a malicious mrconfig
454 file to delete your whole home directory. Such a file might be contained
455 inside a repository that your main ~/.mrconfig checks out. To
456 avoid worries about evil commands in a mrconfig file, mr defaults to
457 reading all mrconfig files other than the main ~/.mrconfig in untrusted
458 mode. In untrusted mode, mrconfig files are limited to running only known
459 safe commands (like "git clone") in a carefully checked manner.
461 To configure mr to trust other mrconfig files, list them in ~/.mrtrust.
462 One mrconfig file should be listed per line. Either the full pathname
463 should be listed, or the pathname can start with "~/" to specify a file
464 relative to your home directory.
466 =head1 OFFLINE LOG FILE
468 The ~/.mrlog file contains commands that mr has remembered to run later,
469 due to being offline. You can delete or edit this file to remove commands,
470 or even to add other commands for 'mr online' to run. If the file is
471 present, mr assumes it is in offline mode.
475 mr can be extended to support things such as unison and git-svn. Some
476 files providing such extensions are available in /usr/share/mr/. See
477 the documentation in the files for details about using them.
481 mr returns nonzero if a command failed in any of the repositories.
485 Copyright 2007-2011 Joey Hess <joey@kitenet.net>
487 Licensed under the GNU GPL version 2 or higher.
489 http://kitenet.net/~joey/code/mr/
496 use Cwd qw(getcwd abs_path);
498 # things that can happen when mr runs a command
507 my $config_overridden=0;
517 my $directory=getcwd();
519 $ENV{MR_CONFIG}=find_mrconfig();
526 my (@ok, @failed, @skipped);
532 my ($action, $dir, $topdir, $subdir) = @_;
534 if (exists $vcs{$dir}) {
539 foreach my $vcs_test (
541 length $a <=> length $b
544 } grep { /_test$/ } keys %{$config{$topdir}{$subdir}}) {
545 my ($vcs)=$vcs_test=~/(.*)_test/;
546 $test="my_$vcs_test() {\n$config{$topdir}{$subdir}{$vcs_test}\n}\n".$test;
547 $test.="if my_$vcs_test; then echo $vcs; fi\n";
549 $test=$config{$topdir}{$subdir}{lib}."\n".$test
550 if exists $config{$topdir}{$subdir}{lib};
552 print "mr $action: running vcs test >>$test<<\n" if $verbose;
557 print STDERR "mr $action: found multiple possible repository types ($vcs) for ".fulldir($topdir, $subdir)."\n";
561 return $vcs{$dir}=undef;
564 return $vcs{$dir}=$vcs;
569 my ($action, $dir, $topdir, $subdir, $is_checkout) = @_;
571 if (exists $config{$topdir}{$subdir}{$action}) {
572 return $config{$topdir}{$subdir}{$action};
579 my $vcs=vcs_test(@_);
582 exists $config{$topdir}{$subdir}{$vcs."_".$action}) {
583 return $config{$topdir}{$subdir}{$vcs."_".$action};
591 my ($topdir, $subdir) = @_;
592 return $subdir =~ /^\// ? $subdir : $topdir.$subdir;
596 my ($action, $dir, $topdir, $subdir, $force_checkout) = @_;
597 my $fulldir=fulldir($topdir, $subdir);
599 $ENV{MR_CONFIG}=$configfiles{$topdir};
600 my $lib=exists $config{$topdir}{$subdir}{lib} ?
601 $config{$topdir}{$subdir}{lib}."\n" : "";
602 my $is_checkout=($action eq 'checkout');
603 my $is_update=($action =~ /update/);
606 $ENV{MR_ACTION}=$action;
608 foreach my $testname ("skip", "deleted") {
609 my $testcommand=findcommand($testname, $dir, $topdir, $subdir, $is_checkout);
611 if (defined $testcommand) {
612 my $test="set -e;".$lib.
613 "my_action(){ $testcommand\n }; my_action '$action'";
614 print "mr $action: running $testname test >>$test<<\n" if $verbose;
615 my $ret=system($test);
617 if (($? & 127) == 2) {
618 print STDERR "mr $action: interrupted\n";
622 print STDERR "mr $action: $testname test received signal ".($? & 127)."\n";
626 if ($ret >> 8 == 0) {
627 if ($testname eq "deleted") {
629 print STDERR "mr error: $dir should be deleted yet still exists\n";
633 print "mr $action: skip $dir skipped\n" if $verbose;
640 if (! $force_checkout) {
642 print "mr $action: $dir already exists, skipping checkout\n" if $verbose;
646 $dir=~s/^(.*)\/[^\/]+\/?$/$1/;
651 return action("checkout", $dir, $topdir, $subdir);
655 my $command=findcommand($action, $dir, $topdir, $subdir, $is_checkout);
657 if ($is_checkout && ! -d $dir) {
658 print "mr $action: creating parent directory $dir\n" if $verbose;
659 system("mkdir", "-p", $dir);
662 if (! $no_chdir && ! chdir($dir)) {
663 print STDERR "mr $action: failed to chdir to $dir: $!\n";
666 elsif (! defined $command) {
667 my $vcs=vcs_test(@_);
668 if (! defined $vcs) {
669 print STDERR "mr $action: unknown repository type and no defined $action command for $fulldir\n";
673 print STDERR "mr $action: no defined action for $vcs repository $fulldir, skipping\n";
680 $actionmsg="mr $action: $fulldir";
684 $s=~s/^\Q$fulldir\E\/?//;
685 $actionmsg="mr $action: $fulldir (in subdir $s)";
687 print "$actionmsg\n" unless $quiet;
689 my $hookret=hook("pre_$action", $topdir, $subdir);
690 return $hookret if $hookret != OK;
692 $command="set -e; ".$lib.
693 "my_action(){ $command\n }; my_action ".
694 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
695 print "mr $action: running >>$command<<\n" if $verbose;
698 my $output = qx/$command 2>&1/;
701 print "$actionmsg\n";
702 print STDERR $output;
706 $ret=system($command);
709 if (($? & 127) == 2) {
710 print STDERR "mr $action: interrupted\n";
714 print STDERR "mr $action: received signal ".($? & 127)."\n";
717 print STDERR "mr $action: failed ($ret)\n" if $verbose;
718 if ($ret >> 8 != 0) {
719 print STDERR "mr $action: command failed\n";
720 if (-e "$ENV{HOME}/.mrlog" && $action ne 'remember') {
721 # recreate original command line to
722 # remember, and avoid recursing
724 @ARGV=('-n', $action, @orig);
725 action("remember", $dir, $topdir, $subdir);
730 print STDERR "mr $action: command died ($ret)\n";
735 if ($is_checkout && ! -d $dir) {
736 print STDERR "mr $action: $dir missing after checkout\n";;
740 my $ret=hook("post_$action", $topdir, $subdir);
741 return $ret if $ret != OK;
743 if (($is_checkout || $is_update)) {
744 my $ret=hook("fixups", $topdir, $subdir);
745 return $ret if $ret != OK;
754 my ($hook, $topdir, $subdir) = @_;
756 my $command=$config{$topdir}{$subdir}{$hook};
757 return OK unless defined $command;
758 my $lib=exists $config{$topdir}{$subdir}{lib} ?
759 $config{$topdir}{$subdir}{lib}."\n" : "";
760 my $shell="set -e;".$lib.
761 "my_hook(){ $command\n }; my_hook";
762 print "mr $hook: running >>$shell<<\n" if $verbose;
765 my $output = qx/$shell 2>&1/;
768 print STDERR $output;
775 if (($? & 127) == 2) {
776 print STDERR "mr $hook: interrupted\n";
780 print STDERR "mr $hook: received signal ".($? & 127)."\n";
791 # run actions on multiple repos, in parallel
801 while (@fhs or @repos) {
802 while ((!$jobs || $running < $jobs) && @repos) {
804 my $repo = shift @repos;
805 pipe(my $outfh, CHILD_STDOUT);
806 pipe(my $errfh, CHILD_STDERR);
808 unless ($pid = fork) {
809 die "mr $action: cannot fork: $!" unless defined $pid;
810 open(STDOUT, ">&CHILD_STDOUT") || die "mr $action cannot reopen stdout: $!";
811 open(STDERR, ">&CHILD_STDERR") || die "mr $action cannot reopen stderr: $!";
816 exit action($action, @$repo);
820 push @active, [$pid, $repo];
821 push @fhs, [$outfh, $errfh];
824 my ($rin, $rout) = ('','');
826 foreach my $fh (@fhs) {
827 next unless defined $fh;
828 vec($rin, fileno($fh->[0]), 1) = 1 if defined $fh->[0];
829 vec($rin, fileno($fh->[1]), 1) = 1 if defined $fh->[1];
831 $nfound = select($rout=$rin, undef, undef, 1);
832 foreach my $channel (0, 1) {
833 foreach my $i (0..$#fhs) {
834 next unless defined $fhs[$i];
835 my $fh = $fhs[$i][$channel];
836 next unless defined $fh;
837 if (vec($rout, fileno($fh), 1) == 1) {
839 if (sysread($fh, $r, 1024) == 0) {
841 $fhs[$i][$channel] = undef;
842 if (! defined $fhs[$i][0] &&
843 ! defined $fhs[$i][1]) {
844 waitpid($active[$i][0], 0);
845 print STDOUT $out[$i][0];
846 print STDERR $out[$i][1];
847 record($active[$i][1], $? >> 8);
849 splice(@active, $i, 1);
854 $out[$i][$channel] .= $r;
862 my $dir=shift()->[0];
867 print "\n" unless $quiet;
869 elsif ($ret == FAILED) {
871 chdir($dir) unless $no_chdir;
872 print STDERR "mr: Starting interactive shell. Exit shell to continue.\n";
873 system((getpwuid($<))[8], "-i");
876 print "\n" unless $quiet;
878 elsif ($ret == SKIPPED) {
881 elsif ($ret == ABORT) {
885 die "unknown exit status $ret";
891 if (! @ok && ! @failed && ! @skipped) {
892 die "mr $action: no repositories found to work on\n";
894 print "mr $action: finished (".join("; ",
895 showstat($#ok+1, "ok", "ok"),
896 showstat($#failed+1, "failed", "failed"),
897 showstat($#skipped+1, "skipped", "skipped"),
898 ).")\n" unless $quiet;
901 print "mr $action: (skipped: ".join(" ", @skipped).")\n" unless $quiet;
904 print STDERR "mr $action: (failed: ".join(" ", @failed).")\n";
914 return "$count ".($count > 1 ? $plural : $singular);
919 # an ordered list of repos
922 foreach my $topdir (sort keys %config) {
923 foreach my $subdir (sort keys %{$config{$topdir}}) {
927 order => $config{$topdir}{$subdir}{order},
932 $a->{order} <=> $b->{order}
934 $a->{topdir} cmp $b->{topdir}
936 $a->{subdir} cmp $b->{subdir}
942 my $topdir=$repo->{topdir};
943 my $subdir=$repo->{subdir};
944 my $ret=($subdir =~/^\//) ? $subdir : $topdir.$subdir;
949 # figure out which repos to act on
952 foreach my $repo (repolist()) {
953 my $topdir=$repo->{topdir};
954 my $subdir=$repo->{subdir};
956 next if $subdir eq 'DEFAULT';
957 my $dir=repodir($repo);
959 $dir.="/" unless $dir=~/\/$/;
960 $d.="/" unless $d=~/\/$/;
961 next if $dir ne $d && $dir !~ /^\Q$d\E/;
962 if (defined $max_depth) {
963 my @a=split('/', $dir);
964 my @b=split('/', $d);
965 do { } while (@a && @b && shift(@a) eq shift(@b));
966 next if @a > $max_depth || @b > $max_depth;
968 push @repos, [$dir, $topdir, $subdir];
971 # fallback to find a leaf repo
972 foreach my $repo (reverse repolist()) {
973 my $topdir=$repo->{topdir};
974 my $subdir=$repo->{subdir};
976 next if $subdir eq 'DEFAULT';
977 my $dir=repodir($repo);
979 $dir.="/" unless $dir=~/\/$/;
980 $d.="/" unless $d=~/\/$/;
981 if ($d=~/^\Q$dir\E/) {
982 push @repos, [$dir, $topdir, $subdir];
1004 sub is_trusted_config {
1005 my $config=shift; # must be abs_pathed already
1007 # We always trust ~/.mrconfig.
1008 return 1 if $config eq abs_path("$ENV{HOME}/.mrconfig");
1010 return 1 if $trust_all;
1012 my $trustfile=$ENV{HOME}."/.mrtrust";
1015 $trusted{"$ENV{HOME}/.mrconfig"}=1;
1016 if (open (TRUST, "<", $trustfile)) {
1019 s/^~\//$ENV{HOME}\//;
1020 $trusted{abs_path($_)}=1;
1026 return $trusted{$config};
1030 sub is_trusted_repo {
1033 # Tightly limit what is allowed in a repo name.
1034 # No ../, no absolute paths, and no unusual filenames
1035 # that might try to escape to the shell.
1036 return $repo =~ /^[-_.+\/A-Za-z0-9]+$/ &&
1037 $repo !~ /\.\./ && $repo !~ /^\//;
1040 sub is_trusted_checkout {
1043 # To determine if the command is safe, compare it with the
1044 # *_trusted_checkout config settings. Those settings are
1045 # templates for allowed commands, so make sure that each word
1046 # of the command matches the corresponding word of the template.
1049 foreach my $word (split(' ', $command)) {
1051 if ($word=~/^'(.*)'$/) {
1054 elsif ($word=~/^"(.*)"$/) {
1061 foreach my $key (grep { /_trusted_checkout$/ }
1062 keys %{$config{''}{DEFAULT}}) {
1063 my @twords=split(' ', $config{''}{DEFAULT}{$key});
1064 next if @words > @twords;
1068 for (my $c=0; $c < @twords && $match; $c++) {
1069 if ($twords[$c] eq '$url') {
1070 # Match all the typical characters found in
1071 # urls, plus @ which svn can use. Note
1072 # that the "url" might also be a local
1075 defined $words[$c] &&
1076 $words[$c] =~ /^[-_.+:@\/A-Za-z0-9]+$/
1080 elsif ($twords[$c] eq '$repo') {
1081 # If a repo is not specified, assume it
1082 # will be the last path component of the
1083 # url, or something derived from it, and
1085 if (! defined $words[$c] && defined $url) {
1086 ($words[$c])=$url=~/\/([^\/]+)\/?$/;
1090 defined $words[$c] &&
1091 is_trusted_repo($words[$c])
1094 elsif (defined $words[$c] && $words[$c]=~/^($twords[$c])$/) {
1108 my ($err, $file, $line, $url)=@_;
1111 die "$err in untrusted $url line $line\n".
1112 "(To trust this url, --trust-all can be used; but please use caution;\n".
1113 "this can allow arbitrary code execution!)\n";
1116 die "$err in untrusted $file line $line\n".
1117 "(To trust this file, list it in ~/.mrtrust.)\n";
1125 my $bootstrap_url=shift;
1131 if (ref $f eq 'GLOB') {
1137 my $absf=abs_path($f);
1138 if ($loaded{$absf}) {
1143 $trusted=is_trusted_config($absf);
1145 if (! defined $dir) {
1146 ($dir)=$f=~/^(.*\/)[^\/]+$/;
1147 if (! defined $dir) {
1152 $dir=abs_path($dir)."/";
1154 if (! exists $configfiles{$dir}) {
1155 $configfiles{$dir}=$f;
1158 # copy in defaults from first parent
1160 while ($parent=~s/^(.*\/)[^\/]+\/?$/$1/) {
1161 if ($parent eq '/') {
1164 if (exists $config{$parent} &&
1165 exists $config{$parent}{DEFAULT}) {
1166 $config{$dir}{DEFAULT}={ %{$config{$parent}{DEFAULT}} };
1175 print "mr: loading config $f\n" if $verbose;
1176 open($in, "<", $f) || die "mr: open $f: $!\n";
1179 close $in unless ref $f eq 'GLOB';
1187 next if /^\s*\#/ || /^\s*$/;
1188 if (/^\[([^\]]*)\]\s*$/) {
1192 if (! is_trusted_repo($section) ||
1193 $section eq 'ALIAS' ||
1194 $section eq 'DEFAULT') {
1195 trusterror("mr: illegal section \"[$section]\"", $f, $line, $bootstrap_url)
1198 $section=expandenv($section) if $trusted;
1199 if ($section ne 'ALIAS' &&
1200 ! exists $config{$dir}{$section} &&
1201 exists $config{$dir}{DEFAULT}) {
1203 $config{$dir}{$section}={ %{$config{$dir}{DEFAULT}} };
1206 elsif (/^(\w+)\s*=\s*(.*)/) {
1211 while (@lines && $lines[0]=~/^\s(.+)/) {
1219 # Untrusted files can only contain a few
1220 # settings in specific known-safe formats.
1221 if ($parameter eq 'checkout') {
1222 if (! is_trusted_checkout($value)) {
1223 trusterror("mr: illegal checkout command \"$value\"", $f, $line, $bootstrap_url);
1226 elsif ($parameter eq 'order') {
1227 # not interpreted as a command, so
1230 elsif ($value eq 'true' || $value eq 'false') {
1231 # skip=true , deleted=true etc are
1235 trusterror("mr: illegal setting \"$parameter=$value\"", $f, $line, $bootstrap_url);
1239 if ($parameter eq "include") {
1240 print "mr: including output of \"$value\"\n" if $verbose;
1241 unshift @lines, `$value`;
1243 print STDERR "mr: include command exited nonzero ($?)\n";
1248 if (! defined $section) {
1249 die "$f line $.: parameter ($parameter) not in section\n";
1251 if ($section eq 'ALIAS') {
1252 $alias{$parameter}=$value;
1254 elsif ($parameter eq 'lib') {
1255 $config{$dir}{$section}{lib}.=$value."\n";
1258 $config{$dir}{$section}{$parameter}=$value;
1259 if ($parameter =~ /.*_(.*)/) {
1260 $knownactions{$1}=1;
1263 $knownactions{$parameter}=1;
1265 if ($parameter eq 'chain' &&
1266 length $dir && $section ne "DEFAULT") {
1267 my $chaindir="$section";
1268 if ($chaindir !~ m!/!) {
1269 $chaindir=$dir.$chaindir;
1271 if (-e "$chaindir/.mrconfig") {
1272 my $ret=system($value);
1274 if (($? & 127) == 2) {
1275 print STDERR "mr: chain test interrupted\n";
1279 print STDERR "mr: chain test received signal ".($? & 127)."\n";
1283 push @toload, ["$chaindir/.mrconfig", $chaindir];
1290 die "$f line $line: parse error\n";
1294 foreach my $c (@toload) {
1299 sub startingconfig {
1300 %alias=%config=%configfiles=%knownactions=%loaded=();
1301 my $datapos=tell(DATA);
1303 seek(DATA,$datapos,0); # rewind
1308 # the section to modify or add
1309 my $targetsection=shift;
1310 # fields to change in the section
1311 # To remove a field, set its value to "".
1312 my %changefields=@_;
1318 open(my $in, "<", $f) || die "mr: open $f: $!\n";
1323 my $formatfield=sub {
1325 my @value=split(/\n/, shift);
1327 return "$field = ".shift(@value)."\n".
1328 join("", map { "\t$_\n" } @value);
1332 while ($out[$#out] =~ /^\s*$/) {
1333 unshift @blanks, pop @out;
1335 foreach my $field (sort keys %changefields) {
1336 if (length $changefields{$field}) {
1337 push @out, "$field = $changefields{$field}\n";
1338 delete $changefields{$field};
1348 if (/^\s*\#/ || /^\s*$/) {
1351 elsif (/^\[([^\]]*)\]\s*$/) {
1352 if (defined $section &&
1353 $section eq $targetsection) {
1357 $section=expandenv($1);
1361 elsif (/^(\w+)\s*=\s(.*)/) {
1366 while (@lines && $lines[0]=~/^\s(.+)/) {
1372 if ($section eq $targetsection) {
1373 if (exists $changefields{$parameter}) {
1374 if (length $changefields{$parameter}) {
1375 $value=$changefields{$parameter};
1377 delete $changefields{$parameter};
1381 push @out, $formatfield->($parameter, $value);
1385 if (defined $section &&
1386 $section eq $targetsection) {
1389 elsif (%changefields) {
1390 push @out, "\n[$targetsection]\n";
1391 foreach my $field (sort keys %changefields) {
1392 if (length $changefields{$field}) {
1393 push @out, $formatfield->($field, $changefields{$field});
1398 open(my $out, ">", $f) || die "mr: write $f: $!\n";
1406 # actions that do not operate on all repos
1407 if ($action eq 'help') {
1410 elsif ($action eq 'config') {
1413 elsif ($action eq 'register') {
1416 elsif ($action eq 'bootstrap') {
1419 elsif ($action eq 'remember' ||
1420 $action eq 'offline' ||
1421 $action eq 'online') {
1422 my @repos=selectrepos;
1423 action($action, @{$repos[0]}) if @repos;
1427 if (!$jobs || $jobs > 1) {
1428 mrs($action, selectrepos());
1431 foreach my $repo (selectrepos()) {
1432 record($repo, action($action, @$repo));
1438 exec($config{''}{DEFAULT}{help}) || die "exec: $!";
1443 die "mr config: not enough parameters\n";
1446 if ($section=~/^\//) {
1447 # try to convert to a path relative to the config file
1448 my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/;
1449 $dir=abs_path($dir);
1450 $dir.="/" unless $dir=~/\/$/;
1451 if ($section=~/^\Q$dir\E(.*)/) {
1457 if (/^([^=]+)=(.*)$/) {
1458 $changefields{$1}=$2;
1462 foreach my $topdir (sort keys %config) {
1463 if (exists $config{$topdir}{$section} &&
1464 exists $config{$topdir}{$section}{$_}) {
1465 print $config{$topdir}{$section}{$_}."\n";
1467 last if $section eq 'DEFAULT';
1471 die "mr config: $section $_ not set\n";
1475 modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields;
1480 if ($config_overridden) {
1481 # Find the directory that the specified config file is
1483 ($directory)=abs_path($ENV{MR_CONFIG})=~/^(.*\/)[^\/]+$/;
1486 # Find the closest known mrconfig file to the current
1488 $directory.="/" unless $directory=~/\/$/;
1490 foreach my $topdir (reverse sort keys %config) {
1491 next unless length $topdir;
1492 if ($directory=~/^\Q$topdir\E/) {
1493 $ENV{MR_CONFIG}=$configfiles{$topdir};
1499 if (! $foundconfig) {
1500 $directory=""; # no config file, use builtin
1504 my $subdir=shift @ARGV;
1505 if (! chdir($subdir)) {
1506 print STDERR "mr register: failed to chdir to $subdir: $!\n";
1510 $ENV{MR_REPO}=getcwd();
1511 my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT', 0);
1512 if (! defined $command) {
1513 die "mr register: unknown repository type\n";
1516 $ENV{MR_REPO}=~s/.*\/(.*)/$1/;
1517 $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n".
1518 "my_action(){ $command\n }; my_action ".
1519 join(" ", map { s/\\/\\\\/g; s/"/\"/g; '"'.$_.'"' } @ARGV);
1520 print "mr register: running >>$command<<\n" if $verbose;
1521 exec($command) || die "exec: $!";
1525 my $url=shift @ARGV;
1526 my $dir=shift @ARGV || ".";
1528 if (! defined $url || ! length $url) {
1529 die "mr: bootstrap requires url\n";
1532 # Download the config file to a temporary location.
1533 eval q{use File::Temp};
1535 my $tmpconfig=File::Temp->new();
1536 my @curlargs = ("curl", "-A", "mr", "-L", "-s", $url, "-o", $tmpconfig);
1537 push(@curlargs, "-k") if $insecure;
1538 my $curlstatus = system(@curlargs);
1539 die "mr bootstrap: invalid SSL certificate for $url (consider -k)\n" if $curlstatus >> 8 == 60;
1540 die "mr bootstrap: download of $url failed\n" if $curlstatus != 0;
1543 system("mkdir", "-p", $dir);
1545 chdir($dir) || die "chdir $dir: $!";
1547 # Special case to handle checkout of the "." repo, which
1548 # would normally be skipped.
1549 my $topdir=abs_path(".")."/";
1550 my @repo=($topdir, $topdir, ".");
1551 loadconfig($tmpconfig, $topdir, $url);
1552 record(\@repo, action("checkout", @repo, 1))
1553 if exists $config{$topdir}{"."}{"checkout"};
1555 if (-e ".mrconfig") {
1556 print STDERR "mr bootstrap: .mrconfig file already exists, not overwriting with $url\n";
1559 eval q{use File::Copy};
1561 move($tmpconfig, ".mrconfig") || die "rename: $!";
1564 # Reload the config file (in case we got a different version)
1565 # and checkout everything else.
1567 loadconfig(".mrconfig");
1568 dispatch("checkout");
1569 @skipped=grep { abs_path($_) ne abs_path($topdir) } @skipped;
1570 showstats("bootstrap");
1574 # alias expansion and command stemming
1577 if (exists $alias{$action}) {
1578 $action=$alias{$action};
1580 if (! exists $knownactions{$action}) {
1581 my @matches = grep { /^\Q$action\E/ }
1582 keys %knownactions, keys %alias;
1583 if (@matches == 1) {
1584 $action=$matches[0];
1586 elsif (@matches == 0) {
1587 die "mr: unknown action \"$action\" (known actions: ".
1588 join(", ", sort keys %knownactions).")\n";
1591 die "mr: ambiguous action \"$action\" (matches: ".
1592 join(", ", @matches).")\n";
1600 while (length $dir) {
1601 if (-e "$dir/.mrconfig") {
1602 return "$dir/.mrconfig";
1604 $dir=~s/\/[^\/]*$//;
1606 return "$ENV{HOME}/.mrconfig";
1611 Getopt::Long::Configure("bundling", "no_permute");
1612 my $result=GetOptions(
1613 "d|directory=s" => sub { $directory=abs_path($_[1]) },
1614 "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
1615 "p|path" => sub { }, # now default, ignore
1616 "v|verbose" => \$verbose,
1617 "q|quiet" => \$quiet,
1618 "s|stats" => \$stats,
1619 "k|insecure" => \$insecure,
1620 "i|interactive" => \$interactive,
1621 "n|no-recurse:i" => \$max_depth,
1622 "j|jobs:i" => \$jobs,
1623 "t|trust-all" => \$trust_all,
1625 if (! $result || @ARGV < 1) {
1626 die("Usage: mr [options] action [params ...]\n".
1627 "(Use mr help for man page.)\n");
1630 $ENV{MR_SWITCHES}="";
1631 foreach my $option (@saved) {
1632 last if $option eq $ARGV[0];
1633 $ENV{MR_SWITCHES}.="$option ";
1639 print STDERR "mr: interrupted\n";
1643 # This can happen if it's run in a directory that was removed
1644 # or other strangeness.
1645 if (! defined $directory) {
1646 die("mr: failed to determine working directory\n");
1648 # Make sure MR_CONFIG is an absolute path, but don't use abs_path since
1649 # the config file might be a symlink to elsewhere, and the directory it's
1650 # in is significant.
1651 if ($ENV{MR_CONFIG} !~ /^\//) {
1652 $ENV{MR_CONFIG}=getcwd()."/".$ENV{MR_CONFIG};
1654 # Try to set MR_PATH to the path to the program.
1656 use FindBin qw($Bin $Script);
1657 $ENV{MR_PATH}=$Bin."/".$Script;
1675 loadconfig("$ENV{HOME}/.mrconfig");
1676 loadconfig($ENV{MR_CONFIG});
1677 #use Data::Dumper; print Dumper(\%config);
1679 my $action=expandaction(shift @ARGV);
1686 # Finally, some useful actions that mr knows about by default.
1687 # These can be overridden in ~/.mrconfig.
1702 echo "mr (warning): $@" >&2
1708 if [ -z "$1" ] || [ -z "$2" ]; then
1709 error "mr: usage: hours_since action num"
1711 for dir in .git .svn .bzr CVS .hg _darcs _FOSSIL_; do
1712 if [ -e "$MR_REPO/$dir" ]; then
1713 flagfile="$MR_REPO/$dir/.mr_last$1"
1717 if [ -z "$flagfile" ]; then
1718 error "cannot determine flag filename"
1720 delta=`perl -wle 'print -f shift() ? int((-M _) * 24) : 9999' "$flagfile"`
1721 if [ "$delta" -lt "$2" ]; then
1729 LANG=C bzr info | egrep -q '^Checkout'
1732 if [ "$MR_ACTION" = checkout ] || [ -d "$MR_REPO" ]; then
1739 svn_test = test -d "$MR_REPO"/.svn
1740 git_test = test -d "$MR_REPO"/.git
1741 bzr_test = test -d "$MR_REPO"/.bzr
1742 cvs_test = test -d "$MR_REPO"/CVS
1743 hg_test = test -d "$MR_REPO"/.hg
1744 darcs_test = test -d "$MR_REPO"/_darcs
1745 fossil_test = test -f "$MR_REPO"/_FOSSIL_
1747 test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags &&
1748 test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config &&
1749 test "`GIT_CONFIG="$MR_REPO"/config git config --get core.bare`" = true
1751 svn_update = svn update "$@"
1752 git_update = git pull "$@"
1754 if is_bzr_checkout; then
1757 bzr merge --pull "$@"
1759 cvs_update = cvs update "$@"
1760 hg_update = hg pull "$@" && hg update "$@"
1761 darcs_update = darcs pull -a "$@"
1762 fossil_update = fossil pull "$@"
1764 svn_status = svn status "$@"
1765 git_status = git status -s "$@" || true
1766 bzr_status = bzr status --short "$@"
1767 cvs_status = cvs status "$@"
1768 hg_status = hg status "$@"
1769 darcs_status = darcs whatsnew -ls "$@" || true
1770 fossil_status = fossil changes "$@"
1772 svn_commit = svn commit "$@"
1773 git_commit = git commit -a "$@" && git push --all
1775 if is_bzr_checkout; then
1778 bzr commit "$@" && bzr push
1780 cvs_commit = cvs commit "$@"
1781 hg_commit = hg commit -m "$@" && hg push
1782 darcs_commit = darcs record -a -m "$@" && darcs push -a
1783 fossil_commit = fossil commit "$@"
1785 git_record = git commit -a "$@"
1787 if is_bzr_checkout; then
1788 bzr commit --local "$@"
1792 hg_record = hg commit -m "$@"
1793 darcs_record = darcs record -a -m "$@"
1794 fossil_record = fossil commit "$@"
1797 git_push = git push "$@"
1798 bzr_push = bzr push "$@"
1800 hg_push = hg push "$@"
1801 darcs_push = darcs push -a "$@"
1802 fossil_push = fossil push "$@"
1804 svn_diff = svn diff "$@"
1805 git_diff = git diff "$@"
1806 bzr_diff = bzr diff "$@"
1807 cvs_diff = cvs diff "$@"
1808 hg_diff = hg diff "$@"
1809 darcs_diff = darcs diff -u "$@"
1810 fossil_diff = fossil diff "$@"
1812 svn_log = svn log "$@"
1813 git_log = git log "$@"
1814 bzr_log = bzr log "$@"
1815 cvs_log = cvs log "$@"
1816 hg_log = hg log "$@"
1817 darcs_log = darcs changes "$@"
1818 git_bare_log = git log "$@"
1819 fossil_log = fossil timeline "$@"
1824 url=`LC_ALL=C svn info . | grep -i '^URL:' | cut -d ' ' -f 2`
1825 if [ -z "$url" ]; then
1826 error "cannot determine svn url"
1828 echo "Registering svn url: $url in $MR_CONFIG"
1829 mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'"
1831 url="`LC_ALL=C git config --get remote.origin.url`" || true
1832 if [ -z "$url" ]; then
1833 error "cannot determine git url"
1835 echo "Registering git url: $url in $MR_CONFIG"
1836 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'"
1838 url="`LC_ALL=C bzr info . | egrep -i 'checkout of branch|parent branch' | awk '{print $NF}'`"
1839 if [ -z "$url" ]; then
1840 error "cannot determine bzr url"
1842 echo "Registering bzr url: $url in $MR_CONFIG"
1843 mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr branch '$url' '$MR_REPO'"
1845 repo=`cat CVS/Repository`
1847 if [ -z "$root" ]; then
1848 error "cannot determine cvs root"
1850 echo "Registering cvs repository $repo at root $root"
1851 mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'"
1853 url=`hg showconfig paths.default`
1854 echo "Registering mercurial repo url: $url in $MR_CONFIG"
1855 mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'"
1857 url=`cat _darcs/prefs/defaultrepo`
1858 echo "Registering darcs repository $url in $MR_CONFIG"
1859 mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url' '$MR_REPO'"
1861 url="`LC_ALL=C GIT_CONFIG=config git config --get remote.origin.url`" || true
1862 if [ -z "$url" ]; then
1863 error "cannot determine git url"
1865 echo "Registering git url: $url in $MR_CONFIG"
1866 mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'"
1868 url=`fossil remote-url`
1869 repo=`fossil info | grep repository | sed -e 's/repository:*.//g' -e 's/ //g'`
1870 echo "Registering fossil repository $url in $MR_CONFIG"
1871 mr -c "$MR_CONFIG" config "`pwd`" checkout="mkdir -p '$MR_REPO' && cd '$MR_REPO' && fossil open '$repo'"
1873 svn_trusted_checkout = svn co $url $repo
1874 svn_alt_trusted_checkout = svn checkout $url $repo
1875 git_trusted_checkout = git clone $url $repo
1876 bzr_trusted_checkout = bzr checkout|clone|branch|get $url $repo
1878 hg_trusted_checkout = hg clone $url $repo
1879 darcs_trusted_checkout = darcs get $url $repo
1880 git_bare_trusted_checkout = git clone --bare $url $repo
1881 # fossil: messy to do
1887 SHOWMANFILE="man -f"
1893 SHOWMANFILE="man -l"
1896 if [ ! -e "$MR_PATH" ]; then
1897 error "cannot find program path"
1899 tmp=$(mktemp -t mr.XXXXXXXXXX) || error "mktemp failed"
1900 trap "rm -f $tmp" exit
1901 pod2man -c mr "$MR_PATH" > "$tmp" || error "pod2man failed"
1902 $SHOWMANFILE "$tmp" || error "man failed"
1908 if [ -s ~/.mrlog ]; then
1909 info "running offline commands"
1910 mv -f ~/.mrlog ~/.mrlog.old
1911 if ! sh -e ~/.mrlog.old; then
1912 error "offline command failed; left in ~/.mrlog.old"
1916 info "no offline commands to run"
1921 info "offline mode enabled"
1923 info "remembering command: 'mr $@'"
1924 command="mr -d '$(pwd)' $MR_SWITCHES"
1926 command="$command '$w'"
1928 if [ ! -e ~/.mrlog ] || ! grep -q -F "$command" ~/.mrlog; then
1929 echo "$command" >> ~/.mrlog
1932 ed = echo "A horse is a horse, of course, of course.."
1933 T = echo "I pity the fool."
1934 right = echo "Not found."
1936 # vim:sw=8:sts=0:ts=8:noet