aboutsummaryrefslogtreecommitdiffstats
path: root/library/kzykhys/git/src/PHPGit/Command/ResetCommand.php
blob: f70f53e2e2cf964b1de3d80cf64b927249de5e16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php

namespace PHPGit\Command;

use PHPGit\Command;
use PHPGit\Exception\GitException;

/**
 * Reset current HEAD to the specified state - `git reset`
 *
 * @author Kazuyuki Hayashi <hayashi@valnur.net>
 */
class ResetCommand extends Command
{

    /**
     * Resets the index entries for all **$paths** to their state at **$commit**
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset();
     * ```
     *
     * @param string|array|\Traversable $paths  The paths to reset
     * @param string                    $commit The commit
     *
     * @return bool
     */
    public function __invoke($paths, $commit = null)
    {
        $builder = $this->git->getProcessBuilder()
            ->add('reset');

        if ($commit) {
            $builder->add($commit)->add('--');
        }

        if (!is_array($paths) && !($paths instanceof \Traversable)) {
            $paths = array($paths);
        }

        foreach ($paths as $path) {
            $builder->add($path);
        }

        try {
            $this->git->run($builder->getProcess());
        } catch (GitException $e) {
            // Confirm exit code
        }

        return true;
    }

    /**
     * Resets the current branch head to **$commit**
     *
     * Does not touch the index file nor the working tree at all (but resets the head to **$commit**,
     * just like all modes do).
     * This leaves all your changed files "Changes to be committed", as git status would put it.
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset->soft();
     * ```
     *
     * @param string $commit The commit
     *
     * @return bool
     */
    public function soft($commit = null)
    {
        return $this->mode('soft', $commit);
    }

    /**
     * Resets the current branch head to **$commit**
     *
     * Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit)
     * and reports what has not been updated. This is the default action.
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset->mixed();
     * ```
     *
     * @param string $commit The commit
     *
     * @return bool
     */
    public function mixed($commit = null)
    {
        return $this->mode('mixed', $commit);
    }

    /**
     * Resets the current branch head to **$commit**
     *
     * Resets the index and working tree. Any changes to tracked files in the working tree since **$commit** are discarded
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset->hard();
     * ```
     *
     * @param string $commit The commit
     *
     * @return bool
     */
    public function hard($commit = null)
    {
        return $this->mode('hard', $commit);
    }

    /**
     * Resets the current branch head to **$commit**
     *
     * Resets the index and updates the files in the working tree that are different between **$commit** and HEAD,
     * but keeps those which are different between the index and working tree
     * (i.e. which have changes which have not been added). If a file that is different between **$commit** and
     * the index has unstaged changes, reset is aborted
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset->merge();
     * ```
     *
     * @param string $commit The commit
     *
     * @return bool
     */
    public function merge($commit = null)
    {
        return $this->mode('merge', $commit);
    }

    /**
     * Resets the current branch head to **$commit**
     *
     * Resets index entries and updates files in the working tree that are different between **$commit** and HEAD.
     * If a file that is different between **$commit** and HEAD has local changes, reset is aborted.
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset->keep();
     * ```
     *
     * @param string $commit The commit
     *
     * @return bool
     */
    public function keep($commit = null)
    {
        return $this->mode('keep', $commit);
    }

    /**
     * Resets the current branch head to **$commit**
     *
     * Possibly updates the index (resetting it to the tree of **$commit**) and the working tree depending on **$mode**
     *
     * ``` php
     * $git = new PHPGit\Git();
     * $git->setRepository('/path/to/repo');
     * $git->reset->mode('hard');
     * ```
     *
     * @param string $mode   --<mode>
     * @param string $commit The commit
     *
     * @throws \InvalidArgumentException
     * @return bool
     */
    public function mode($mode, $commit = null)
    {
        if (!in_array($mode, array('soft', 'mixed', 'hard', 'merge', 'keep'))) {
            throw new \InvalidArgumentException('$mode must be one of the following: soft, mixed, hard, merge, keep');
        }

        $builder = $this->git->getProcessBuilder()
            ->add('reset')
            ->add('--' . $mode);

        if ($commit) {
            $builder->add($commit);
        }

        $this->git->run($builder->getProcess());

        return true;
    }

}