File Coverage

lib/Rex/Commands/SCM.pm
Criterion Covered Total %
statement 36 38 94.7
branch 4 8 50.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 50 56 89.2


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::Commands::SCM - Sourcecontrol for Subversion and Git.
8              
9             =head1 DESCRIPTION
10              
11             With this module you can checkout subversion and git repositories.
12              
13             Version <= 1.0: All these functions will not be reported.
14              
15             All these functions are not idempotent.
16              
17             =head1 SYNOPSIS
18              
19             use Rex::Commands::SCM;
20              
21             set repository => "myrepo",
22             url => 'git@foo.bar:myrepo.git';
23              
24             set repository => "myrepo2",
25             url => "https://foo.bar/myrepo",
26             type => "subversion",
27             username => "myuser",
28             password => "mypass";
29              
30             task "checkout", sub {
31             checkout "myrepo";
32              
33             checkout "myrepo",
34             path => "webapp";
35              
36             checkout "myrepo",
37             path => "webapp",
38             branch => 1.6; # branch only for git
39              
40             # For Git only, will replay any local commits on top of pulled commits
41             checkout "myrepo",
42             path => "script_dir",
43             rebase => TRUE;
44              
45             checkout "myrepo2";
46             };
47              
48              
49             =head1 EXPORTED FUNCTIONS
50              
51             =cut
52              
53             package Rex::Commands::SCM;
54              
55 2     2   28 use v5.12.5;
  2         7  
56 2     2   10 use warnings;
  2         5  
  2         96  
57              
58             our $VERSION = '1.14.2.2'; # TRIAL VERSION
59              
60 2     2   14 use Rex::Logger;
  2         4  
  2         12  
61 2     2   48 use Rex::Config;
  2         6  
  2         13  
62              
63             require Rex::Exporter;
64 2     2   13 use base qw(Rex::Exporter);
  2         4  
  2         175  
65 2     2   15 use vars qw(@EXPORT %REPOS);
  2         4  
  2         790  
66             @EXPORT = qw(checkout);
67              
68             Rex::Config->register_set_handler(
69             "repository" => sub {
70             my ( $name, %option ) = @_;
71             $REPOS{$name} = \%option;
72             }
73             );
74              
75             =head2 checkout($name, %data);
76              
77             With this function you can checkout a repository defined with I. See Synopsis.
78              
79             =cut
80              
81             sub checkout {
82 2     2 1 2738 my ( $name, %data ) = @_;
83              
84 2 50       48 my $type = $REPOS{"$name"}->{"type"} ? $REPOS{$name}->{"type"} : "git";
85 2         26 my $class = "Rex::SCM::\u$type";
86              
87 2 50       28 my $co_to = exists $data{"path"} ? $data{"path"} : "";
88              
89 2 50       20 if ( $data{"path"} ) {
90 2         18 $data{"path"} = undef;
91 2         9 delete $data{"path"};
92             }
93              
94 2     1   618 eval "use $class;";
  1     1   48  
  1         6  
  1         15  
  1         30  
  1         10  
  1         69  
95 2 50       64 if ($@) {
96 0         0 Rex::Logger::info( "Error loading SCM: $@\n", "warn" );
97 0         0 die("Error loading SCM: $@");
98             }
99              
100 2         25 my $scm = $class->new;
101              
102 2         18 my $repo = Rex::Config->get("repository");
103 2         20 Rex::Logger::debug("Checking out $repo -> $co_to");
104 2         19 $scm->checkout( $REPOS{$name}, $co_to, \%data );
105             }
106              
107             1;