Search in sources :

Example 1 with CheckoutConflictException

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

the class JGitConnection method merge.

@Override
public MergeResult merge(String commit) throws GitException {
    org.eclipse.jgit.api.MergeResult jGitMergeResult;
    MergeResult.MergeStatus status;
    try {
        Ref ref = repository.findRef(commit);
        if (ref == null) {
            throw new GitException("Invalid reference to commit for merge " + commit);
        }
        // Shorten local branch names by removing '/refs/heads/' from the beginning
        String name = ref.getName();
        if (name.startsWith(Constants.R_HEADS)) {
            name = name.substring(Constants.R_HEADS.length());
        }
        jGitMergeResult = getGit().merge().include(name, ref.getObjectId()).call();
    } catch (CheckoutConflictException exception) {
        jGitMergeResult = new org.eclipse.jgit.api.MergeResult(exception.getConflictingPaths());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
    switch(jGitMergeResult.getMergeStatus()) {
        case ALREADY_UP_TO_DATE:
            status = MergeResult.MergeStatus.ALREADY_UP_TO_DATE;
            break;
        case CONFLICTING:
            status = MergeResult.MergeStatus.CONFLICTING;
            break;
        case FAILED:
            status = MergeResult.MergeStatus.FAILED;
            break;
        case FAST_FORWARD:
            status = MergeResult.MergeStatus.FAST_FORWARD;
            break;
        case MERGED:
            status = MergeResult.MergeStatus.MERGED;
            break;
        case NOT_SUPPORTED:
            status = MergeResult.MergeStatus.NOT_SUPPORTED;
            break;
        case CHECKOUT_CONFLICT:
            status = MergeResult.MergeStatus.CONFLICTING;
            break;
        default:
            throw new IllegalStateException("Unknown merge status " + jGitMergeResult.getMergeStatus());
    }
    ObjectId[] jGitMergedCommits = jGitMergeResult.getMergedCommits();
    List<String> mergedCommits = new ArrayList<>();
    if (jGitMergedCommits != null) {
        for (ObjectId jGitMergedCommit : jGitMergedCommits) {
            mergedCommits.add(jGitMergedCommit.getName());
        }
    }
    List<String> conflicts;
    if (org.eclipse.jgit.api.MergeResult.MergeStatus.CHECKOUT_CONFLICT.equals(jGitMergeResult.getMergeStatus())) {
        conflicts = jGitMergeResult.getCheckoutConflicts();
    } else {
        Map<String, int[][]> jGitConflicts = jGitMergeResult.getConflicts();
        conflicts = jGitConflicts != null ? new ArrayList<>(jGitConflicts.keySet()) : Collections.emptyList();
    }
    Map<String, ResolveMerger.MergeFailureReason> jGitFailing = jGitMergeResult.getFailingPaths();
    ObjectId newHead = jGitMergeResult.getNewHead();
    return newDto(MergeResult.class).withFailed(jGitFailing != null ? new ArrayList<>(jGitFailing.keySet()) : Collections.emptyList()).withNewHead(newHead != null ? newHead.getName() : null).withMergeStatus(status).withConflicts(conflicts).withMergedCommits(mergedCommits);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) GitException(org.eclipse.che.api.git.exception.GitException) MergeResult(org.eclipse.che.api.git.shared.MergeResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref)

Example 2 with CheckoutConflictException

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

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

the class JGitConnection method pull.

@Override
public PullResponse pull(PullParams params) throws GitException, UnauthorizedException {
    String remoteName = params.getRemote();
    String remoteUri;
    try {
        if (repository.getRepositoryState().equals(RepositoryState.MERGING)) {
            throw new GitException(ERROR_PULL_MERGING);
        }
        String fullBranch = repository.getFullBranch();
        if (!fullBranch.startsWith(Constants.R_HEADS)) {
            throw new DetachedHeadException(ERROR_PULL_HEAD_DETACHED);
        }
        String branch = fullBranch.substring(Constants.R_HEADS.length());
        StoredConfig config = repository.getConfig();
        if (remoteName == null) {
            remoteName = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
            if (remoteName == null) {
                remoteName = Constants.DEFAULT_REMOTE_NAME;
            }
        }
        remoteUri = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName, ConfigConstants.CONFIG_KEY_URL);
        String remoteBranch;
        RefSpec fetchRefSpecs = null;
        String refSpec = params.getRefSpec();
        if (refSpec != null) {
            fetchRefSpecs = //
            (refSpec.indexOf(':') < 0) ? //
            new RefSpec(Constants.R_HEADS + refSpec + ":" + fullBranch) : new RefSpec(refSpec);
            remoteBranch = fetchRefSpecs.getSource();
        } else {
            remoteBranch = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_MERGE);
        }
        if (remoteBranch == null) {
            remoteBranch = fullBranch;
        }
        FetchCommand fetchCommand = getGit().fetch();
        fetchCommand.setRemote(remoteName);
        if (fetchRefSpecs != null) {
            fetchCommand.setRefSpecs(fetchRefSpecs);
        }
        int timeout = params.getTimeout();
        if (timeout > 0) {
            fetchCommand.setTimeout(timeout);
        }
        FetchResult fetchResult = (FetchResult) executeRemoteCommand(remoteUri, fetchCommand, params.getUsername(), params.getPassword());
        Ref remoteBranchRef = fetchResult.getAdvertisedRef(remoteBranch);
        if (remoteBranchRef == null) {
            remoteBranchRef = fetchResult.getAdvertisedRef(Constants.R_HEADS + remoteBranch);
        }
        if (remoteBranchRef == null) {
            throw new GitException(format(ERROR_PULL_REF_MISSING, remoteBranch));
        }
        org.eclipse.jgit.api.MergeResult mergeResult = getGit().merge().include(remoteBranchRef).call();
        if (mergeResult.getMergeStatus().equals(org.eclipse.jgit.api.MergeResult.MergeStatus.ALREADY_UP_TO_DATE)) {
            return newDto(PullResponse.class).withCommandOutput("Already up-to-date");
        }
        if (mergeResult.getConflicts() != null) {
            StringBuilder message = new StringBuilder(ERROR_PULL_MERGE_CONFLICT_IN_FILES);
            message.append(lineSeparator());
            Map<String, int[][]> allConflicts = mergeResult.getConflicts();
            for (String path : allConflicts.keySet()) {
                message.append(path).append(lineSeparator());
            }
            message.append(ERROR_PULL_AUTO_MERGE_FAILED);
            throw new GitException(message.toString());
        }
    } catch (CheckoutConflictException exception) {
        StringBuilder message = new StringBuilder(ERROR_CHECKOUT_CONFLICT);
        message.append(lineSeparator());
        for (String path : exception.getConflictingPaths()) {
            message.append(path).append(lineSeparator());
        }
        message.append(ERROR_PULL_COMMIT_BEFORE_MERGE);
        throw new GitException(message.toString(), exception);
    } catch (IOException | GitAPIException exception) {
        String errorMessage;
        if (exception.getMessage().equals("Invalid remote: " + remoteName)) {
            errorMessage = ERROR_NO_REMOTE_REPOSITORY;
        } else {
            errorMessage = generateExceptionMessage(exception);
        }
        throw new GitException(errorMessage, exception);
    }
    return newDto(PullResponse.class).withCommandOutput("Successfully pulled from " + remoteUri);
}
Also used : FetchResult(org.eclipse.jgit.transport.FetchResult) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) CheckoutConflictException(org.eclipse.jgit.api.errors.CheckoutConflictException) StoredConfig(org.eclipse.jgit.lib.StoredConfig) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Ref(org.eclipse.jgit.lib.Ref) RefSpec(org.eclipse.jgit.transport.RefSpec) PullResponse(org.eclipse.che.api.git.shared.PullResponse) FetchCommand(org.eclipse.jgit.api.FetchCommand) DetachedHeadException(org.eclipse.jgit.api.errors.DetachedHeadException)

Aggregations

GitException (org.eclipse.che.api.git.exception.GitException)3 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Ref (org.eclipse.jgit.lib.Ref)2 File (java.io.File)1 List (java.util.List)1 Optional (java.util.Optional)1 GitConflictException (org.eclipse.che.api.git.exception.GitConflictException)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 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)1 MergeResult (org.eclipse.che.api.git.shared.MergeResult)1 PullResponse (org.eclipse.che.api.git.shared.PullResponse)1 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)1 FetchCommand (org.eclipse.jgit.api.FetchCommand)1 DetachedHeadException (org.eclipse.jgit.api.errors.DetachedHeadException)1