-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMain.java
331 lines (274 loc) · 8.96 KB
/
Main.java
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.errors.AmbiguousObjectException;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.io.DisabledOutputStream;
public class Main {
static int processed = 0;
static long startTime = new Date().getTime();
static final int urlStartIdx = "https://github.com/".length();
public static class InsertThread extends Thread
{
private final int _index;
private final int _maxIndex;
private File[] _files;
private Collection<SolrInputDocument> _docs = new ArrayList<SolrInputDocument>();
private HttpSolrServer _server;
private Pattern _capitals = Pattern.compile(".*([a-z])([A-Z]).*");
DiffFormatter df = new DiffFormatter(
DisabledOutputStream.INSTANCE);
public InsertThread(int index, int maxIndex, File[] files)
{
_index = index;
_maxIndex = maxIndex;
_files = files;
}
public void run()
{
_server = new HttpSolrServer(
"http://localhost:8983/solr/");
_server.setMaxRetries(5);
int i = 0;
for (File f : _files)
{
if (i % _maxIndex == _index)
{
String filename = f.getAbsolutePath() + "\\.git";
System.out.println(filename);
System.out.println("Total repositories: " + i);
try
{
convertRepo(filename);
}
catch (Error e)
{
System.out.println(e.getMessage());
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
} catch (AmbiguousObjectException e) {
e.printStackTrace();
} catch (MissingObjectException e) {
e.printStackTrace();
} catch (IncorrectObjectTypeException e) {
e.printStackTrace();
} catch (CorruptObjectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
}
}
i++;
}
}
private void convertRepo(String path)
throws IOException, AmbiguousObjectException,
MissingObjectException, IncorrectObjectTypeException,
CorruptObjectException, SolrServerException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File(path)).build();
RevWalk walk = new RevWalk(repository);
Config storedConfig = repository.getConfig();
Set<String> remotes = storedConfig.getSubsections("remote");
String remoteGithub = "";
for (String remoteName : remotes) {
String url = storedConfig.getString("remote", remoteName, "url");
if (url.startsWith("https://github.com"))
{
if (!"".equals(remoteGithub))
{
System.out.println("Found second url - " + remoteGithub + "," + url);
}
remoteGithub = url.substring(urlStartIdx);
break;
}
else
{
System.out.println("Found non-github url:" + url);
}
}
int batchSize = 10000;
df.setRepository(repository);
df.setContext(0);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
boolean foundStart = false;
for (Ref ref : repository.getAllRefs().values()) {
try {
if ("HEAD".equals(ref.getName())) {
walk.markStart(walk.parseCommit(ref.getObjectId()));
foundStart = true;
break;
}
} catch (Exception notACommit) {
System.out.println(notACommit.getMessage());
continue;
}
}
if (!foundStart) {
System.out.println("Eror: could not find HEAD for " + path);
return;
}
int cnt = 0;
for (RevCommit commit : walk) {
try {
cnt++;
StringBuilder search = new StringBuilder(5000);
SolrInputDocument document = new SolrInputDocument();
if (commit.getParentCount() > 0) {
RevCommit parent = walk.parseCommit(commit.getParent(0)
.getId());
java.util.List<DiffEntry> diffs = df.scan(parent.getTree(),
commit.getTree());
if (diffs.size() > 50)
{
// we're aiming to find out who was the lead on a project
// ignore massive merges / refactorings
continue;
}
for (Object obj : diffs) {
DiffEntry diff = (DiffEntry) obj;
String file = diff.getNewPath().toLowerCase();
ChangeType mode = diff.getChangeType();
if (ChangeType.DELETE.equals(mode) ||
ChangeType.RENAME.equals(mode) ||
ChangeType.COPY.equals(mode)) {
// since the aim is to find who was the lead on a project
// just count things that look like real work, not moving
// stuff around
continue;
}
Matcher m = _capitals.matcher(file);
String tokenizedFile = m.replaceAll("\1 \2");
tokenizedFile = tokenizedFile.replace("/", " ");
tokenizedFile = tokenizedFile.replace("_", " ");
tokenizedFile = tokenizedFile.replace("-", " ");
tokenizedFile = tokenizedFile.replace(".", " ");
search.append(tokenizedFile);
}
}
PersonIdent commitAuthor = commit.getAuthorIdent();
document.addField("id", remoteGithub + "." + commit.getId(), 1.0f);
String author = commitAuthor.getName();
author = author.replace(".", " ");
document.addField("author", author);
document.addField("email", nvl(commitAuthor.getEmailAddress(), " "));
document.addField("company", getCompany(commitAuthor.getEmailAddress()));
document.addField("date", commitAuthor.getWhen());
document.addField("message", commit.getFullMessage());
document.addField("name", commit.getName());
document.addField("github", remoteGithub);
Calendar cal = Calendar.getInstance();
cal.setTime(commitAuthor.getWhen());
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
document.addField("year", "" + year);
document.addField("year-month", "" + year + "-" + month);
search.append(" ").append(author);
search.append(" ").append(commit.getFullMessage());
document.addField("search", search.toString());
_docs.add(document);
if (cnt % batchSize == 0) {
commitDocs();
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e);
}
}
commitDocs();
}
private void commitDocs() {
try {
_server.add(_docs);
_server.commit();
synchronized (Main.class) {
processed += _docs.size();
}
_docs = new ArrayList<SolrInputDocument>();
logProgress();
}
catch (Exception e) {
System.out.println(e);
}
}
protected void logProgress()
{
double elapsed = ( (new Date()).getTime() - startTime ) / 1000;
double diffsPerSecond = processed / ( elapsed );
System.out.println("Commits per second:" + diffsPerSecond + ", elapsed time = " + elapsed + ", commits processed: " + processed + ", thread #" + _index);
}
}
/**
* @param args
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Date startDate = new Date();
int maxThreads = Runtime.getRuntime().availableProcessors();
File[] files = new File("E:\\VMs\\expert-search\\repos3\\").listFiles();
for (int i = 1; i <= maxThreads; i++)
{
System.out.println("Starting thread " + i + " threads");
new InsertThread(i, maxThreads, files).start();
}
System.out.println("Starting " + maxThreads + " threads");
Date endDate = new Date();
System.out.println(startDate.toGMTString());
System.out.println(endDate.toGMTString());
}
public static String nvl(String a, String b) {
if (a == null) {
return b;
}
return a;
}
private static String getCompany(String emailAddress)
{
if (emailAddress == null)
{
emailAddress = "";
}
if (emailAddress.contains("@"))
{
String company = emailAddress.split("@")[1];
if (company.contains("."))
{
company = company.substring(0, company.lastIndexOf("."));
}
if (company.contains("."))
{
int start = company.lastIndexOf(".");
company = company.substring(start, company.length());
}
return company;
}
return emailAddress;
}
}