| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::SCM::Digest::SCM::Git; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 66455 | use strict; | 
|  | 8 |  |  |  |  | 29 |  | 
|  | 8 |  |  |  |  | 205 |  | 
| 4 | 8 |  |  | 8 |  | 39 | use warnings; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 200 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 8 |  |  | 8 |  | 301 | use App::SCM::Digest::Utils qw(system_ad); | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 388 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 8 |  |  | 8 |  | 2276 | use autodie; | 
|  | 8 |  |  |  |  | 92768 |  | 
|  | 8 |  |  |  |  | 40 |  | 
| 9 | 8 |  |  | 8 |  | 50626 | use List::Util qw(first); | 
|  | 8 |  |  |  |  | 21 |  | 
|  | 8 |  |  |  |  | 9218 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new | 
| 12 |  |  |  |  |  |  | { | 
| 13 | 20 |  |  | 20 | 0 | 8489 | my ($class) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 20 |  |  |  |  | 102340 | my $res = system("git --version >/dev/null"); | 
| 16 | 20 | 50 |  |  |  | 360 | if ($res != 0) { | 
| 17 | 0 |  |  |  |  | 0 | die "Unable to find git executable."; | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 20 |  |  |  |  | 190 | my $self = {}; | 
| 21 | 20 |  |  |  |  | 141 | bless $self, $class; | 
| 22 | 20 |  |  |  |  | 592 | return $self; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub clone | 
| 26 |  |  |  |  |  |  | { | 
| 27 | 10 |  |  | 10 | 0 | 117 | my ($self, $url, $name) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 10 |  |  |  |  | 298 | my $res = system_ad("git clone $url $name"); | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 2 |  |  |  |  | 3435 | return 1; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub open_repository | 
| 35 |  |  |  |  |  |  | { | 
| 36 | 7 |  |  | 7 | 0 | 613 | my ($self, $path) = @_; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 7 |  |  |  |  | 76 | chdir $path; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 4 |  |  |  |  | 2360 | return 1; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub is_usable | 
| 44 |  |  |  |  |  |  | { | 
| 45 | 2 |  |  | 2 | 0 | 7 | my ($self) = @_; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 2 |  |  |  |  | 6368 | my @branches = `git branch -l`; | 
| 48 | 2 | 50 |  |  |  | 115 | return (@branches ? 1 : 0); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub prune | 
| 52 |  |  |  |  |  |  | { | 
| 53 | 8 |  |  | 8 | 0 | 21 | my ($self) = @_; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 8 |  | 50 |  |  | 43 | my $original_branch = $self->branch_name() || ''; | 
| 56 | 8 |  |  |  |  | 44 | my $current_branch = $original_branch; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 8 |  |  |  |  | 57238 | my @lines = `git remote prune origin`; | 
| 59 | 8 |  |  |  |  | 135 | for my $line (@lines) { | 
| 60 | 0 |  |  |  |  | 0 | chomp $line; | 
| 61 | 0 | 0 |  |  |  | 0 | if (my ($branch) = ($line =~ /^ \* \[pruned\] origin\/(.*)$/)) { | 
| 62 | 0 | 0 |  |  |  | 0 | if ($branch eq $current_branch) { | 
| 63 | 0 |  |  |  |  | 0 | my @branches = @{$self->branches()}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 64 |  |  |  |  |  |  | my $other_branch = | 
| 65 | 0 |  |  | 0 |  | 0 | first { $_->[0] ne $current_branch } | 
| 66 | 0 |  |  |  |  | 0 | @branches; | 
| 67 | 0 |  |  |  |  | 0 | $self->checkout($other_branch->[0]); | 
| 68 | 0 |  |  |  |  | 0 | $current_branch = $other_branch->[0]; | 
| 69 |  |  |  |  |  |  | } | 
| 70 | 0 |  |  |  |  | 0 | system_ad("git branch -D $branch"); | 
| 71 | 0 | 0 |  |  |  | 0 | if ($branch eq $original_branch) { | 
| 72 | 0 |  |  |  |  | 0 | $original_branch = undef; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 8 | 50 |  |  |  | 67 | if ($original_branch) { | 
| 77 | 8 |  |  |  |  | 106 | $self->checkout($original_branch); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 8 |  |  |  |  | 46 | return 1; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub pull | 
| 84 |  |  |  |  |  |  | { | 
| 85 | 8 |  |  | 8 | 0 | 29 | my ($self) = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 8 |  |  |  |  | 58 | $self->prune(); | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 8 |  |  |  |  | 58 | system_ad("git pull -X theirs"); | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 8 |  |  |  |  | 9364 | return 1; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub branches | 
| 95 |  |  |  |  |  |  | { | 
| 96 | 5 |  |  | 5 | 0 | 2755 | my ($self) = @_; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | my @branches = | 
| 99 | 8 |  |  |  |  | 63 | map  { s/^\s+//; s/^origin\///; s/\s+$//; $_ } | 
|  | 8 |  |  |  |  | 48 |  | 
|  | 8 |  |  |  |  | 33 |  | 
|  | 8 |  |  |  |  | 38 |  | 
| 100 | 12 |  |  |  |  | 75 | grep { !/ -> / } | 
| 101 | 5 |  |  |  |  | 17205 | map  { chomp; $_ } | 
|  | 12 |  |  |  |  | 59 |  | 
|  | 12 |  |  |  |  | 44 |  | 
| 102 |  |  |  |  |  |  | `git branch -r`; | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 5 |  |  |  |  | 28 | my @results; | 
| 105 | 5 |  |  |  |  | 45 | my $current_branch = $self->branch_name(); | 
| 106 | 5 |  |  |  |  | 116 | for my $branch (@branches) { | 
| 107 | 8 |  |  |  |  | 95 | $self->checkout($branch); | 
| 108 | 8 |  |  |  |  | 58 | $self->pull(); | 
| 109 | 8 |  |  |  |  | 48430 | my $commit = `git log -1 --format="%H" $branch`; | 
| 110 | 8 |  |  |  |  | 94 | chomp $commit; | 
| 111 | 8 |  |  |  |  | 123 | push @results, [ $branch => $commit ]; | 
| 112 |  |  |  |  |  |  | } | 
| 113 | 5 | 100 |  |  |  | 43 | if ($current_branch ne 'HEAD') { | 
| 114 | 4 |  |  |  |  | 44 | $self->checkout($current_branch); | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 5 |  |  |  |  | 155 | return \@results; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | sub branch_name | 
| 121 |  |  |  |  |  |  | { | 
| 122 | 16 |  |  | 16 | 0 | 963 | my ($self) = @_; | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | # todo: This prints a warning if there are no branches; there is | 
| 125 |  |  |  |  |  |  | # probably a better way of dealing with this problem. | 
| 126 | 16 |  |  |  |  | 69362 | my $branch = `git rev-parse --abbrev-ref HEAD 2>/dev/null`; | 
| 127 | 16 |  |  |  |  | 180 | chomp $branch; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 16 |  |  |  |  | 451 | return $branch; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | sub checkout | 
| 133 |  |  |  |  |  |  | { | 
| 134 | 24 |  |  | 24 | 0 | 160 | my ($self, $branch) = @_; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 24 |  |  |  |  | 431 | system_ad("git checkout $branch"); | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 24 |  |  |  |  | 140983 | return 1; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub commits_from | 
| 142 |  |  |  |  |  |  | { | 
| 143 | 3 |  |  | 3 | 0 | 1136 | my ($self, $branch, $from) = @_; | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 3 |  |  |  |  | 26 | $self->checkout($branch); | 
| 146 | 3 |  |  |  |  | 15796 | my @new_commits = reverse map { chomp; $_ } `git rev-list $from..HEAD`; | 
|  | 3 |  |  |  |  | 22 |  | 
|  | 3 |  |  |  |  | 20 |  | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 3 |  |  |  |  | 75 | return \@new_commits; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub has | 
| 152 |  |  |  |  |  |  | { | 
| 153 | 2 |  |  | 2 | 0 | 1881 | my ($self, $id) = @_; | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 2 |  |  |  |  | 11778 | my $res = system("git show -s $id >/dev/null 2>&1"); | 
| 156 |  |  |  |  |  |  |  | 
| 157 | 2 |  |  |  |  | 92 | return ($res == 0); | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | sub show | 
| 161 |  |  |  |  |  |  | { | 
| 162 | 1 |  |  | 1 | 0 | 6 | my ($self, $id) = @_; | 
| 163 |  |  |  |  |  |  |  | 
| 164 | 1 |  |  |  |  | 5016 | my @data = `git show -s $id`; | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 1 |  |  |  |  | 30 | return \@data; | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | sub show_all | 
| 170 |  |  |  |  |  |  | { | 
| 171 | 1 |  |  | 1 | 0 | 945 | my ($self, $id) = @_; | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 1 |  |  |  |  | 5757 | my @data = (`git show $id`); | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 1 |  |  |  |  | 28 | return \@data; | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | 1; | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | __END__ | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =head1 NAME | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | App::SCM::Digest::SCM::Git | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | Git L<App::SCM::Digest::SCM> implementation. | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =cut |