1<?php
2/*
3 *  $Id: 3f33af215009e25b840efa512fc5673cf745b29c $
4 *
5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 *
17 * This software consists of voluntary contributions made by many individuals
18 * and is licensed under the LGPL. For more information please see
19 * <http://phing.info>.
20 */
21
22require_once 'phing/Task.php';
23require_once 'phing/tasks/ext/git/GitBaseTask.php';
24
25/**
26 * Wrapper aroung git-log
27 *
28 * @author Evan Kaufman <evan@digitalflophouse.com>
29 * @author Victor Farazdagi <simple.square@gmail.com>
30 * @version $Id: 3f33af215009e25b840efa512fc5673cf745b29c $
31 * @package phing.tasks.ext.git
32 * @see VersionControl_Git
33 * @since 2.4.5
34 */
35class GitLogTask extends GitBaseTask
36{
37    /**
38     * Generate a diffstat. See --stat of git-log
39     * @var string|boolean
40     */
41    private $stat = false;
42
43    /**
44     * Names + status of changed files. See --name-status of git-log
45     * @var boolean
46     */
47    private $nameStatus = false;
48
49    /**
50     * Number of commits to show. See -<n>|-n|--max-count of git-log
51     * @var integer
52     */
53    private $maxCount;
54
55    /**
56     * Don't show commits with more than one parent. See --no-merges of git-log
57     * @var boolean
58     */
59    private $noMerges = false;
60
61    /**
62     * Commit format. See --format of git-log
63     * @var string
64     */
65    private $format = 'medium';
66
67    /**
68     * Date format. See --date of git-log
69     * @var string
70     */
71    private $date;
72
73    /**
74     * <since> argument to git-log
75     * @var string
76     */
77    private $sinceCommit;
78
79    /**
80     * <until> argument to git-log
81     * @var string
82     */
83    private $untilCommit = 'HEAD';
84
85    /**
86     * <path> arguments to git-log
87     * Accepts one or more paths delimited by PATH_SEPARATOR
88     * @var string
89     */
90    private $paths;
91
92    /**
93     * Property name to set with output value from git-log
94     * @var string
95     */
96    private $outputProperty;
97
98    /**
99     * The main entry point for the task
100     */
101    public function main()
102    {
103        if (null === $this->getRepository()) {
104            throw new BuildException('"repository" is required parameter');
105        }
106
107        $client = $this->getGitClient(false, $this->getRepository());
108        $command = $client->getCommand('log');
109        $command
110            ->setOption('stat', $this->getStat())
111            ->setOption('name-status', $this->isNameStatus())
112            ->setOption('no-merges', $this->isNoMerges())
113            ->setOption('format', $this->getFormat());
114
115        if (null !== $this->getMaxCount()) {
116            $command->setOption('max-count', $this->getMaxCount());
117        }
118
119        if (null !== $this->getDate()) {
120            $command->setOption('date', $this->getDate());
121        }
122
123        if (null !== $this->getSince()) {
124            $command->setOption('since', $this->getSince());
125        }
126        $command->setOption('until', $this->getUntil());
127
128        $command->addDoubleDash(true);
129        if (null !== $this->getPaths()) {
130            $command->addDoubleDash(false);
131            $paths = explode(PATH_SEPARATOR, $this->getPaths());
132            foreach ($paths as $path) {
133                $command->addArgument($path);
134            }
135        }
136
137        $this->log('git-log command: ' . $command->createCommandString(), Project::MSG_INFO);
138
139        try {
140            $output = $command->execute();
141        } catch (Exception $e) {
142            throw new BuildException('Task execution failed', $e);
143        }
144
145        if (null !== $this->outputProperty) {
146            $this->project->setProperty($this->outputProperty, $output);
147        }
148
149        $this->log(
150            sprintf('git-log: commit log for "%s" repository', $this->getRepository()),
151            Project::MSG_INFO);
152        $this->log('git-log output: ' . trim($output), Project::MSG_INFO);
153    }
154
155    public function setStat($stat)
156    {
157        $this->stat = $stat;
158    }
159
160    public function getStat()
161    {
162        return $this->stat;
163    }
164
165    public function setNameStatus($flag)
166    {
167        $this->nameStatus = (boolean)$flag;
168    }
169
170    public function getNameStatus()
171    {
172        return $this->nameStatus;
173    }
174
175    public function isNameStatus()
176    {
177        return $this->getNameStatus();
178    }
179
180    public function setMaxCount($count)
181    {
182        $this->maxCount = (int)$count;
183    }
184
185    public function getMaxCount()
186    {
187        return $this->maxCount;
188    }
189
190    public function setNoMerges($flag)
191    {
192        $this->noMerges = (bool)$flag;
193    }
194
195    public function getNoMerges()
196    {
197        return $this->noMerges;
198    }
199
200    public function isNoMerges()
201    {
202        return $this->getNoMerges();
203    }
204
205    public function setFormat($format)
206    {
207        $this->format = $format;
208    }
209
210    public function getFormat()
211    {
212        return $this->format;
213    }
214
215    public function setDate($date)
216    {
217        $this->date = $date;
218    }
219
220    public function getDate()
221    {
222        return $this->date;
223    }
224
225    public function setSince($since)
226    {
227        $this->sinceCommit = $since;
228    }
229
230    public function getSince()
231    {
232        return $this->sinceCommit;
233    }
234
235    public function setAfter($after)
236    {
237        $this->setSince($after);
238    }
239
240    public function setUntil($until)
241    {
242        $this->untilCommit = $until;
243    }
244
245    public function getUntil()
246    {
247        return $this->untilCommit;
248    }
249
250    public function setBefore($before)
251    {
252        $this->setUntil($before);
253    }
254
255    public function setPaths($paths)
256    {
257        $this->paths = $paths;
258    }
259
260    public function getPaths()
261    {
262        return $this->paths;
263    }
264
265    public function setOutputProperty($prop)
266    {
267        $this->outputProperty = $prop;
268    }
269
270}
271