-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage2.php
167 lines (138 loc) · 4.42 KB
/
stage2.php
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
<?php
require_once('config.php');
function download($url, $dir)
{
$output = [];
$return = 0;
exec('timeout ' . config()['downloader']['timeout'] . 's git clone ' . escapeshellarg($url) . ' ' . escapeshellarg($dir) . ' 2>&1', $output, $return);
if ($return) return false;
return true;
}
function download_metadata($repo)
{
$url = 'https://api.github.com/repos/' . $repo;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, config()['github']['useragent']);
curl_setopt($ch, CURLOPT_USERPWD, config_user());
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$api_response = curl_exec($ch);
curl_close($ch);
$api_response = json_decode($api_response, true);
return $api_response;
}
function process($dir)
{
$oldcwd = getcwd();
chdir($dir);
$data = [];
// Get a list of all commits
$commits = [];
exec('git log --cherry-pick --topo-order --pretty=format:"%H;%aI;%cI;%P" --cherry-pick ', $commits);
$data['commits'] = [];
foreach ($commits as $commit) {
$commit = explode(';', $commit);
$data['commits'][$commit[0]] = $commit;
}
// Find all files in master branch which match the pattern
$files = [];
$includes = implode(' ', array_map(function($include) {
return '--include '.escapeshellarg($include);
}, config()['downloader']['pattern']));
$terms = implode(' ', array_map(function($term) {
return '-e '.escapeshellarg($term);
}, config()['downloader']['term']));
exec('grep -rl '.$includes.' '.$terms, $files);
$files = array_unique($files);
foreach ($files as $file) {
// Find all revisions for each matched file, there is at least the current one
$file_commits = [];
exec('git log --cherry-pick --topo-order --pretty=format:%H ' . escapeshellarg($file), $file_commits);
$data['files'][$file] = [];
$commits = [];
foreach ($file_commits as $line) {
if (preg_match('/^(?<hash>[a-f0-9]{40})/', $line, $results)) {
$commits[] = $results['hash'];
}
}
foreach ($commits as $commit) {
// Dump content of each file at each revision
$content = shell_exec('git show ' . escapeshellarg($commit) . ':' . escapeshellarg($file) . ' 2> /dev/null');
// $content can be empty - we do expect that if a file has been deleted temporarily!
$data['files'][$file][$commit] = [
'commit' => $commit,
'source' => mb_convert_encoding($content, 'UTF-8'),
'filename' => $file,
];
}
}
chdir($oldcwd);
return $data;
}
function wrapper($repo)
{
$basedir = config()['downloader']['tmp_dir'];
$dir = $basedir . DIRECTORY_SEPARATOR . base64_encode($repo);
$lockfile = $basedir . DIRECTORY_SEPARATOR . base64_encode($repo) . '.lock';
$datafile = 'repos/' . base64_encode($repo) . '.json';
// Create lock dir if non existent yet
if (!file_exists(dirname($lockfile))) {
mkdir(dirname($lockfile), 0777, true);
}
// Create data dir if non existent yet
if (!file_exists(dirname($datafile))) {
mkdir(dirname($datafile), 0777, true);
}
// Attempt to lock
$lock = fopen($lockfile, 'w');
if (flock($lock, LOCK_EX | LOCK_NB, $blocked) && !file_exists($datafile)) {
$data = ['name' => $repo];
$data['github'] = download_metadata($repo);
$skip = false;
if (!isset($data['github']['private']) || $data['github']['private'] != false) {
echo "$repo is private\n";
$skip = true;
}
if (isset($data['github']['size'])) {
if ($data['github']['size'] * 1024 > config()['downloader']['max_size']) {
echo "$repo is too large\n";
$skip = true;
}
} else {
echo "$repo not found\n";
$skip = true;
}
if (!$skip) {
echo "Downloading $repo\n";
@mkdir($dir, 0777, true);
// Credentials are only evaluated for private repositories
// Specifying them enforces proper bailout with 403
// Omitting them causes git to hang
$url = 'https://' . config_user() . '@github.com/' . $repo . '.git';
if (download($url, $dir)) {
echo "Processing $repo\n";
$data = array_merge($data, process($dir));
} else {
echo "Download for $repo failed\n";
$data['failed'] = true;
}
} else {
echo "Skippping $repo\n";
$data['skipped'] = true;
}
file_put_contents($datafile, json_encode($data));
// Clean up
exec('rm -rf ' . escapeshellarg($dir));
// Release lock
flock($lock, LOCK_UN);
fclose($lock);
unlink($lockfile);
}
}
$repos = file('repos.txt');
foreach ($repos as $repo) {
$repo = trim($repo);
wrapper($repo);
}