Completed
Push — develop ( 5f7df1...9cb564 )
by Matteo
10s
created

LogCommand::showLog()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.2007
Metric Value
dl 0
loc 35
ccs 21
cts 25
cp 0.84
rs 6.7272
cc 7
eloc 20
nc 32
nop 5
crap 7.2007
1
<?php
2
/**
3
 * GitElephant - An abstraction layer for git written in PHP
4
 * Copyright (C) 2013  Matteo Giachino
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
18
 */
19
20
namespace GitElephant\Command;
21
22
use \GitElephant\Objects\Object;
23
use \GitElephant\Objects\Branch;
24
use \GitElephant\Objects\TreeishInterface;
25
use \GitElephant\Repository;
26
27
/**
28
 * Log command generator
29
 *
30
 * @author Matteo Giachino <[email protected]>
31
 * @author Dhaval Patel <[email protected]>
32
 */
33
class LogCommand extends BaseCommand
34
{
35
    const GIT_LOG = 'log';
36
37
    /**
38
     * constructor
39
     *
40
     * @param \GitElephant\Repository $repo The repository object this command 
41
     *                                      will interact with
42
     */
43 20
    public function __construct(Repository $repo = null)
44
    {
45 20
        parent::__construct($repo);
46 20
    }
47
48
    /**
49
     * Build an object log command
50
     *
51
     * @param \GitElephant\Objects\Object             $obj    the Object to get the log for
52
     * @param \GitElephant\Objects\Branch|string|null $branch the branch to consider
53
     * @param int|null                                $limit  limit to n entries
54
     * @param int|null                                $offset skip n entries
55
     *
56
     * @throws \RuntimeException
57
     * @return string
58
     */
59 2
    public function showObjectLog(Object $obj, $branch = null, $limit = null, $offset = null)
60
    {
61 2
        $subject = null;
62 2
        if (null !== $branch) {
63 1
            if ($branch instanceof Branch) {
64 1
                $subject .= $branch->getName();
65 1
            } else {
66
                $subject .= (string) $branch;
67
            }
68 1
        }
69
70 2
        return $this->showLog($subject, $obj->getFullPath(), $limit, $offset);
71
    }
72
73
    /**
74
     * Build a generic log command
75
     *
76
     * @param \GitElephant\Objects\TreeishInterface|string $ref         the reference to build the log for
77
     * @param string|null                                  $path        the physical path to the tree relative to the
78
     *                                                                  repository root
79
     * @param int|null                                     $limit       limit to n entries
80
     * @param int|null                                     $offset      skip n entries
81
     * @param boolean|false                                $firstParent skip commits brought in to branch by a merge
82
     *
83
     * @throws \RuntimeException
84
     * @return string
85
     */
86 20
    public function showLog($ref, $path = null, $limit = null, $offset = null, $firstParent = false)
87
    {
88 20
        $this->clearAll();
89
90 20
        $this->addCommandName(self::GIT_LOG);
91 20
        $this->addCommandArgument('-s');
92 20
        $this->addCommandArgument('--pretty=raw');
93 20
        $this->addCommandArgument('--no-color');
94
95 20
        if (null !== $limit) {
96 16
            $limit = (int) $limit;
97 16
            $this->addCommandArgument('--max-count=' . $limit);
98 16
        }
99
100 20
        if (null !== $offset) {
101 1
            $offset = (int) $offset;
102 1
            $this->addCommandArgument('--skip=' . $offset);
103 1
        }
104
105 20
        if (true === $firstParent) {
106
            $this->addCommandArgument('--first-parent');
107
        }
108
109 20
        if ($ref instanceof TreeishInterface) {
110
            $ref = $ref->getSha();
111
        }
112
113 20
        if (null !== $path && !empty($path)) {
114 5
            $this->addPath($path);
115 5
        }
116
117 20
        $this->addCommandSubject($ref);
118
119 20
        return $this->getCommand();
120
    }
121
}
122