|
10 | 10 | use Stolt\GitUserBend\Exceptions\NotAGitRepository; |
11 | 11 | use Stolt\GitUserBend\Exceptions\UnresolvablePair; |
12 | 12 | use Stolt\GitUserBend\Exceptions\UnresolvablePersona; |
13 | | -use Stolt\GitUserBend\Git\User; |
14 | 13 | use Stolt\GitUserBend\Persona; |
15 | 14 |
|
16 | 15 | class Repository |
@@ -162,6 +161,25 @@ public function getPersonaFromConfiguration(): Persona |
162 | 161 | throw new UnresolvablePersona('Unable to resolve persona from Git configuration.'); |
163 | 162 | } |
164 | 163 |
|
| 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 | + |
165 | 183 | /** |
166 | 184 | * @return boolean |
167 | 185 | */ |
@@ -199,13 +217,40 @@ public function getPairUserFromConfiguration(): User |
199 | 217 | } |
200 | 218 |
|
201 | 219 | /** |
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 |
203 | 246 | * @return boolean |
204 | 247 | */ |
205 | 248 | public function setUser(User $user): bool |
206 | 249 | { |
207 | 250 | chdir($this->directory); |
208 | 251 |
|
| 252 | + $this->storePreviousUser(); |
| 253 | + |
209 | 254 | $commands = [ |
210 | 255 | "git config --local user.email \"{$user->getEmail()}\"", |
211 | 256 | "git config --local user.name \"{$user->getName()}\"", |
@@ -239,4 +284,23 @@ private function factorUser(array $output): User |
239 | 284 |
|
240 | 285 | return new User($name, $email, $alias); |
241 | 286 | } |
| 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 | + } |
242 | 306 | } |
0 commit comments