| 1 | package App::RepoSync::Export; | ||
| 2 | use 5.10.0; | ||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Cwd qw(realpath getcwd); | ||
| 6 | use YAML; | ||
| 7 | use JSON; | ||
| 8 | use File::Spec; | ||
| 9 | use File::Find::Rule; | ||
| 10 | use File::Basename; | ||
| 11 | use Config::Tiny; | ||
| 12 | use App::RepoSync::SystemUtil qw(system_or_die chdir_qx); | ||
| 13 | use Term::ANSIColor; | ||
| 14 | |||
| 15 | sub parse_svn_info { | ||
| 16 | my $cmd = shift || 'svn info'; | ||
| 17 | my $info; | ||
| 18 | unless( $info = qx(LC_ALL=C $cmd 2> /dev/null ) ) { | ||
| 19 | say 'svn: info failed: ' . getcwd; | ||
| 20 | |||
| 21 | # if uprade is needed, | ||
| 22 | my $ret = qx(LC_ALL=C $cmd 2>&1); | ||
| 23 | if ($cmd eq 'svn info' && $ret =~ /svn upgrade/) { | ||
| 24 | say "svn: try upgrading: " . getcwd; | ||
| 25 | system_or_die('svn upgrade 2>&1 > /dev/null'); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | unless( $info = qx(LC_ALL=C $cmd 2> /dev/null ) ) { | ||
| 30 | say "svn: info still failed: " . getcwd; | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | my ($url) = ($info =~ m{URL: (\S+)}); | ||
| 35 | my ($revision) = ($info =~ m{Revision: (\S+)}); | ||
| 36 | return { | ||
| 37 | url => $url, | ||
| 38 | revision => $revision, | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub traverse_dir { | ||
| 43 | my ($dir,$cb) = @_; | ||
| 44 | |||
| 45 | opendir(my $dh, $dir ) || die "can't opendir $dir: $!"; | ||
| 46 | |||
| 47 | my @result; | ||
| 48 | my @dirs = readdir($dh); | ||
| 49 | closedir $dh; | ||
| 50 | for my $subdir ( @dirs ) { | ||
| 51 | my $abspath = File::Spec->join( $dir, $subdir ); | ||
| 52 | next if $subdir eq '.' || $subdir eq '..' ; | ||
| 53 | |||
| 54 | if( -d $abspath ) { | ||
| 55 | my $path = $cb->( $subdir, $dir ); | ||
| 56 | |||
| 57 | next if $path && $path == -1; | ||
| 58 | |||
| 59 | if( $path ) { | ||
| 60 | push @result, $path; | ||
| 61 | } else { | ||
| 62 | push @result, traverse_dir( $abspath , $cb); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | return @result; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub run { | ||
| 70 | my ($class,$export_root) = @_; | ||
| 71 | my $sync_root = getcwd(); | ||
| 72 | |||
| 73 | $export_root = realpath( $export_root ); | ||
| 74 | |||
| 75 | my @repos = traverse_dir $export_root, sub { | ||
| 76 | my ($subdir,$parent) = @_; | ||
| 77 | my $path = File::Spec->join( $parent, $subdir ); | ||
| 78 | my $subpath = substr( $path , length( $sync_root ) + 1 ); | ||
| 79 | |||
| 80 | return if $subdir =~ /^\./; # do not descent | ||
| 81 | return if ! -d $path; | ||
| 82 | if( -d File::Spec->join( $path, '.svn' ) ) { | ||
| 83 | |||
| 84 | chdir $path; | ||
| 85 | my $info = parse_svn_info; | ||
| 86 | unless( $info ) { | ||
| 87 | chdir $sync_root; | ||
| 88 | return -1; | ||
| 89 | } | ||
| 90 | |||
| 91 | my ($name) = basename($path); | ||
| 92 | my ($url) = ($info =~ m{URL: (\S+)}); | ||
| 93 | my ($revision) = ($info =~ m{Revision: (\S+)}); | ||
| 94 | chdir $sync_root; | ||
| 95 | return { | ||
| 96 | type => 'svn', | ||
| 97 | path => $subpath, | ||
| 98 | name => $name, | ||
| 99 | %$info, | ||
| 100 | }; | ||
| 101 | } | ||
| 102 | elsif( -d File::Spec->join( $path , '.git' ) ) { | ||
| 103 | chdir $path; | ||
| 104 | my $remote_string = qx(git remote -v 2> /dev/null); | ||
| 105 | |||
| 106 | unless( $remote_string ) { | ||
| 107 | # try to fetch .git/refs/remotes/git-svn | ||
| 108 | if( -e File::Spec->join( $path, '.git', 'refs','remotes', 'git-svn' ) ) { | ||
| 109 | my $info = parse_svn_info 'git svn info'; | ||
| 110 | chdir $sync_root; | ||
| 111 | return { | ||
| 112 | type => 'git-svn', | ||
| 113 | %$info, | ||
| 114 | }; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | my %remotes = map { | ||
| 119 | m{(\w+)\s+(\S+)\s+}; | ||
| 120 | ($1 => $2); | ||
| 121 | } split /\n/,$remote_string; | ||
| 122 | my $url = $remotes{origin}; | ||
| 123 | chdir $sync_root; | ||
| 124 | return { | ||
| 125 | type => 'git', | ||
| 126 | path => $subpath, | ||
| 127 | remotes => \%remotes, | ||
| 128 | url => $url, | ||
| 129 | }; | ||
| 130 | } | ||
| 131 | elsif( -d File::Spec->join( $path , '.hg' ) ) { | ||
| 132 | chdir $path; | ||
| 133 | |||
| 134 | # try to read .hg/hgrc | ||
| 135 | my $config_path = File::Spec->join( $path, '.hg', 'hgrc' ); | ||
| 136 | my $cfg = Config::Tiny->read($config_path); | ||
| 137 | my $paths = $cfg->{paths}; | ||
| 138 | my $branch = qx(hg branch); | ||
| 139 | chomp( $branch ); | ||
| 140 | chdir $sync_root; | ||
| 141 | return { | ||
| 142 | type => 'hg', | ||
| 143 | path => $subpath, | ||
| 144 | url => $paths->{default} || $paths->{'default-push'}, | ||
| 145 | branch => $branch, | ||
| 146 | remotes => $paths, | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | return; | ||
| 150 | }; | ||
| 151 | |||
| 152 | return @repos; | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; |