-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-set-user
executable file
·53 lines (49 loc) · 1.61 KB
/
gh-set-user
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
#!/usr/bin/env php
<?php
error_reporting(~E_WARNING);
$context = stream_context_create([
'http' => [
'method' => 'GET',
'user_agent' => 'goncalomb/dotfiles'
]
]);
echo "Who are you on GitHub? (e.g. 'user' or 'user/repository'):\n";
$value = trim(fgets(STDIN));
if (preg_match('/^([^\/]+)(?:\/([^\/]+))?$/', $value, $matches)) {
echo "\nFetching data...\n";
$name = $email = null;
if (empty($matches[2])) {
$url = 'https://api.github.com/users/' . urlencode($matches[1]);
$data = json_decode(file_get_contents($url, false, $context), true);
if ($data) {
$name = $data['name'];
$email = $data['email'];
}
} else {
$url = 'https://api.github.com/repos/' . urlencode($matches[1]) . '/' . urlencode($matches[2]) . '/commits?author=' . urlencode($matches[1]);
$data = json_decode(file_get_contents($url, false, $context), true);
if ($data && count($data)) {
$name = $data[0]['commit']['author']['name'];
$email = $data[0]['commit']['author']['email'];
}
}
if ($name) {
echo "Setting user.name to '$name'.\n";
passthru('git config --global user.name ' . escapeshellarg($name));
if ($email) {
echo "Setting user.email to '$email'.\n";
passthru('git config --global user.email ' . escapeshellarg($email));
} else {
echo "Email not found, user.email not set!\n";
}
echo "Setting personal GitHub URL rewrite (ghme:).\n";
passthru('git config --global ' . escapeshellarg('[email protected]:' . $matches[1] . '/.insteadOf') . ' ghme:');
echo "\n";
passthru('git config --global --list');
} else {
echo "Error, user.name and user.email not set!\n";
}
} else {
echo "Invalid input.\n";
}
?>