Skip to content

Commit

Permalink
Add 'name' special parameter which allows selection of which repos to…
Browse files Browse the repository at this point in the history
… act on via -r / --repos
  • Loading branch information
Adam Spiers committed Nov 6, 2011
1 parent b666b61 commit b9a4e45
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions mr
Expand Up @@ -212,6 +212,21 @@ Use the specified mrconfig file. The default is to use both B<~/.mrconfig>
as well as look for a .mrconfig file in the current directory, or in one
of its parent directories.
=item -r repo1,repo2,...
=item --repos repo1,repo2,...
Out of the repositories available (subject to options such as C<-d>
and C<-c>), only act on the repositories listed. This should be a
comma-separated list of names as specified by the C<name> special
parameter (or default values thereof). They are still acted on in the
order specified via the "order" parameter, rather than the order in
the comma-separated list.
This option only filters the repositories already available; it cannot
augment that list. If this option is not specified, the default is to
run on all available repositories.
=item -v
=item --verbose
Expand Down Expand Up @@ -343,6 +358,15 @@ A few parameters have special meanings:
=over 4
=item name
The "name" parameter can be used to specify a unique name for this
repository. If not specified, defaults to the "basename" (i.e. final
segment) of the repository path listed in the section header.
This value can be referred to via the C<--repos> option in order to
limit which repositories are acted on.
=item skip
If the "skip" parameter is set and its command returns true, then B<mr>
Expand Down Expand Up @@ -491,12 +515,14 @@ my $no_chdir=0;
my $jobs=1;
my $trust_all=0;
my $directory=getcwd();
my $repos="";

$ENV{MR_CONFIG}=find_mrconfig();

# globals :-(
my %config;
my %configfiles;
my %repos; # maps repo short names to [ $dir, $section ] arrayrefs.
my %knownactions;
my %alias;
my (@ok, @failed, @skipped);
Expand Down Expand Up @@ -924,11 +950,30 @@ sub repodir {
# figure out which repos to act on
sub selectrepos {
my @repos;
my %selected = $repos ? map { $_ => 1 } split(/,/, $repos) : ();
foreach my $repo (repolist()) {
my $topdir=$repo->{topdir};
my $subdir=$repo->{subdir};

next if $subdir eq 'DEFAULT';

if (%selected) {
my $name = $config{$topdir}{$subdir}{effective_name};
if (defined $name) {
if (! $selected{$name}) {
print "Filtering out repo '$name'\n" if $verbose;
next;
}
else {
print "Allowing selected repo '$name'\n" if $verbose;
}
}
else {
print "Filtering out non-named repo for $subdir\n" if $verbose;
next;
}
}

my $dir=repodir($repo);
my $d=$directory;
$dir.="/" unless $dir=~/\/$/;
Expand Down Expand Up @@ -1093,6 +1138,63 @@ sub trusterror {
}
}

sub register_repo_names {
foreach my $dir (keys %config) {
foreach my $section (keys %{ $config{$dir} }) {
next if $section eq 'DEFAULT';

my $name;
if (exists $config{$dir}{$section}{name}) {
$name = $config{$dir}{$section}{name};
if (exists $repos{$name}) {
my ($prev_dir, $prev_section) = @{ $repos{$name} };
my $this_config = $configfiles{$dir};
my $prev_config = $configfiles{$prev_dir};
s/^$ENV{HOME}/~/ for $prev_config, $prev_section, $this_config, $section;
die <<EOF;
mr error: attempted to set '$name' for:
$section via $this_config
but was already defined for:
$prev_section via $prev_config
EOF
}
}
else {
(my $default_name = $section) =~ s!.*/!!;
if (exists $repos{$default_name}) {
my ($prev_dir, $prev_section) = @{ $repos{$default_name} };
my $this_config = $configfiles{$dir};
my $prev_config = $configfiles{$prev_dir};
s/^$ENV{HOME}/~/ for $prev_config, $prev_section, $this_config, $section;
warn <<EOF;
mr error: could not allocate unique name '$default_name' for
$section via $this_config
since already allocated to
$prev_section via $prev_config
Add a 'name = ...' line if you want to be able to
reference this repository by name.
EOF
}
else {
$name = $default_name;
}
}
if (defined $name) {
$repos{$name} = [ $dir, $section ];
$config{$dir}{$section}{effective_name} = $name;
}
}
}
}

my %loaded;
sub loadconfig {
my $f=shift;
Expand Down Expand Up @@ -1229,6 +1331,19 @@ sub loadconfig {
elsif ($parameter eq 'lib') {
$config{$dir}{$section}{lib}.=$value."\n";
}
elsif ($parameter eq 'name') {
my $where = $f;
$where .= " line $." if $. > 0;
if ($section eq 'DEFAULT') {
die "$where: name not allowed in DEFAULT section\n";
}
if (exists $config{$dir}{$section}{name}) {
die "$where: $section attempted to define name twice\n";
}
else {
$config{$dir}{$section}{name}=$value;
}
}
else {
$config{$dir}{$section}{$parameter}=$value;
if ($parameter =~ /.*_(.*)/) {
Expand Down Expand Up @@ -1582,6 +1697,7 @@ sub getopts {
my $result=GetOptions(
"d|directory=s" => sub { $directory=abs_path($_[1]) },
"c|config=s" => sub { $ENV{MR_CONFIG}=$_[1]; $config_overridden=1 },
"r|repos=s" => \$repos,
"p|path" => sub { }, # now default, ignore
"v|verbose" => \$verbose,
"q|quiet" => \$quiet,
Expand Down Expand Up @@ -1644,6 +1760,7 @@ sub main {
startingconfig();
loadconfig("$ENV{HOME}/.mrconfig");
loadconfig($ENV{MR_CONFIG});
register_repo_names();
#use Data::Dumper; print Dumper(\%config);

my $action=expandaction(shift @ARGV);
Expand Down

0 comments on commit b9a4e45

Please sign in to comment.