Skip to content

Commit 00ba0d9

Browse files
committed
Add unpair command
1 parent bebeab4 commit 00ba0d9

File tree

9 files changed

+412
-12
lines changed

9 files changed

+412
-12
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
55

66
## [Unreleased]
77

8+
## [v1.3.0] - 2024-07-15
9+
10+
### Added
11+
12+
- New `unpair` command.
13+
814
## [v1.2.2] - 2024-07-12
915

1016
### Added
@@ -57,7 +63,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
5763
## v1.0.0 - 2017-04-03
5864
- First release.
5965

60-
[Unreleased]: https://github.com/raphaelstolt/git-user-bend/compare/v1.2.2...HEAD
66+
[Unreleased]: https://github.com/raphaelstolt/git-user-bend/compare/v1.3.0...HEAD
67+
[v1.3.0]: https://github.com/raphaelstolt/git-user-bend/compare/v1.2.2...v1.3.0
6168
[v1.2.2]: https://github.com/raphaelstolt/git-user-bend/compare/v1.2.1...v1.2.2
6269
[v1.2.1]: https://github.com/raphaelstolt/git-user-bend/compare/v1.2.0...v1.2.1
6370
[v1.2.0]: https://github.com/raphaelstolt/git-user-bend/compare/v1.1.2...v1.2.0

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ used for the Git `user.email` configuration.
9292
git-user-bend pair "<alias1,aliasN>" [<directory>]
9393
```
9494

95+
To end a pair programming session and restore the default `user.name` and `user.email` the `unpair` command is available.
96+
97+
``` bash
98+
git-user-bend unpair <directory>
99+
```
100+
95101
To check the persona, pair or respectively the Git user configuration of a repository the `whoami` command is a
96102
pleasant shortcut.
97103
``` bash

bin/git-user-bend

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if (false === $autoloaded) {
2323
exit(1);
2424
}
2525

26+
use Stolt\GitUserBend\Commands\UnpairCommand;
2627
use Stolt\GitUserBend\Git\Repository;
2728
use Stolt\GitUserBend\Persona\Storage;
2829
use Stolt\GitUserBend\Helpers\Str as OsHelper;
@@ -44,7 +45,7 @@ if ((new OsHelper())->isWindows()) {
4445

4546
define('HOME_DIRECTORY', $homeDiretory);
4647
define('WORKING_DIRECTORY', getcwd());
47-
define('VERSION', '1.2.2');
48+
define('VERSION', '1.3.0');
4849
define(
4950
'STORAGE_FILE',
5051
HOME_DIRECTORY . DIRECTORY_SEPARATOR . Storage::FILE_NAME
@@ -59,6 +60,7 @@ $application->add(new AddCommand($storage));
5960
$application->add(new ImportCommand($storage, $repository));
6061
$application->add(new ExportCommand($storage, $repository));
6162
$application->add(new PairCommand($storage, $repository));
63+
$application->add(new UnpairCommand($repository));
6264
$application->add(new PersonasCommand($storage));
6365
$application->add(new RetireCommand($storage));
6466
$application->add(new UseCommand($storage, $repository));

src/Commands/UnpairCommand.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Stolt\GitUserBend\Commands;
4+
5+
use Stolt\GitUserBend\Exceptions\CommandFailed;
6+
use Stolt\GitUserBend\Exceptions\Exception;
7+
use Stolt\GitUserBend\Git\Repository;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class UnpairCommand extends Command
14+
{
15+
/**
16+
* @var Repository
17+
*/
18+
private Repository $repository;
19+
20+
/**
21+
* Initialize.
22+
*
23+
* @param Repository $repository
24+
*/
25+
public function __construct(Repository $repository)
26+
{
27+
$this->repository = $repository;
28+
29+
parent::__construct();
30+
}
31+
32+
/**
33+
* Command configuration.
34+
*
35+
* @return void
36+
*/
37+
protected function configure(): void
38+
{
39+
$this->setName('unpair');
40+
$this->setDescription('Finishes current pair programming session');
41+
42+
$directoryArgumentDescription = 'The directory of the Git repository';
43+
$this->addArgument(
44+
'directory',
45+
InputArgument::OPTIONAL,
46+
$directoryArgumentDescription,
47+
WORKING_DIRECTORY
48+
);
49+
}
50+
51+
/**
52+
* Execute command.
53+
*
54+
* @param InputInterface $input
55+
* @param OutputInterface $output
56+
*
57+
* @return integer
58+
*/
59+
protected function execute(InputInterface $input, OutputInterface $output): int
60+
{
61+
$directory = $input->getArgument('directory');
62+
63+
try {
64+
$this->repository->setDirectory((string) $directory);
65+
66+
$formerPersona = $this->repository->getFormerPersonaFromConfiguration();
67+
68+
$outputContent = "<info>Reset user config to <comment>"
69+
. "'{$formerPersona->getName()} <{$formerPersona->getEmail()}>'</comment>.</info>";
70+
$output->writeln($outputContent);
71+
72+
return self::SUCCESS;
73+
74+
} catch (Exception $e) {
75+
$error = "<error>Error:</error> " . $e->getInforizedMessage();
76+
$output->writeln($error);
77+
78+
return self::FAILURE;
79+
}
80+
}
81+
}

src/Git/Repository.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Stolt\GitUserBend\Exceptions\NotAGitRepository;
1111
use Stolt\GitUserBend\Exceptions\UnresolvablePair;
1212
use Stolt\GitUserBend\Exceptions\UnresolvablePersona;
13-
use Stolt\GitUserBend\Git\User;
1413
use Stolt\GitUserBend\Persona;
1514

1615
class Repository
@@ -162,6 +161,25 @@ public function getPersonaFromConfiguration(): Persona
162161
throw new UnresolvablePersona('Unable to resolve persona from Git configuration.');
163162
}
164163

164+
/**
165+
* @throws UnresolvablePersona
166+
* @throws InvalidPersona
167+
*/
168+
public function getFormerPersonaFromConfiguration(): Persona
169+
{
170+
chdir($this->directory);
171+
172+
$command = 'git config --get-regexp "^user.former.*"';
173+
exec($command, $output, $returnValue);
174+
175+
if ($returnValue === 0) {
176+
$localFormerGitUser = $this->factorFormerUser($output);
177+
return $localFormerGitUser->factorPersona();
178+
}
179+
180+
throw new UnresolvablePersona('Unable to resolve former persona from Git configuration.');
181+
}
182+
165183
/**
166184
* @return boolean
167185
*/
@@ -199,13 +217,40 @@ public function getPairUserFromConfiguration(): User
199217
}
200218

