[Bps-public-commit] Shipwright branch, master, updated. bf5a8c736428b93e8ed25b9f647381a63a8789d8

sunnavy at bestpractical.com sunnavy at bestpractical.com
Sun Apr 26 04:48:05 EDT 2009


The branch, master has been updated
       via  bf5a8c736428b93e8ed25b9f647381a63a8789d8 (commit)
      from  e16d5cfaa2fdaec805f93d688a27ee5a114c463a (commit)

Summary of changes:
 lib/Shipwright/Backend.pm       |    4 +
 lib/Shipwright/Backend/Base.pm  |    7 +-
 lib/Shipwright/Backend/Git.pm   |  170 +++++++++++++++++++++++++++++++++++++++
 lib/Shipwright/Script/Update.pm |    6 +-
 lib/Shipwright/Test.pm          |   46 ++++++++++-
 lib/Shipwright/Util.pm          |   11 ++-
 t/71.script_cmds.t              |    9 ++-
 7 files changed, 242 insertions(+), 11 deletions(-)
 create mode 100644 lib/Shipwright/Backend/Git.pm

- Log -----------------------------------------------------------------
commit bf5a8c736428b93e8ed25b9f647381a63a8789d8
Author: sunnavy <sunnavy at gmail.com>
Date:   Sun Apr 26 16:47:52 2009 +0800

    comment

diff --git a/lib/Shipwright/Backend.pm b/lib/Shipwright/Backend.pm
index ba602d3..206473e 100644
--- a/lib/Shipwright/Backend.pm
+++ b/lib/Shipwright/Backend.pm
@@ -28,6 +28,10 @@ sub new {
         $args{repository} = $abs_path if $abs_path;
         $module = 'Shipwright::Backend::FS';
     }
+    elsif ( $args{repository} =~ m{^\s*git:} ) {
+        $args{repository} =~ s{^\s*git:}{};
+        $module = 'Shipwright::Backend::Git';
+    }
     else {
         croak "invalid repository: $args{repository}\n";
     }
