Completed
Push — develop ( 2eea19...05bbe8 )
by
unknown
13:03
created

StashCommand::branch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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
    public function __construct(Repository $repo = null)
41
    {
42
        parent::__construct($repo);
43
    }
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
    public function save($message = null, $includeUntracked = false, $keepIndex = false)
55
    {
56
        $this->clearAll();
57
        $this->addCommandName(self::STASH_COMMAND . ' save');
58
        if (!is_null($message)) {
59
            $this->addCommandSubject($message);
60
        }
61
62
        if ($includeUntracked) {
63
            $this->addCommandArgument('--include-untracked');
64
        }
65
66
        if ($keepIndex) {
67
            $this->addCommandArgument('--keep-index');
68
        }
69
70
        return $this->getCommand();
71
    }
72
73
    /**
74
     * Shows stash list
75
     *
76
     * @param array|null $options
77
     *
78
     * @return string
79
     */
80
    public function listStashes(array $options = null)
81
    {
82
        $this->clearAll();
83
        $this->addCommandName(self::STASH_COMMAND . ' list');
84
        if (null !== $options) {
85
            $this->addCommandSubject($options);
86
        }
87
        return $this->getCommand();
88
    }
89
90
    /**
91
     * Shows details for a specific stash
92
     *
93
     * @param string $stash
94
     *
95
     * @return string
96
     */
97
    public function show($stash)
98
    {
99
        $stash = $this->normalizeStashName($stash);
100
        $this->clearAll();
101
        $this->addCommandName(self::STASH_COMMAND . ' show');
102
        $this->addCommandSubject($stash);
103
        return $this->getCommand();
104
    }
105
106
    /**
107
     * Drops a stash
108
     *
109
     * @param string $stash
110
     *
111
     * @return string
112
     */
113
    public function drop($stash)
114
    {
115
        $stash = $this->normalizeStashName($stash);
116
        $this->clearAll();
117
        $this->addCommandName(self::STASH_COMMAND . ' drop');
118
        $this->addCommandSubject($stash);
119
        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
    public function apply($stash, $index = false)
131
    {
132
        $stash = $this->normalizeStashName($stash);
133
        $this->clearAll();
134
        $this->addCommandName(self::STASH_COMMAND . ' apply');
135
        $this->addCommandSubject($stash);
136
        if ($index) {
137
            $this->addCommandArgument('--index');
138
        }
139
        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
    public function pop($stash, $index = false)
151
    {
152
        $stash = $this->normalizeStashName($stash);
153
        $this->clearAll();
154
        $this->addCommandName(self::STASH_COMMAND . ' pop');
155
        $this->addCommandSubject($stash);
156
        if ($index) {
157
            $this->addCommandArgument('--index');
158
        }
159
        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
    public function branch($branch, $stash)
171
    {
172
        $stash = $this->normalizeStashName($stash);
173
        $this->clearAll();
174
        $this->addCommandName(self::STASH_COMMAND . ' branch');
175
        $this->addCommandSubject($branch);
176
        $this->addCommandSubject2($stash);
177
        return $this->getCommand();
178
    }
179
180
    /**
181
     *  Remove all the stashed states.
182
     *
183
     */
184
    public function clear()
185
    {
186
        $this->clearAll();
187
        $this->addCommandName(self::STASH_COMMAND . ' clear');
188
        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
    public function create()
197
    {
198
        $this->clearAll();
199
        $this->addCommandName(self::STASH_COMMAND . ' create');
200
        return $this->getCommand();
201
    }
202
203
    /**
204
     * @param $stash
205
     *
206
     * @return string
207
     */
208
    private function normalizeStashName($stash)
209
    {
210
        if (0 !== strpos($stash, 'stash@{')) {
211
            $stash = 'stash@{' . $stash . '}';
212
        }
213
        return $stash;
214
    }
215
216
}
217