From 4d36da3584cb7d5126785a8bf0588268fc3d204d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:06:20 -0400 Subject: [PATCH 01/16] fixed git fake bare checkout Got the order and variables right, which I screwed up before, and added a function to handle the messy checkout --- mrconfig.git-fake-bare | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/mrconfig.git-fake-bare b/mrconfig.git-fake-bare index 83f2202..7f3e9db 100644 --- a/mrconfig.git-fake-bare +++ b/mrconfig.git-fake-bare @@ -1,11 +1,30 @@ # An example of how to add a new revision control system type to mr. -# git fake bare repositories have a detached workspace. This file adds +# git fake bare repositories have a detached workspace. One potential +# application is storing dotfiles in git, keeping them checked out in +# one $HOME, but checked into different git repositories. This file adds # support for them, separate from the normal git support. # To make mr use this file, add a line like this inside the [DEFAULT] # section of your ~/.mrconfig #include = cat /path/to/mrconfig.git-fake-bare +lib = + # git doesn't have an easy way to check out such a repo, so + # do it by hand + git_fake_bare_checkout() { + set -x + url="$1" + repo="$2" + worktree="${3%%/}/" + git clone --no-checkout "$url" "$repo" + cd "$repo" + git read-tree HEAD + git config core.worktree "$worktree" + git checkout-index -a --prefix="$worktree" || true + mv .git/* . + rmdir .git + } + git_fake_bare_test = test -d "$MR_REPO"/refs/heads && test -d "$MR_REPO"/refs/tags && test -d "$MR_REPO"/objects && test -f "$MR_REPO"/config && @@ -45,12 +64,4 @@ git_fake_bare_register = mr -c "$MR_CONFIG" config "`pwd`" \ lib="GIT_WORK_TREE=$work_tree; export GIT_WORK_TREE" echo "Registering git url: $url in $MR_CONFIG (with worktree $worktree)" - mr -c "$MR_CONFIG" config "`pwd`" \ - checkout=" \ - git clone --no-checkout $url $MR_REPO && \ - cd $MR_REPO && \ - git read-tree HEAD && \ - git checkout-index -a --prefix='$work_tree' || true; \ - git config core.worktree '$worktree' && \ - mv .git/* . && \ - rmdir .git" + mr -c "$MR_CONFIG" config "`pwd`" checkout="git_fake_bare_checkout '$url' '$MR_REPO' '$worktree'" -- 2.39.5 From 7d34338b3d0554847d3efdb3f08040850d0a60fd Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:08:55 -0400 Subject: [PATCH 02/16] quote all url and dir strings written by mr reg This isn't perfect (fails if there's a single quote), but is much better than nothing. --- debian/changelog | 2 +- mr | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/debian/changelog b/debian/changelog index 865c63e..5a517b0 100644 --- a/debian/changelog +++ b/debian/changelog @@ -28,7 +28,7 @@ mr (0.9) UNRELEASED; urgency=low type and use includes, and partly because it's currently too ugly to be in mr itself due to bugs and limitations in git. - -- Joey Hess Thu, 25 Oct 2007 21:43:36 -0400 + -- Joey Hess Fri, 26 Oct 2007 01:08:11 -0400 mr (0.8) unstable; urgency=low diff --git a/mr b/mr index 5ced95e..f9a208f 100755 --- a/mr +++ b/mr @@ -1123,21 +1123,21 @@ svn_register = error "cannot determine svn url" fi echo "Registering svn url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co $url $MR_REPO" + mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'" git_register = url="$(LANG=C git-config --get remote.origin.url)" || true if [ -z "$url" ]; then error "cannot determine git url" fi echo "Registering git url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone $url $MR_REPO" + mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'" bzr_register = url=$(cat .bzr/branch/parent) if [ -z "$url" ]; then error "cannot determine bzr url" fi echo "Registering bzr url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone $url $MR_REPO" + mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'" cvs_register = repo=$(cat CVS/Repository) root=$(cat CVS/Root) @@ -1145,22 +1145,22 @@ cvs_register = error "cannot determine cvs root" fi echo "Registering cvs repository $repo at root $root" - mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d $MR_REPO $repo" + mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'" hg_register = url=$(hg showconfig paths.default) echo "Registering mercurial repo url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone $url $MR_REPO" + mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'" darcs_register = url=$(cat _darcs/prefs/defaultrepo) echo "Registering darcs repository $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get $url $MR_REPO" + mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url'p '$MR_REPO'" git_bare_register = url="$(LANG=C GIT_CONFIG=config git-config --get remote.origin.url)" || true if [ -z "$url" ]; then error "cannot determine git url" fi echo "Registering git url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare $url $MR_REPO" + mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'" help = if [ ! -e "$MR_PATH" ]; then -- 2.39.5 From 81612f267ea283552bf2f1f1ba8ce0f59b2da337 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:14:37 -0400 Subject: [PATCH 03/16] meh, try again at getting register to use the right configs --- mr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mr b/mr index f9a208f..494e89b 100755 --- a/mr +++ b/mr @@ -440,13 +440,13 @@ elsif ($action eq 'register') { } $ENV{MR_REPO}=getcwd(); - my $command=findcommand("register", $ENV{MR_CONFIG}, $directory, 'DEFAULT'); + my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT'); if (! defined $command) { die "mr $action: unknown repository type\n"; } $ENV{MR_REPO}=~s/.*\/(.*)/$1/; - $command="set -e; ".$config{$ENV{MR_CONFIG}}{$directory}{lib}."\n". + $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n". "my_action(){ $command\n }; my_action ". join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV); print "mr $action: running >>$command<<\n" if $verbose; -- 2.39.5 From cce0d243e003bcbc741955b6b4282fcf07c24a04 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:28:47 -0400 Subject: [PATCH 04/16] improvements to git fake bare checkouts --- mrconfig.git-fake-bare | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mrconfig.git-fake-bare b/mrconfig.git-fake-bare index 7f3e9db..57849cf 100644 --- a/mrconfig.git-fake-bare +++ b/mrconfig.git-fake-bare @@ -9,17 +9,21 @@ #include = cat /path/to/mrconfig.git-fake-bare lib = + # called to tell git where the worktree is + git_fake_bare_worktree() { + GIT_WORK_TREE="$1"; export GIT_WORK_TREE + } # git doesn't have an easy way to check out such a repo, so # do it by hand git_fake_bare_checkout() { set -x url="$1" repo="$2" - worktree="${3%%/}/" - git clone --no-checkout "$url" "$repo" + GIT_WORK_TREE= git clone --no-checkout "$url" "$repo" cd "$repo" + mkdir -p "$GIT_WORK_TREE" git read-tree HEAD - git config core.worktree "$worktree" + git config core.worktree "$GIT_WORK_TREE" git checkout-index -a --prefix="$worktree" || true mv .git/* . rmdir .git @@ -61,7 +65,6 @@ git_fake_bare_register = if [ ! -d "$worktree" ]; then error "git worktree $worktree does not exist" fi - mr -c "$MR_CONFIG" config "`pwd`" \ - lib="GIT_WORK_TREE=$work_tree; export GIT_WORK_TREE" + mr -c "$MR_CONFIG" config "`pwd`" lib="git_fake_bare_worktree '$worktree'" echo "Registering git url: $url in $MR_CONFIG (with worktree $worktree)" - mr -c "$MR_CONFIG" config "`pwd`" checkout="git_fake_bare_checkout '$url' '$MR_REPO' '$worktree'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="git_fake_bare_checkout '$url' '$MR_REPO'" -- 2.39.5 From 7005061db5ed43de6ffc18a974a778c9e7a8c9f4 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:31:34 -0400 Subject: [PATCH 05/16] add usage example --- mrconfig.git-fake-bare | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mrconfig.git-fake-bare b/mrconfig.git-fake-bare index 57849cf..730d85d 100644 --- a/mrconfig.git-fake-bare +++ b/mrconfig.git-fake-bare @@ -8,6 +8,11 @@ # section of your ~/.mrconfig #include = cat /path/to/mrconfig.git-fake-bare +# And an example repo using it would look something like: +#[.dotfiles] +#lib = git_fake_bare_worktree $HOME +#checkout = git_fake_bare_checkout git://... .dotfiles + lib = # called to tell git where the worktree is git_fake_bare_worktree() { -- 2.39.5 From 6007e5d1f073c08daa25bd3057eaf3d3ac307071 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:36:17 -0400 Subject: [PATCH 06/16] fix use of old hours_since syntax --- mrconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mrconfig b/mrconfig index a209a2b..4fae2d6 100644 --- a/mrconfig +++ b/mrconfig @@ -7,7 +7,7 @@ skip = ! wantsrc [linux-2.6] checkout = git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git # only update once every 12 hours -skip = ([ "$1" = update ] && [ $(hours_since "$1") -lt 12 ]) +skip = ([ "$1" = update ] && ! hours_since "$1" 12) [debian-cd] checkout = svn co svn://svn.debian.org/debian-cd/trunk debian-cd -- 2.39.5 From 5a5c5478ebd0c79622e6c68ecd32f04ec150ef9c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:37:58 -0400 Subject: [PATCH 07/16] remove set -x --- mrconfig.git-fake-bare | 1 - 1 file changed, 1 deletion(-) diff --git a/mrconfig.git-fake-bare b/mrconfig.git-fake-bare index 730d85d..5ae396c 100644 --- a/mrconfig.git-fake-bare +++ b/mrconfig.git-fake-bare @@ -21,7 +21,6 @@ lib = # git doesn't have an easy way to check out such a repo, so # do it by hand git_fake_bare_checkout() { - set -x url="$1" repo="$2" GIT_WORK_TREE= git clone --no-checkout "$url" "$repo" -- 2.39.5 From 42e5ed9236d5417f5827df3648e72f8e7ffe5b95 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 01:51:37 -0400 Subject: [PATCH 08/16] rename file --- debian/changelog | 2 +- debian/rules | 3 ++- mrconfig.git-fake-bare => lib/git-fake-bare | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) rename mrconfig.git-fake-bare => lib/git-fake-bare (98%) diff --git a/debian/changelog b/debian/changelog index 5a517b0..5631d7f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -23,7 +23,7 @@ mr (0.9) UNRELEASED; urgency=low * Add support for including one mrconfig file from another. Unlike chaining, this doesn't change the paths, and is not tied to a particular subdirectory. It's useful for loading up library mrconfig files. - * Split git fake-bare support out into a mrconfig.git-fake-bare. + * Split git fake-bare support out into a lib/git-fake-bare. Partly because it's a good example of how to add a new revision control type and use includes, and partly because it's currently too ugly to be in mr itself due to bugs and limitations in git. diff --git a/debian/rules b/debian/rules index 7815fff..1cc28db 100755 --- a/debian/rules +++ b/debian/rules @@ -19,7 +19,8 @@ binary-indep: build dh_clean -k dh_install mr usr/bin dh_installdocs README TODO - dh_installexamples mrconfig mrconfig.* + dh_installexamples mrconfig mrconfig.complex + dh_install lib/* usr/share/mr dh_installman *.1 dh_installchangelogs dh_compress diff --git a/mrconfig.git-fake-bare b/lib/git-fake-bare similarity index 98% rename from mrconfig.git-fake-bare rename to lib/git-fake-bare index 5ae396c..231b631 100644 --- a/mrconfig.git-fake-bare +++ b/lib/git-fake-bare @@ -6,7 +6,7 @@ # To make mr use this file, add a line like this inside the [DEFAULT] # section of your ~/.mrconfig -#include = cat /path/to/mrconfig.git-fake-bare +#include = cat /usr/share/mr/git-fake-bare # And an example repo using it would look something like: #[.dotfiles] -- 2.39.5 From d5108dc849fc3a3b75e02e5913a3b917e800a3bc Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 02:08:45 -0400 Subject: [PATCH 09/16] arn't you glad you use functions? don't you wish everybody did? --- mr | 237 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 128 insertions(+), 109 deletions(-) diff --git a/mr b/mr index 494e89b..4de2872 100755 --- a/mr +++ b/mr @@ -356,126 +356,23 @@ loadconfig($ENV{MR_CONFIG}); #use Data::Dumper; #print Dumper(\%config); -# alias expansion and command stemming -my $action=shift @ARGV; -if (exists $alias{$action}) { - $action=$alias{$action}; -} -if (! exists $knownactions{$action}) { - my @matches = grep { /^\Q$action\E/ } - keys %knownactions, keys %alias; - if (@matches == 1) { - $action=$matches[0]; - } - elsif (@matches == 0) { - die "mr: unknown action \"$action\" (known actions: ". - join(", ", sort keys %knownactions).")\n"; - } - else { - die "mr: ambiguous action \"$action\" (matches: ". - join(", ", @matches).")\n"; - } -} +my $action=expandaction(shift @ARGV); # commands that do not operate on all repos if ($action eq 'help') { - exec($config{''}{DEFAULT}{$action}) || die "exec: $!"; + help(@ARGV); } elsif ($action eq 'config') { - if (@ARGV < 2) { - die "mr config: not enough parameters\n"; - } - my $section=shift; - if ($section=~/^\//) { - # try to convert to a path relative to the config file - my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/; - $dir=abs_path($dir); - $dir.="/" unless $dir=~/\/$/; - if ($section=~/^\Q$dir\E(.*)/) { - $section=$1; - } - } - my %changefields; - foreach (@ARGV) { - if (/^([^=]+)=(.*)$/) { - $changefields{$1}=$2; - } - else { - my $found=0; - foreach my $topdir (sort keys %config) { - if (exists $config{$topdir}{$section} && - exists $config{$topdir}{$section}{$_}) { - print $config{$topdir}{$section}{$_}."\n"; - $found=1; - last if $section eq 'DEFAULT'; - } - } - if (! $found) { - die "mr $action: $section $_ not set\n"; - } - } - } - modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields; - exit 0; + config(@ARGV); } elsif ($action eq 'register') { - if (! $config_overridden) { - # Find the closest known mrconfig file to the current - # directory. - $directory.="/" unless $directory=~/\/$/; - foreach my $topdir (reverse sort keys %config) { - next unless length $topdir; - if ($directory=~/^\Q$topdir\E/) { - $ENV{MR_CONFIG}=$configfiles{$topdir}; - $directory=$topdir; - last; - } - } - } - if (@ARGV) { - my $subdir=shift @ARGV; - if (! chdir($subdir)) { - print STDERR "mr $action: failed to chdir to $subdir: $!\n"; - } - } - - $ENV{MR_REPO}=getcwd(); - my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT'); - if (! defined $command) { - die "mr $action: unknown repository type\n"; - } - - $ENV{MR_REPO}=~s/.*\/(.*)/$1/; - $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n". - "my_action(){ $command\n }; my_action ". - join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV); - print "mr $action: running >>$command<<\n" if $verbose; - exec($command) || die "exec: $!"; -} - -# an ordered list of repos -my @list; -foreach my $topdir (sort keys %config) { - foreach my $subdir (sort keys %{$config{$topdir}}) { - push @list, { - topdir => $topdir, - subdir => $subdir, - order => $config{$topdir}{$subdir}{order}, - }; - } + register(@ARGV); } -@list = sort { - $a->{order} <=> $b->{order} - || - $a->{topdir} cmp $b->{topdir} - || - $a->{subdir} cmp $b->{subdir} - } @list; # work out what repos to act on my @repos; my $nochdir=0; -foreach my $repo (@list) { +foreach my $repo (repolist()) { my $topdir=$repo->{topdir}; my $subdir=$repo->{subdir}; @@ -490,7 +387,7 @@ foreach my $repo (@list) { } if (! @repos) { # fallback to find a leaf repo - foreach my $repo (reverse @list) { + foreach my $repo (reverse repolist()) { my $topdir=$repo->{topdir}; my $subdir=$repo->{subdir}; @@ -797,6 +694,27 @@ sub showstat { #{{{ return; } #}}} +# an ordered list of repos +sub repolist { #{{{ + my @list; + foreach my $topdir (sort keys %config) { + foreach my $subdir (sort keys %{$config{$topdir}}) { + push @list, { + topdir => $topdir, + subdir => $subdir, + order => $config{$topdir}{$subdir}{order}, + }; + } + } + return sort { + $a->{order} <=> $b->{order} + || + $a->{topdir} cmp $b->{topdir} + || + $a->{subdir} cmp $b->{subdir} + } @list; +} #}}} + my %loaded; sub loadconfig { #{{{ my $f=shift; @@ -1025,6 +943,107 @@ sub modifyconfig { #{{{ print $out @out; close $out; } #}}} + +sub help { #{{{ + exec($config{''}{DEFAULT}{$action}) || die "exec: $!"; +} #}}} + +sub config { #{{{ + if (@_ < 2) { + die "mr config: not enough parameters\n"; + } + my $section=shift; + if ($section=~/^\//) { + # try to convert to a path relative to the config file + my ($dir)=$ENV{MR_CONFIG}=~/^(.*\/)[^\/]+$/; + $dir=abs_path($dir); + $dir.="/" unless $dir=~/\/$/; + if ($section=~/^\Q$dir\E(.*)/) { + $section=$1; + } + } + my %changefields; + foreach (@_) { + if (/^([^=]+)=(.*)$/) { + $changefields{$1}=$2; + } + else { + my $found=0; + foreach my $topdir (sort keys %config) { + if (exists $config{$topdir}{$section} && + exists $config{$topdir}{$section}{$_}) { + print $config{$topdir}{$section}{$_}."\n"; + $found=1; + last if $section eq 'DEFAULT'; + } + } + if (! $found) { + die "mr $action: $section $_ not set\n"; + } + } + } + modifyconfig($ENV{MR_CONFIG}, $section, %changefields) if %changefields; + exit 0; +} #}}} + +sub register { #{{{ + if (! $config_overridden) { + # Find the closest known mrconfig file to the current + # directory. + $directory.="/" unless $directory=~/\/$/; + foreach my $topdir (reverse sort keys %config) { + next unless length $topdir; + if ($directory=~/^\Q$topdir\E/) { + $ENV{MR_CONFIG}=$configfiles{$topdir}; + $directory=$topdir; + last; + } + } + } + if (@ARGV) { + my $subdir=shift @ARGV; + if (! chdir($subdir)) { + print STDERR "mr $action: failed to chdir to $subdir: $!\n"; + } + } + + $ENV{MR_REPO}=getcwd(); + my $command=findcommand("register", $ENV{MR_REPO}, $directory, 'DEFAULT'); + if (! defined $command) { + die "mr $action: unknown repository type\n"; + } + + $ENV{MR_REPO}=~s/.*\/(.*)/$1/; + $command="set -e; ".$config{$directory}{DEFAULT}{lib}."\n". + "my_action(){ $command\n }; my_action ". + join(" ", map { s/\//\/\//g; s/"/\"/g; '"'.$_.'"' } @ARGV); + print "mr $action: running >>$command<<\n" if $verbose; + exec($command) || die "exec: $!"; +} #}}} + +# alias expansion and command stemming +sub expandaction { #{{{ + my $action=shift; + if (exists $alias{$action}) { + $action=$alias{$action}; + } + if (! exists $knownactions{$action}) { + my @matches = grep { /^\Q$action\E/ } + keys %knownactions, keys %alias; + if (@matches == 1) { + $action=$matches[0]; + } + elsif (@matches == 0) { + die "mr: unknown action \"$action\" (known actions: ". + join(", ", sort keys %knownactions).")\n"; + } + else { + die "mr: ambiguous action \"$action\" (matches: ". + join(", ", @matches).")\n"; + } + } + return $action; +} # Finally, some useful actions that mr knows about by default. # These can be overridden in ~/.mrconfig. -- 2.39.5 From 3462c6fcaa77e2fe0d2c69b6ab62533be631506d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 02:20:33 -0400 Subject: [PATCH 10/16] more reorganisation --- mr | 159 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 87 insertions(+), 72 deletions(-) diff --git a/mr b/mr index 4de2872..8108762 100755 --- a/mr +++ b/mr @@ -314,6 +314,7 @@ my $config_overridden=0; my $verbose=0; my $stats=0; my $no_recurse=0; +my $no_chdir=0; my $jobs=1; my %config; my %configfiles; @@ -321,24 +322,13 @@ my %knownactions; my %alias; my $directory=getcwd(); -Getopt::Long::Configure("no_permute"); -my $result=GetOptions( - "d|directory=s" => sub { $directory=abs_path($_[1]) }, - "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 }, - "v|verbose" => \$verbose, - "s|stats" => \$stats, - "n|no-recurse" => \$no_recurse, - "j|jobs=i" => \$jobs, -); -if (! $result || @ARGV < 1) { - die("Usage: mr [-d directory] action [params ...]\n". - "(Use mr help for man page.)\n"); +getopts(); -} +# This can happen if it's run in a directory that was removed +# or other strangeness. if (! defined $directory) { die("mr: failed to determine working directory\n"); } - # Make sure MR_CONFIG is an absolute path, but don't use abs_path since # the config file might be a symlink to elsewhere, and the directory it's # in is significant. @@ -358,7 +348,7 @@ loadconfig($ENV{MR_CONFIG}); my $action=expandaction(shift @ARGV); -# commands that do not operate on all repos +# actions that do not operate on all repos if ($action eq 'help') { help(@ARGV); } @@ -369,67 +359,18 @@ elsif ($action eq 'register') { register(@ARGV); } -# work out what repos to act on -my @repos; -my $nochdir=0; -foreach my $repo (repolist()) { - my $topdir=$repo->{topdir}; - my $subdir=$repo->{subdir}; - - next if $subdir eq 'DEFAULT'; - my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir; - my $d=$directory; - $dir.="/" unless $dir=~/\/$/; - $d.="/" unless $d=~/\/$/; - next if $no_recurse && $d ne $dir; - next if $dir ne $d && $dir !~ /^\Q$d\E/; - push @repos, [$dir, $topdir, $subdir]; -} -if (! @repos) { - # fallback to find a leaf repo - foreach my $repo (reverse repolist()) { - my $topdir=$repo->{topdir}; - my $subdir=$repo->{subdir}; - - next if $subdir eq 'DEFAULT'; - my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir; - my $d=$directory; - $dir.="/" unless $dir=~/\/$/; - $d.="/" unless $d=~/\/$/; - if ($d=~/^\Q$dir\E/) { - push @repos, [$dir, $topdir, $subdir]; - last; - } - } - $nochdir=1; -} - -# run the action on each repository and print stats my (@ok, @failed, @skipped); if ($jobs > 1) { - mrs(@repos); + mrs(selectrepos()); } else { - foreach my $repo (@repos) { + foreach my $repo (selectrepos()) { record($repo, action($action, @$repo)); } } -if (! @ok && ! @failed && ! @skipped) { - die "mr $action: no repositories found to work on\n"; -} -print "mr $action: finished (".join("; ", - showstat($#ok+1, "ok", "ok"), - showstat($#failed+1, "failed", "failed"), - showstat($#skipped+1, "skipped", "skipped"), -).")\n"; -if ($stats) { - if (@skipped) { - print "mr $action: (skipped: ".join(" ", @skipped).")\n"; - } - if (@failed) { - print STDERR "mr $action: (failed: ".join(" ", @failed).")\n"; - } -} + +showstats(); + if (@failed) { exit 1; } @@ -536,7 +477,7 @@ sub action { #{{{ } } - if (! $nochdir && ! chdir($dir)) { + if (! $no_chdir && ! chdir($dir)) { print STDERR "mr $action: failed to chdir to $dir: $!\n"; return FAILED; } @@ -552,7 +493,7 @@ sub action { #{{{ } } else { - if (! $nochdir) { + if (! $no_chdir) { print "mr $action: $topdir$subdir\n"; } else { @@ -596,6 +537,8 @@ sub action { #{{{ # run actions on multiple repos, in parallel sub mrs { #{{{ + my @repos=@_; + $| = 1; my @active; my @fhs; @@ -684,6 +627,25 @@ sub record { #{{{ } } #}}} +sub showstats { #{{{ + if (! @ok && ! @failed && ! @skipped) { + die "mr $action: no repositories found to work on\n"; + } + print "mr $action: finished (".join("; ", + showstat($#ok+1, "ok", "ok"), + showstat($#failed+1, "failed", "failed"), + showstat($#skipped+1, "skipped", "skipped"), + ).")\n"; + if ($stats) { + if (@skipped) { + print "mr $action: (skipped: ".join(" ", @skipped).")\n"; + } + if (@failed) { + print STDERR "mr $action: (failed: ".join(" ", @failed).")\n"; + } + } +} #}}} + sub showstat { #{{{ my $count=shift; my $singular=shift; @@ -715,6 +677,43 @@ sub repolist { #{{{ } @list; } #}}} +# figure out which repos to act on +sub selectrepos { #{{{ + my @repos; + foreach my $repo (repolist()) { + my $topdir=$repo->{topdir}; + my $subdir=$repo->{subdir}; + + next if $subdir eq 'DEFAULT'; + my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir; + my $d=$directory; + $dir.="/" unless $dir=~/\/$/; + $d.="/" unless $d=~/\/$/; + next if $no_recurse && $d ne $dir; + next if $dir ne $d && $dir !~ /^\Q$d\E/; + push @repos, [$dir, $topdir, $subdir]; + } + if (! @repos) { + # fallback to find a leaf repo + foreach my $repo (reverse repolist()) { + my $topdir=$repo->{topdir}; + my $subdir=$repo->{subdir}; + + next if $subdir eq 'DEFAULT'; + my $dir=($subdir =~/^\//) ? $subdir : $topdir.$subdir; + my $d=$directory; + $dir.="/" unless $dir=~/\/$/; + $d.="/" unless $d=~/\/$/; + if ($d=~/^\Q$dir\E/) { + push @repos, [$dir, $topdir, $subdir]; + last; + } + } + $no_chdir=1; + } + return @repos; +} #}}} + my %loaded; sub loadconfig { #{{{ my $f=shift; @@ -1043,7 +1042,23 @@ sub expandaction { #{{{ } } return $action; -} +} #}}} + +sub getopts { #{{{ + Getopt::Long::Configure("no_permute"); + my $result=GetOptions( + "d|directory=s" => sub { $directory=abs_path($_[1]) }, + "c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 }, + "v|verbose" => \$verbose, + "s|stats" => \$stats, + "n|no-recurse" => \$no_recurse, + "j|jobs=i" => \$jobs, + ); + if (! $result || @ARGV < 1) { + die("Usage: mr [-d directory] action [params ...]\n". + "(Use mr help for man page.)\n"); + } +} #}}} # Finally, some useful actions that mr knows about by default. # These can be overridden in ~/.mrconfig. -- 2.39.5 From 4c3cf0ae7e4d13fdf8be036bfb935a149e1e0fdd Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Fri, 26 Oct 2007 08:20:38 +0200 Subject: [PATCH 11/16] add a vim modeline to preserve joey's tabbing prefs --- lib/git-fake-bare | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/git-fake-bare b/lib/git-fake-bare index 231b631..f059ff7 100644 --- a/lib/git-fake-bare +++ b/lib/git-fake-bare @@ -72,3 +72,5 @@ git_fake_bare_register = mr -c "$MR_CONFIG" config "`pwd`" lib="git_fake_bare_worktree '$worktree'" echo "Registering git url: $url in $MR_CONFIG (with worktree $worktree)" mr -c "$MR_CONFIG" config "`pwd`" checkout="git_fake_bare_checkout '$url' '$MR_REPO'" + +# vim:sw=8:sts=0:ts=8:noet -- 2.39.5 From f5910eff03b17bfdd89a654aa4b5859ab8e9262c Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Fri, 26 Oct 2007 08:21:53 +0200 Subject: [PATCH 12/16] Get rid of GIT_WORK_TREE Since we store core.worktree on checkout, we can get rid of the variable and make it all a bit easier in the config file, passing worktree to the checkout helper function. --- lib/git-fake-bare | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/git-fake-bare b/lib/git-fake-bare index f059ff7..ab9fb12 100644 --- a/lib/git-fake-bare +++ b/lib/git-fake-bare @@ -10,25 +10,21 @@ # And an example repo using it would look something like: #[.dotfiles] -#lib = git_fake_bare_worktree $HOME -#checkout = git_fake_bare_checkout git://... .dotfiles +#checkout = git_fake_bare_checkout git://... .dotfiles lib = - # called to tell git where the worktree is - git_fake_bare_worktree() { - GIT_WORK_TREE="$1"; export GIT_WORK_TREE - } # git doesn't have an easy way to check out such a repo, so # do it by hand git_fake_bare_checkout() { url="$1" repo="$2" - GIT_WORK_TREE= git clone --no-checkout "$url" "$repo" + worktree="$3" + git clone --no-checkout "$url" "$repo" cd "$repo" - mkdir -p "$GIT_WORK_TREE" + mkdir -p "$worktree" git read-tree HEAD - git config core.worktree "$GIT_WORK_TREE" git checkout-index -a --prefix="$worktree" || true + git config core.worktree "$worktree" mv .git/* . rmdir .git } @@ -69,8 +65,8 @@ git_fake_bare_register = if [ ! -d "$worktree" ]; then error "git worktree $worktree does not exist" fi - mr -c "$MR_CONFIG" config "`pwd`" lib="git_fake_bare_worktree '$worktree'" echo "Registering git url: $url in $MR_CONFIG (with worktree $worktree)" - mr -c "$MR_CONFIG" config "`pwd`" checkout="git_fake_bare_checkout '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" \ + checkout="git_fake_bare_checkout '$url' '$MR_REPO' '$worktree'" # vim:sw=8:sts=0:ts=8:noet -- 2.39.5 From ee2f2dfa1c149b6d6d7ace168fe81bccac2dc85e Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Fri, 26 Oct 2007 08:27:05 +0200 Subject: [PATCH 13/16] replace `pwd` calls with $PWD also ensure that $PWD is defined by adding it to lib --- lib/git-fake-bare | 2 +- mr | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/git-fake-bare b/lib/git-fake-bare index ab9fb12..545115b 100644 --- a/lib/git-fake-bare +++ b/lib/git-fake-bare @@ -66,7 +66,7 @@ git_fake_bare_register = error "git worktree $worktree does not exist" fi echo "Registering git url: $url in $MR_CONFIG (with worktree $worktree)" - mr -c "$MR_CONFIG" config "`pwd`" \ + mr -c "$MR_CONFIG" config "$PWD" \ checkout="git_fake_bare_checkout '$url' '$MR_REPO' '$worktree'" # vim:sw=8:sts=0:ts=8:noet diff --git a/mr b/mr index 494e89b..a611e38 100755 --- a/mr +++ b/mr @@ -1038,6 +1038,7 @@ ls = list [DEFAULT] order = 10 lib = + PWD="$(pwd)" error() { echo "mr: $@" >&2 exit 1 @@ -1123,21 +1124,21 @@ svn_register = error "cannot determine svn url" fi echo "Registering svn url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "$PWD" checkout="svn co '$url' '$MR_REPO'" git_register = url="$(LANG=C git-config --get remote.origin.url)" || true if [ -z "$url" ]; then error "cannot determine git url" fi echo "Registering git url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "$PWD" checkout="git clone '$url' '$MR_REPO'" bzr_register = url=$(cat .bzr/branch/parent) if [ -z "$url" ]; then error "cannot determine bzr url" fi echo "Registering bzr url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "$PWD" checkout="bzr clone '$url' '$MR_REPO'" cvs_register = repo=$(cat CVS/Repository) root=$(cat CVS/Root) @@ -1145,22 +1146,22 @@ cvs_register = error "cannot determine cvs root" fi echo "Registering cvs repository $repo at root $root" - mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'" + mr -c "$MR_CONFIG" config "$PWD" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'" hg_register = url=$(hg showconfig paths.default) echo "Registering mercurial repo url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "$PWD" checkout="hg clone '$url' '$MR_REPO'" darcs_register = url=$(cat _darcs/prefs/defaultrepo) echo "Registering darcs repository $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url'p '$MR_REPO'" + mr -c "$MR_CONFIG" config "$PWD" checkout="darcs get '$url'p '$MR_REPO'" git_bare_register = url="$(LANG=C GIT_CONFIG=config git-config --get remote.origin.url)" || true if [ -z "$url" ]; then error "cannot determine git url" fi echo "Registering git url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "$PWD" checkout="git clone --bare '$url' '$MR_REPO'" help = if [ ! -e "$MR_PATH" ]; then -- 2.39.5 From 273d6e796f78781580939f34a9958ce34c36e90e Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 02:30:28 -0400 Subject: [PATCH 14/16] just a few more functions.. --- mr | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/mr b/mr index 8108762..2187aa0 100755 --- a/mr +++ b/mr @@ -321,6 +321,7 @@ my %configfiles; my %knownactions; my %alias; my $directory=getcwd(); +my (@ok, @failed, @skipped); getopts(); @@ -347,29 +348,8 @@ loadconfig($ENV{MR_CONFIG}); #print Dumper(\%config); my $action=expandaction(shift @ARGV); - -# actions that do not operate on all repos -if ($action eq 'help') { - help(@ARGV); -} -elsif ($action eq 'config') { - config(@ARGV); -} -elsif ($action eq 'register') { - register(@ARGV); -} - -my (@ok, @failed, @skipped); -if ($jobs > 1) { - mrs(selectrepos()); -} -else { - foreach my $repo (selectrepos()) { - record($repo, action($action, @$repo)); - } -} - -showstats(); +dispatch($action); +showstats($action); if (@failed) { exit 1; @@ -377,7 +357,33 @@ if (@failed) { elsif (! @ok && @skipped) { exit 1; } -exit 0; +else { + exit 0; +} + +sub dispatch { #{{{ + my $action=shift; + + # actions that do not operate on all repos + if ($action eq 'help') { + help(@ARGV); + } + elsif ($action eq 'config') { + config(@ARGV); + } + elsif ($action eq 'register') { + register(@ARGV); + } + + if ($jobs > 1) { + mrs($action, selectrepos()); + } + else { + foreach my $repo (selectrepos()) { + record($repo, action($action, @$repo)); + } + } +} #}}} sub rcs_test { #{{{ my ($action, $dir, $topdir, $subdir) = @_; @@ -537,6 +543,7 @@ sub action { #{{{ # run actions on multiple repos, in parallel sub mrs { #{{{ + my $action=shift; my @repos=@_; $| = 1; -- 2.39.5 From de0874e8884604b08e3a3b9a1bf682cf2b88f1f2 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 26 Oct 2007 02:36:35 -0400 Subject: [PATCH 15/16] Revert "replace `pwd` calls with $PWD" This actually makes mr slower by calling pwd on *every* run. Instead, call pwd just when it's needed, on register opterations that don't need to be fast, and that happen to call it a max of 1 time anyway. --- lib/git-fake-bare | 2 +- mr | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/git-fake-bare b/lib/git-fake-bare index 545115b..ab9fb12 100644 --- a/lib/git-fake-bare +++ b/lib/git-fake-bare @@ -66,7 +66,7 @@ git_fake_bare_register = error "git worktree $worktree does not exist" fi echo "Registering git url: $url in $MR_CONFIG (with worktree $worktree)" - mr -c "$MR_CONFIG" config "$PWD" \ + mr -c "$MR_CONFIG" config "`pwd`" \ checkout="git_fake_bare_checkout '$url' '$MR_REPO' '$worktree'" # vim:sw=8:sts=0:ts=8:noet diff --git a/mr b/mr index 13807f8..2187aa0 100755 --- a/mr +++ b/mr @@ -1079,7 +1079,6 @@ ls = list [DEFAULT] order = 10 lib = - PWD="$(pwd)" error() { echo "mr: $@" >&2 exit 1 @@ -1165,21 +1164,21 @@ svn_register = error "cannot determine svn url" fi echo "Registering svn url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "$PWD" checkout="svn co '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="svn co '$url' '$MR_REPO'" git_register = url="$(LANG=C git-config --get remote.origin.url)" || true if [ -z "$url" ]; then error "cannot determine git url" fi echo "Registering git url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "$PWD" checkout="git clone '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone '$url' '$MR_REPO'" bzr_register = url=$(cat .bzr/branch/parent) if [ -z "$url" ]; then error "cannot determine bzr url" fi echo "Registering bzr url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "$PWD" checkout="bzr clone '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="bzr clone '$url' '$MR_REPO'" cvs_register = repo=$(cat CVS/Repository) root=$(cat CVS/Root) @@ -1187,22 +1186,22 @@ cvs_register = error "cannot determine cvs root" fi echo "Registering cvs repository $repo at root $root" - mr -c "$MR_CONFIG" config "$PWD" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="cvs -d '$root' co -d '$MR_REPO' '$repo'" hg_register = url=$(hg showconfig paths.default) echo "Registering mercurial repo url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "$PWD" checkout="hg clone '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="hg clone '$url' '$MR_REPO'" darcs_register = url=$(cat _darcs/prefs/defaultrepo) echo "Registering darcs repository $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "$PWD" checkout="darcs get '$url'p '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="darcs get '$url'p '$MR_REPO'" git_bare_register = url="$(LANG=C GIT_CONFIG=config git-config --get remote.origin.url)" || true if [ -z "$url" ]; then error "cannot determine git url" fi echo "Registering git url: $url in $MR_CONFIG" - mr -c "$MR_CONFIG" config "$PWD" checkout="git clone --bare '$url' '$MR_REPO'" + mr -c "$MR_CONFIG" config "`pwd`" checkout="git clone --bare '$url' '$MR_REPO'" help = if [ ! -e "$MR_PATH" ]; then -- 2.39.5 From a7e0d6673b1641708d06e4b7280351a09e2116c7 Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Fri, 26 Oct 2007 09:40:12 +0200 Subject: [PATCH 16/16] Fix git_fake_bare_update By removing the check for Git repo type: it's fake-bare anyway. --- lib/git-fake-bare | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/git-fake-bare b/lib/git-fake-bare index ab9fb12..8afb84a 100644 --- a/lib/git-fake-bare +++ b/lib/git-fake-bare @@ -35,14 +35,9 @@ git_fake_bare_test = test "$(GIT_CONFIG="$MR_REPO"/config git-config --get core.bare)" = false git_fake_bare_update = - # all this is because of a bug in git-fetch, which requires GIT_DIR set - local git_dir_override; git_dir_override=.git - case "$(get_git_repo_type "$MR_REPO")" in - fake-bare) git_dir_override="$MR_REPO";; - esac args="$@" [ -z "$args" ] && args="-t origin master" - eval GIT_DIR="$git_dir_override" git pull "$args" + eval GIT_DIR="$MR_REPO" git pull "$args" git_fake_bare_status = git status "$@" || true -- 2.39.5