StashCommand::drop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Command;
22
23
use GitElephant\Repository;
24
25
/**
26
 * Stash command generator
27
 *
28
 * @author Matteo Giachino <[email protected]>
29
 * @author Kirk Madera <[email protected]>
30
 */
31
class StashCommand extends BaseCommand
32
{
33
    public const STASH_COMMAND = 'stash';
34
35
    /**
36
     * constructor
37
     *
38
     * @param \GitElephant\Repository $repo The repository object this command
39
     *                                      will interact with
40
     */
41 9
    public function __construct(Repository $repo = null)
42
    {
43 9
        parent::__construct($repo);
44 9
    }
45
46
    /**
47
     *  Save your local modifications to a new stash, and run git reset --hard to revert them.
48
     *
49
     * @param string|null $message
50
     * @param boolean $includeUntracked
51
     * @param boolean $keepIndex
52
     *
53
     * @return string
54
     */
55 1
    public function save($message = null, $includeUntracked = false, $keepIndex = false): string
56
    {
57 1
        $this->clearAll();
58
59 1
        $this->addCommandName(self::STASH_COMMAND . ' save');
60
61 1
        if (!is_null($message)) {
62 1
            $this->addCommandSubject($message);
63
        }
64
65 1
        if ($includeUntracked) {
66 1
            $this->addCommandArgument('--include-untracked');
67
        }
68
69 1
        if ($keepIndex) {
70 1
            $this->addCommandArgument('--keep-index');
71
        }
72
73 1
        return $this->getCommand();
74
    }
75
76
    /**
77
     * Shows stash list
78
     *
79
     * @param array|null $options
80
     *
81
     * @return string
82
     */
83 1
    public function listStashes(array $options = null): string
84
    {
85 1
        $this->clearAll();
86
87 1
        $this->addCommandName(self::STASH_COMMAND . ' list');
88
        
89 1
        if (null !== $options) {
90 1
            $this->addCommandSubject($options);
91
        }
92
93 1
        return $this->getCommand();
94
    }
95
96
    /**
97
     * Shows details for a specific stash
98
     *
99
     * @param string|int $stash
100
     *
101
     * @return string
102
     */
103 1
    public function show($stash): string
104
    {
105 1
        $stash = $this->normalizeStashName($stash);
106 1
        $this->clearAll();
107 1
        $this->addCommandName(self::STASH_COMMAND . ' show');
108 1
        $this->addCommandSubject($stash);
109
110 1
        return $this->getCommand();
111
    }
112
113
    /**
114
     * Drops a stash
115
     *
116
     * @param string $stash
117
     *
118
     * @return string
119
     */
120 1
    public function drop($stash): string
121
    {
122 1
        $stash = $this->normalizeStashName($stash);
123 1
        $this->clearAll();
124 1
        $this->addCommandName(self::STASH_COMMAND . ' drop');
125 1
        $this->addCommandSubject($stash);
126
127 1
        return $this->getCommand();
128
    }
129
130
    /**
131
     * Applies a stash
132
     *
133
     * @param string $stash
134
     * @param boolean $index
135
     *
136
     * @return string
137
     */
138 1
    public function apply($stash, $index = false): string
139
    {
140 1
        $stash = $this->normalizeStashName($stash);
141 1
        $this->clearAll();
142 1
        $this->addCommandName(self::STASH_COMMAND . ' apply');
143 1
        $this->addCommandSubject($stash);
144 1
        if ($index) {
145 1
            $this->addCommandArgument('--index');
146
        }
147
148 1
        return $this->getCommand();
149
    }
150
151
    /**
152
     * Applies a stash, then removes it from the stash
153
     *
154
     * @param string $stash
155
     * @param boolean $index
156
     *
157
     * @return string
158
     */
159 1
    public function pop($stash, $index = false): string
160
    {
161 1
        $stash = $this->normalizeStashName($stash);
162 1
        $this->clearAll();
163 1
        $this->addCommandName(self::STASH_COMMAND . ' pop');
164 1
        $this->addCommandSubject($stash);
165 1
        if ($index) {
166 1
            $this->addCommandArgument('--index');
167
        }
168
169 1
        return $this->getCommand();
170
    }
171
172
    /**
173
     *  Creates and checks out a new branch named <branchname> starting from the commit at which the <stash> was originally created
174
     *
175
     * @param string $branch
176
     * @param string $stash
177
     *
178
     * @return string
179
     */
180 1
    public function branch($branch, $stash): string
181
    {
182 1
        $stash = $this->normalizeStashName($stash);
183 1
        $this->clearAll();
184 1
        $this->addCommandName(self::STASH_COMMAND . ' branch');
185 1
        $this->addCommandSubject($branch);
186 1
        $this->addCommandSubject2($stash);
187
188 1
        return $this->getCommand();
189
    }
190
191
    /**
192
     *  Remove all the stashed states.
193
     */
194 1
    public function clear(): string
195
    {
196 1
        $this->clearAll();
197 1
        $this->addCommandName(self::STASH_COMMAND . ' clear');
198
199 1
        return $this->getCommand();
200
    }
201
202
    /**
203
     * Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the
204
     * ref namespace.
205
     */
206 1
    public function create(): string
207
    {
208 1
        $this->clearAll();
209 1
        $this->addCommandName(self::STASH_COMMAND . ' create');
210
211 1
        return $this->getCommand();
212
    }
213
214
    /**
215
     * @param int|string $stash
216
     *
217
     * @return string
218
     */
219 5
    private function normalizeStashName($stash): string
220
    {
221 5
        if (0 !== strpos($stash, 'stash@{')) {
222 5
            $stash = 'stash@{' . $stash . '}';
223
        }
224
225 5
        return $stash;
226
    }
227
}
228