Search in sources :

Example 1 with PullResponse

use of org.eclipse.che.api.git.shared.PullResponse 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)

Example 2 with PullResponse

use of org.eclipse.che.api.git.shared.PullResponse in project che by eclipse.

the class PullPresenter method onPullClicked.

/** {@inheritDoc} */
@Override
public void onPullClicked() {
    view.close();
    final StatusNotification notification = notificationManager.notify(constant.pullProcess(), PROGRESS, FLOAT_MODE);
    service.pull(appContext.getDevMachine(), project.getLocation(), getRefs(), view.getRepositoryName()).then(new Operation<PullResponse>() {

        @Override
        public void apply(PullResponse response) throws OperationException {
            GitOutputConsole console = gitOutputConsoleFactory.create(PULL_COMMAND_NAME);
            console.print(response.getCommandOutput(), GREEN_COLOR);
            consolesPanelPresenter.addCommandOutput(appContext.getDevMachine().getId(), console);
            notification.setStatus(SUCCESS);
            if (response.getCommandOutput().contains("Already up-to-date")) {
                notification.setTitle(constant.pullUpToDate());
            } else {
                project.synchronize();
                notification.setTitle(constant.pullSuccess(view.getRepositoryUrl()));
            }
        }
    }).catchError(new Operation<PromiseError>() {

        @Override
        public void apply(PromiseError error) throws OperationException {
            notification.setStatus(FAIL);
            if (getErrorCode(error.getCause()) == ErrorCodes.MERGE_CONFLICT) {
                project.synchronize();
            }
            handleError(error.getCause(), PULL_COMMAND_NAME);
        }
    });
}
Also used : PullResponse(org.eclipse.che.api.git.shared.PullResponse) PromiseError(org.eclipse.che.api.promises.client.PromiseError) GitOutputConsole(org.eclipse.che.ide.ext.git.client.outputconsole.GitOutputConsole) StatusNotification(org.eclipse.che.ide.api.notification.StatusNotification) Operation(org.eclipse.che.api.promises.client.Operation) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 3 with PullResponse

use of org.eclipse.che.api.git.shared.PullResponse in project che by eclipse.

the class GitServiceClientImpl method pull.

@Override
public Promise<PullResponse> pull(DevMachine devMachine, Path project, String refSpec, String remote) {
    PullRequest pullRequest = dtoFactory.createDto(PullRequest.class).withRemote(remote).withRefSpec(refSpec);
    String url = devMachine.getWsAgentBaseUrl() + PULL + "?projectPath=" + project;
    return asyncRequestFactory.createPostRequest(url, pullRequest).send(dtoUnmarshallerFactory.newUnmarshaller(PullResponse.class));
}
Also used : PullResponse(org.eclipse.che.api.git.shared.PullResponse) PullRequest(org.eclipse.che.api.git.shared.PullRequest)

Aggregations

PullResponse (org.eclipse.che.api.git.shared.PullResponse)3 IOException (java.io.IOException)1 GitException (org.eclipse.che.api.git.exception.GitException)1 PullRequest (org.eclipse.che.api.git.shared.PullRequest)1 Operation (org.eclipse.che.api.promises.client.Operation)1 OperationException (org.eclipse.che.api.promises.client.OperationException)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 StatusNotification (org.eclipse.che.ide.api.notification.StatusNotification)1 GitOutputConsole (org.eclipse.che.ide.ext.git.client.outputconsole.GitOutputConsole)1 FetchCommand (org.eclipse.jgit.api.FetchCommand)1 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)1 DetachedHeadException (org.eclipse.jgit.api.errors.DetachedHeadException)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 Ref (org.eclipse.jgit.lib.Ref)1 StoredConfig (org.eclipse.jgit.lib.StoredConfig)1 FetchResult (org.eclipse.jgit.transport.FetchResult)1 RefSpec (org.eclipse.jgit.transport.RefSpec)1