6cb1783f0460b750ecfe846d6bdbcee56c1807bb
[yaffs-website] / vendor / consolidation / robo / src / Task / Vcs / GitStack.php
1 <?php
2 namespace Robo\Task\Vcs;
3
4 use Robo\Task\CommandStack;
5 use Robo\Common\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     public function cloneRepo($repo, $to = "", $branch = "")
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     public function cloneShallow($repo, $to = '', $branch = "", $depth = 1)
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);
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     public function tag($tag_name, $message = "")
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 }