}
private void displayDownload() {
final Project.NameKey projectKey = changeDetail.getChange().getProject();
final String projectName = projectKey.get();
final CopyableLabel copyLabel = new CopyableLabel("");
final DownloadCommandPanel commands = new DownloadCommandPanel();
final DownloadUrlPanel urls = new DownloadUrlPanel(commands);
final Set<DownloadScheme> allowedSchemes = Gerrit.getConfig().getDownloadSchemes();
copyLabel.setStyleName(Gerrit.RESOURCES.css().downloadLinkCopyLabel());
if (changeDetail.isAllowsAnonymous()
&& Gerrit.getConfig().getGitDaemonUrl() != null
&& (allowedSchemes.contains(DownloadScheme.ANON_GIT) ||
allowedSchemes.contains(DownloadScheme.DEFAULT_DOWNLOADS))) {
StringBuilder r = new StringBuilder();
r.append(Gerrit.getConfig().getGitDaemonUrl());
r.append(projectName);
r.append(" ");
r.append(patchSet.getRefName());
urls.add(new DownloadUrlLink(DownloadScheme.ANON_GIT, Util.M
.anonymousDownload("Git"), r.toString()));
}
String hostPageUrl = GWT.getHostPageBaseURL();
if (!hostPageUrl.endsWith("/")) {
hostPageUrl += "/";
}
if (changeDetail.isAllowsAnonymous()
&& (allowedSchemes.contains(DownloadScheme.ANON_HTTP) ||
allowedSchemes.contains(DownloadScheme.DEFAULT_DOWNLOADS))) {
StringBuilder r = new StringBuilder();
if (Gerrit.getConfig().getGitHttpUrl() != null) {
r.append(Gerrit.getConfig().getGitHttpUrl());
} else {
r.append(hostPageUrl);
}
r.append(projectName);
r.append(" ");
r.append(patchSet.getRefName());
urls.add(new DownloadUrlLink(DownloadScheme.ANON_HTTP, Util.M
.anonymousDownload("HTTP"), r.toString()));
}
if (Gerrit.getConfig().getSshdAddress() != null
&& hasUserName()
&& (allowedSchemes.contains(DownloadScheme.SSH) ||
allowedSchemes.contains(DownloadScheme.DEFAULT_DOWNLOADS))) {
String sshAddr = Gerrit.getConfig().getSshdAddress();
final StringBuilder r = new StringBuilder();
r.append("ssh://");
r.append(Gerrit.getUserAccount().getUserName());
r.append("@");
if (sshAddr.startsWith("*:") || "".equals(sshAddr)) {
r.append(Window.Location.getHostName());
}
if (sshAddr.startsWith("*")) {
sshAddr = sshAddr.substring(1);
}
r.append(sshAddr);
r.append("/");
r.append(projectName);
r.append(" ");
r.append(patchSet.getRefName());
urls.add(new DownloadUrlLink(DownloadScheme.SSH, "SSH", r.toString()));
}
if ((hasUserName() || siteReliesOnHttp())
&& (allowedSchemes.contains(DownloadScheme.HTTP)
|| allowedSchemes.contains(DownloadScheme.DEFAULT_DOWNLOADS))) {
final StringBuilder r = new StringBuilder();
if (Gerrit.getConfig().getGitHttpUrl() != null
&& (changeDetail.isAllowsAnonymous() || siteReliesOnHttp())) {
r.append(Gerrit.getConfig().getGitHttpUrl());
} else {
String base = hostPageUrl;
int p = base.indexOf("://");
int s = base.indexOf('/', p + 3);
if (s < 0) {
s = base.length();
}
String host = base.substring(p + 3, s);
if (host.contains("@")) {
host = host.substring(host.indexOf('@') + 1);
}
r.append(base.substring(0, p + 3));
r.append(Gerrit.getUserAccount().getUserName());
r.append('@');
r.append(host);
r.append(base.substring(s));
}
r.append(projectName);
r.append(" ");
r.append(patchSet.getRefName());
urls.add(new DownloadUrlLink(DownloadScheme.HTTP, "HTTP", r.toString()));
}
if (allowedSchemes.contains(DownloadScheme.REPO_DOWNLOAD)) {
// This site prefers usage of the 'repo' tool, so suggest
// that for easy fetch.
//
final StringBuilder r = new StringBuilder();
r.append("repo download ");
r.append(projectName);
r.append(" ");
r.append(changeDetail.getChange().getChangeId());
r.append("/");
r.append(patchSet.getPatchSetId());
final String cmd = r.toString();
commands.add(new DownloadCommandLink(DownloadCommand.REPO_DOWNLOAD,
"repo download") {
@Override
void setCurrentUrl(DownloadUrlLink link) {
urls.setVisible(false);
copyLabel.setText(cmd);
}
});
}
if (!urls.isEmpty()) {
commands.add(new DownloadCommandLink(DownloadCommand.CHECKOUT, "checkout") {
@Override
void setCurrentUrl(DownloadUrlLink link) {
urls.setVisible(true);
copyLabel.setText("git fetch " + link.urlData
+ " && git checkout FETCH_HEAD");
}
});
commands.add(new DownloadCommandLink(DownloadCommand.PULL, "pull") {
@Override
void setCurrentUrl(DownloadUrlLink link) {
urls.setVisible(true);
copyLabel.setText("git pull " + link.urlData);
}
});
commands.add(new DownloadCommandLink(DownloadCommand.CHERRY_PICK,
"cherry-pick") {
@Override
void setCurrentUrl(DownloadUrlLink link) {
urls.setVisible(true);
copyLabel.setText("git fetch " + link.urlData
+ " && git cherry-pick FETCH_HEAD");
}
});
commands.add(new DownloadCommandLink(DownloadCommand.FORMAT_PATCH,
"patch") {
@Override
void setCurrentUrl(DownloadUrlLink link) {
urls.setVisible(true);
copyLabel.setText("git fetch " + link.urlData
+ " && git format-patch -1 --stdout FETCH_HEAD");
}
});
}