Permalink
Browse files

re-use existing repos; set default tag of HEAD

  • Loading branch information...
1 parent a12d339 commit 3ed8c8c4112b17ab19c949c7649d2fd329a53289 @perlpilot committed Aug 11, 2010
Showing with 28 additions and 10 deletions.
  1. +28 −10 lib/Dist/Zilla/Plugin/GitObtain.pm
@@ -3,6 +3,7 @@ package Dist::Zilla::Plugin::GitObtain;
# ABSTRACT: obtain files from a git repository before building a distribution
use Git::Wrapper;
+use File::Spec::Functions;
use File::Path qw/ make_path remove_tree /;
use Moose;
use namespace::autoclean;
@@ -39,6 +40,7 @@ sub BUILDARGS {
next;
}
my ($url,$tag) = split ' ', $repos{$project};
+ $tag ||= 'HEAD';
$repos{$project} = { url => $url, tag => $tag };
}
@@ -55,20 +57,36 @@ sub before_build {
my $self = shift;
if (-d $self->git_dir) {
- $self->log("using existing directory " . $self->git_dir);
+ $self->log("using existing dir " . $self->git_dir);
} else {
- $self->log("creating directory " . $self->git_dir);
- make_path($self->git_dir) or die "Can't create directory " . $self->git_dir . " -- $!";
+ $self->log("creating dir " . $self->git_dir);
+ make_path($self->git_dir) or die "Can't create dir " . $self->git_dir . " -- $!";
}
for my $project (keys %{$self->_repos}) {
my ($url,$tag) = map { $self->_repos->{$project}{$_} } qw/url tag/;
- $self->log("cloning $project");
- my $git = Git::Wrapper->new($self->git_dir);
- $git->clone($url,$project) or die "Can't clone repository $url -- $!";
- next unless $tag;
- $self->log("checkout $project revision $tag");
- my $git_tag = Git::Wrapper->new($self->git_dir . '/' . $project);
- $git_tag->checkout($tag);
+ my $dir = catfile($self->git_dir, $project);
+ if (-d $dir) {
+ if (-e catfile($dir, ".git")) {
+ my $git = Git::Wrapper->new($dir);
+ my ($wc_url) = $git->config("remote.origin.url");
+ if ($wc_url eq $url) {
+ $self->log("checkout $project revision $tag");
+ $git->checkout($tag);
+ } else {
+ die "$project directory is not a GIT repository for $url ($wc_url)\n";
+ }
+ } else {
+ die "$project directory exists but is not a GIT repository\n";
+ }
+ } else {
+ $self->log("cloning $project ($url)");
+ my $git = Git::Wrapper->new($self->git_dir);
+ $git->clone($url,$project) or die "Can't clone repository $url -- $!";
+ next unless $tag;
+ $self->log("checkout $project revision $tag");
+ my $git_tag = Git::Wrapper->new($dir);
+ $git_tag->checkout($tag);
+ }
}
}

0 comments on commit 3ed8c8c

Please sign in to comment.