-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate.php
More file actions
242 lines (209 loc) · 6.74 KB
/
Update.php
File metadata and controls
242 lines (209 loc) · 6.74 KB
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
<?php
namespace Phug\Split\Command;
use Exception;
use Phug\Split;
use Phug\Split\Command\Options\HashPrefix;
use Phug\Split\Git\Author;
use Phug\Split\Git\Commit;
use Phug\Split\Git\EmptyLogList;
use Phug\Split\Git\Log;
/**
* Replay commits from the mono-repository to the sub-packages repositories.
*/
class Update extends Dist
{
use HashPrefix;
/**
* @option n, no-push
*
* Do not push the replayed commits.
*
* @var bool
*/
public $noPush = false;
/**
* Cache for "user" property from .git/config
*
* @var array|null
*/
private $localUser = null;
/**
* @option m, maximum-commits-replay
*
* Maximum number of commits to replay.
*
* @var int|numeric-string @phan-suppress-current-line PhanUnextractableAnnotationSuffix
*/
public $maximumCommitsReplay = 20; // @phan-suppress-current-line PhanUndeclaredTypeProperty
protected function getPackage(string $directory, array $data): array
{
return [
'name' => (string) $data['name'],
'directory' => (string) realpath($directory),
'children' => [],
];
}
/**
* Get list of commit to replay.
*
* @param Split $cli
* @param string $hash
*
* @throws Exception
*
* @return Log
*/
protected function getReplayLog(Split $cli, string $hash): Log
{
$max = $this->maximumCommitsReplay;
$log = $this->latest((int) $max, '.');
$commits = [];
foreach ($log as /* @var Commit $commit */ $commit) {
if ($commit->getHash() === $hash) {
return new Log($commits);
}
array_unshift($commits, $commit);
}
$cli->writeLine('No matching commit found in the last '.$max.' commits, new step added.', 'yellow');
return new Log([$log[0]]);
}
/**
* Configure git local user to be the current author name and email.
*
* @param Author $author
*/
protected function setGitCommitter(Author $author): void
{
$this->git('config user.name '.$this->gitEscape($author->getName()));
$this->git('config user.email '.$this->gitEscape($author->getEmail()));
}
/**
* Copy the current directory recursively to a given destination path.
*
* @suppressWarnings(PHPMD.LongVariableName)
*
* @param string $destination
*/
protected function copyCurrentDirectory(string $destination): void
{
shell_exec('cp -r . '.escapeshellarg($destination).' 2>&1');
// @codeCoverageIgnoreStart
if (!file_exists($destination)) {
shell_exec('xcopy . '.escapeshellarg($destination).' /e /i /h');
}
// @codeCoverageIgnoreEnd
}
/**
* Get GIT local user config (.git/config file settings).
*
* @return array
*/
protected function getLocalUserConfig(): array
{
if ($this->localUser === null) {
$this->localUser = file_exists('.git/config')
? parse_ini_file('.git/config', true)['user'] ?? []
: [];
}
return $this->localUser;
}
/**
* Set a git config.
*
* @param string $name
* @param string|null $value
*
* @return string|null
*/
protected function setGitConfig(string $name, ?string $value): ?string
{
$config = $value === null
? "--unset $name"
: "$name ".$this->gitEscape($value);
return $this->git('config '.$config);
}
/**
* Apply the given commit in the current repository.
*
* @param Split $cli
* @param array $package
* @param Commit $commit
* @param string $distributionDirectory
* @param string $branch
*
* @suppressWarnings(PHPMD.LongVariableName)
*/
protected function cherryPickCommit(
Split $cli,
array $package,
Commit $commit,
string $distributionDirectory,
string $branch
): void {
$hash = $commit->getHash();
$this->git('config advice.detachedHead false');
$this->git("checkout -f $hash", [], '2>&1');
rename("$distributionDirectory/.git", $this->output.'/.git.temp');
$this->remove($distributionDirectory);
$this->copyCurrentDirectory($distributionDirectory);
$this->remove("$distributionDirectory/.git");
rename($this->output.'/.git.temp', "$distributionDirectory/.git");
$cli->chdir($distributionDirectory);
$this->setGitCommitter($commit->getCommit()->getAuthor());
$author = $commit->getAuthor();
$commitMessageFile = sys_get_temp_dir().'/commit-message-'.mt_rand(0, 99999999);
file_put_contents($commitMessageFile, $commit->getMessage()."\n\n".$this->hashPrefix.$hash);
$this->git('add .');
$this->git('commit --file='.escapeshellarg($commitMessageFile), [
'author' => $author,
'date' => $author->getDate(),
], '2>&1');
unlink($commitMessageFile);
if (!$this->noPush) {
$name = $package['name'];
$push = $this->git("push origin $branch", [], '2>&1');
$cli->writeLine("Pushing $name\n".$push, strpos($push, 'error:') === false ? 'light_cyan' : 'red');
}
$localUser = $this->getLocalUserConfig();
foreach (['name', 'email'] as $userConfig) {
$this->setGitConfig("user.$userConfig", $localUser[$userConfig] ?? null);
}
}
/**
* Distribute and update the sub-package.
*
* @param Split $cli
* @param array $package
* @param string $branch
*
* @throws Exception
*
* @suppressWarnings(PHPMD.LongVariableName)
*
* @return bool
*/
protected function distributePackage(Split $cli, array $package, string $branch): bool
{
if (!parent::distributePackage($cli, $package, $branch)) {
return false;
}
$hash = $this->getCurrentLinkedCommitHash();
$distributionDirectory = (string) getcwd();
$sourceDirectory = $package['directory'];
$cli->chdir($sourceDirectory);
$this->git('stash', [], '2>&1');
try {
$log = $hash ? $this->getReplayLog($cli, $hash) : $this->latest(1, '.');
foreach ($log as $commit) {
$cli->chdir($sourceDirectory);
$this->cherryPickCommit($cli, $package, $commit, $distributionDirectory, $branch);
}
} catch (EmptyLogList $exception) {
$cli->writeLine($package['name'].' is already up to date.', 'green');
}
$cli->chdir($sourceDirectory);
$this->git("checkout -f $branch", [], '2>&1');
$this->git('stash pop', [], '2>&1');
return true;
}
}