FastExport/Stitch.pm

filter: Statementstmt 92.1% 153 / 166 Branchbran 74.1% 43 / 58 Conditioncond 80.0% 24 / 30 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 86.1% 248 / 288 CC 54 SCAR 25.8
CC
54
Cov
87%
_translate_block
18.7
next_block
18.0
stitch
15.1
CRAP 13.1
↑ Summary FastExport.pm »
1 package Git::FastExport::Stitch;
2 3 $Git::FastExport::Stitch::VERSION = '0.108';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Cwd qw( cwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Git::FastExport;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 48 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
13 48 my ( $class, $options, @args ) = @_;
14  
15 # create the object
16 48 my $self = bless {
17  
18 # internal structures
19 mark => 1_000_000, # mark counter in the new repo
20 mark_map => {},
21 commits => {},
22 repo => {},
23 name => {},
24 cache => {},
25  
26 # default options
27 select => 'last',
28  
29 }, $class;
30  
31 # set the options
32 48 for my $key (qw( select )) {
33 48 ▶ $self->{$key} = $options->{$key} if exists $options->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{$key} 42 6 48
34 }
35 croak "Invalid value for 'select' option: '$self->{select}'"
36 48 ▶ if $self->{select} !~ /^(?:first|last|random)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"select"} =~ /^(?:first|last|random)$/ 1 47 48
37  
38 # process the remaining args
39 47 ▶ $self->stitch( splice @args, 0, 2 ) while @args;
Branch: 1 branch
BranchTrueFalseTotal
@args and do { $self->stitch(splice(@args, 0, 2)) } 1 46 47
40  
41 46 return $self;
42 }
43  
44 # add a new repo to stich in
45 66 ▶sub stitch {
Subroutine stitch: called CC=13 SCAR=27.2
Pod documented
46 66 my ( $self, $repo, $dir ) = @_;
47  
48 # $repo is either a Git::Repository object or a valid path
49 my $export = blessed($repo) && $repo->isa('Git::Repository')
50 ? $repo # a Git::Repository object
51 66 ▶ : eval { # assume a path
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($repo) && $repo->isa('Git::Repository') ? : 21 45 66
Condition66%2/3
builtin::blessed($repo) and $repo->isa("Git::Repository")
!ll&&!rl&&r
45021
MC/DC50%1/2
builtin::blessed($repo) and $repo->isa("Git::Repository")
builtin::blessed($repo) $repo->isa("Git::Repository")
Truth table66%2/3
A and B
A= builtin::blessed($repo)B= $repo->isa("Git::Repository")
ABresult
0X0
100
111
52 45 my $r;
53 45 my $orig = cwd;
54  
55 # chdir and create a Git::Repository object there
56 45 ▶ if ( defined $repo ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $repo) { } 45 0 45
57 45 ▶ chdir $repo or croak "Can't chdir to $repo: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $repo or croak("Can't chdir to ${repo}: $!") 44 1 45
58 44 $r = Git::Repository->new();
59 44 ▶ chdir $orig or croak "Can't chdir back to $orig: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $orig or croak("Can't chdir back to ${orig}: $!") 44 0 44
60 }
61 0 else { die "Undefined repository path" }
62 44 $r;
63 };
64 66 ▶ $@ =~ s/ at .*\z//s, croak $@ if !$export;
Branch: 1 branch
BranchTrueFalseTotal
unless $export 1 65 66
65  
66 # do not stich a repo with itself
67 65 $repo = $export->git_dir;
68 65 ▶ croak "Already stitching repository $repo" if exists $self->{repo}{$repo};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"repo"}{$repo} 2 63 65
69  
70 # pick the refs suffix:
71 # use base directory without the .git extension or non ASCII characters
72 63 my @parts = File::Spec->splitdir( ( File::Spec->splitpath( $repo, 1 ) )[1] );
73 63 my $name = pop @parts;
74 63 ▶ $name = pop @parts if $name eq '.git';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq ".git" 63 0 63
75 63 $name =~ s/\.git$//;
76 63 ▶ $dir = $name if not defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 63 0 63
77 63 $name =~ y/-A-Za-z0-9_/-/cs;
78 63 $name =~ s/^-|-$//g;
79  
80 # check if the name is not used already and pick a replacement if it is
81 63 ▶ if ( exists $self->{name}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"name"}{$name}) 0 63 63
82 0 my $suffix = "A";
83 0 ▶ $suffix++ while ( exists $self->{name}{"$name-$suffix"} );
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"name"}{"$name-$suffix"} and do { ++$suffix } 0 0 0
84 0 $name .= "-$suffix";
85 }
86  
87 # git fast-export appeared in git 1.5.4
88 0 ▶ croak "stitch() requires a git version greater or equal to 1.5.4, this is only version ${\$export->version}"
Branch: 1 branch
BranchTrueFalseTotal
if $export->version_lt("1.5.4") 0 63 63
89 if $export->version_lt('1.5.4');
90  
91 # initiate the Git::FastExport stream
92 63 my $stream =
93 $export->command(qw( fast-export --progress=1 --all --date-order ))
94 ->stdout;
95  
96 # set up the internal structures
97 63 $self->{repo}{$repo}{repo} = $repo;
98 63 $self->{repo}{$repo}{dir} = $dir;
99 63 $self->{repo}{$repo}{git} = $export;
100 63 $self->{repo}{$repo}{parser} = Git::FastExport->new($stream);
101 63 $self->{repo}{$repo}{name} = $name;
102 63 $self->{repo}{$repo}{block} = $self->{repo}{$repo}{parser}->next_block();
103 63 $self->_translate_block($repo);
104  
105 63 return $self;
106 }
107  
108 # return the next block in the stitched stream
109 628 ▶sub next_block {
Subroutine next_block: called CC=18 SCAR=28.9
Pod documented
110 628 my ($self) = @_;
111 628 my $repo = $self->{repo};
112  
113 # keep a list of next blocks (per repo)
114 # any undef block means the stream is finished
115 628 delete $repo->{$_} for grep { !defined $repo->{$_}{block} } keys %$repo;
116  
117 # no repo left, we're done
118 628 ▶ return if ! keys %$repo;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$repo 32 596 628
119  
120 # return any non-commit block directly
121 596 ▶ if ( my $next
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = &first(sub { $repo->{$_}{"block"}{"type"} ne "commit"; } , keys %$repo)) 332 264 596
122 973 ▶ = first { $repo->{$_}{block}{type} ne 'commit' } keys %$repo )
Subroutine __ANON__: called CC=1 SCAR=0.0
123 {
124 332 my $block = $repo->{$next}{block};
125 332 $repo->{$next}{block} = $repo->{$next}{parser}->next_block();
126 332 $self->_translate_block( $next );
127 332 return $block;
128 }
129  
130 # select the oldest available commit
131 264 my ($next) = keys %$repo;
132 $next
133 = $repo->{$next}{block}{committer_date} < $repo->{$_}{block}{committer_date} ? $next : $_
134 264 ▶ for keys %$repo;
Branch: 1 branch
BranchTrueFalseTotal
$repo->{$next}{'block'}{'committer_date'} < $repo->{$_}{'block'}{'committer_date'} ? : 128 370 498
135 264 my $commit = $repo->{$next}{block};
136  
137 # fetch the next block
138 264 $repo->{$next}{block} = $repo->{$next}{parser}->next_block();
139 264 $self->_translate_block( $next );
140  
141 # prepare the attachement algorithm
142 264 $repo = $repo->{$next};
143 264 my $commits = $self->{commits};
144  
145 # first commit in the old repo linked to latest commit in new repo
146 264 ▶ if ( $self->{last} && !$commit->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last"} and not $commit->{"from"}) 30 234 264
Condition100%3/3
$self->{"last"} and not $commit->{"from"}
!ll&&!rl&&r
3220230
MC/DC100%2/2
$self->{"last"} and not $commit->{"from"}
$self->{"last"} not $commit->{"from"}
Truth table100%3/3
A and not(B)
A= $self->{"last"}B= not $commit->{"from"}
ABresult
0X0
100
111
147 30 $commit->{from} = ["from :$self->{last}"];
148 }
149  
150 # update historical information
151 264 my ($id) = $commit->{mark}[0] =~ /:(\d+)/g;
152 264 $self->{last} = $id; # last commit applied
153 264 my $ref = ( split / /, $commit->{header} )[1];
154 my $node = $commits->{$id} = {
155 name => $id,
156 repo => $repo->{repo},
157 ref => $ref,
158 children => [],
159 parents => {},
160 merge => exists $commit->{merge},
161 264 };
162  
163 # mark our original source
164 264 $commit->{header} =~ s/$/-$repo->{name}/;
165  
166 # this commit's parents
167 274 ▶ my @parents = map {/:(\d+)/g} @{ $commit->{from} || [] },
Condition100%2/2
$commit->{'from'} || []
l!l
23232
MC/DC100%1/1
$commit->{'from'} || []
$commit->{'from'}
Truth table100%2/2
A
A= $commit->{'from'}
Aresult
11
00
168 264 ▶ @{ $commit->{merge} || [] };
Condition100%2/2
$commit->{'merge'} || []
l!l
38226
MC/DC100%1/1
$commit->{'merge'} || []
$commit->{'merge'}
Truth table100%2/2
A
A= $commit->{'merge'}
Aresult
11
00
169  
170 # get the reference parent list used by _last_alien_child()
171 264 my $parents = {};
172 264 for my $parent (@parents) {
173 274 ▶ if ( $commits->{$parent}{repo} eq $node->{repo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commits->{$parent}{'repo'} eq $node->{'repo'}) { } 244 30 274
174 244 push @{ $parents->{ $node->{repo} } }, $parent;
175 }
176 else { # record the parents from the other repositories
177 30 for my $repo ( grep $_ ne $node->{repo},
178 30 keys %{ $commits->{$parent}{parents} } )
179 {
180 8 push @{ $parents->{$repo} },
181 8 ▶ @{ $commits->{$parent}{parents}{$repo} || [] };
Condition50%1/2
$commits->{$parent}{'parents'}{$repo} || []
l!l
80
MC/DC0%0/1
$commits->{$parent}{'parents'}{$repo} || []
$commits->{$parent}{'parents'}{$repo}
Truth table50%1/2
A
A= $commits->{$parent}{'parents'}{$repo}
Aresult
11
00
182 }
183 }
184 }
185  
186 # map each parent to its last "alien" commit
187 my %parent_map = map {
188 264 $_ => $self->_last_alien_child( $commits->{$_}, $ref, $parents )->{name}
189 274 } @parents;
190  
191 # map parent marks
192 264 ▶ for ( @{ $commit->{from} || [] }, @{ $commit->{merge} || [] } ) {
Condition100%4/4
$commit->{'from'} || []
l!l
23232
$commit->{'merge'} || []
l!l
38226
MC/DC100%1/1
$commit->{'from'} || []
$commit->{'from'}
MC/DC100%1/1
$commit->{'merge'} || []
$commit->{'merge'}
Truth table100%2/2
A
A= $commit->{'from'}
Aresult
11
00
Truth table100%2/2
A
A= $commit->{'merge'}
Aresult
11
00
193 274 s/:(\d+)/:$parent_map{$1}/g;
194 }
195  
196 # update the parents information
197 264 for my $parent ( map { $commits->{ $parent_map{$_} } } @parents ) {
198 274 push @{ $parent->{children} }, $node->{name};
199 274 push @{ $node->{parents}{ $parent->{repo} } }, $parent->{name};
200 }
201  
202 # dump the commit
203 264 return $commit;
204 }
205  
206 659 ▶sub _translate_block {
Subroutine _translate_block: called CC=12 SCAR=29.3
207 659 my ( $self, $repo ) = @_;
208 659 my $mark_map = $self->{mark_map};
209 659 my $block = $self->{repo}{$repo}{block};
210  
211 # nothing to do
212 659 ▶ return if !defined $block;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $block 62 597 659
213  
214 # mark our original source
215 $block->{header} =~ s/$/-$self->{repo}{$repo}{name}/
216 597 ▶ if $block->{type} =~ /^(?:reset|tag)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $block->{"type"} =~ /^(?:reset|tag)$/ 69 528 597
217  
218 # map to the new mark
219 597 ▶ for ( @{ $block->{mark} || [] } ) {
Condition100%2/2
$block->{'mark'} || []
l!l
264333
MC/DC100%1/1
$block->{'mark'} || []
$block->{'mark'}
Truth table100%2/2
A
A= $block->{'mark'}
Aresult
11
00
220 264 s/:(\d+)/:$self->{mark}/;
221 264 $mark_map->{$repo}{$1} = $self->{mark}++;
222 }
223  
224 # update marks in from & merge
225 597 ▶ for ( @{ $block->{from} || [] }, @{ $block->{merge} || [] } ) {
Condition100%4/4
$block->{'from'} || []
l!l
208389
$block->{'merge'} || []
l!l
38559
MC/DC100%1/1
$block->{'from'} || []
$block->{'from'}
MC/DC100%1/1
$block->{'merge'} || []
$block->{'merge'}
Truth table100%2/2
A
A= $block->{'from'}
Aresult
11
00
Truth table100%2/2
A
A= $block->{'merge'}
Aresult
11
00
226 250 s/:(\d+)/:$mark_map->{$repo}{$1}/g;
227 }
228  
229 # update marks & dir in files
230 597 for ( @{ $block->{files} } ) {
231 0 s/^M (\d+) :(\d+)/M $1 :$mark_map->{$repo}{$2}/;
232 0 my $dir = $self->{repo}{$repo}{dir};
233 0 ▶ if ( defined $dir && $dir ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir and $dir ne "") 0 0 0
Condition0%0/3
defined $dir and $dir ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dir and $dir ne ""
defined $dir $dir ne ""
Truth table0%0/3
A and B
A= defined $dirB= $dir ne ""
ABresult
0X0
100
111
234 0 s!^(M \d+ :\d+) (\"?)(.*)!$1 $2$dir/$3!; # filemodify
235 0 s!^D (\"?)(.*)!D $1$dir/$2!; # filedelete
236  
237 # /!\ quotes may happen - die and fix if needed
238 0 ▶ die "Choked on quoted paths in $repo! Culprit:\n$_\n"
Branch: 1 branch
BranchTrueFalseTotal
if /^[CR] \S+ \S+ / 0 0 0
239 if /^[CR] \S+ \S+ /;
240  
241 # filecopy | filerename
242 0 s!^([CR]) (\"?)(\S+) (\"?)(\S+)!$1 $2$dir/$3 $4$dir/$5!;
243 }
244 }
245 }
246  
247 # find the last child of this node
248 # that has either no child
249 # or a child in our repo
250 # or an alien child that has the same parent list
251 274 ▶sub _last_alien_child {
Subroutine _last_alien_child: called CC=10 SCAR=23.2
252 274 my ( $self, $node, $ref, $parents ) = @_;
253 274 my $commits = $self->{commits};
254  
255 274 my $from = $node->{name};
256 274 my $repo = $node->{repo};
257  
258 274 while (1) {
259  
260 # no children nodes
261 457 ▶ return $node if ( !@{ $node->{children} } );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$node->{"children"};} 222 235 457
262  
263 # some children nodes are local
264 return $node
265 235 ▶ if grep { $commits->{$_}{repo} eq $repo } @{ $node->{children} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$commits->{$_}{"repo"} eq $repo;} @{$$node{"children"};} 52 183 235
266  
267 # all children are alien to us
268 183 my @valid;
269 183 for my $id ( @{ $node->{children} } ) {
270  
271 237 my $peer = $commits->{$id};
272  
273 # parents of $peer in $peer's repo contains
274 # all parents from $parent in $peer's repo
275 237 my %pparents;
276 237 ▶ @{pparents}{ @{ $peer->{parents}{ $peer->{repo} } || [] } } = ();
Condition100%2/2
$peer->{'parents'}{$peer->{'repo'}} || []
l!l
28209
MC/DC100%1/1
$peer->{'parents'}{$peer->{'repo'}} || []
$peer->{'parents'}{$peer->{'repo'}}
Truth table100%2/2
A
A= $peer->{'parents'}{$peer->{'repo'}}
Aresult
11
00
277 next
278 if grep !exists $pparents{$_},
279 237 ▶ @{ $parents->{ $peer->{repo} } };
Branch: 1 branch
BranchTrueFalseTotal
if grep((!exists $pparents{$_}), @{$$parents{$peer->{"repo"}};}) 0 237 237
280  
281 # this child node has a valid parent list
282 237 push @valid, $id;
283 }
284  
285 # compute the commit to attach to, using the requested algorithm
286 183 ▶ if (@valid) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valid) 183 0 183
287 my $node_id = $self->{cache}{"$from $node->{name}"} ||=
288 $self->{select} eq 'last' ? $valid[-1]
289 183 ▶ : $self->{select} eq 'first' ? $valid[0]
Branch: 2 branches
BranchTrueFalseTotal
$self->{'select'} eq 'last' ? : 68 70 138
$self->{'select'} eq 'first' ? : 70 0 70
Condition66%2/3
$self->{"cache"}{qq[$from $node->{"name"}]} ||= $self->{"select"} eq "last" ? $valid[-1] : ($self->{"select"} eq "first" ? $valid[0] : $valid[rand @valid])
l!l&&r!l&&!r
451380
MC/DC0%0/2
$self->{"cache"}{qq[$from $node->{"name"}]} ||= $self->{"select"} eq "last" ? $valid[-1] : ($self->{"select"} eq "first" ? $valid[0] : $valid[rand @valid])
$self->{"cache"}{qq[$from $node->{"name"}]} $self->{"select"} eq "last" ? $valid[-1] : ($self->{"select"} eq "first" ? $valid[0] : $valid[rand @valid])
Truth table66%2/3
A ||= B
A= $self->{"cache"}{qq[$from $node->{"name"}]}B= $self->{"select"} eq "last" ? $valid[-1] : ($self->{"select"} eq "first" ? $valid[0] : $valid[rand @valid])
ABresult
1X1
011
000
290 : $valid[ rand @valid ];
291 183 $node = $commits->{$node_id};
292 }
293 }
294  
295 # return last valid child
296 0 return $node;
297 }
298  
299 3 'progress 1 objects';
300  
301 __END__
↑ Summary FastExport.pm »