| 1 | 3 | ▶ | use strictures 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | |||||||||||||
| 3 | package Mojito::Page::Git; | ||||||||||||
| 4 | 3 | $Mojito::Page::Git::VERSION = '0.25'; | |||||||||||
| 5 | 3 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use Git::Wrapper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use IO::File; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use Mojito::Auth; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 3 | ▶ | use Data::Dumper::Concise; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 3 | with('Mojito::Role::DB'); | |||||||||||
| 15 | 3 | with('Mojito::Role::Config'); | |||||||||||
| 16 | |||||||||||||
| 17 | =head1 Name | ||||||||||||
| 18 | |||||||||||||
| 19 | Mojito::Page::Git - the git backend | ||||||||||||
| 20 | |||||||||||||
| 21 | =cut | ||||||||||||
| 22 | |||||||||||||
| 23 | 3 | has dir => ( | |||||||||||
| 24 | is => 'rw', | ||||||||||||
| 25 | lazy => 1, | ||||||||||||
| 26 | builder => '_build_dir', | ||||||||||||
| 27 | ); | ||||||||||||
| 28 | 0 | ▶ | sub _build_dir { | ||||||||||
|
Subroutine
_build_dir: not called CC=2 SCAR=17.9
| |||||||||||||
| 29 | die "You must have a 'git_repo' path in your conf file. e.g. git_repo => '/user/you/repos/mojito'" | ||||||||||||
| 30 | 0 | ▶ | if !$_[0]->config->{git_repo}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 0 | $_[0]->config->{git_repo}; | |||||||||||
| 32 | } | ||||||||||||
| 33 | 3 | has git => ( | |||||||||||
| 34 | is => 'ro', | ||||||||||||
| 35 | lazy => 1, | ||||||||||||
| 36 | builder => '_build_git', | ||||||||||||
| 37 | ); | ||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | sub _build_git { | ||||||||||
|
Subroutine
_build_git: not called CC=1 SCAR=6.9
| |||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||
| 41 | 0 | my $git = Git::Wrapper->new( $self->dir ); | |||||||||||
| 42 | 0 | $git->init; | |||||||||||
| 43 | 0 | return $git; | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | =head1 Methods | ||||||||||||
| 47 | |||||||||||||
| 48 | =head2 commit_page | ||||||||||||
| 49 | |||||||||||||
| 50 | Commit a page revision to the git repository. | ||||||||||||
| 51 | |||||||||||||
| 52 | =cut | ||||||||||||
| 53 | |||||||||||||
| 54 | 0 | ▶ | sub commit_page { | ||||||||||
|
Subroutine
commit_page: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||
| 55 | 0 | my ( $self, $page_struct, $params ) = @_; | |||||||||||
| 56 | |||||||||||||
| 57 | 0 | my $page_id = $params->{id}; | |||||||||||
| 58 | 0 | ▶ | my $file = File::Spec->catfile( $self->dir, $page_id ) | ||||||||||
|
Condition0%0/2
'File::Spec'->catfile($self->dir, $page_id) || die(q[Can't ->catfile on dir], $self->dir, " and page_id $page_id $@")
MC/DC0%0/1
'File::Spec'->catfile($self->dir, $page_id) || die(q[Can't ->catfile on dir], $self->dir, " and page_id $page_id $@")
'File::Spec'->catfile($self->dir, $page_id)
Truth table0%0/2
A
A= 'File::Spec'->catfile($self->dir, $page_id)
| |||||||||||||
| 59 | || die "Can't ->catfile on dir", $self->dir, " and page_id $page_id $@"; | ||||||||||||
| 60 | 0 | ▶ | my $io = IO::File->new( ">" . $file ) | ||||||||||
|
Condition0%0/2
'IO::File'->new('>' . $file) || die("Can't create new IO::File for file: $file. $@")
MC/DC0%0/1
'IO::File'->new('>' . $file) || die("Can't create new IO::File for file: $file. $@")
'IO::File'->new('>' . $file)
Truth table0%0/2
A
A= 'IO::File'->new('>' . $file)
| |||||||||||||
| 61 | || die "Can't create new IO::File for file: $file. $@"; | ||||||||||||
| 62 | 0 | $io->print( $page_struct->{page_source} ); | |||||||||||
| 63 | |||||||||||||
| 64 | 0 | ▶ | return if !$self->git->status->is_dirty; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | |||||||||||||
| 66 | 0 | $self->git->add( {}, ${page_id} ); | |||||||||||
| 67 | 0 | ▶ | if ( $params->{username} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | ▶ | if ( my $auth = Mojito::Auth->new( config => $self->config, username => $params->{username} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | ▶ | if ( my $user = $auth->get_user ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | my $name = $user->{first_name} . ' ' . $user->{last_name}; | |||||||||||
| 71 | 0 | $self->git->config( 'user.name', $name ); | |||||||||||
| 72 | 0 | $self->git->config( 'user.email', $user->{email} ); | |||||||||||
| 73 | } | ||||||||||||
| 74 | } | ||||||||||||
| 75 | } | ||||||||||||
| 76 | 0 | ▶ | $self->git->commit( { message => $params->{commit_message}||'no commit message' }, $page_id ); | ||||||||||
|
Condition0%0/2
$params->{'commit_message'} || 'no commit message'
MC/DC0%0/1
$params->{'commit_message'} || 'no commit message'
$params->{'commit_message'}
Truth table0%0/2
A
A= $params->{'commit_message'}
| |||||||||||||
| 77 | |||||||||||||
| 78 | 0 | return; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | =head2 rm_page | ||||||||||||
| 82 | |||||||||||||
| 83 | Remove a page from the repo (done when deleting a page from the DB) | ||||||||||||
| 84 | |||||||||||||
| 85 | =cut | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | ▶ | sub rm_page { | ||||||||||
|
Subroutine
rm_page: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 88 | 0 | my ( $self, $page_id ) = @_; | |||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | try { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 91 | 0 | $self->git->rm( {}, ${page_id} ); | |||||||||||
| 92 | 0 | $self->git->commit( { message => "Delete page" }, $page_id ); | |||||||||||
| 93 | } | ||||||||||||
| 94 | 0 | ▶ | catch { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 95 | 0 | warn "Tried to delete page: $page_id, but had problems: $_"; | |||||||||||
| 96 | 0 | }; | |||||||||||
| 97 | |||||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | =head2 diff_page | ||||||||||||
| 101 | |||||||||||||
| 102 | Get the diff between two versions of a page. | ||||||||||||
| 103 | Diff a Page: $m and $n are the number of ^ we'll use from HEAD. | ||||||||||||
| 104 | Example: | ||||||||||||
| 105 | |||||||||||||
| 106 | diff/3/1 would mean git diff HEAD^^^ HEAD^ $page_id | ||||||||||||
| 107 | |||||||||||||
| 108 | =cut | ||||||||||||
| 109 | |||||||||||||
| 110 | 0 | ▶ | sub diff_page { | ||||||||||
|
Subroutine
diff_page: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 111 | 0 | my ( $self, $page_id, $m, $n ) = @_; | |||||||||||
| 112 | |||||||||||||
| 113 | # Defaults | ||||||||||||
| 114 | 0 | ▶ | $m //= 1; | ||||||||||
|
Condition0%0/2
$m //= 1
MC/DC0%0/1
$m //= 1
$m
Truth table0%0/2
A
A= $m
| |||||||||||||
| 115 | 0 | ▶ | $n //= 0; | ||||||||||
|
Condition0%0/2
$n //= 0
MC/DC0%0/1
$n //= 0
$n
Truth table0%0/2
A
A= $n
| |||||||||||||
| 116 | 0 | my $base = ('^') x $m; | |||||||||||
| 117 | 0 | my $place = ('^') x $n; | |||||||||||
| 118 | 0 | my @diff = $self->git->diff( {}, "HEAD${base}..HEAD${place}", ${page_id} ); | |||||||||||
| 119 | 0 | my $diff = join "\n", @diff; | |||||||||||
| 120 | |||||||||||||
| 121 | 0 | return $diff; | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | =head2 search_word | ||||||||||||
| 125 | |||||||||||||
| 126 | Search for a word using git grep and return the list of matching document ids. | ||||||||||||
| 127 | NOTE: A document can be returned more than once so we'll make hash of documents | ||||||||||||
| 128 | with the count of how many times they matched. | ||||||||||||
| 129 | |||||||||||||
| 130 | =cut | ||||||||||||
| 131 | |||||||||||||
| 132 | 0 | ▶ | sub search_word { | ||||||||||
|
Subroutine
search_word: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 133 | 0 | my ( $self, $search_word ) = ( shift, shift ); | |||||||||||
| 134 | |||||||||||||
| 135 | # warn "** Searching on $search_word"; | ||||||||||||
| 136 | 0 | my @search_hits; | |||||||||||
| 137 | 0 | my $no_hits = 0; | |||||||||||
| 138 | 0 | ▶ | try { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 139 | 0 | @search_hits = $self->git->grep( { 'ignore_case' => 1 }, $search_word ); | |||||||||||
| 140 | } | ||||||||||||
| 141 | 0 | ▶ | catch { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 142 | 0 | $no_hits = 1; | |||||||||||
| 143 | 0 | }; | |||||||||||
| 144 | 0 | ▶ | return if $no_hits; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | |||||||||||||
| 146 | 0 | my @page_ids = map { my ($file) = $_ =~ /^(\w+)\:/; $file; } @search_hits; | |||||||||||
| 147 | 0 | my %hit_hash = (); | |||||||||||
| 148 | 0 | %hit_hash = map { $_ => ++$hit_hash{$_} } @page_ids; | |||||||||||
| 149 | 0 | return \%hit_hash; | |||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | =head2 get_author_for | ||||||||||||
| 153 | |||||||||||||
| 154 | Given a page id find the author of the last commit. | ||||||||||||
| 155 | |||||||||||||
| 156 | =cut | ||||||||||||
| 157 | |||||||||||||
| 158 | 0 | ▶ | sub get_author_for { | ||||||||||
|
Subroutine
get_author_for: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 159 | 0 | my ($self, $page_id) = (shift, shift); | |||||||||||
| 160 | |||||||||||||
| 161 | 0 | my $author = "Author Unknown"; | |||||||||||
| 162 | 0 | ▶ | try { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 163 | 0 | my @log = $self->git->log( {}, ${page_id} ); | |||||||||||
| 164 | 0 | $author = $log[0]->author; | |||||||||||
| 165 | 0 | $author =~ s/\s+\<.*\>//; | |||||||||||
| 166 | } | ||||||||||||
| 167 | 0 | ▶ | catch { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 168 | 0 | warn "Could not git log for page: ${page_id}"; | |||||||||||
| 169 | 0 | }; | |||||||||||
| 170 | |||||||||||||
| 171 | 0 | return $author; | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 3 | 1 | |||||||||||