Completed
Pull Request — develop (#132)
by
unknown
02:58
created

StashCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Repository;
23
24
/**
25
 * Stash command generator
26
 *
27
 * @author Matteo Giachino <[email protected]>
28
 * @author Kirk Madera <[email protected]>
29
 */
30
class StashCommand extends BaseCommand
31
{
32
    const STASH_COMMAND = 'stash';
33
34
    /**
35
     * constructor
36
     *
37
     * @param \GitElephant\Repository $repo The repository object this command
38
     *                                      will interact with
39
     */
40 9
    public function __construct(Repository $repo = null)
41
    {
42 9
        parent::__construct($repo);
43 9
    }
44
45
    /**
46
     *  Save your local modifications to a new stash, and run git reset --hard to revert them.
47
     *
48
     * @param string|null $message
49
     * @param boolean $includeUntracked
50
     * @param boolean $keepIndex
51
     *
52
     * @return string
53
     */
54 1
    public function save($message = null, $includeUntracked = false, $keepIndex = false)
55
    {
56 1
        $this->clearAll();
57 1
        $this->addCommandName(self::STASH_COMMAND . ' save');
58 1
        if (!is_null($message)) {
59 1
            $this->addCommandSubject($message);
60
        }
61
62 1
        if ($includeUntracked) {
63 1
            $this->addCommandArgument('--include-untracked');
64
        }
65
66 1
        if ($keepIndex) {
67 1
            $this->addCommandArgument('--keep-index');
68
        }
69
70 1
        return $this->getCommand();
71
    }
72
73
    /**
74
     * Shows stash list
75
     *
76
     * @param array|null $options
77
     *
78
     * @return string
79
     */
80 1
    public function listStashes(array $options = null)
81
    {
82 1
        $this->clearAll();
83 1
        $this->addCommandName(self::STASH_COMMAND . ' list');
84 1
        if (null !== $options) {
85 1
            $this->addCommandSubject($options);
86
        }
87 1
        return $this->getCommand();
88
    }
89
90
    /**
91
     * Shows details for a specific stash
92
     *
93
     * @param string $stash
94
     *
95
     * @return string
96
     */
97 1
    public function show($stash)
98
    {
99 1
        $stash = $this->normalizeStashName($stash);
100 1
        $this->clearAll();
101 1
        $this->addCommandName(self::STASH_COMMAND . ' show');
102 1
        $this->addCommandSubject($stash);
103 1
        return $this->getCommand();
104
    }
105
106
    /**
107
     * Drops a stash
108
     *
109
     * @param string $stash
110
     *
111
     * @return string
112
     */
113 1
    public function drop($stash)
114
    {
115 1
        $stash = $this->normalizeStashName($stash);
116 1
        $this->clearAll();
117 1
        $this->addCommandName(self::STASH_COMMAND . ' drop');
118 1
        $this->addCommandSubject($stash);
119 1
        return $this->getCommand();
120
    }
121
122
    /**
123
     * Applies a stash
124
     *
125
     * @param string $stash
126
     * @param boolean $index
127
     *
128
     * @return string
129
     */
130 1
    public function apply($stash, $index = false)
131
    {
132 1
        $stash = $this->normalizeStashName($stash);
133 1
        $this->clearAll();
134 1
        $this->addCommandName(self::STASH_COMMAND . ' apply');
135 1
        $this->addCommandSubject($stash);
136 1
        if ($index) {
137 1
            $this->addCommandArgument('--index');
138
        }
139 1
        return $this->getCommand();
140
    }
141
142
    /**
143
     * Applies a stash, then removes it from the stash
144
     *
145
     * @param string $stash
146
     * @param boolean $index
147
     *
148
     * @return string
149
     */
150 1
    public function pop($stash, $index = false)
151
    {
152 1
        $stash = $this->normalizeStashName($stash);
153 1
        $this->clearAll();
154 1
        $this->addCommandName(self::STASH_COMMAND . ' pop');
155 1
        $this->addCommandSubject($stash);
156 1
        if ($index) {
157 1
            $this->addCommandArgument('--index');
158
        }
159 1
        return $this->getCommand();
160
    }
161
162
    /**
163
     *  Creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created
164
     *
165
     * @param string $branch
166
     * @param string $stash
167
     *
168
     * @return string
169
     */
170 1
    public function branch($branch, $stash)
171
    {
172 1
        $stash = $this->normalizeStashName($stash);
173 1
        $this->clearAll();
174 1
        $this->addCommandName(self::STASH_COMMAND . ' branch');
175 1
        $this->addCommandSubject($branch);
176 1
        $this->addCommandSubject2($stash);
177 1
        return $this->getCommand();
178
    }
179
180
    /**
181
     *  Remove all the stashed states.
182
     *
183
     */
184 1
    public function clear()
185
    {
186 1
        $this->clearAll();
187 1
        $this->addCommandName(self::STASH_COMMAND . ' clear');
188 1
        return $this->getCommand();
189
    }
190
191
    /**
192
     * Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the
193
     * ref namespace.
194
     *
195
     */
196 1
    public function create()
197
    {
198 1
        $this->clearAll();
199 1
        $this->addCommandName(self::STASH_COMMAND . ' create');
200 1
        return $this->getCommand();
201
    }
202
203
    /**
204
     * @param $stash
205
     *
206
     * @return string
207
     */
208 5
    private function normalizeStashName($stash)
209
    {
210 5
        if (0 !== strpos($stash, 'stash@{')) {
211 5
            $stash = 'stash@{' . $stash . '}';
212
        }
213 5
        return $stash;
214
    }
215
216
}
217