| 1 | package Git::LowLevel::Reference; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: class representing a reference in a git repository | ||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use MooseX::Privacy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Git::LowLevel::Tree; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | has 'repository' => (is => 'ro', isa => 'Git::LowLevel', required => 1); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | has 'reference' => (is => 'ro', isa => 'Str', required=>1); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | has '_hash' => (is => 'rw', isa => 'Str', default => "empty", traits => [qw/Private/]); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | has 'tree' => (is => 'rw', isa => 'Git::LowLevel::Tree'); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub exist | ||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||
| 20 | 13 | ▶ | my $self =shift; | ||||||||||||||||||||||||||
|
Subroutine
exist: called CC=3 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 13 | my $help = readpipe($self->repository->gitcmd() . " show-ref --dereference"); | |||||||||||||||||||||||||||
| 23 | 13 | my @lines = split /\n/,$help; | |||||||||||||||||||||||||||
| 24 | 13 | for my $line (@lines) | |||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 0 | $line =~/^([0-9a-fA-F]+)\s(.+)$/; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | if ($2 eq $self->reference) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 0 | $self->_hash($1); | |||||||||||||||||||||||||||
| 30 | 0 | return 1; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 13 | return 0; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub hash | ||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
hash: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | die("reference does not exist") unless $self->exist(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | return $self->_hash; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub getTree | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
getTree: called CC=6 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 7 | my $help; | |||||||||||||||||||||||||||
| 50 | 7 | ▶ | if ($self->exist) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | $help = readpipe($self->repository->gitcmd() . " cat-file -p " . $self->_hash); | |||||||||||||||||||||||||||
| 52 | 0 | my @lines = split /\n/,$help; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($lines[0]=~/^tree\s(.*)$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 0 | $help=$1; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 7 | ▶ | if (!defined($help) || length($help) == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $help or length $help == 0
MC/DC0%0/2
not defined $help or length $help == 0
not defined $help
length $help == 0
Truth table33%1/3
not(A) or B
A= not defined $helpB= length $help == 0
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 7 | $help="empty"; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | if ($help eq "empty") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 7 | $self->tree(Git::LowLevel::Tree->new(repository=> $self->repository, reference=> $self, hash => $help)); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else | ||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | 0 | $self->tree(Git::LowLevel::Tree->new(repository=> $self->repository, reference=> $self, hash => $help, changed=>1)); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 7 | return $self->tree; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub find | ||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $path = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return $self->getTree()->find($path); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub commit | ||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
commit: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 3 | my $comment = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | die("no comment given") unless defined($comment); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 3 | $self->exist; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | return unless defined($self->tree); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 3 | $self->tree->save(); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | my $cmd = "/bin/echo -e \'" . $comment. "\' | " . $self->repository->gitcmd() . " commit-tree " . $self->tree->hash; | |||||||||||||||||||||||||||
| 96 | 3 | ▶ | if (length($self->_hash) == 40) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 0 | $cmd .= " -p " . $self->_hash; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 3 | my $help = readpipe($cmd); | |||||||||||||||||||||||||||
| 101 | 3 | chomp($help); | |||||||||||||||||||||||||||
| 102 | 3 | $self->_hash($help); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 3 | $help = readpipe($self->repository->gitcmd() . " update-ref " . $self->reference . " " . $self->_hash); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | 1; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =pod | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 NAME | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Git::LowLevel::Reference - class representing a reference in a git repository | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 VERSION | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | version 0.3 | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Class representing a Reference object within the Git::LowLevel distribution. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | References in B<git> normally start with C<refs/heads/> e.g. C<refs/heads/master> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | To get a reference to an existing or non existing reference your first require | ||||||||||||||||||||||||||||
| 133 | a L<Git::LowLevel> object representing a B<git> repository. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | my $repository = Git::LowLevel->new(git_dir => "/tmp/repo"); | ||||||||||||||||||||||||||||
| 136 | my $ref = $repository->getReference(); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | The main methods within a reference object are getTree to fetch a tree object of the selected | ||||||||||||||||||||||||||||
| 139 | reference, find to find a specific path within the tree and commit to commit any changes to | ||||||||||||||||||||||||||||
| 140 | the tree and update the reference within B<git>. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head2 repository | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | the repository the rerference lives in | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 reference | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | the reference path e.g. refs/heads/master | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 _hash | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | the hash pointing to by the reference | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 tree | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | the tree of this reference | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 METHODS | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 exist | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | checks if the reference already exists | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 = exist, 0 = not exist | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 hash | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | returns the hash pointing to by the reference | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 getTree | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | return the tree pointing to by the hash | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 find | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | find a path within the tree of the reference | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 commit | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | commit all changes and update the reference | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Dominik Meyer <dmeyer@federationhq.de> | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | This software is Copyright (c) 2018 by Dominik Meyer. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | The GNU General Public License, Version 2, June 1991 | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||