diff --git a/lib/Shipwright/Backend/Base.pm b/lib/Shipwright/Backend/Base.pm
index ec2adbb..6dd823d 100644
--- a/lib/Shipwright/Backend/Base.pm
+++ b/lib/Shipwright/Backend/Base.pm
@@ -79,7 +79,10 @@ sub initialize {
 
     # share_root can't keep empty dirs, we have to create them manually
     for (qw/scripts sources/) {
-        mkdir catdir( $dir, $_ );
+        my $sub_dir = catdir( $dir, $_ );
+        mkdir $sub_dir;
+        open my $fh, '>', catfile( $sub_dir, '.exists' ) or confess $!;
+        close $fh;
     }
     chmod 0644, catfile( $dir, 't', 'test' );
 
@@ -854,7 +857,7 @@ sub has_branch_support {
     return;
 }
 
-*_cmd = *_update_file = *_subclass_method;
+*_cmd = *_update_file = *_update_dir = *_subclass_method;
 
 =back
 
diff --git a/lib/Shipwright/Backend/Git.pm b/lib/Shipwright/Backend/Git.pm
new file mode 100644
index 0000000..dbf9e76
--- /dev/null
+++ b/lib/Shipwright/Backend/Git.pm
@@ -0,0 +1,170 @@
+package Shipwright::Backend::Git;
+
+use warnings;
+use strict;
+use Carp;
+use File::Spec::Functions qw/catfile catdir/;
+use Shipwright::Util;
+use File::Temp qw/tempdir/;
+use File::Copy qw/copy/;
+use File::Copy::Recursive qw/dircopy/;
+use Cwd qw/getcwd/;
+use Shipwright::Backend::FS;
+
+our %REQUIRE_OPTIONS = ( import => [qw/source/] );
+
+use base qw/Shipwright::Backend::Base/;
+
+=head1 NAME
+
+Shipwright::Backend::Git - git repository backend
+
+=head1 DESCRIPTION
+
+This module implements an Git repository backend for Shipwright.
+
+=head1 METHODS
+
+=over
+
+=item initialize
+
+initialize a project.
+
+=cut
+
+sub initialize {
+    my $self = shift;
+
+    my $dir = $self->SUPER::initialize(@_);
+
+    my $cwd = getcwd;
+    chdir $dir;
+    Shipwright::Util->run( [ $ENV{'SHIPWRIGHT_GIT'}, 'init' ] );
+    Shipwright::Util->run( [ $ENV{'SHIPWRIGHT_GIT'}, 'add', '.' ] );
+    Shipwright::Util->run(
+        [ $ENV{'SHIPWRIGHT_GIT'}, 'commit', '-m', 'creating repository' ] );
+
+    my $path = $self->repository;
+    $path =~ s!^file://!!;    # this is always true since we check that before
+
+    Shipwright::Util->run( [ 'rm', '-rf', $path ] );
+
+    dircopy( catdir( $dir, '.git' ), $path )
+      or confess "can't copy $dir to " . $path . ": $!";
+    chdir $cwd;
+}
+
+my $cloned_dir;
+
+sub cloned_dir {
+    my $self = shift;
+    return $cloned_dir if $cloned_dir;
+
+    my $base_dir =
+      tempdir( 'shipwright_backend_git_XXXXXX', CLEANUP => 1, TMPDIR => 1 );
+
+    my $target = catdir( $base_dir, 'clone' );
+    Shipwright::Util->run(
+        [ $ENV{'SHIPWRIGHT_GIT'}, 'clone', $self->repository, $target ] );
+    return $cloned_dir = $target;
+}
+
+=item check_repository
+
+check if the given repository is valid.
+
+=cut
+
+sub check_repository {
+    my $self = shift;
+    my %args = @_;
+
+    if ( $args{action} eq 'create' ) {
+        if ( $self->repository =~ m{^file://} ) {
+            return 1;
+        }
+        else {
+            $self->log->error(
+                "git backend only supports creating local repository");
+            return;
+        }
+    }
+    else {
+
+        return $self->SUPER::check_repository(@_);
+    }
+    return;
+}
+
+sub fs_backend {
+    my $self = shift;
+    return $self->{fs_backend} if $self->{fs_backend};
+    # XXX TODO not a great place to clone, need refactor
+    my $cloned_dir = $self->cloned_dir;
+    $self->{fs_backend} = Shipwright::Backend::FS->new(
+        repository => $self->cloned_dir,
+    );
+    return $self->{fs_backend};
+}
+
+sub _cmd {
+    my $self = shift;
+    return $self->fs_backend->_cmd(@_);
+}
+
+sub _yml {
+    my $self = shift;
+    return $self->fs_backend->_yml(@_);
+}
+
+sub info {
+    my $self = shift;
+    return $self->fs_backend->info(@_);
+}
+
+sub _update_dir {
+    my $self = shift;
+    return $self->fs_backend->_update_dir(@_);
+}
+
+sub _update_file {
+    my $self = shift;
+    return $self->fs_backend->_update_file(@_);
+}
+
+sub import {
+    my $self = shift;
+    return $self->fs_backend->import(@_);
+}
+
+sub DESTROY {
+    my $self = shift;
+    my $cwd  = getcwd;
+    chdir $self->cloned_dir;
+    Shipwright::Util->run( [ $ENV{'SHIPWRIGHT_GIT'}, 'add', '.' ] );
+    #TODO comment need to be something special
+    Shipwright::Util->run(
+        [ $ENV{'SHIPWRIGHT_GIT'}, 'commit', '-m', 'comment', '-a' ], 1 );
+    Shipwright::Util->run( [ $ENV{'SHIPWRIGHT_GIT'}, 'push' ] );
+    chdir $cwd;
+}
+
+=back
+
+=cut
+
+1;
+
+__END__
+
+=head1 AUTHORS
+
+sunnavy  C<< <sunnavy at bestpractical.com> >>
+
+=head1 LICENCE AND COPYRIGHT
+
+Shipwright is Copyright 2007-2009 Best Practical Solutions, LLC.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
diff --git a/lib/Shipwright/Script/Update.pm b/lib/Shipwright/Script/Update.pm
index a5328f5..9203d93 100644
--- a/lib/Shipwright/Script/Update.pm
+++ b/lib/Shipwright/Script/Update.pm
@@ -39,7 +39,11 @@ my ( $shipwright, $map, $source, $branches );
 sub run {
     my $self = shift;
 
-    $shipwright = Shipwright->new( repository => $self->repository, );
+    $shipwright = Shipwright->new(
+        repository => $self->repository,
+        log_level  => $self->log_level,
+        log_file   => $self->log_file
+    );
 
     if ( $self->builder ) {
         $shipwright->backend->update( path => '/bin/shipwright-builder' );
diff --git a/lib/Shipwright/Test.pm b/lib/Shipwright/Test.pm
index 31e87b5..9708b05 100644
--- a/lib/Shipwright/Test.pm
+++ b/lib/Shipwright/Test.pm
@@ -9,9 +9,11 @@ use File::Temp qw/tempdir/;
 use IPC::Cmd qw/can_run/;
 use File::Spec::Functions qw/catfile catdir/;
 use Shipwright::Util;
+use Cwd 'getcwd';
 
 our @EXPORT =
-  qw/has_svk has_svn skip_svk skip_svn create_fs_repo create_svk_repo create_svn_repo devel_cover_enabled test_cmd/;
+  qw/has_svk has_svn skip_svk skip_svn create_fs_repo create_svk_repo
+  create_svn_repo devel_cover_enabled test_cmd skip_git create_git_repo/;
 
 =head1 NAME
 
@@ -61,6 +63,19 @@ sub has_svn {
     return;
 }
 
+=head2 has_git
+
+check to see if we have git or not
+
+=cut
+
+sub has_git {
+    if (   can_run( $ENV{'SHIPWRIGHT_GIT'} ) ) {
+        return 1;
+    }
+    return;
+}
+
 =head2 skip_svn
 
 if skip svn when test.
@@ -87,6 +102,20 @@ sub skip_svk {
     return 1;
 }
 
+=head2 skip_git
+
+if skip git when test.
+skip test git unless env SHIPWRIGHT_TEST_GIT is set to true and
+the system has git
+
+=cut
+
+sub skip_git {
+    return if $ENV{'SHIPWRIGHT_TEST_GIT'} && has_git();
+    return 1;
+}
+
+
 =head2 create_fs_repo 
 
 create a repo for fs
@@ -97,6 +126,21 @@ sub create_fs_repo {
     return tempdir( 'shipwright_test_fs_XXXXXX', CLEANUP => 1, TMPDIR => 1 );
 }
 
+=head2 create_fs_repo 
+
+create a repo for git
+
+=cut
+
+sub create_git_repo {
+    my $dir = tempdir( 'shipwright_test_git_XXXXXX', CLEANUP => 1, TMPDIR => 1 );
+    my $cwd = getcwd();
+    chdir $dir;
+    Shipwright::Util->run( [$ENV{'SHIPWRIGHT_GIT'}, 'init' ] );
+    chdir $cwd;
+    return 'file://' . catdir( $dir, '.git' );
+}
+
 =head2 create_svk_repo 
 
 create a repo for svk, will set $ENV{SVKROOT} accrodingly.
diff --git a/lib/Shipwright/Util.pm b/lib/Shipwright/Util.pm
index dec1318..58b0812 100644
--- a/lib/Shipwright/Util.pm
+++ b/lib/Shipwright/Util.pm
@@ -55,20 +55,21 @@ sub run {
     my $cmd            = shift;
     my $ignore_failure = shift;
 
-    my $log = Log::Log4perl->get_logger('Shipwright::Util');
+    my $log; # = Log::Log4perl->get_logger('Shipwright::Util');
 
     my ( $out, $err );
-    $log->info( "run cmd: " . join ' ', @$cmd );
+    $log->info( "run cmd: " . join ' ', @$cmd ) if $log;
     Shipwright::Util->select('null');
     run3( $cmd, \*STDIN, \$out, \$err );
     Shipwright::Util->select('stdout');
 
-    $log->debug("run output:\n$out") if $out;
-    $log->error("run err:\n$err")   if $err;
+    $log->debug("run output:\n$out") if $out && $log;
+    $log->error("run err:\n$err")   if $err && $log;
 
     if ($?) {
         $log->error(
-            'failed to run ' . join( ' ', @$cmd ) . " with exit number $?" );
+            'failed to run ' . join( ' ', @$cmd ) . " with exit number $?" )
+            if $log;
         unless ($ignore_failure) {
             confess <<"EOF";
 something wrong when execute @$cmd: $?
diff --git a/t/71.script_cmds.t b/t/71.script_cmds.t
index 07aba95..0e353f0 100644
--- a/t/71.script_cmds.t
+++ b/t/71.script_cmds.t
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 106;
+use Test::More tests => 140;
 
 use Shipwright;
 use Shipwright::Test;
@@ -15,7 +15,6 @@ Shipwright::Test->init;
 
 my $install_base = catdir( tmpdir(), 'vessel_71_scripts_cmds' );
 my $build_base   = catdir( tmpdir(), 'shipwright_build_71_scripts_cmds' );
-
 {
 
     # fs backend
@@ -23,6 +22,12 @@ my $build_base   = catdir( tmpdir(), 'shipwright_build_71_scripts_cmds' );
 }
 
 SKIP: {
+    skip "git: no git found or env SHIPWRIGHT_TEST_GIT not set", 34
+      if skip_git();
+    start_test( 'git:' . create_git_repo() );
+}
+
+SKIP: {
     skip "svn: no svn found or env SHIPWRIGHT_TEST_SVN not set", 36
       if skip_svn();
 

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list