Search in sources :

Example 1 with R_REMOTES

use of org.eclipse.jgit.lib.Constants.R_REMOTES in project n4js by eclipse.

the class GitUtils method clone.

/**
 * Clones a branch of a remote Git repository to the local file system.
 *
 * @param remoteUri
 *            the remote Git repository URI as String.
 * @param localClonePath
 *            the local file system path.
 * @param branch
 *            the name of the branch to be cloned.
 */
private static void clone(final String remoteUri, final Path localClonePath, final String branch) {
    checkNotNull(remoteUri, "remoteUri");
    checkNotNull(localClonePath, "clonePath");
    checkNotNull(branch, "branch");
    final File destinationFolder = localClonePath.toFile();
    if (!destinationFolder.exists()) {
        try {
            createDirectories(localClonePath);
        } catch (final IOException e) {
            final String message = "Error while creating directory for local repository under " + localClonePath + ".";
            LOGGER.error(message, e);
            throw new RuntimeException(message, e);
        }
        LOGGER.info("Creating folder for repository at '" + localClonePath + "'.");
    }
    checkState(destinationFolder.exists(), "Repository folder does not exist folder does not exist: " + destinationFolder.getAbsolutePath());
    final File[] existingFiles = destinationFolder.listFiles();
    if (!isEmpty(existingFiles)) {
        try (final Git git = open(localClonePath.toFile())) {
            LOGGER.info("Repository already exists. Aborting clone phase. Files in " + destinationFolder + " are: " + Joiner.on(',').join(existingFiles));
            final List<URIish> originUris = getOriginUris(git);
            if (!hasRemote(originUris, remoteUri)) {
                LOGGER.info("Desired remote URI differs from the current one. Desired: '" + remoteUri + "' origin URIs: '" + Joiner.on(',').join(originUris) + "'.");
                LOGGER.info("Cleaning up current git clone and running clone phase from scratch.");
                deleteRecursively(destinationFolder);
                clone(remoteUri, localClonePath, branch);
            } else {
                final String currentBranch = git.getRepository().getBranch();
                if (!currentBranch.equals(branch)) {
                    LOGGER.info("Desired branch differs from the current one. Desired: '" + branch + "' current: '" + currentBranch + "'.");
                    git.pull().setProgressMonitor(createMonitor()).call();
                    // check if the remote desired branch exists or not.
                    final Ref remoteBranchRef = from(git.branchList().setListMode(REMOTE).call()).firstMatch(ref -> ref.getName().equals(R_REMOTES + ORIGIN + "/" + branch)).orNull();
                    // we have to delete the repository content and run a full clone from scratch.
                    if (null == remoteBranchRef) {
                        LOGGER.info("Cleaning up current git clone and running clone phase from scratch.");
                        deleteRecursively(destinationFolder);
                        clone(remoteUri, localClonePath, currentBranch);
                    } else {
                        git.pull().setProgressMonitor(createMonitor()).call();
                        LOGGER.info("Pulled from upstream.");
                    }
                }
            }
            return;
        } catch (final Exception e) {
            final String msg = "Error when performing git pull in " + localClonePath + " from " + remoteUri + ".";
            LOGGER.error(msg, e);
            throw new RuntimeException("Error when performing git pull in " + localClonePath + " from " + remoteUri + ".", e);
        }
    }
    LOGGER.info("Cloning repository from '" + remoteUri + "'...");
    final CloneCommand cloneCommand = cloneRepository().setURI(remoteUri).setDirectory(destinationFolder).setBranch(branch).setProgressMonitor(createMonitor()).setTransportConfigCallback(TRANSPORT_CALLBACK);
    try (final Git git = cloneCommand.call()) {
        LOGGER.info("Repository content has been successfully cloned to '" + git.getRepository().getDirectory() + "'.");
    } catch (final GitAPIException e) {
        final String message = "Error while cloning repository.";
        LOGGER.error(message, e);
        LOGGER.info("Trying to clean up local repository content: " + destinationFolder + ".");
        deleteRecursively(destinationFolder);
        LOGGER.info("Inconsistent checkout directory was successfully cleaned up.");
        throw new RuntimeException(message, e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Exceptions(org.eclipse.xtext.xbase.lib.Exceptions) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) URISyntaxException(java.net.URISyntaxException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) SshTransport(org.eclipse.jgit.transport.SshTransport) Logger(org.apache.log4j.Logger) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ResetCommand(org.eclipse.jgit.api.ResetCommand) Optional(com.google.common.base.Optional) FluentIterable.from(com.google.common.collect.FluentIterable.from) URIish(org.eclipse.jgit.transport.URIish) Path(java.nio.file.Path) Git.cloneRepository(org.eclipse.jgit.api.Git.cloneRepository) R_REMOTES(org.eclipse.jgit.lib.Constants.R_REMOTES) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git.open(org.eclipse.jgit.api.Git.open) Collection(java.util.Collection) HEAD(org.eclipse.jgit.lib.Constants.HEAD) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) Arrays2.isEmpty(org.eclipse.n4js.utils.collections.Arrays2.isEmpty) Files.createDirectories(java.nio.file.Files.createDirectories) Preconditions.checkState(com.google.common.base.Preconditions.checkState) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MASTER(org.eclipse.jgit.lib.Constants.MASTER) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Ref(org.eclipse.jgit.lib.Ref) HARD(org.eclipse.jgit.api.ResetCommand.ResetType.HARD) Iterables.toArray(com.google.common.collect.Iterables.toArray) Logger.getLogger(org.apache.log4j.Logger.getLogger) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) Joiner(com.google.common.base.Joiner) Iterables(com.google.common.collect.Iterables) CloneCommand(org.eclipse.jgit.api.CloneCommand) StringUtils(org.eclipse.jgit.util.StringUtils) FileDeleter(org.eclipse.n4js.utils.io.FileDeleter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Iterables.size(com.google.common.collect.Iterables.size) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) OutputStreamWriter(java.io.OutputStreamWriter) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) DEFAULT_REMOTE_NAME(org.eclipse.jgit.lib.Constants.DEFAULT_REMOTE_NAME) REMOTE(org.eclipse.jgit.api.ListBranchCommand.ListMode.REMOTE) Git(org.eclipse.jgit.api.Git) Collections(java.util.Collections) CloneCommand(org.eclipse.jgit.api.CloneCommand) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) URISyntaxException(java.net.URISyntaxException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) File(java.io.File)

Aggregations

Joiner (com.google.common.base.Joiner)1 Optional (com.google.common.base.Optional)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Throwables (com.google.common.base.Throwables)1 FluentIterable.from (com.google.common.collect.FluentIterable.from)1 Iterables (com.google.common.collect.Iterables)1 Iterables.size (com.google.common.collect.Iterables.size)1 Iterables.toArray (com.google.common.collect.Iterables.toArray)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 URISyntaxException (java.net.URISyntaxException)1 Files.createDirectories (java.nio.file.Files.createDirectories)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1