Skip to content

Commit a56bb27

Browse files
committed
Split Update::distributePackage method
1 parent a5ba9f0 commit a56bb27

17 files changed

Lines changed: 119 additions & 82 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ before_script:
4040
script:
4141
- if [[ "$STYLECHECK" != "on" && "$COVERAGE" != "on" ]]; then vendor/bin/phpunit; fi;
4242
- if [[ "$COVERAGE" = "on" ]]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml; fi;
43-
- if [[ "$MULTITEST" = "on" ]]; then vendor/bin/multi-tester --verbose --quiet-install; fi;
43+
- if [[ "$STYLECHECK" = "on" ]]; then composer style-check; fi;
4444

4545
after_success:
4646
- if [[ "$COVERAGE" = "on" ]]; then bash <(curl -s https://codecov.io/bash); fi;

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@
5252
"style-check": [
5353
"@phpcs",
5454
"@phpstan",
55-
"@phpmd",
56-
"@psalm"
55+
"@phpmd"
5756
],
5857
"phpunit": "phpunit --verbose",
5958
"phpcs": "php-cs-fixer fix -v --diff --dry-run",
6059
"phpstan": "phpstan analyse src tests",
61-
"phpmd": "phpmd src text rulesets.xml",
60+
"phpmd": "phpmd src text phpmd.xml",
6261
"psalm": "psalm"
6362
}
6463
}

rulesets.xml renamed to phpmd.xml

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,7 @@
55
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
66
<description>Custom rules for checking my project</description>
77

8-
<rule ref="rulesets/codesize.xml">
9-
<exclude name="TooManyPublicMethods"/>
10-
</rule>
11-
12-
<rule ref="rulesets/codesize.xml/TooManyPublicMethods">
13-
<properties>
14-
<property name="maxmethods" value="20" />
15-
</properties>
16-
</rule>
17-
18-
<rule ref="rulesets/codesize.xml">
19-
<exclude name="WeightedMethodCount"/>
20-
</rule>
21-
22-
<rule ref="rulesets/codesize.xml/WeightedMethodCount">
23-
<properties>
24-
<property name="maximum" value="80" />
25-
</properties>
26-
</rule>
27-
28-
<rule ref="rulesets/design.xml">
29-
<exclude name="EvalExpression"/>
30-
</rule>
31-
8+
<rule ref="rulesets/codesize.xml" />
329
<rule ref="rulesets/cleancode.xml" />
3310
<rule ref="rulesets/controversial.xml" />
3411
<rule ref="rulesets/design.xml" />

src/Phug/Split/Command/CommandBase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ protected function git(string $command, array $options = [], string $redirect =
6767
*
6868
* @throws Exception
6969
*
70+
* @suppressWarnings(PHPMD.StaticAccess)
71+
*
7072
* @return Log|Commit[]
7173
*/
7274
protected function latest($count = 1, string $directory = ''): Log

src/Phug/Split/Command/Update.php

Lines changed: 96 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Phug\Split;
77
use Phug\Split\Command\Options\HashPrefix;
88
use Phug\Split\Git\Author;
9+
use Phug\Split\Git\Commit;
910
use Phug\Split\Git\EmptyLogList;
1011
use Phug\Split\Git\Log;
1112

@@ -83,6 +84,98 @@ protected function setGitCommitter(Author $author): void
8384
$this->git('config user.email '.$this->gitEscape($author->getEmail()));
8485
}
8586

