|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite\Workflow |
|
9
|
|
|
* @subpackage Composer |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Workflow\Composer; |
|
16
|
|
|
use Netresearch\Kite\Service\Composer\Package; |
|
17
|
|
|
use Netresearch\Kite\Task; |
|
18
|
|
|
use Netresearch\Kite\Workflow; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Go through all packages and merge the given branch into the current, when it exists |
|
23
|
|
|
* |
|
24
|
|
|
* @category Netresearch |
|
25
|
|
|
* @package Netresearch\Kite\Workflow |
|
26
|
|
|
* @subpackage Composer |
|
27
|
|
|
* @author Christian Opitz <[email protected]> |
|
28
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
29
|
|
|
* @link http://www.netresearch.de |
|
30
|
|
|
*/ |
|
31
|
|
|
class Merge extends Base |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Configures the arguments/options |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function configureVariables() |
|
39
|
|
|
{ |
|
40
|
|
|
return array( |
|
41
|
|
|
'branch' => array( |
|
42
|
|
|
'type' => 'string', |
|
43
|
|
|
'label' => 'The branch to merge in', |
|
44
|
|
|
'argument' => true, |
|
45
|
|
|
'required' => true |
|
46
|
|
|
), |
|
47
|
|
|
'squash' => array( |
|
48
|
|
|
'type' => 'bool', |
|
49
|
|
|
'label' => 'Whether to merge with --squash', |
|
50
|
|
|
'option' => true, |
|
51
|
|
|
), |
|
52
|
|
|
'delete' => array( |
|
53
|
|
|
'type' => 'bool', |
|
54
|
|
|
'label' => 'Whether to delete the branch after merge', |
|
55
|
|
|
'option' => true, |
|
56
|
|
|
), |
|
57
|
|
|
'message' => array( |
|
58
|
|
|
'type' => 'bool', |
|
59
|
|
|
'label' => 'Message for commits (if any)', |
|
60
|
|
|
'option' => true, |
|
61
|
|
|
'shortcut' => 'm' |
|
62
|
|
|
), |
|
63
|
|
|
'no-diagnose' => array( |
|
64
|
|
|
'type' => 'bool', |
|
65
|
|
|
'label' => 'Don\'t do a diagnose upfront', |
|
66
|
|
|
'option' => true, |
|
67
|
|
|
), |
|
68
|
|
|
'--' |
|
69
|
|
|
) + parent::configureVariables(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Assemble the tasks |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function assemble() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->callback( |
|
80
|
|
|
function () { |
|
81
|
|
|
$mergeBranch = $this->get('branch'); |
|
82
|
|
|
$diagnose = !$this->get('no-diagnose'); |
|
83
|
|
|
$delete = $this->get('delete', false); |
|
84
|
|
|
$squash = $this->get('squash', false); |
|
85
|
|
|
$optArg[$mergeBranch == 'master' ? 'ff' : 'no-ff'] = true; |
|
|
|
|
|
|
86
|
|
|
$message = $this->get('message', ''); |
|
87
|
|
|
|
|
88
|
|
|
if ($diagnose) { |
|
89
|
|
|
$this->sub('Netresearch\Kite\Workflow\Composer\Diagnose', array('fix' => true)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$mergePackages = $this->getMergePackages($mergeBranch, !$diagnose); |
|
93
|
|
|
if (!$mergePackages) { |
|
|
|
|
|
|
94
|
|
|
$this->console->output("<warning>Could not find branch $mergeBranch in any installed package</warning>"); |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
foreach ($mergePackages as $package) { |
|
99
|
|
|
$this->mergePackage($package, $mergeBranch, $message, $squash); |
|
100
|
|
|
|
|
101
|
|
|
if ($delete) { |
|
102
|
|
|
$this->git('branch', $package->path, array('d' => $mergeBranch)); |
|
103
|
|
|
$this->git('push', $package->path, array('origin', ':' . $mergeBranch)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->rewriteRequirements($mergePackages, true); |
|
108
|
|
|
$this->pushPackages(); |
|
109
|
|
|
|
|
110
|
|
|
// Each package containing the branch should now be at the tip of it's |
|
111
|
|
|
// current branch. Anyway we do a composer update in order to update lock file |
|
112
|
|
|
// and eventually changed dependencies |
|
113
|
|
|
$this->doComposerUpdate(); |
|
114
|
|
|
} |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the packages which have the branch to merge |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $mergeBranch mergeBranch |
|
122
|
|
|
* @param bool $pull Whether to pull when branch exists |
|
123
|
|
|
* |
|
124
|
|
|
* @return Package[] |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function getMergePackages($mergeBranch, $pull) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
$mergePackages = array(); |
|
129
|
|
|
foreach ($this->getPackages() as $package) { |
|
130
|
|
|
if ($mergeBranch === $package->branch) { |
|
131
|
|
|
$checkout = $this->confirm("{$package->name} is checked out at {$mergeBranch} - do you want to checkout another branch from master and merge into that?"); |
|
132
|
|
|
if ($checkout) { |
|
133
|
|
|
$choices = array('Create new branch'); |
|
134
|
|
|
foreach ($package->branches as $choiceBranch) { |
|
135
|
|
|
if ($choiceBranch !== $mergeBranch) { |
|
136
|
|
|
$choices[] = $choiceBranch; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
$checkoutBranch = $inferFromBranch = $this->choose('Select branch:', $choices, 0); |
|
140
|
|
|
if ($checkoutBranch == $choices[0]) { |
|
141
|
|
|
$checkoutBranch = $this->answer('Branch name:'); |
|
142
|
|
|
$package->branches[] = $checkoutBranch; |
|
143
|
|
|
$inferFromBranch = 'master'; |
|
144
|
|
|
} |
|
145
|
|
|
$this->git('fetch', $package->path, array('force' => true, 'origin', $inferFromBranch . ':' . $checkoutBranch)); |
|
146
|
|
|
$this->git('checkout', $package->path, array($checkoutBranch)); |
|
147
|
|
|
$package->branch = $checkoutBranch; |
|
148
|
|
|
$package->version = 'dev-' . $checkoutBranch; |
|
149
|
|
|
} else { |
|
150
|
|
|
continue; |
|
151
|
|
|
} |
|
152
|
|
|
} elseif (in_array($mergeBranch, $package->branches, true)) { |
|
153
|
|
|
// Pull is actually not needed here, as we have the current state |
|
154
|
|
|
// from composer service - but we don't know if we should rebase or |
|
155
|
|
|
// merge - thus we simply pull here |
|
156
|
|
|
$this->git('pull', $package->path); |
|
157
|
|
|
} else { |
|
158
|
|
|
continue; |
|
159
|
|
|
} |
|
160
|
|
|
$mergePackages[$package->name] = $package; |
|
161
|
|
|
} |
|
162
|
|
|
return $mergePackages; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
?> |
|
166
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.