Search in sources :

Example 11 with GitException

use of org.eclipse.che.api.git.exception.GitException 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 12 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class JGitConnection method tagDelete.

@Override
public void tagDelete(String name) throws GitException {
    try {
        Ref tagRef = repository.findRef(name);
        if (tagRef == null) {
            throw new GitException("Tag " + name + " not found. ");
        }
        RefUpdate updateRef = repository.updateRef(tagRef.getName());
        updateRef.setRefLogMessage("tag deleted", false);
        updateRef.setForceUpdate(true);
        Result deleteResult = updateRef.delete();
        if (deleteResult != Result.FORCED && deleteResult != Result.FAST_FORWARD) {
            throw new GitException(format(ERROR_TAG_DELETE, name, deleteResult));
        }
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) GitException(org.eclipse.che.api.git.exception.GitException) IOException(java.io.IOException) RefUpdate(org.eclipse.jgit.lib.RefUpdate) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) TrackingRefUpdate(org.eclipse.jgit.transport.TrackingRefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result) RebaseResult(org.eclipse.jgit.api.RebaseResult) PushResult(org.eclipse.jgit.transport.PushResult) MergeResult(org.eclipse.che.api.git.shared.MergeResult) FetchResult(org.eclipse.jgit.transport.FetchResult)

Example 13 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class JGitConnection method log.

/** @see org.eclipse.che.api.git.GitConnection#log(LogParams) */
@Override
public LogPage log(LogParams params) throws GitException {
    LogCommand logCommand = getGit().log();
    try {
        setRevisionRange(logCommand, params);
        logCommand.setSkip(params.getSkip());
        logCommand.setMaxCount(params.getMaxCount());
        List<String> fileFilter = params.getFileFilter();
        if (fileFilter != null) {
            fileFilter.forEach(logCommand::addPath);
        }
        String filePath = params.getFilePath();
        if (!isNullOrEmpty(filePath)) {
            logCommand.addPath(filePath);
        }
        Iterator<RevCommit> revIterator = logCommand.call().iterator();
        List<Revision> commits = new ArrayList<>();
        while (revIterator.hasNext()) {
            RevCommit commit = revIterator.next();
            Revision revision = getRevision(commit, filePath);
            commits.add(revision);
        }
        return new LogPage(commits);
    } catch (GitAPIException | IOException exception) {
        String errorMessage = exception.getMessage();
        if (ERROR_LOG_NO_HEAD_EXISTS.equals(errorMessage)) {
            throw new GitException(errorMessage, ErrorCodes.INIT_COMMIT_WAS_NOT_PERFORMED);
        } else {
            LOG.error("Failed to retrieve log. ", exception);
            throw new GitException(exception);
        }
    }
}
Also used : GitException(org.eclipse.che.api.git.exception.GitException) LogPage(org.eclipse.che.api.git.LogPage) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Revision(org.eclipse.che.api.git.shared.Revision) LogCommand(org.eclipse.jgit.api.LogCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 14 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class JGitConnection method add.

@Override
public void add(AddParams params) throws GitException {
    AddCommand addCommand = getGit().add().setUpdate(params.isUpdate());
    List<String> filePatterns = params.getFilePattern();
    if (filePatterns.isEmpty()) {
        filePatterns = AddRequest.DEFAULT_PATTERN;
    }
    filePatterns.forEach(addCommand::addFilepattern);
    try {
        addCommand.call();
        addDeletedFilesToIndex(filePatterns);
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitException(org.eclipse.che.api.git.exception.GitException) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 15 with GitException

use of org.eclipse.che.api.git.exception.GitException in project che by eclipse.

the class JGitConnection method remoteAdd.

@Override
public void remoteAdd(RemoteAddParams params) throws GitException {
    String remoteName = params.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new GitException(ERROR_ADD_REMOTE_NAME_MISSING);
    }
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections("remote");
    if (remoteNames.contains(remoteName)) {
        throw new GitException(format(ERROR_ADD_REMOTE_NAME_ALREADY_EXISTS, remoteName));
    }
    String url = params.getUrl();
    if (isNullOrEmpty(url)) {
        throw new GitException(ERROR_ADD_REMOTE_URL_MISSING);
    }
    RemoteConfig remoteConfig;
    try {
        remoteConfig = new RemoteConfig(config, remoteName);
    } catch (URISyntaxException exception) {
        // Not happen since it is newly created remote.
        throw new GitException(exception.getMessage(), exception);
    }
    try {
        remoteConfig.addURI(new URIish(url));
    } catch (URISyntaxException exception) {
        throw new GitException("Remote url " + url + " is invalid. ");
    }
    List<String> branches = params.getBranches();
    if (branches.isEmpty()) {
        remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*").setForceUpdate(true));
    } else {
        for (String branch : branches) {
            remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch).setForceUpdate(true));
        }
    }
    remoteConfig.update(config);
    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) RefSpec(org.eclipse.jgit.transport.RefSpec) GitException(org.eclipse.che.api.git.exception.GitException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Aggregations

GitException (org.eclipse.che.api.git.exception.GitException)33 IOException (java.io.IOException)20 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)18 ArrayList (java.util.ArrayList)10 File (java.io.File)9 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)9 StoredConfig (org.eclipse.jgit.lib.StoredConfig)9 Ref (org.eclipse.jgit.lib.Ref)8 URISyntaxException (java.net.URISyntaxException)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 GitConnection (org.eclipse.che.api.git.GitConnection)5 CheckoutConflictException (org.eclipse.jgit.api.errors.CheckoutConflictException)5 Branch (org.eclipse.che.api.git.shared.Branch)4 GitUser (org.eclipse.che.api.git.shared.GitUser)4 MergeResult (org.eclipse.che.api.git.shared.MergeResult)4 Remote (org.eclipse.che.api.git.shared.Remote)4 FetchCommand (org.eclipse.jgit.api.FetchCommand)4 RebaseResult (org.eclipse.jgit.api.RebaseResult)4 FetchResult (org.eclipse.jgit.transport.FetchResult)4 PushResult (org.eclipse.jgit.transport.PushResult)4