87+
/**
88+
* Copy the current directory recursively to a given destination path.
89+
*
90+
* @suppressWarnings(PHPMD.LongVariableName)
91+
*
92+
* @param string $destination
93+
*/
94+
protected function copyCurrentDirectory(string $destination): void
95+
{
96+
shell_exec('cp -r . '.escapeshellarg($destination).' 2>&1');
97+
98+
// @codeCoverageIgnoreStart
99+
if (!file_exists($destination)) {
100+
shell_exec('xcopy . '.escapeshellarg($destination).' /e /i /h');
101+
}
102+
// @codeCoverageIgnoreEnd
103+
}
104+
105+
/**
106+
* Get GIT local user config (.git/config file settings).
107+
*
108+
* @return array
109+
*/
110+
protected function getLocalUserConfig(): array
111+
{
112+
static $localUser = null;
113+
114+
if ($localUser === null) {
115+
$localUser = file('.git/config')
116+
? parse_ini_file('.git/config', true)['user'] ?? []
117+
: [];
118+
}
119+
120+
return $localUser;
121+
}
122+
123+
/**
124+
* Apply the given commit in the current repository.
125+
*
126+
* @param Split $cli
127+
* @param array $package
128+
* @param Commit $commit
129+
* @param string $distributionDirectory
130+
* @param string $branch
131+
*
132+
* @suppressWarnings(PHPMD.LongVariableName)
133+
*/
134+
protected function cherryPickCommit(
135+
Split $cli,
136+
array $package,
137+
Commit $commit,
138+
string $distributionDirectory,
139+
string $branch
140+
): void {
141+
$hash = $commit->getHash();
142+
$this->git('config advice.detachedHead false');
143+
$this->git("checkout -f $hash", [], '2>&1');
144+
rename("$distributionDirectory/.git", $this->output.'/.git.temp');
145+
$this->remove($distributionDirectory);
146+
$this->copyCurrentDirectory($distributionDirectory);
147+
148+
$this->remove("$distributionDirectory/.git");
149+
rename($this->output.'/.git.temp', "$distributionDirectory/.git");
150+
$cli->chdir($distributionDirectory);
151+
$this->setGitCommitter($commit->getCommit()->getAuthor());
152+
$author = $commit->getAuthor();
153+
154+
$commitMessageFile = sys_get_temp_dir().'/commit-message-'.mt_rand(0, 99999999);
155+
file_put_contents($commitMessageFile, $commit->getMessage()."\n\n".$this->hashPrefix.$hash);
156+
$this->git('add .');
157+
$this->git('commit --file='.escapeshellarg($commitMessageFile), [
158+
'author' => $author,
159+
'date' => $author->getDate(),
160+
], '2>&1');
161+
unlink($commitMessageFile);
162+
163+
if (!$this->noPush) {
164+
$name = $package['name'];
165+
$push = (string) $this->git("push origin $branch", [], '2>&1');
166+
$cli->writeLine("Pushing $name\n".$push, strpos($push, 'error:') === false ? 'light_cyan' : 'red');
167+
}
168+
169+
$localUser = $this->getLocalUserConfig();
170+
171+
foreach (['name', 'email'] as $userConfig) {
172+
$config = empty($localUser[$userConfig])
173+
? "--unset user.$userConfig"
174+
: "user.$userConfig ".$this->gitEscape($localUser[$userConfig]);
175+
$this->git('config '.$config);
176+
}
177+
}
178+
86179
/**
87180
* Distribute and update the sub-package.
88181
*
@@ -92,6 +185,8 @@ protected function setGitCommitter(Author $author): void
92185
*
93186
* @throws Exception
94187
*
188+
* @suppressWarnings(PHPMD.LongVariableName)
189+
*
95190
* @return bool
96191
*/
97192
protected function distributePackage(Split $cli, array $package, string $branch): bool
@@ -103,9 +198,6 @@ protected function distributePackage(Split $cli, array $package, string $branch)
103198
$hash = $this->getCurrentLinkedCommitHash();
104199
$distributionDirectory = getcwd();
105200
$sourceDirectory = $package['directory'];
106-
$localUser = file('.git/config')
107-
? parse_ini_file('.git/config', true)['user'] ?? []
108-
: [];
109201

110202
$cli->chdir($sourceDirectory);
111203
$this->git('stash', [], '2>&1');
@@ -115,46 +207,7 @@ protected function distributePackage(Split $cli, array $package, string $branch)
115207

