Completed
Push — master ( 1146bb...277540 )
by Greg
03:09
created

GitStack::cloneShallow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 4
1
<?php
2
namespace Robo\Task\Vcs;
3
4
use Robo\Task\CommandStack;
5
use Symfony\Component\Process\ProcessUtils;
6
7
/**
8
 * Runs Git commands in stack. You can use `stopOnFail()` to point that stack should be terminated on first fail.
9
 *
10
 * ``` php
11
 * <?php
12
 * $this->taskGitStack()
13
 *  ->stopOnFail()
14
 *  ->add('-A')
15
 *  ->commit('adding everything')
16
 *  ->push('origin','master')
17
 *  ->tag('0.6.0')
18
 *  ->push('origin','0.6.0')
19
 *  ->run()
20
 *
21
 * $this->taskGitStack()
22
 *  ->stopOnFail()
23
 *  ->add('doc/*')
24
 *  ->commit('doc updated')
25
 *  ->push()
26
 *  ->run();
27
 * ?>
28
 * ```
29
 */
30
class GitStack extends CommandStack
31
{
32
    /**
33
     * @param string $pathToGit
34
     */
35
    public function __construct($pathToGit = 'git')
36
    {
37
        $this->executable = $pathToGit;
38
    }
39
40
    /**
41
     * Executes `git clone`
42
     *
43
     * @param string $repo
44
     * @param string $to
45
     *
46
     * @return $this
47
     */
48 View Code Duplication
    public function cloneRepo($repo, $to = "", $branch = "")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $cmd = ['clone', $repo, $to];
51
        if (!empty($branch)) {
52
            $cmd[] = "--branch $branch";
53
        }
54
        return $this->exec($cmd);
55
    }
56
57
    /**
58
     * Executes `git clone` with depth 1 as default
59
     *
60
     * @param string $repo
61
     * @param string $to
62
     * @param string $branch
63
     * @param int    $depth
64
     *
65
     * @return $this
66
     */
67 View Code Duplication
    public function cloneShallow($repo, $to = '', $branch = "", $depth = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $cmd = ["clone --depth $depth", $repo, $to];
70
        if (!empty($branch)) {
71
            $cmd[] = "--branch $branch";
72
        }
73
74
        return $this->exec($cmd);
75
    }
76
77
    /**
78
     * Executes `git add` command with files to add pattern
79
     *
80
     * @param string $pattern
81
     *
82
     * @return $this
83
     */
84
    public function add($pattern)
85
    {
86
        return $this->exec([__FUNCTION__, $pattern]);
87
    }
88
89
    /**
90
     * Executes `git commit` command with a message
91
     *
92
     * @param string $message
93
     * @param string $options
94
     *
95
     * @return $this
96
     */
97
    public function commit($message, $options = "")
98
    {
99
        $message = ProcessUtils::escapeArgument($message);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Proces...Utils::escapeArgument() has been deprecated with message: since version 3.3, to be removed in 4.0. Use a command line array or give env vars to the `Process::start/run()` method instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
        return $this->exec([__FUNCTION__, "-m $message", $options]);
101
    }
102
103
    /**
104
     * Executes `git pull` command.
105
     *
106
     * @param string $origin
107
     * @param string $branch
108
     *
109
     * @return $this
110
     */
111
    public function pull($origin = '', $branch = '')
112
    {
113
        return $this->exec([__FUNCTION__, $origin, $branch]);
114
    }
115
116
    /**
117
     * Executes `git push` command
118
     *
119
     * @param string $origin
120
     * @param string $branch
121
     *
122
     * @return $this
123
     */
124
    public function push($origin = '', $branch = '')
125
    {
126
        return $this->exec([__FUNCTION__, $origin, $branch]);
127
    }
128
129
    /**
130
     * Performs git merge
131
     *
132
     * @param string $branch
133
     *
134
     * @return $this
135
     */
136
    public function merge($branch)
137
    {
138
        return $this->exec([__FUNCTION__, $branch]);
139
    }
140
141
    /**
142
     * Executes `git checkout` command
143
     *
144
     * @param string $branch
145
     *
146
     * @return $this
147
     */
148
    public function checkout($branch)
149
    {
150
        return $this->exec([__FUNCTION__, $branch]);
151
    }
152
153
    /**
154
     * Executes `git tag` command
155
     *
156
     * @param string $tag_name
157
     * @param string $message
158
     *
159
     * @return $this
160
     */
161 View Code Duplication
    public function tag($tag_name, $message = "")
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        if ($message != "") {
164
            $message = "-m '$message'";
165
        }
166
        return $this->exec([__FUNCTION__, $message, $tag_name]);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function run()
173
    {
174
        $this->printTaskInfo("Running git commands...");
175
        return parent::run();
176
    }
177
}
178