1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Wrapper around git-checkout
22
 *
23
 * @author  Victor Farazdagi <simple.square@gmail.com>
24
 * @package phing.tasks.ext.git
25
 * @see     VersionControl_Git
26
 * @since   2.4.3
27
 */
28
class GitCheckoutTask extends GitBaseTask
29
{
30
    /**
31
     * Branch name
32
     *
33
     * @var string
34
     */
35
    private $branchname;
36

37
    /**
38
     * If not HEAD, specify starting point
39
     *
40
     * @var string
41
     */
42
    private $startPoint;
43

44
    /**
45
     * --force, -f key to git-checkout
46
     *
47
     * @var boolean
48
     */
49
    private $force = false;
50

51
    /**
52
     * --quiet, -q key to git-checkout
53
     *
54
     * @var boolean
55
     */
56
    private $quiet = false;
57

58
    /**
59
     * When creating a new branch, set up "upstream" configuration.
60
     * --track key to git-checkout
61
     *
62
     * @var boolean
63
     */
64
    private $track = false;
65

66
    /**
67
     * Do not set up "upstream" configuration
68
     * --no-track key to git-checkout
69
     *
70
     * @var boolean
71
     */
72
    private $noTrack = false;
73

74
    /**
75
     * -b, -B, -m  options to git-checkout
76
     * Respective task options:
77
     * create, forceCreate, merge
78
     *
79
     * @var array
80
     */
81
    private $extraOptions = [
82
        'b' => false,
83
        'B' => false,
84
        'm' => false,
85
    ];
86

87
    /**
88
     * The main entry point for the task
89
     */
90 1
    public function main()
91
    {
92 1
        if (null === $this->getRepository()) {
93 1
            throw new BuildException('"repository" is required parameter');
94
        }
95 1
        if (null === $this->getBranchname()) {
96 1
            throw new BuildException('"branchname" is required parameter');
97
        }
98

99 1
        $client = $this->getGitClient(false, $this->getRepository());
100 1
        $command = $client->getCommand('checkout');
101
        $command
102 1
            ->setOption('no-track', $this->isNoTrack())
103 1
            ->setOption('q', $this->isQuiet())
104 1
            ->setOption('force', $this->isForce())
105 1
            ->setOption('b', $this->isCreate())
106 1
            ->setOption('B', $this->isForceCreate())
107 1
            ->setOption('m', $this->isMerge());
108 1
        if ($this->isNoTrack()) {
109 0
            $command->setOption('track', $this->isTrack());
110
        }
111

112 1
        $command->addArgument($this->getBranchname());
113

114 1
        if (null !== $this->getStartPoint()) {
115 1
            $command->addArgument($this->getStartPoint());
116
        }
117

118 1
        $this->log('git-checkout command: ' . $command->createCommandString(), Project::MSG_INFO);
119

120
        try {
121 1
            $output = $command->execute();
122 1
        } catch (Exception $e) {
123 1
            throw new BuildException('Task execution failed.', $e);
124
        }
125

126 1
        $this->log(
127 1
            sprintf('git-checkout: checkout "%s" repository', $this->getRepository()),
128 1
            Project::MSG_INFO
129
        );
130 1
        $this->log('git-checkout output: ' . str_replace('\'', '', trim($output)), Project::MSG_INFO);
131
    }
132

133
    /**
134
     * @param $branchname
135
     */
136 1
    public function setBranchname($branchname)
137
    {
138 1
        $this->branchname = $branchname;
139
    }
140

141
    /**
142
     * @return string
143
     */
144 1
    public function getBranchname()
145
    {
146 1
        return $this->branchname;
147
    }
148

149
    /**
150
     * @param $startPoint
151
     */
152 1
    public function setStartPoint($startPoint)
153
    {
154 1
        $this->startPoint = $startPoint;
155
    }
156

157
    /**
158
     * @return string
159
     */
160 1
    public function getStartPoint()
161
    {
162 1
        return $this->startPoint;
163
    }
164

165
    /**
166
     * @param $flag
167
     */
168 1
    public function setForce($flag)
169
    {
170 1
        $this->force = $flag;
171
    }
172

173
    /**
174
     * @return bool
175
     */
176 1
    public function getForce()
177
    {
178 1
        return $this->force;
179
    }
180

181
    /**
182
     * @return bool
183
     */
184 1
    public function isForce()
185
    {
186 1
        return $this->getForce();
187
    }
188

189
    /**
190
     * @param $flag
191
     */
192 1
    public function setQuiet($flag)
193
    {
194 1
        $this->quiet = $flag;
195
    }
196

197
    /**
198
     * @return bool
199
     */
200 1
    public function getQuiet()
201
    {
202 1
        return $this->quiet;
203
    }
204

205
    /**
206
     * @return bool
207
     */
208 1
    public function isQuiet()
209
    {
210 1
        return $this->getQuiet();
211
    }
212

213
    /**
214
     * @param $flag
215
     */
216 0
    public function setTrack($flag)
217
    {
218 0
        $this->track = $flag;
219
    }
220

221
    /**
222
     * @return bool
223
     */
224 0
    public function getTrack()
225
    {
226 0
        return $this->track;
227
    }
228

229
    /**
230
     * @return bool
231
     */
232 0
    public function isTrack()
233
    {
234 0
        return $this->getTrack();
235
    }
236

237
    /**
238
     * @param $flag
239
     */
240 0
    public function setNoTrack($flag)
241
    {
242 0
        $this->noTrack = $flag;
243
    }
244

245
    /**
246
     * @return bool
247
     */
248 1
    public function getNoTrack()
249
    {
250 1
        return $this->noTrack;
251
    }
252

253
    /**
254
     * @return bool
255
     */
256 1
    public function isNoTrack()
257
    {
258 1
        return $this->getNoTrack();
259
    }
260

261
    /**
262
     * @param $flag
263
     */
264 1
    public function setCreate($flag)
265
    {
266 1
        $this->extraOptions['b'] = $flag;
267
    }
268

269 1
    public function getCreate()
270
    {
271 1
        return $this->extraOptions['b'];
272
    }
273

274 1
    public function isCreate()
275
    {
276 1
        return $this->getCreate();
277
    }
278

279
    // -B flag is not found in all versions of git
280
    // --force is present everywhere
281
    /**
282
     * @param $flag
283
     */
284 1
    public function setForceCreate($flag)
285
    {
286 1
        $this->setForce($flag);
287
    }
288

289 1
    public function getForceCreate()
290
    {
291 1
        return $this->extraOptions['B'];
292
    }
293

294 1
    public function isForceCreate()
295
    {
296 1
        return $this->getForceCreate();
297
    }
298

299
    /**
300
     * @param $flag
301
     */
302 1
    public function setMerge($flag)
303
    {
304 1
        $this->extraOptions['m'] = $flag;
305
    }
306

307 1
    public function getMerge()
308
    {
309 1
        return $this->extraOptions['m'];
310
    }
311

312 1
    public function isMerge()
313
    {
314 1
        return $this->getMerge();
315
    }
316
}

Read our documentation on viewing source code .

Loading