Analizo/Batch/Job/Git.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 86.3% 19 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 27 / 27 Podpod 5.8% 1 / 17 totaltotal 89.2% 166 / 186 CC 14 SCAR 7.7
CC
14
Cov
97%
_calculate_previous_relevant
5.0
files
4.0
relevant
3.0
CRAP 2.2
« Analizo/Extractor.pm ↑ Summary Analizo/GlobalMetrics.pm »
1 package Analizo::Batch::Job::Git;
2  
3 11 ▶use parent 'Analizo::Batch::Job';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Digest::SHA qw/ sha1_hex /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use File::Copy::Recursive qw(dircopy);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use File::Path qw(remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 85 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 85 my ($class, $directory, $id, $data) = @_;
13 85 $class->SUPER::new(directory => $directory, actual_directory => $directory, id => $id, data => $data);
14 }
15  
16 83 ▶sub batch($$) {
Subroutine batch: called CC=2 SCAR=7.0
Pod undocumented
17 83 my ($self, $batch) = @_;
18 83 ▶ if ($batch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($batch) 83 0 83
19 83 ▶ $self->{finder} = sub { $batch->find($_[0]); };
Subroutine __ANON__: called CC=1 SCAR=0.0
20 83 $batch->share_filters_with($self);
21 }
22 83 return undef;
23 }
24  
25 1 ▶sub parallel_prepare {
Subroutine parallel_prepare: called CC=1 SCAR=0.0
Pod undocumented
26 1 my ($self) = @_;
27 1 $self->{actual_directory} = _create_work_directory($self->directory);
28 }
29  
30 1 ▶sub parallel_cleanup {
Subroutine parallel_cleanup: called CC=1 SCAR=0.0
Pod undocumented
31 1 my ($self) = @_;
32 1 my $actual_directory = _create_work_directory($self->directory);
33 1 remove_tree($actual_directory);
34 }
35  
36 2 ▶sub _create_work_directory {
Subroutine _create_work_directory: called CC=2 SCAR=6.9
37 2 my ($original_dir) = @_;
38 2 my $basename = 'analizo.' . $$ . '.' . sha1_hex(abs_path($original_dir));
39 2 my $newdir = File::Spec->catfile(File::Spec->tmpdir(), $basename);
40 2 ▶ if (! -d $newdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $newdir) 1 1 2
41 # Assume that the same directory may have been created before by the same
42 # process.
43 1 dircopy($original_dir, $newdir);
44 }
45 2 return $newdir;
46 }
47  
48 2 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod undocumented
49 2 my ($self) = @_;
50 # change directory
51 2 $self->{oldcwd} = getcwd();
52 2 chdir($self->{actual_directory});
53 # checkout
54 2 $self->{old_branch} = git_current_branch();
55 2 $self->git_checkout($self->id);
56 }
57  
58 2 ▶sub cleanup {
Subroutine cleanup: called CC=1 SCAR=0.0
Pod undocumented
59 2 my ($self) = @_;
60 # undo checkout
61 2 $self->git_checkout($self->{old_branch});
62 2 delete($self->{old_branch});
63 # undo directory change
64 2 chdir($self->{oldcwd});
65 2 delete($self->{oldcwd});
66 }
67  
68 142 ▶sub relevant {
Subroutine relevant: called CC=3 SCAR=11.0
Pod undocumented
69 142 my ($self) = @_;
70 142 for my $file (keys(%{$self->changed_files})) {
71 93 ▶ if ($self->filename_matches_filters($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filename_matches_filters($file)) 93 0 93
72 93 return 1;
73 }
74 }
75 49 return 0;
76 }
77  
78 4 ▶sub previous_wanted {
Subroutine previous_wanted: called CC=2 SCAR=6.9
Pod undocumented
79 4 my ($self) = @_;
80 4 ▶ if ($self->is_merge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_merge) { } 1 3 4
81 1 return undef;
82 } else {
83 3 return $self->previous_relevant;
84 }
85 }
86  
87 105 ▶sub previous_relevant {
Subroutine previous_relevant: called CC=2 SCAR=6.9
Pod undocumented
88 105 my ($self) = @_;
89 105 ▶ if (exists($self->{previous_relevant})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"previous_relevant"}) 23 82 105
90 23 return $self->{previous_relevant};
91 }
92 82 my $previous_relevant = $self->_calculate_previous_relevant();
93 82 $self->{previous_relevant} = $previous_relevant;
94 82 return $self->{previous_relevant};
95 }
96  
97 82 ▶sub _calculate_previous_relevant {
Subroutine _calculate_previous_relevant: called CC=5 SCAR=16.1
98 82 my ($self) = @_;
99 82 my $finder = $self->{finder};
100 82 ▶ if ($self->is_first_commit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_first_commit) { } 8 74 82
elsif ($self->is_merge) { } 15 59 74
101 8 return undef;
102 } elsif ($self->is_merge) {
103 15 my @parents = map { &$finder($_) } @{$self->data->{parents}};
104 15 my %grandparents = map { $_ => 1 } (grep { $_} (map { $_->previous_relevant } @parents));
105 15 my @grandparents = keys(%grandparents);
106 15 ▶ if (scalar(@grandparents) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @grandparents == 1) { } 14 1 15
107 14 return $grandparents[0];
108 } else {
109 1 return undef;
110 }
111 } else {
112 59 my $parent = &$finder($self->data->{parents}->[0]);
113 59 ▶ if ($parent->relevant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->relevant) { } 42 17 59
114 42 return $parent->id;
115 } else {
116 17 return $parent->previous_relevant();
117 }
118 }
119 }
120  
121 84 ▶sub is_first_commit {
Subroutine is_first_commit: called CC=1 SCAR=0.0
Pod undocumented
122 84 my ($self) = @_;
123 84 return scalar(@{$self->data->{parents}}) == 0;
124 }
125  
126 80 ▶sub is_merge {
Subroutine is_merge: called CC=1 SCAR=0.0
Pod undocumented
127 80 my ($self) = @_;
128 80 my $ret = scalar(@{$self->data->{parents}}) > 1;
129 80 return $ret;
130 }
131  
132 160 ▶sub changed_files {
Subroutine changed_files: called CC=1 SCAR=0.0
Pod undocumented
133 160 my ($self) = @_;
134 160 return $self->data->{changed_files};
135 }
136  
137 400 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod undocumented
138 400 my ($self) = @_;
139 400 return $self->{data};
140 }
141  
142 2 ▶sub metadata {
Subroutine metadata: called CC=1 SCAR=0.0
Pod undocumented
143 2 my ($self) = @_;
144 2 my $data = $self->data;
145 return [
146 ['previous_commit_id', $self->previous_wanted()],
147 ['author_date', $data->{author_date}],
148 ['author_name', $data->{author_name}],
149 ['author_email', $data->{author_email}],
150 2 ['changed_files', $data->{changed_files}],
151 ['files', $self->files()],
152 ];
153 }
154  
155 2 ▶sub git_checkout {
Subroutine git_checkout: called CC=1 SCAR=0.0
Pod undocumented
156 2 my ($self, $commit) = @_;
157 2 system("git checkout --quiet $commit");
158 }
159  
160 3 ▶sub git_current_branch {
Subroutine git_current_branch: called CC=1 SCAR=0.0
Pod undocumented
161 3 my @branches = `git branch`;
162 3 chomp(@branches);
163 3 my @current = grep { /^\*/ } @branches;
164 3 my $current = $current[0];
165 3 $current =~ s/^\*\s*//;
166 3 return $current;
167 }
168  
169 2 ▶sub files {
Subroutine files: called CC=4 SCAR=14.0
Pod undocumented
170 2 my ($self) = @_;
171 2 ▶ if (defined($self->{files})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"files"}) 0 2 2
172 0 return $self->{files};
173 }
174 2 my @files = `cd $self->{directory} && git ls-tree -r $self->{id}`;
175 2 my %files = ();
176 2 foreach my $line (@files) {
177 9 my ($mode, $type, $sha1, $file) = split(/\s+/,$line);
178 9 ▶ if ($self->filename_matches_filters($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filename_matches_filters($file)) 6 3 9
179 6 $files{$file} = $sha1;
180 }
181 }
182 2 $self->{files} = \%files;
183 2 return $self->{files};
184 }
185  
186 11 1;
« Analizo/Extractor.pm ↑ Summary Analizo/GlobalMetrics.pm »