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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?php
/**
*
* @package phpBB3
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
if ($argc < 2)
{
show_usage($argv);
}
if (file_exists('.git'))
{
echo "[error] git repository already exists\n";
exit(1);
}
// Handle arguments
$scope = get_arg($argv, 1, '');
$developer = get_arg($argv, 2, '');
// Github setup
$username = 'phpbb';
$repository = 'phpbb3';
// Get some basic data
$network = get_network($username, $repository);
$collaborators = get_collaborators($username, $repository);
// Clone the blessed repository
clone_repository($username, $repository, isset($collaborators[$developer]));
switch ($scope)
{
case 'collaborators':
$remotes = array_intersect_key($network, $collaborators);
break;
case 'organisation':
$remotes = array_intersect_key($network, get_organisation_members($username));
break;
case 'contributors':
$remotes = array_intersect_key($network, get_contributors($username, $repository));
break;
case 'network':
$remotes = $network;
break;
default:
show_usage();
}
foreach ($remotes as $remote)
{
if ($remote['username'] == $username)
{
// Skip blessed repository.
continue;
}
add_remote($remote['username'], $remote['repository'], $remote['username'] == $developer);
}
run('git remote update');
function clone_repository($username, $repository, $pushable = false)
{
$url = get_repository_url($username, $repository, false);
run("git clone $url ./");
if ($pushable)
{
$ssh_url = get_repository_url($username, $repository, true);
run("git remote set-url --push origin $ssh_url");
}
}
function add_remote($username, $repository, $pushable = false)
{
$url = get_repository_url($username, $repository, false);
run("git remote add $username $url");
if ($pushable)
{
$ssh_url = get_repository_url($username, $repository, true);
run("git remote set-url --push $username $ssh_url");
}
}
function get_repository_url($username, $repository, $ssh = false)
{
$url_base = ($ssh) ? 'git@github.com:' : 'git://github.com/';
return $url_base . $username . '/' . $repository . '.git';
}
function api_request($query)
{
return json_decode(file_get_contents("http://github.com/api/v2/json/$query"));
}
function get_contributors($username, $repository)
{
$request = api_request("repos/show/$username/$repository/contributors");
$usernames = array();
foreach ($request->contributors as $contributor)
{
$usernames[$contributor->login] = $contributor->login;
}
return $usernames;
}
function get_organisation_members($username)
{
$request = api_request("organizations/$username/public_members");
$usernames = array();
foreach ($request->users as $member)
{
$usernames[$member->login] = $member->login;
}
return $usernames;
}
function get_collaborators($username, $repository)
{
$request = api_request("repos/show/$username/$repository/collaborators");
$usernames = array();
foreach ($request->collaborators as $collaborator)
{
$usernames[$collaborator] = $collaborator;
}
return $usernames;
}
function get_network($username, $repository)
{
$request = api_request("repos/show/$username/$repository/network");
$usernames = array();
foreach ($request->network as $network)
{
$usernames[$network->owner] = array(
'username' => $network->owner,
'repository' => $network->name,
);
}
return $usernames;
}
function show_usage($argv)
{
printf(
"usage: php %s collaborators|organisation|contributors|network [your_github_username]\n",
basename($argv[0])
);
exit(1);
}
function get_arg($argv, $index, $default)
{
return isset($argv[$index]) ? $argv[$index] : $default;
}
function run($cmd)
{
passthru(escapeshellcmd($cmd));
}
|