Search in sources :

Example 1 with InvalidRemoteException

use of org.eclipse.jgit.api.errors.InvalidRemoteException in project bazel by bazelbuild.

the class GitCloner method clone.

public static SkyValue clone(Rule rule, Path outputDirectory, ExtendedEventHandler eventHandler, Map<String, String> clientEnvironment) throws RepositoryFunctionException {
    WorkspaceAttributeMapper mapper = WorkspaceAttributeMapper.of(rule);
    if (mapper.isAttributeValueExplicitlySpecified("commit") == mapper.isAttributeValueExplicitlySpecified("tag")) {
        throw new RepositoryFunctionException(new EvalException(rule.getLocation(), "One of either commit or tag must be defined"), Transience.PERSISTENT);
    }
    GitRepositoryDescriptor descriptor;
    String startingPoint;
    try {
        if (mapper.isAttributeValueExplicitlySpecified("commit")) {
            startingPoint = mapper.get("commit", Type.STRING);
        } else {
            startingPoint = "tags/" + mapper.get("tag", Type.STRING);
        }
        descriptor = new GitRepositoryDescriptor(mapper.get("remote", Type.STRING), startingPoint, mapper.get("init_submodules", Type.BOOLEAN), outputDirectory);
    } catch (EvalException e) {
        throw new RepositoryFunctionException(e, Transience.PERSISTENT);
    }
    // Setup proxy if remote is http or https
    if (descriptor.remote != null && Ascii.toLowerCase(descriptor.remote).startsWith("http")) {
        try {
            new ProxyHelper(clientEnvironment).createProxyIfNeeded(new URL(descriptor.remote));
        } catch (IOException ie) {
            throw new RepositoryFunctionException(ie, Transience.TRANSIENT);
        }
    }
    Git git = null;
    try {
        if (descriptor.directory.exists()) {
            if (isUpToDate(descriptor)) {
                return new HttpDownloadValue(descriptor.directory);
            }
            try {
                FileSystemUtils.deleteTree(descriptor.directory);
            } catch (IOException e) {
                throw new RepositoryFunctionException(e, Transience.TRANSIENT);
            }
        }
        git = Git.cloneRepository().setURI(descriptor.remote).setCredentialsProvider(new NetRCCredentialsProvider()).setDirectory(descriptor.directory.getPathFile()).setCloneSubmodules(false).setNoCheckout(true).setProgressMonitor(new GitProgressMonitor(descriptor.remote, "Cloning " + descriptor.remote, eventHandler)).call();
        git.checkout().setCreateBranch(true).setName("bazel-checkout").setStartPoint(descriptor.checkout).call();
        // the first level.
        if (descriptor.initSubmodules && !git.submoduleInit().call().isEmpty()) {
            git.submoduleUpdate().setProgressMonitor(new GitProgressMonitor(descriptor.remote, "Cloning submodules for " + descriptor.remote, eventHandler)).call();
        }
    } catch (InvalidRemoteException e) {
        throw new RepositoryFunctionException(new IOException("Invalid Git repository URI: " + e.getMessage()), Transience.PERSISTENT);
    } catch (RefNotFoundException | InvalidRefNameException e) {
        throw new RepositoryFunctionException(new IOException("Invalid branch, tag, or commit: " + e.getMessage()), Transience.PERSISTENT);
    } catch (GitAPIException e) {
        // This is a sad attempt to actually get a useful error message out of jGit, which will bury
        // the actual (useful) cause of the exception under several throws.
        StringBuilder errmsg = new StringBuilder();
        errmsg.append(e.getMessage());
        Throwable throwable = e;
        while (throwable.getCause() != null) {
            throwable = throwable.getCause();
            errmsg.append(" caused by " + throwable.getMessage());
        }
        throw new RepositoryFunctionException(new IOException("Error cloning repository: " + errmsg), Transience.PERSISTENT);
    } catch (JGitInternalException e) {
        // caller of the command can handle them effectively." Thanks, jgit.
        throw new RepositoryFunctionException(new IOException(e.getMessage()), Transience.PERSISTENT);
    } finally {
        if (git != null) {
            git.close();
        }
    }
    return new HttpDownloadValue(descriptor.directory);
}
Also used : ProxyHelper(com.google.devtools.build.lib.bazel.repository.downloader.ProxyHelper) EvalException(com.google.devtools.build.lib.syntax.EvalException) IOException(java.io.IOException) URL(java.net.URL) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) InvalidRefNameException(org.eclipse.jgit.api.errors.InvalidRefNameException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) NetRCCredentialsProvider(org.eclipse.jgit.transport.NetRCCredentialsProvider) WorkspaceAttributeMapper(com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Example 2 with InvalidRemoteException

use of org.eclipse.jgit.api.errors.InvalidRemoteException in project compiler by boalang.

the class RepositoryCloner method main.

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    String localpath = args[1];
    String url = args[0];
    REMOTE_URL = url;
    File localPath = new File(localpath);
    if (!localPath.exists())
        localPath.mkdir();
    // then clone
    Git result = null;
    try {
        result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call();
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093
        result.getRepository().close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (result != null && result.getRepository() != null)
            result.getRepository().close();
    }
}
Also used : Git(org.eclipse.jgit.api.Git) File(java.io.File) TransportException(org.eclipse.jgit.api.errors.TransportException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) IOException(java.io.IOException)

Example 3 with InvalidRemoteException

use of org.eclipse.jgit.api.errors.InvalidRemoteException in project compiler by boalang.

the class RepositoryCloner method clone.

public static void clone(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    String localpaths = args[1];
    String url = args[0];
    REMOTE_URL = url;
    File localPath = new File(localpaths);
    if (!localPath.exists())
        localPath.mkdir();
    // then clone
    Git result = null;
    try {
        result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call();
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093
        result.getRepository().close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (result != null && result.getRepository() != null)
            result.getRepository().close();
    }
}
Also used : Git(org.eclipse.jgit.api.Git) File(java.io.File) TransportException(org.eclipse.jgit.api.errors.TransportException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 Git (org.eclipse.jgit.api.Git)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)3 File (java.io.File)2 TransportException (org.eclipse.jgit.api.errors.TransportException)2 ProxyHelper (com.google.devtools.build.lib.bazel.repository.downloader.ProxyHelper)1 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)1 WorkspaceAttributeMapper (com.google.devtools.build.lib.rules.repository.WorkspaceAttributeMapper)1 EvalException (com.google.devtools.build.lib.syntax.EvalException)1 URL (java.net.URL)1 InvalidRefNameException (org.eclipse.jgit.api.errors.InvalidRefNameException)1 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)1 RefNotFoundException (org.eclipse.jgit.api.errors.RefNotFoundException)1 NetRCCredentialsProvider (org.eclipse.jgit.transport.NetRCCredentialsProvider)1