116208
foreach ($log as $commit) {
117209
$cli->chdir($sourceDirectory);
118-
$hash = $commit->getHash();
119-
$this->git('config advice.detachedHead false');
120-
$this->git("checkout -f $hash", [], '2>&1');
121-
rename("$distributionDirectory/.git", $this->output.'/.git.temp');
122-
$this->remove($distributionDirectory);
123-
shell_exec('cp -r . '.escapeshellarg($distributionDirectory).' 2>&1');
124-
125-
// @codeCoverageIgnoreStart
126-
if (!file_exists($distributionDirectory)) {
127-
shell_exec('xcopy . '.escapeshellarg($distributionDirectory).' /e /i /h');
128-
}
129-
// @codeCoverageIgnoreEnd
130-
131-
$this->remove("$distributionDirectory/.git");
132-
rename($this->output.'/.git.temp', "$distributionDirectory/.git");
133-
$cli->chdir($distributionDirectory);
134-
$this->setGitCommitter($commit->getCommit()->getAuthor());
135-
$author = $commit->getAuthor();
136-
137-
$commitMessageFile = sys_get_temp_dir().'/commit-message-'.mt_rand(0, 99999999);
138-
file_put_contents($commitMessageFile, $commit->getMessage()."\n\n".$this->hashPrefix.$hash);
139-
$this->git('add .');
140-
$this->git('commit --file='.escapeshellarg($commitMessageFile), [
141-
'author' => $author,
142-
'date' => $author->getDate(),
143-
], '2>&1');
144-
unlink($commitMessageFile);
145-
146-
if (!$this->noPush) {
147-
$name = $package['name'];
148-
$push = (string) $this->git("push origin $branch", [], '2>&1');
149-
$cli->writeLine("Pushing $name\n".$push, strpos($push, 'error:') === false ? 'light_cyan' : 'red');
150-
}
151-
152-
foreach (['name', 'email'] as $userConfig) {
153-
$config = empty($localUser[$userConfig])
154-
? "--unset user.$userConfig"
155-
: "user.$userConfig ".$this->gitEscape($localUser[$userConfig]);
156-
$this->git('config '.$config);
157-
}
210+
$this->cherryPickCommit($cli, $package, $commit, $distributionDirectory, $branch);
158211
}
159212
} catch (EmptyLogList $exception) {
160213
$cli->writeLine($package['name'].' is already up to date.', 'green');

src/Phug/Split/Git/Log.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public function __construct(array $commits)
5959
*
6060
* @throws InvalidGitLogStringException|Exception
6161
*
62+
* @suppressWarnings(PHPMD.StaticAccess)
63+
*
6264
* @return self
6365
*/
6466
public static function fromGitLogString(string $log): self
@@ -108,6 +110,8 @@ public function offsetGet($offset): Commit
108110
*
109111
* @throws ImmutableLogException
110112
*
113+
* @suppressWarnings(PHPMD.UnusedFormalParameter)
114+
*
111115
* @return void
112116
*/
113117
public function offsetSet($offset, $value): void
@@ -124,6 +128,8 @@ public function offsetSet($offset, $value): void
124128
*
125129
* @throws ImmutableLogException
126130
*
131+
* @suppressWarnings(PHPMD.UnusedFormalParameter)
132+
*
127133
* @return void
128134
*/
129135
public function offsetUnset($offset): void

tests/Phug/Split/Command/AnalyzeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Phug\Test\Split\Command;
3+
namespace Phug\Tests\Split\Command;
44

55
use Nette\Utils\FileSystem;
66
use PHPUnit\Framework\TestCase;

tests/Phug/Split/Command/CommandBaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Phug\Test\Split\Command;
3+
namespace Phug\Tests\Split\Command;
44

55
use Nette\Utils\FileSystem;
66
use PHPUnit\Framework\TestCase;

tests/Phug/Split/Command/CopyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Phug\Test\Split\Command;
3+
namespace Phug\Tests\Split\Command;
44

55
use Nette\Utils\FileSystem;
66
use PHPUnit\Framework\TestCase;

tests/Phug/Split/Command/DistTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Phug\Test\Split\Command;
3+
namespace Phug\Tests\Split\Command;
44

55
use Nette\Utils\FileSystem;
66
use PHPUnit\Framework\TestCase;

0 commit comments

Comments
 (0)