import java.io.IOException;
import java.net.ProxySelector;
+import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.TreeMap;
import org.eclipse.jgit.annotations.NonNull;
+import org.eclipse.jgit.errors.CommandFailedException;
import org.eclipse.jgit.lfs.LfsPointer;
import org.eclipse.jgit.lfs.Protocol;
import org.eclipse.jgit.lfs.errors.LfsConfigInvalidException;
import org.eclipse.jgit.transport.http.HttpConnection;
import org.eclipse.jgit.util.HttpSupport;
import org.eclipse.jgit.util.SshSupport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* Provides means to get a valid LFS connection for a given repository.
*/
public class LfsConnectionFactory {
- private static final Logger log = LoggerFactory
- .getLogger(LfsConnectionFactory.class);
-
private static final int SSH_AUTH_TIMEOUT_SECONDS = 30;
private static final String SCHEME_HTTPS = "https"; //$NON-NLS-1$
private static final String SCHEME_SSH = "ssh"; //$NON-NLS-1$
String lfsUrl = config.getString(ConfigConstants.CONFIG_SECTION_LFS,
null,
ConfigConstants.CONFIG_KEY_URL);
+ Exception ex = null;
if (lfsUrl == null) {
String remoteUrl = null;
for (String remote : db.getRemoteNames()) {
break;
}
if (lfsUrl == null && remoteUrl != null) {
- lfsUrl = discoverLfsUrl(db, purpose, additionalHeaders,
- remoteUrl);
+ try {
+ lfsUrl = discoverLfsUrl(db, purpose, additionalHeaders,
+ remoteUrl);
+ } catch (URISyntaxException | IOException
+ | CommandFailedException e) {
+ ex = e;
+ }
} else {
lfsUrl = lfsUrl + Protocol.INFO_LFS_ENDPOINT;
}
}
if (lfsUrl == null) {
+ if (ex != null) {
+ throw new LfsConfigInvalidException(
+ LfsText.get().lfsNoDownloadUrl, ex);
+ }
throw new LfsConfigInvalidException(LfsText.get().lfsNoDownloadUrl);
}
return lfsUrl;
}
private static String discoverLfsUrl(Repository db, String purpose,
- Map<String, String> additionalHeaders, String remoteUrl) {
- try {
- URIish u = new URIish(remoteUrl);
- if (u.getScheme() == null || SCHEME_SSH.equals(u.getScheme())) {
- Protocol.ExpiringAction action = getSshAuthentication(
- db, purpose, remoteUrl, u);
- additionalHeaders.putAll(action.header);
- return action.href;
- } else {
- return remoteUrl + Protocol.INFO_LFS_ENDPOINT;
- }
- } catch (Exception e) {
- log.error(LfsText.get().cannotDiscoverLfs, e);
- return null; // could not discover
+ Map<String, String> additionalHeaders, String remoteUrl)
+ throws URISyntaxException, IOException, CommandFailedException {
+ URIish u = new URIish(remoteUrl);
+ if (u.getScheme() == null || SCHEME_SSH.equals(u.getScheme())) {
+ Protocol.ExpiringAction action = getSshAuthentication(db, purpose,
+ remoteUrl, u);
+ additionalHeaders.putAll(action.header);
+ return action.href;
+ } else {
+ return remoteUrl + Protocol.INFO_LFS_ENDPOINT;
}
}
private static Protocol.ExpiringAction getSshAuthentication(
Repository db, String purpose, String remoteUrl, URIish u)
- throws IOException {
+ throws IOException, CommandFailedException {
AuthCache cached = sshAuthCache.get(remoteUrl);
Protocol.ExpiringAction action = null;
if (cached != null && cached.validUntil > System.currentTimeMillis()) {
package org.eclipse.jgit.util;
import java.io.IOException;
+import java.text.MessageFormat;
import org.eclipse.jgit.annotations.Nullable;
+import org.eclipse.jgit.errors.CommandFailedException;
+import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RemoteSession;
import org.eclipse.jgit.transport.SshSessionFactory;
* @param timeout
* a timeout in seconds. The timeout may be exceeded in corner
* cases.
- * @return The entire output read from stdout. Stderr is discarded.
+ * @return The entire output read from stdout.
* @throws IOException
+ * @throws CommandFailedException
+ * if the ssh command execution failed, error message contains
+ * the content of stderr.
*/
public static String runSshCommand(URIish sshUri,
@Nullable CredentialsProvider provider, FS fs, String command,
- int timeout) throws IOException {
+ int timeout) throws IOException, CommandFailedException {
RemoteSession session = null;
Process process = null;
StreamCopyThread errorThread = null;
StreamCopyThread outThread = null;
- try (MessageWriter stderr = new MessageWriter();
- MessageWriter stdout = new MessageWriter()) {
+ CommandFailedException failure = null;
+ @SuppressWarnings("resource")
+ MessageWriter stderr = new MessageWriter();
+ try (MessageWriter stdout = new MessageWriter()) {
session = SshSessionFactory.getInstance().getSession(sshUri,
provider, fs, 1000 * timeout);
process = session.exec(command, 0);
}
}
if (process != null) {
+ if (process.exitValue() != 0) {
+ failure = new CommandFailedException(process.exitValue(),
+ MessageFormat.format(
+ JGitText.get().sshCommandFailed, command,
+ stderr.toString()));
+ }
process.destroy();
}
+ stderr.close();
if (session != null) {
SshSessionFactory.getInstance().releaseSession(session);
}
+ if (failure != null) {
+ throw failure;
+ }
}
}