Skip to content

Commit 4db20fe

Browse files
committed
fix: detect author name and email using git config
1 parent 1f5f8d6 commit 4db20fe

File tree

5 files changed

+124
-31
lines changed

5 files changed

+124
-31
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## 3.1.1 - 2021-08-05
9+
10+
### Added
11+
12+
- Nothing.
13+
14+
### Changed
15+
16+
- Nothing.
17+
18+
### Deprecated
19+
20+
- Nothing.
21+
22+
### Removed
23+
24+
- Nothing.
25+
26+
### Fixed
27+
28+
- Detect author name and email using Git config
29+
830
## 3.1.0 - 2021-08-05
931

1032
### Added

src/LibraryStarterKit/Task/Builder/SetupRepository.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Ramsey\Dev\LibraryStarterKit\Task\Builder;
2626
use Symfony\Component\Process\Process;
2727

28+
use function sprintf;
2829
use function trim;
2930

3031
/**
@@ -132,9 +133,15 @@ private function gitAddAllFiles(): self
132133

133134
private function gitInitialCommit(): self
134135
{
136+
$author = sprintf(
137+
'%s <%s>',
138+
(string) $this->getAnswers()->authorName,
139+
(string) $this->getAnswers()->authorEmail,
140+
);
141+
135142
$this
136143
->getEnvironment()
137-
->getProcess(['git', 'commit', '-n', '-m', self::COMMIT_MSG])
144+
->getProcess(['git', 'commit', '-n', '-m', self::COMMIT_MSG, '--author', $author])
138145
->mustRun($this->streamProcessOutput());
139146

140147
return $this;
@@ -155,14 +162,14 @@ private function gitConfigUser(): self
155162
$userName = $this->getUserName();
156163
$userEmail = $this->getUserEmail();
157164

158-
if ($userName === '') {
165+
if ($userName !== $this->getAnswers()->authorName) {
159166
$this
160167
->getEnvironment()
161168
->getProcess(['git', 'config', 'user.name', (string) $this->getAnswers()->authorName])
162169
->mustRun($this->streamProcessOutput());
163170
}
164171

165-
if ($userEmail === '') {
172+
if ($userEmail !== $this->getAnswers()->authorEmail) {
166173
$this
167174
->getEnvironment()
168175
->getProcess(['git', 'config', 'user.email', (string) $this->getAnswers()->authorEmail])

src/LibraryStarterKit/Wizard.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
use function realpath;
4545
use function sprintf;
4646
use function strtolower;
47+
use function trim;
4748

4849
class Wizard extends Command
4950
{
@@ -70,6 +71,14 @@ public function __construct(Setup $setup, ?StyleFactory $styleFactory = null)
7071
if ($this->answers->projectName === null) {
7172
$this->answers->projectName = $this->setup->getProject()->getName();
7273
}
74+
75+
if ($this->answers->authorName === null) {
76+
$this->answers->authorName = $this->getGitUserName();
77+
}
78+
79+
if ($this->answers->authorEmail === null) {
80+
$this->answers->authorEmail = $this->getGitUserEmail();
81+
}
7382
}
7483

7584
public function getSetup(): Setup
@@ -111,6 +120,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
111120
return 0;
112121
}
113122

123+
private function getGitUserName(): ?string
124+
{
125+
$process = $this->getSetup()->getProcess(
126+
['git', 'config', 'user.name'],
127+
);
128+
129+
$process->run();
130+
131+
return trim($process->getOutput()) ?: null;
132+
}
133+
134+
private function getGitUserEmail(): ?string
135+
{
136+
$process = $this->getSetup()->getProcess(
137+
['git', 'config', 'user.email'],
138+
);
139+
140+
$process->run();
141+
142+
return trim($process->getOutput()) ?: null;
143+
}
144+
114145
private function confirmStart(SymfonyStyle $console): bool
115146
{
116147
$getStarted = new ConfirmationQuestion('Are you ready to get started?', false);

tests/LibraryStarterKit/Task/Builder/SetupRepositoryTest.php

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Console\Style\SymfonyStyle;
1515
use Symfony\Component\Process\Process;
1616

17+
use function sprintf;
18+
1719
class SetupRepositoryTest extends TestCase
1820
{
1921
/**
@@ -23,33 +25,53 @@ public function buildProvider(): array
2325
{
2426
return [
2527
[
26-
'configUserName' => '',
27-
'configUserEmail' => '',
28+
'configUserName' => 'Jane Doe',
29+
'configUserEmail' => '[email protected]',
2830
'configDefaultBranch' => '',
31+
'authorName' => 'Jane Doe',
32+
'authorEmail' => '[email protected]',
33+
'expectedName' => 'Jane Doe',
34+
'expectedEmail' => '[email protected]',
2935
'expectedDefaultBranch' => 'main',
3036
],
3137
[
3238
'configUserName' => '',
3339
'configUserEmail' => '',
3440
'configDefaultBranch' => 'my-custom-branch-name',
41+
'authorName' => 'Jane Doe',
42+
'authorEmail' => '[email protected]',
43+
'expectedName' => 'Jane Doe',
44+
'expectedEmail' => '[email protected]',
3545
'expectedDefaultBranch' => 'my-custom-branch-name',
3646
],
3747
[
3848
'configUserName' => 'Frodo Baggins',
3949
'configUserEmail' => '',
4050
'configDefaultBranch' => '',
51+
'authorName' => 'Jane Doe',
52+
'authorEmail' => '[email protected]',
53+
'expectedName' => 'Jane Doe',
54+
'expectedEmail' => '[email protected]',
4155
'expectedDefaultBranch' => 'main',
4256
],
4357
[
4458
'configUserName' => '',
4559
'configUserEmail' => '[email protected]',
4660
'configDefaultBranch' => '',
61+
'authorName' => 'Jane Doe',
62+
'authorEmail' => '[email protected]',
63+
'expectedName' => 'Jane Doe',
64+
'expectedEmail' => '[email protected]',
4765
'expectedDefaultBranch' => 'main',
4866
],
4967
[
5068
'configUserName' => 'Samwise Gamgee',
5169
'configUserEmail' => '[email protected]',
5270
'configDefaultBranch' => 'default',
71+
'authorName' => 'Jane Doe',
72+
'authorEmail' => '[email protected]',
73+
'expectedName' => 'Jane Doe',
74+
'expectedEmail' => '[email protected]',
5375
'expectedDefaultBranch' => 'default',
5476
],
5577
];
@@ -62,10 +84,14 @@ public function testBuild(
6284
string $configUserName,
6385
string $configUserEmail,
6486
string $configDefaultBranch,
87+
string $authorName,
88+
string $authorEmail,
89+
string $expectedName,
90+
string $expectedEmail,
6591
string $expectedDefaultBranch
6692
): void {
67-
$this->answers->authorName = 'Jane Doe';
68-
$this->answers->authorEmail = '[email protected]';
93+
$this->answers->authorName = $authorName;
94+
$this->answers->authorEmail = $authorEmail;
6995

7096
$console = $this->mockery(SymfonyStyle::class);
7197
$console->expects()->section('Setting up Git repository');
@@ -126,38 +152,18 @@ public function testBuild(
126152
->getProcess(['git', 'init'])
127153
->andReturn($processMustRunWritesOutput);
128154

129-
if ($configUserName === '') {
130-
// If there is no global user.name set, then we expect this to be called.
155+
if ($configUserName !== $authorName) {
131156
$environment
132157
->expects()
133-
->getProcess(['git', 'config', 'user.name', 'Jane Doe'])
158+
->getProcess(['git', 'config', 'user.name', $expectedName])
134159
->andReturn($processMustRunWithCallable);
135-
} else {
136-
$environment
137-
->expects()
138-
->getProcess(['git', 'config', 'user.name', 'Jane Doe'])
139-
->never();
140-
$environment
141-
->expects()
142-
->getProcess(['git', 'config', 'user.name', $configUserName])
143-
->never();
144160
}
145161

146-
if ($configUserEmail === '') {
147-
// If there is no global user.email set, then we expect this to be called.
162+
if ($configUserEmail !== $authorEmail) {
148163
$environment
149164
->expects()
150-
->getProcess(['git', 'config', 'user.email', '[email protected]'])
165+
->getProcess(['git', 'config', 'user.email', $expectedEmail])
151166
->andReturn($processMustRunWithCallable);
152-
} else {
153-
$environment
154-
->expects()
155-
->getProcess(['git', 'config', 'user.email', '[email protected]'])
156-
->never();
157-
$environment
158-
->expects()
159-
->getProcess(['git', 'config', 'user.email', $configUserEmail])
160-
->never();
161167
}
162168

163169
$environment
@@ -188,6 +194,8 @@ public function testBuild(
188194
'-n',
189195
'-m',
190196
'chore: initialize project using ramsey/php-library-starter-kit',
197+
'--author',
198+
sprintf('%s <%s>', $expectedName, $expectedEmail),
191199
])
192200
->andReturn($processMustRunWithCallable);
193201

tests/LibraryStarterKit/WizardTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Console\Output\OutputInterface;
2626
use Symfony\Component\Console\Question\ConfirmationQuestion;
2727
use Symfony\Component\Console\Question\Question;
28+
use Symfony\Component\Process\Process;
2829

2930
use function dirname;
3031
use function get_class;
@@ -44,6 +45,12 @@ public function testGetSetup(): void
4445
->path('.starter-kit-answers')
4546
->andReturn('/path/to/.starter-kit-answers');
4647

48+
$nullProcess = $this->mockery(Process::class);
49+
$nullProcess->expects()->run()->twice();
50+
$nullProcess->expects()->getOutput()->twice()->andReturn('');
51+
$setup->expects()->getProcess(['git', 'config', 'user.name'])->andReturn($nullProcess);
52+
$setup->expects()->getProcess(['git', 'config', 'user.email'])->andReturn($nullProcess);
53+
4754
$filesystem = $this->mockery(Filesystem::class);
4855
$filesystem->expects()->exists('/path/to/.starter-kit-answers')->andReturnFalse();
4956

@@ -77,6 +84,12 @@ public function testRunWhenUserChoosesNotToStart(): void
7784
->path('.starter-kit-answers')
7885
->andReturn('/path/to/app/.starter-kit-answers');
7986

87+
$nullProcess = $this->mockery(Process::class);
88+
$nullProcess->expects()->run()->twice();
89+
$nullProcess->expects()->getOutput()->twice()->andReturn('');
90+
$setup->expects()->getProcess(['git', 'config', 'user.name'])->andReturn($nullProcess);
91+
$setup->expects()->getProcess(['git', 'config', 'user.email'])->andReturn($nullProcess);
92+
8093
$filesystem = $this->mockery(Filesystem::class);
8194
$filesystem->expects()->exists('/path/to/app/.starter-kit-answers')->andReturnFalse();
8295
$filesystem->expects()->dumpFile('/path/to/app/.starter-kit-answers', new IsTypeOf('string'));
@@ -139,6 +152,12 @@ public function testRunWhenUserConfirmsStart(): void
139152
->path('.starter-kit-answers')
140153
->andReturn('/path/to/.starter-kit-answers');
141154

155+
$nullProcess = $this->mockery(Process::class);
156+
$nullProcess->expects()->run()->twice();
157+
$nullProcess->expects()->getOutput()->twice()->andReturn('');
158+
$setup->expects()->getProcess(['git', 'config', 'user.name'])->andReturn($nullProcess);
159+
$setup->expects()->getProcess(['git', 'config', 'user.email'])->andReturn($nullProcess);
160+
142161
$filesystem = $this->mockery(Filesystem::class);
143162
$filesystem->expects()->exists('/path/to/.starter-kit-answers')->andReturnFalse();
144163

@@ -257,6 +276,12 @@ public function testRunWhenExceptionIsThrownWithVerbosity(
257276
$setup->shouldReceive('run')->once()->andThrow($exception);
258277
$setup->expects()->path('.starter-kit-answers')->andReturn('/path/to/.starter-kit-answers');
259278

279+
$nullProcess = $this->mockery(Process::class);
280+
$nullProcess->expects()->run()->twice();
281+
$nullProcess->expects()->getOutput()->twice()->andReturn('');
282+
$setup->expects()->getProcess(['git', 'config', 'user.name'])->andReturn($nullProcess);
283+
$setup->expects()->getProcess(['git', 'config', 'user.email'])->andReturn($nullProcess);
284+
260285
$filesystem = $this->mockery(Filesystem::class);
261286
$filesystem->expects()->exists('/path/to/.starter-kit-answers')->andReturnFalse();
262287

0 commit comments

Comments
 (0)