in src/main/java/org/apache/jenkins/gitpubsub/ASFGitSCMFileSystem.java [451:534]
public SCMRevision getRevision(@NonNull String remote, StandardCredentials credentials,
@NonNull String refOrHash)
throws IOException, InterruptedException {
if (refOrHash.startsWith(Constants.R_TAGS)) {
// check if this is an annotated tag... if it is we need to get the tag object revision
String tagUrl = buildTemplateWithRemote("{+server}{?p}{;a,h}", remote)
.set("a", "tag")
.set("h", refOrHash)
.expand();
Document doc;
try {
doc = fetchDocument(tagUrl);
} catch (HttpStatusException e) {
if (e.getStatusCode() == 404) {
// must be a lightweight tag
doc = null;
} else {
throw e;
}
}
if (doc != null) {
long time;
Elements elements = doc.select("table.object_header tr td span.datetime");
try {
time = new SimpleDateFormat(RFC_2822).parse(elements.get(0).text())
.getTime();
} catch (ParseException e) {
throw new IOException(
"Unexpected date format, expected RFC 2822, got " + elements.get(1).text());
} catch (IndexOutOfBoundsException e) {
throw new IOException(
"Unexpected response body for page " + tagUrl + ", expecting two timestamps only got " + elements.size());
}
// now let's get the revision of the tag object...
String actionUrl = buildTemplateWithRemote("{+server}{?p}{;a}", remote)
.set("a", "tags")
.expand();
doc = fetchDocument(actionUrl);
elements = doc.select("table.tags tr td a.name");
for (Element element : elements) {
if (refOrHash.equals(Constants.R_TAGS + element.text())) {
Elements links = element.parent().parent().select("td.selflink a");
if (links.isEmpty()) {
// assumption violated, bail
break;
}
String href = links.get(0).attr("href");
Matcher matcher = URL_EXTRACT_H.matcher(href);
if (matcher.matches()) {
return new GitTagSCMRevision(
new GitTagSCMHead(refOrHash.substring(Constants.R_TAGS.length()), time),
matcher.group(1));
}
}
}
}
}
String commitUrl = buildTemplateWithRemote("{+server}{?p}{;a,h}", remote)
.set("a", "commit")
.set("h", refOrHash)
.expand();
Document doc = fetchDocument(commitUrl);
Elements elements = doc.select("table.object_header tr td.sha1");
String revision = elements.get(0).text();
if (refOrHash.startsWith(Constants.R_TAGS)) {
elements = doc.select("table.object_header tr td span.datetime");
long time;
try {
time =
new SimpleDateFormat(RFC_2822).parse(elements.get(1).text()).getTime();
} catch (ParseException e) {
throw new IOException("Unexpected date format, expected RFC 2822, got " + elements.get(1).text());
}
return new GitTagSCMRevision(new GitTagSCMHead(refOrHash.substring(Constants.R_TAGS.length()), time),
revision);
} else if (refOrHash.startsWith(Constants.R_HEADS)) {
return new AbstractGitSCMSource.SCMRevisionImpl(
new SCMHead(refOrHash.substring(Constants.R_HEADS.length())), revision);
} else {
// TODO fix for hash without branch name
return null;
}
}