This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdrushsubtree.subtreecommander.class.inc
274 lines (237 loc) · 8.5 KB
/
drushsubtree.subtreecommander.class.inc
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* @file
*/
require_once('drushsubtree.commander.class.inc');
/**
* SubtreeCommander class.
*/
class SubtreeCommander extends Commander {
private $name;
private $uri;
private $branch;
private $message;
private $squash;
private $pull;
private $merge;
/**
* Construct SubtreeCommander object. Fill in properties from config and defaults.
*
* @param string name
* Key for subtree in buildmanager.config.yml.
*
* @param array properties
* Properties for subtree provided in buildmanager.config.yml.
*/
public function __construct($name, $properties) {
$this->name = $name;
$this->uri = (isset($properties['uri'])) ? $properties['uri'] : '';
$this->branch = (isset($properties['branch'])) ? $properties['branch'] : '';
$this->message = (isset($properties['message']))? $properties['message']: '';
$this->squash = (isset($properties['squash'])) ? $properties['squash'] : TRUE;
$this->pull = (isset($properties['pull'])) ? $properties['pull'] : TRUE;
$this->merge = (isset($properties['merge'])) ? $properties['merge'] : TRUE;
}
/**
* Generate git subtree add command to be run from top-level of repo.
*
* This command is stored in SubtreeCommander's command property.
*
* @param bool $fail_quietly
* Set to true if you don't want to warn user that subtree add can't be run.
* Use this if you're calling add() to find out if you need to add a
* subtree, but have no reason to believe there's an error if a
* subtree/directory already exists.
*
* @return string|bool
* Executable shell command. False for don't do anything.
*/
function add($fail_quietly = FALSE) {
$prefix = _drushsubtree_get_prefix($this->name);
if (file_exists($prefix)) {
// Subtree has already been created. Or there's a directory in our way.
if ($fail_quietly) {
// We're done.
return FALSE;
}
drush_log(dt("This directory already exists: !prefix", array('!prefix' => $prefix)), 'warning');
// We're done. Return.
return drush_set_error(dt("Git subtree add command will be skipped. If !prefix is not a subtree, remove this directory, commit, then re-try. If it is a subtree, you don't need to add it again.", array('!prefix' => $prefix)));
}
// Make sure we have minimum required $details.
$uri = $this->uri;
$branch = $this->branch;
if (!$this->uri || !$this->branch) {
// We're done. Return.
return FALSE;
}
// Squash commit history of subtree into a single commit?
$squash = ($this->squash) ? ' --squash' : '';
// Add custom commit message?
$message = _drushsubtree_add_subtree_commit_message($this->message);
// Build the command to add subtree for the first time.
$this->command = "git subtree add --prefix={$prefix} {$squash} {$message} $uri $branch";
return $this->command;
}
/**
* Generate git subtree pull command to be run from top-level of repo.
*
* This command is stored in SubtreeCommander's command property.
*
* @return string|bool
* Executable shell command. False for don't do anything.
*/
function pull() {
$directory = $this->name;
// Check to see if pulls are disabled (e.g. for local development).
if ($this->pull == FALSE) {
drush_log(dt('git subtree pull is disabled for !project', array('!project' => $directory)), 'warning');
return '';
}
drush_log(dt('Preparing to pull subtree: !subtree_name', array('!subtree_name' => $directory)), 'ok');
// Get params for git subtree pull command.
$prefix = _drushsubtree_get_prefix($directory);
$uri = $this->uri;
$branch = $this->branch;
// @todo Confirm $prefix is a subtree, not just a directory.
// Add custom commit message?
$message = _drushsubtree_add_subtree_commit_message($this->message);
// Squash commit history of subtree into a single commit?
$squash = ($this->squash) ? ' --squash' : '';
$this->command = "git subtree pull --prefix={$prefix} {$squash} {$message} {$uri} {$branch}";
return $this->command;
}
/**
* Generate git subtree merge command to be run from top-level of repo.
*
* This command is stored in SubtreeCommander's command property.
*
* Note: This terminology is a little confusing. To "checkout" a particular
* tagged version for a subtree you need to do a git subtree "merge". There is
* no subtree checkout command.
*
* @param string $id
* Git commit ID to merge to.
*
* @param string $tag
* (Optional) Git tag corresponding to commit ID.
*
* @return string|bool
* Executable shell command. False for don't do anything.
*/
function merge($id, $tag = '') {
$directory = $this->name;
// Check to see if merges are disabled (e.g. for local development).
if ($this->merge == FALSE) {
drush_log(dt('git subtree merge is disabled for !project', array('!project' => $directory)), 'ok');
return '';
}
// Notify user about what we're doing.
$text = dt('Preparing to merge !subtree_name subtree to id: !id !tag', array(
'!subtree_name' => $directory,
'!id' => substr($id, 0, 10),
'!tag' => ($tag) ? "(tag {$tag})" : ''));
drush_log($text, 'ok');
// Get params for subtree merge command.
$prefix = _drushsubtree_get_prefix($directory);
$message = _drushsubtree_add_subtree_commit_message($this->message);
// Note: Squash is hardcoded. Sometime if you don't use --squash, merges
// won't work. If user has set --squash to FALSE, notify user that their
// setting is not going to be respected here (unlike subtree pull where
// their setting will be honored).
if ($this->squash == FALSE) {
drush_log(dt('Even though squash is set to TRUE in your buildmanager config, git subtree merges are all executed WITH --squash option.'), 'notice');
}
// Build command.
$this->command = "git subtree merge --squash --prefix={$prefix} {$message} {$id}";
return $this->command;
}
/**
* Generate git subtree push command to be run from top-level of repo.
*
* This command is stored in SubtreeCommander's command property.
*
* @return string|bool
* Executable shell command. False for don't do anything.
*/
function push() {
$prefix = _drushsubtree_get_prefix($this->name);
if (!file_exists($prefix)) {
// We're done. Return.
return drush_set_error('drushsubtree',
dt("Oops. There's no subtree here to push: !prefix",
array('!prefix' => $prefix)));
}
// @todo Add an elseif here. If the directory exists, but this is not a
// subtree, notify user. Suggest removing/re-adding.
// Make sure we have minimum required $details.
$uri = $this->uri;
$branch = $this->branch;
if (!$this->uri || !$this->branch) {
// We're done. Return.
return drush_set_error('drushsubtree', dt("Missing required properties for subtree push in buildmanager config file: uri and/or branch."));
}
// Build the command to add subtree for the first time.
$this->command = "git subtree push --prefix={$prefix} $uri $branch";
return $this->command;
}
/**
* Generate git subtree split command to be run from top-level of repo.
*
* This command is stored in SubtreeCommander's command property.
*
* @return string|bool
* Executable shell command. False for don't do anything. This command
* returns commit ID for the tip of the subtree project's commit history.
*/
function split() {
$prefix = _drushsubtree_get_prefix($this->name);
$this->command = "git subtree split --prefix={$prefix}";
return $this->command;
}
/**
* Getters and setters for properties.
*/
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
function getUri() {
return $this->uri;
}
function setUri($uri) {
$this->uri = $uri;
}
function getBranch() {
return $this->branch;
}
function setBranch($branch) {
$this->branch = $branch;
}
function getMessage() {
return $this->message;
}
function setMessage($message) {
$this->message = $message;
}
function getSquash() {
return $this->squash;
}
function setSquash($squash) {
$this->squash = $squash;
}
function getPull() {
return $this->pull;
}
function setPull($pull) {
$this->pull = $pull;
}
function getMerge() {
return $this->merge;
}
function setMerge($merge) {
$this->merge = $merge;
}
}