-
Notifications
You must be signed in to change notification settings - Fork 2
/
AnalyzerUtil.java
204 lines (171 loc) · 6.79 KB
/
AnalyzerUtil.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
/**
* Copyright (c) 2022 Eclipse contributors and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.cbi.p2repo.aggregator.analyzer.util;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.cbi.p2repo.aggregator.analyzer.AnalyzerFactory;
import org.eclipse.cbi.p2repo.aggregator.analyzer.AnalyzerPackage;
import org.eclipse.cbi.p2repo.aggregator.analyzer.ContributionAnalysis;
import org.eclipse.cbi.p2repo.aggregator.analyzer.GitRepository;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
public final class AnalyzerUtil {
private AnalyzerUtil() {
throw new RuntimeException("Not instantiable");
}
public static final Comparator<ContributionAnalysis> CONTRIBUTION_ANALYSIS_COMPARATOR = new Comparator<ContributionAnalysis>() {
private static final Comparator<String> comparator = CommonPlugin.INSTANCE.getComparator();
@Override
public int compare(ContributionAnalysis c1, ContributionAnalysis c2) {
String label1 = c1.getLabel();
String label2 = c2.getLabel();
return comparator.compare(label1, label2);
}
};
public static final Comparator<ContributionAnalysis> CONTRIBUTION_ANALYSIS_COMPARATOR_WITH_RANK = new Comparator<ContributionAnalysis>() {
private static final Comparator<String> comparator = CommonPlugin.INSTANCE.getComparator();
@Override
public int compare(ContributionAnalysis c1, ContributionAnalysis c2) {
int compare = Integer.compare(c2.getRank(), c1.getRank());
if (compare != 0) {
return compare;
}
String label1 = c1.getLabel();
String label2 = c2.getLabel();
return comparator.compare(label1, label2);
}
};
public static Float getAgeInDays(ContributionAnalysis contributionAnalysis) {
long lastModified = contributionAnalysis.getLastModified();
if (lastModified != Long.MIN_VALUE) {
return getAgeInDays(lastModified);
}
return null;
}
public static Float getGitRepositoryAgeInDays(ContributionAnalysis contributionAnalysis) {
Float result = null;
for (TreeIterator<EObject> eAllContents = contributionAnalysis.eAllContents(); eAllContents.hasNext();) {
EObject eObject = eAllContents.next();
if (eObject instanceof GitRepository) {
Float ageInDays = getAgeInDays((GitRepository) eObject);
if (ageInDays != null) {
if (result == null) {
result = ageInDays;
} else {
result = Math.min(ageInDays, result);
}
}
}
}
return result;
}
public static Float getAgeInDays(GitRepository gitRepository) {
long lastModified = gitRepository.getLastModified();
if (lastModified != Long.MIN_VALUE) {
return getAgeInDays(lastModified);
}
return null;
}
public static float getAgeInDays(long lastModified) {
return getAgeInDays(lastModified, System.currentTimeMillis());
}
public static float getAgeInDays(long lastModified, long relativeTo) {
long ageInMillis = Math.max(0, relativeTo - lastModified);
float ageInDays = ((float) ageInMillis) / 1000 / 60 / 60 / 24;
return ageInDays;
}
public static String format(Date date) {
return AnalyzerFactory.eINSTANCE.convertToString(AnalyzerPackage.Literals.DATE, date);
}
/**
* https://github.com/merks/org.eclipse.simrel.build.git
* git@github.com:merks/org.eclipse.simrel.build.git
*/
private static final Pattern ECLIPSE_GIT_URL_PATTERN = Pattern
.compile("(?:https?://git.eclipse.org/r/|ssh://[^@]+@git.eclipse.org:29418/)(.*)");
/**
* https://git.eclipse.org/r/simrel/org.eclipse.simrel.build.git
* ssh://user_id@git.eclipse.org:29418/simrel/org.eclipse.simrel.build.git
*/
private static final Pattern GITHUB_URL_PATTERN = Pattern.compile("(?:https?://github.com/|git@github.com:)(.*)");
private static final Pattern GIT_CONFIG_URL_PATTERN = Pattern.compile("\n[ \t]*url[ \t]*=[ \t]*([^\n\r \t]+)");
private static final Pattern GIT_CONFIG_BRANCH_PATTERN = Pattern
.compile("\n\\[[ \t]*branch[ \t]+\"([^\"]+)\"[ \t]*\\]");
private static final String GIT_ECLIPSE = "https://git.eclipse.org/";
private static final String GITHUB = "https://github.com/";
private static final String RAW_GITHUB = "https://raw.githubusercontent.com/";
// private static final String GITLAB_ECLIPSE = "https://gitlab.eclipse.org";
public record Repo(URI base, URI path, String branch) {
public URI getPlainURI() {
String base = this.base.toString();
Matcher eclipseGitMatcher = ECLIPSE_GIT_URL_PATTERN.matcher(base);
if (eclipseGitMatcher.matches()) {
return URI.createURI(
GIT_ECLIPSE + "c/" + eclipseGitMatcher.group(1) + ".git/log/" + path + "?h=" + branch);
}
Matcher githubGitMatcher = GITHUB_URL_PATTERN.matcher(base);
if (githubGitMatcher.matches()) {
return URI.createURI(
GITHUB + githubGitMatcher.group(1).replaceAll("\\.git$", "") + "/blob/" + branch
+ "/" + path);
}
return null;
}
public URI getRawURI() {
String base = this.base.toString();
Matcher eclipseGitMatcher = ECLIPSE_GIT_URL_PATTERN.matcher(base);
if (eclipseGitMatcher.matches()) {
return URI.createURI(
GIT_ECLIPSE + "c/" + eclipseGitMatcher.group(1) + ".git/log/" + path + "?h=" + branch);
}
Matcher githubGitMatcher = GITHUB_URL_PATTERN.matcher(base);
if (githubGitMatcher.matches()) {
return URI.createURI(
RAW_GITHUB + githubGitMatcher.group(1).replaceAll("\\.git$", "") + "/" + branch + "/" + path);
}
return null;
}
}
public static Repo getGitRepositoryURI(URI uri) {
URI resolvedURI = CommonPlugin.resolve(uri);
if (resolvedURI.isFile()) {
Path contributionPath = Path.of(resolvedURI.toFileString());
for (Path path = contributionPath.getParent(); path != null
&& Files.isReadable(path); path = path.getParent()) {
// Look for a git repository configuration.
Path configPath = path.resolve(".git/config");
if (Files.isRegularFile(configPath)) {
try {
String config = Files.readString(configPath);
// Determine branch and repo URL.
Matcher branchMatcher = GIT_CONFIG_BRANCH_PATTERN.matcher(config);
Matcher urlMatcher = GIT_CONFIG_URL_PATTERN.matcher(config);
if (urlMatcher.find() && branchMatcher.find()) {
URI repoRelativeURI = URI.createFileURI(path.relativize(contributionPath).toString());
String repo = urlMatcher.group(1);
return new Repo(URI.createURI(repo), repoRelativeURI, branchMatcher.group(1));
}
} catch (IOException e) {
}
break;
}
}
}
return null;
}
}