FastExport/Stitch.pm

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