201219
/**
202-
* @param User $user The use to configure locally.
220+
* @throws UnresolvablePersona
221+
*/
222+
public function storePreviousUser(): bool
223+
{
224+
chdir($this->directory);
225+
226+
$persona = $this->getPersonaFromConfiguration();
227+
228+
$storageCommands = [
229+
"git config --local user.former.email {$persona->getEmail()}",
230+
"git config --local user.former.name '{$persona->getName()}'",
231+
];
232+
233+
foreach ($storageCommands as $command) {
234+
exec($command, $output, $returnValue);
235+
if ($returnValue !== 0) {
236+
return false;
237+
}
238+
}
239+
240+
return true;
241+
}
242+
243+
/**
244+
* @param User $user The use to configure locally.
245+
* @throws UnresolvablePersona
203246
* @return boolean
204247
*/
205248
public function setUser(User $user): bool
206249
{
207250
chdir($this->directory);
208251

252+
$this->storePreviousUser();
253+
209254
$commands = [
210255
"git config --local user.email \"{$user->getEmail()}\"",
211256
"git config --local user.name \"{$user->getName()}\"",
@@ -239,4 +284,23 @@ private function factorUser(array $output): User
239284

240285
return new User($name, $email, $alias);
241286
}
287+
288+
/**
289+
* @param array $output
290+
* @return User
291+
*/
292+
private function factorFormerUser(array $output): User
293+
{
294+
foreach ($output as $keyValueLine) {
295+
list($key, $value) = explode(' ', $keyValueLine, 2);
296+
$key = str_replace('user.former.', '', $key);
297+
$user[$key] = str_replace("'", '', $value);
298+
}
299+
300+
$name = $user['name'] ?? null;
301+
$email = $user['email'] ?? null;
302+
$alias = $user['alias'] ?? User::REPOSITORY_USER_ALIAS;
303+
304+
return new User($name, $email, $alias);
305+
}
242306
}

tests/Commands/PairCommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\Attributes\Test;
88
use Stolt\GitUserBend\Commands\PairCommand;
99
use Stolt\GitUserBend\Git\Repository;
10+
use Stolt\GitUserBend\Git\User;
1011
use Stolt\GitUserBend\Persona;
1112
use Stolt\GitUserBend\Persona\Collection;
1213
use Stolt\GitUserBend\Persona\Storage;
@@ -231,7 +232,7 @@ public function returnsExpectedWarningWhenSetPairPersonaFails(): void
231232
#[Group('integration')]
232233
public function setsPairPersonasAndIncrementsUsageFrequencies(): void
233234
{
234-
$this->createTemporaryGitRepository();
235+
$this->createTemporaryGitRepository(new User('John Doe', '[email protected]'));
235236

236237
$existingStorageContent = <<<CONTENT
237238
[{"alias":"jd","name":"John Doe","email":"[email protected]","usage_frequency":11},

0 commit comments

Comments
 (0)