Search in sources :

Example 1 with RefNotFoundException

use of org.eclipse.jgit.api.errors.RefNotFoundException in project che by eclipse.

the class JGitConnection method checkout.

@Override
public void checkout(CheckoutParams params) throws GitException {
    CheckoutCommand checkoutCommand = getGit().checkout();
    String startPoint = params.getStartPoint();
    String name = params.getName();
    String trackBranch = params.getTrackBranch();
    // checkout files?
    List<String> files = params.getFiles();
    boolean shouldCheckoutToFile = name != null && new File(getWorkingDir(), name).exists();
    if (shouldCheckoutToFile || !files.isEmpty()) {
        if (shouldCheckoutToFile) {
            checkoutCommand.addPath(params.getName());
        } else {
            files.forEach(checkoutCommand::addPath);
        }
    } else {
        // checkout branch
        if (startPoint != null && trackBranch != null) {
            throw new GitException("Start point and track branch can not be used together.");
        }
        if (params.isCreateNew() && name == null) {
            throw new GitException("Branch name must be set when createNew equals to true.");
        }
        if (startPoint != null) {
            checkoutCommand.setStartPoint(startPoint);
        }
        if (params.isCreateNew()) {
            checkoutCommand.setCreateBranch(true);
            checkoutCommand.setName(name);
        } else if (name != null) {
            checkoutCommand.setName(name);
            List<String> localBranches = branchList(LIST_LOCAL).stream().map(Branch::getDisplayName).collect(Collectors.toList());
            if (!localBranches.contains(name)) {
                Optional<Branch> remoteBranch = branchList(LIST_REMOTE).stream().filter(branch -> branch.getName().contains(name)).findFirst();
                if (remoteBranch.isPresent()) {
                    checkoutCommand.setCreateBranch(true);
                    checkoutCommand.setStartPoint(remoteBranch.get().getName());
                }
            }
        }
        if (trackBranch != null) {
            if (name == null) {
                checkoutCommand.setName(cleanRemoteName(trackBranch));
            }
            checkoutCommand.setCreateBranch(true);
            checkoutCommand.setStartPoint(trackBranch);
        }
        checkoutCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
    }
    try {
        checkoutCommand.call();
    } catch (CheckoutConflictException exception) {
        throw new GitConflictException(exception.getMessage(), exception.getConflictingPaths());
    } catch (RefAlreadyExistsException exception) {
        throw new GitRefAlreadyExistsException(exception.getMessage());
    } catch (RefNotFoundException exception) {
        throw new GitRefNotFoundException(exception.getMessage());
    } catch (InvalidRefNameException exception) {
        throw new GitInvalidRefNameException(exception.getMessage());
    } catch (GitAPIException exception) {
        if (exception.getMessage().endsWith("already exists")) {
            throw new GitException(format(ERROR_CHECKOUT_BRANCH_NAME_EXISTS, name != null ? name : cleanRemoteName(trackBranch)));
        }
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Optional(java.util.Optional) GitException(org.eclipse.che.api.git.exception.GitException) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitRefNotFoundException(org.eclipse.che.api.git.exception.GitRefNotFoundException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) Branch(org.eclipse.che.api.git.shared.Branch) RefAlreadyExistsException(org.eclipse.jgit.api.errors.RefAlreadyExistsException) GitRefAlreadyExistsException(org.eclipse.che.api.git.exception.GitRefAlreadyExistsException) GitRefAlreadyExistsException(org.eclipse.che.api.git.exception.GitRefAlreadyExistsException) InvalidRefNameException(org.eclipse.jgit.api.errors.InvalidRefNameException) GitInvalidRefNameException(org.eclipse.che.api.git.exception.GitInvalidRefNameException) ArrayList(java.util.ArrayList) List(java.util.List) GitConflictException(org.eclipse.che.api.git.exception.GitConflictException) GitRefNotFoundException(org.eclipse.che.api.git.exception.GitRefNotFoundException) GitInvalidRefNameException(org.eclipse.che.api.git.exception.GitInvalidRefNameException) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File)

Example 2 with RefNotFoundException

use of org.eclipse.jgit.api.errors.RefNotFoundException 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 3 with RefNotFoundException

use of org.eclipse.jgit.api.errors.RefNotFoundException in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepository method refresh.

/**
 * Get the working directory ready.
 */
public String refresh(String label) {
    Git git = null;
    try {
        git = createGitClient();
        if (shouldPull(git)) {
            FetchResult fetchStatus = fetch(git, label);
            if (deleteUntrackedBranches && fetchStatus != null) {
                deleteUntrackedLocalBranches(fetchStatus.getTrackingRefUpdates(), git);
            }
            // checkout after fetch so we can get any new branches, tags, ect.
            checkout(git, label);
            if (isBranch(git, label)) {
                // merge results from fetch
                merge(git, label);
                if (!isClean(git, label)) {
                    logger.warn("The local repository is dirty or ahead of origin. Resetting" + " it to origin/" + label + ".");
                    resetHard(git, label, LOCAL_BRANCH_REF_PREFIX + label);
                }
            }
        } else {
            // nothing to update so just checkout
            checkout(git, label);
        }
        // always return what is currently HEAD as the version
        return git.getRepository().findRef("HEAD").getObjectId().getName();
    } catch (RefNotFoundException e) {
        throw new NoSuchLabelException("No such label: " + label, e);
    } catch (NoRemoteRepositoryException e) {
        throw new NoSuchRepositoryException("No such repository: " + getUri(), e);
    } catch (GitAPIException e) {
        throw new NoSuchRepositoryException("Cannot clone or checkout repository: " + getUri(), e);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot load environment", e);
    } finally {
        try {
            if (git != null) {
                git.close();
            }
        } catch (Exception e) {
            this.logger.warn("Could not close git repository", e);
        }
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) FetchResult(org.eclipse.jgit.transport.FetchResult) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) NoRemoteRepositoryException(org.eclipse.jgit.errors.NoRemoteRepositoryException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) NoRemoteRepositoryException(org.eclipse.jgit.errors.NoRemoteRepositoryException) IOException(java.io.IOException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException)

Aggregations

GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 RefNotFoundException (org.eclipse.jgit.api.errors.RefNotFoundException)3 IOException (java.io.IOException)2 Git (org.eclipse.jgit.api.Git)2 InvalidRefNameException (org.eclipse.jgit.api.errors.InvalidRefNameException)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 File (java.io.File)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 GitConflictException (org.eclipse.che.api.git.exception.GitConflictException)1 GitException (org.eclipse.che.api.git.exception.GitException)1 GitInvalidRefNameException (org.eclipse.che.api.git.exception.GitInvalidRefNameException)1 GitRefAlreadyExistsException (org.eclipse.che.api.git.exception.GitRefAlreadyExistsException)1 GitRefNotFoundException (org.eclipse.che.api.git.exception.GitRefNotFoundException)1 Branch (org.eclipse.che.api.git.shared.Branch)1