Search in sources :

Example 1 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project che by eclipse.

the class JGitConnection method remoteList.

@Override
public List<Remote> remoteList(String remoteName, boolean verbose) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
    if (remoteName != null && remoteNames.contains(remoteName)) {
        remoteNames.clear();
        remoteNames.add(remoteName);
    }
    List<Remote> result = new ArrayList<>(remoteNames.size());
    for (String remote : remoteNames) {
        try {
            List<URIish> uris = new RemoteConfig(config, remote).getURIs();
            result.add(newDto(Remote.class).withName(remote).withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
        } catch (URISyntaxException exception) {
            throw new GitException(exception.getMessage(), exception);
        }
    }
    return result;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) GitException(org.eclipse.che.api.git.exception.GitException) ArrayList(java.util.ArrayList) Remote(org.eclipse.che.api.git.shared.Remote) URISyntaxException(java.net.URISyntaxException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) HashSet(java.util.HashSet)

Example 2 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project che by eclipse.

the class JGitConfigImpl method getList.

@Override
public List<String> getList() throws GitException {
    List<String> results = new ArrayList<>();
    // Iterate all sections and subsections, printing all values
    StoredConfig config = repository.getConfig();
    for (String section : config.getSections()) {
        for (String subsection : config.getSubsections(section)) {
            Set<String> names = config.getNames(section, subsection);
            addConfigValues(section, subsection, names, results);
        }
        Set<String> names = config.getNames(section);
        addConfigValues(section, null, names, results);
    }
    return results;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) ArrayList(java.util.ArrayList)

Example 3 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig in project che by eclipse.

the class JGitConnection method remoteDelete.

@Override
public void remoteDelete(String name) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE);
    if (!remoteNames.contains(name)) {
        throw new GitException("error: Could not remove config section 'remote." + name + "'");
    }
    config.unsetSection(ConfigConstants.CONFIG_REMOTE_SECTION, name);
    Set<String> branches = config.getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION);
    for (String branch : branches) {
        String r = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
        if (name.equals(r)) {
            config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_REMOTE);
            config.unset(ConfigConstants.CONFIG_BRANCH_SECTION, branch, ConfigConstants.CONFIG_KEY_MERGE);
            List<Branch> remoteBranches = branchList(LIST_REMOTE);
            for (Branch remoteBranch : remoteBranches) {
                if (remoteBranch.getDisplayName().startsWith(name)) {
                    branchDelete(remoteBranch.getName(), true);
                }
            }
        }
    }
    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) GitException(org.eclipse.che.api.git.exception.GitException) Branch(org.eclipse.che.api.git.shared.Branch) IOException(java.io.IOException)

Example 4 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig 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 5 with StoredConfig

use of org.eclipse.jgit.lib.StoredConfig 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

StoredConfig (org.eclipse.jgit.lib.StoredConfig)40 IOException (java.io.IOException)26 Repository (org.eclipse.jgit.lib.Repository)19 File (java.io.File)9 ArrayList (java.util.ArrayList)8 URISyntaxException (java.net.URISyntaxException)6 GitException (org.eclipse.che.api.git.exception.GitException)6 SimpleDateFormat (java.text.SimpleDateFormat)5 GitBlitException (com.gitblit.GitBlitException)4 RepositoryModel (com.gitblit.models.RepositoryModel)4 Point (java.awt.Point)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)4 TeamModel (com.gitblit.models.TeamModel)3 Change (com.gitblit.models.TicketModel.Change)3 UserModel (com.gitblit.models.UserModel)3 HashSet (java.util.HashSet)3 Git (org.eclipse.jgit.api.Git)3 RefSpec (org.eclipse.jgit.transport.RefSpec)3 AccessPermission (com.gitblit.Constants.AccessPermission)2