@@ -35,6 +35,13 @@ class SiteCheckoutCommand extends SiteBaseCommand {
35
35
*/
36
36
protected $ branch ;
37
37
38
+ /**
39
+ * Stores current branch of the checked out code.
40
+ *
41
+ * @var array currentBranch.
42
+ */
43
+ protected $ currentBranch ;
44
+
38
45
/**
39
46
* {@inheritdoc}
40
47
*/
@@ -64,23 +71,31 @@ protected function configure() {
64
71
protected function interact (InputInterface $ input , OutputInterface $ output ) {
65
72
parent ::interact ($ input , $ output );
66
73
74
+ // Validate repo.
75
+ $ this ->_validateRepo ();
76
+
77
+ $ remoteBranches = $ this ->getRemoteBranches ();
78
+ $ defaultBranch = $ this ->getDefaultBranch ();
79
+ $ this ->currentBranch = $ this ->getCurrentBranch ();
80
+
67
81
$ branch = $ input ->getOption ('branch ' );
68
82
if (!$ branch ) {
69
- // Typical branches.
70
- $ branches = ['8.x ' , 'master ' ];
71
83
72
- if (isset ($ this ->config ['repo ' ]['branch ' ])) {
73
- // Populate branches from config.
74
- $ siteBranch = $ this ->config ['repo ' ]['branch ' ];
75
- }
84
+ $ options = array_values (array_unique (array_merge (
85
+ ['8.x ' ],
86
+ [$ defaultBranch ],
87
+ [$ this ->currentBranch ],
88
+ $ remoteBranches
89
+ )));
90
+
91
+ $ branch = $ this ->io ->choice (
92
+ $ this ->trans ('Select a branch ' ),
93
+ $ options ,
94
+ isset ($ this ->currentBranch ) ? $ this ->currentBranch : $ defaultBranch ,
95
+ TRUE
96
+ );
76
97
77
- $ branch = $ this ->io ->choice (
78
- $ this ->trans ('Select a branch ' ),
79
- array_values (array_unique (array_merge ([$ siteBranch ], $ branches ))),
80
- isset ($ siteBranch ) ? $ siteBranch : '8.x ' ,
81
- true
82
- );
83
- $ input ->setOption ('branch ' , reset ($ branch ));
98
+ $ input ->setOption ('branch ' , reset ($ branch ));
84
99
}
85
100
}
86
101
@@ -90,12 +105,14 @@ protected function interact(InputInterface $input, OutputInterface $output) {
90
105
protected function execute (InputInterface $ input , OutputInterface $ output ) {
91
106
parent ::execute ($ input , $ output );
92
107
93
- // Validate repo.
94
- $ this ->_validateRepo ();
95
-
96
108
// Validate branch.
97
109
$ this ->_validateBranch ($ input );
98
110
111
+ if ($ this ->branch == $ this ->currentBranch ) {
112
+ $ this ->io ->commentBlock ('Current branch selected, skipping checkout command. ' );
113
+ return ;
114
+ }
115
+
99
116
$ this ->io ->comment (sprintf ('Checking out %s (%s) on %s ' ,
100
117
$ this ->siteName ,
101
118
$ this ->branch ,
@@ -273,4 +290,68 @@ protected function gitCheckout($branch, $destination) {
273
290
274
291
return TRUE ;
275
292
}
293
+
294
+ /**
295
+ * Pulls a list of branches from remote.
296
+ *
297
+ * @param $repo
298
+ *
299
+ * @return mixed
300
+ * @throws SiteCommandException
301
+ */
302
+ protected function getRemoteBranches () {
303
+ $ command = sprintf ('git ls-remote --heads %s ' ,
304
+ $ this ->repo ['url ' ]
305
+ );
306
+
307
+ $ shellProcess = $ this ->getShellProcess ();
308
+
309
+ if ($ shellProcess ->exec ($ command , TRUE )) {
310
+ preg_match_all ("|refs/heads/(.*)| " , $ shellProcess ->getOutput (), $ matches );
311
+ if (!empty ($ matches [1 ] && is_array ($ matches [1 ]))) {
312
+ return $ matches [1 ];
313
+ }
314
+ }
315
+ else {
316
+ throw new SiteCommandException ($ shellProcess ->getOutput ());
317
+
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Helper to retrieve the default branch from yml.
323
+ *
324
+ * @return mixed
325
+ */
326
+ protected function getDefaultBranch () {
327
+ // Get branch from yml.
328
+ if (isset ($ this ->config ['repo ' ]['branch ' ])) {
329
+ // Populate branches from config.
330
+ return $ this ->config ['repo ' ]['branch ' ];
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Helper to retrieve the current working branch on the site's directory.
336
+ *
337
+ * @return mixed
338
+ */
339
+ protected function getCurrentBranch () {
340
+ if ($ this ->fileExists ($ this ->destination )) {
341
+ // Get branch from site directory.
342
+ $ command = sprintf ('cd %s && git branch ' ,
343
+ $ this ->shellPath ($ this ->destination )
344
+ );
345
+
346
+ $ shellProcess = $ this ->getShellProcess ();
347
+
348
+ if ($ shellProcess ->exec ($ command , TRUE )) {
349
+ preg_match_all ("|\*\s(.*)| " , $ shellProcess ->getOutput (), $ matches );
350
+ if (!empty ($ matches [1 ] && is_array ($ matches [1 ]))) {
351
+ return reset ($ matches [1 ]);
352
+ }
353
+ }
354
+ }
355
+ }
356
+
276
357
}
0 commit comments