| 1 | package App::RepoSync::Command::Import; | ||
| 2 | use 5.10.0; | ||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base qw( CLI::Framework::Command ); | ||
| 6 | use File::Path qw(mkpath); | ||
| 7 | use Try::Tiny; | ||
| 8 | use Cwd; | ||
| 9 | use YAML; | ||
| 10 | use App::RepoSync::Export; | ||
| 11 | use App::RepoSync::SystemUtil qw(system_or_die chdir_qx); | ||
| 12 | use Term::ANSIColor; | ||
| 13 | |||
| 14 | sub option_spec { | ||
| 15 | [ 'help|h' => 'show help' ], | ||
| 16 | [ 'verbose|v' => 'be verbose' ], | ||
| 17 | # [ 'db=s' => 'path to SQLite database file' ], | ||
| 18 | } | ||
| 19 | |||
| 20 | sub info { | ||
| 21 | print color('green'), @_ , color('reset') , "\n"; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub warning { | ||
| 25 | print color('red'), @_ , color('reset') , "\n"; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub run { | ||
| 29 | my ($self,$opts,@args) = @_; | ||
| 30 | my ($import_file) = @args; | ||
| 31 | |||
| 32 | my $back_dir = getcwd(); | ||
| 33 | |||
| 34 | my $verbose = $opts->{verbose}; | ||
| 35 | my $svn_opts = $verbose ? qq() : qq(-q); | ||
| 36 | my $git_opts = $verbose ? qq(--verbose) : qq(--quiet); | ||
| 37 | my $git_svn_opts = $verbose ? qq() : qq(--quiet); | ||
| 38 | my $hg_opts = $verbose ? qq() : qq(--quiet); | ||
| 39 | |||
| 40 | my $data = YAML::LoadFile $import_file; | ||
| 41 | |||
| 42 | info "importing @{[ scalar @{ $data->{repos} } ]} repositories."; | ||
| 43 | |||
| 44 | for my $repo ( @{ $data->{repos} } ) { | ||
| 45 | given ($repo->{type} ) { | ||
| 46 | when('svn') { | ||
| 47 | my $path = $repo->{path}; | ||
| 48 | my $url = $repo->{url}; | ||
| 49 | if ( -e $path ) { | ||
| 50 | info "svn: updating $path from $url"; | ||
| 51 | |||
| 52 | system_or_die("svn update $svn_opts --trust-server-cert --non-interactive $path", | ||
| 53 | "svn update"); | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | info "svn: checking out $url into $path"; | ||
| 57 | system_or_die("svn checkout $svn_opts --trust-server-cert --non-interactive $url $path", | ||
| 58 | "svn checkout"); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | when('git') { | ||
| 62 | my $path = $repo->{path}; | ||
| 63 | my $url = $repo->{url}; | ||
| 64 | my %remotes = %{ $repo->{remotes} }; | ||
| 65 | if( -e $path ) { | ||
| 66 | info "git: updating $path"; | ||
| 67 | info "git: remote update and prune"; | ||
| 68 | |||
| 69 | system_or_die("git remote update --prune",'git remote update',$path); | ||
| 70 | |||
| 71 | # should we update current working copy ? | ||
| 72 | my $dirty = chdir_qx("git diff",$path); | ||
| 73 | if( $dirty ) { | ||
| 74 | warning "$path (dirty)"; | ||
| 75 | next; | ||
| 76 | } | ||
| 77 | |||
| 78 | info "git: pulling $path"; | ||
| 79 | for my $remote ( values %remotes ) { | ||
| 80 | system_or_die("git pull $git_opts $remote HEAD","git pull",$path); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | info "git: cloning $url into $path"; | ||
| 85 | system_or_die("git clone $git_opts $url $path","git clone"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | when('git-svn') { | ||
| 89 | my $path = $repo->{path}; | ||
| 90 | my $url = $repo->{url}; | ||
| 91 | |||
| 92 | if( -e $path ) { | ||
| 93 | info "git-svn: updating $path"; | ||
| 94 | system_or_die("git svn rebase $git_svn_opts --fetch-all"); | ||
| 95 | } else { | ||
| 96 | info "git-svn: checking out $url into $path"; | ||
| 97 | system_or_die("git svn clone $git_svn_opts $url $path","checkout svn through git-svn"); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | when('hg') { | ||
| 101 | my $path = $repo->{path}; | ||
| 102 | my $url = $repo->{url}; | ||
| 103 | |||
| 104 | if( -e $path ) { | ||
| 105 | chdir $path; | ||
| 106 | my $dirty = qx(hg diff); | ||
| 107 | chdir $back_dir; | ||
| 108 | next if $dirty; | ||
| 109 | |||
| 110 | info "hg: updating $path"; | ||
| 111 | system_or_die("hg pull","hg pull $hg_opts",$path); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | info "hg: checking out $url into $path"; | ||
| 115 | system_or_die("hg clone $hg_opts $url $path","hg clone"); | ||
| 116 | } | ||
| 117 | |||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | info "done. @{[ scalar @{ $data->{repos} } ]} repositories imported."; | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; |