Example usage for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_REMOTE

List of usage examples for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_REMOTE

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_REMOTE.

Prototype

String CONFIG_KEY_REMOTE

To view the source code for org.eclipse.jgit.lib ConfigConstants CONFIG_KEY_REMOTE.

Click Source Link

Document

The "remote" key

Usage

From source file:com.puppetlabs.geppetto.forge.jenkins.RepositoryInfo.java

License:Open Source License

/**
 * Obtains the remote URL that is referenced by the given <code>branchName</code>
 *
 * @return The URL or <code>null</code> if it hasn't been configured
 *         for the given branch./*ww w  .j a  va 2  s  .  co m*/
 */
public static String getRemoteURL(Repository repository) throws IOException {
    StoredConfig repoConfig = repository.getConfig();
    String configuredRemote = repoConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
            repository.getBranch(), ConfigConstants.CONFIG_KEY_REMOTE);
    return configuredRemote == null ? null
            : repoConfig.getString(ConfigConstants.CONFIG_REMOTE_SECTION, configuredRemote,
                    ConfigConstants.CONFIG_KEY_URL);
}

From source file:com.rimerosolutions.ant.git.tasks.FetchTask.java

License:Apache License

@Override
public void doExecute() {
    try {/*from w  ww .  j a va  2  s  .c o m*/
        StoredConfig config = git.getRepository().getConfig();
        List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(config);

        if (remoteConfigs.isEmpty()) {
            URIish uri = new URIish(getUri());

            RemoteConfig remoteConfig = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);
            remoteConfig.addURI(uri);
            remoteConfig.addFetchRefSpec(new RefSpec("+" + Constants.R_HEADS + "*:" + Constants.R_REMOTES
                    + Constants.DEFAULT_REMOTE_NAME + "/*"));

            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
                    ConfigConstants.CONFIG_KEY_REMOTE, Constants.DEFAULT_REMOTE_NAME);
            config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, Constants.MASTER,
                    ConfigConstants.CONFIG_KEY_MERGE, Constants.R_HEADS + Constants.MASTER);

            remoteConfig.update(config);
            config.save();
        }

        List<RefSpec> specs = new ArrayList<RefSpec>(3);

        specs.add(new RefSpec(
                "+" + Constants.R_HEADS + "*:" + Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/*"));
        specs.add(new RefSpec("+" + Constants.R_NOTES + "*:" + Constants.R_NOTES + "*"));
        specs.add(new RefSpec("+" + Constants.R_TAGS + "*:" + Constants.R_TAGS + "*"));

        FetchCommand fetchCommand = git.fetch().setDryRun(dryRun).setThin(thinPack).setRemote(getUri())
                .setRefSpecs(specs).setRemoveDeletedRefs(removeDeletedRefs);

        setupCredentials(fetchCommand);

        if (getProgressMonitor() != null) {
            fetchCommand.setProgressMonitor(getProgressMonitor());
        }

        FetchResult fetchResult = fetchCommand.call();

        GitTaskUtils.validateTrackingRefUpdates(FETCH_FAILED_MESSAGE, fetchResult.getTrackingRefUpdates());

        log(fetchResult.getMessages());

    } catch (URISyntaxException e) {
        throw new GitBuildException("Invalid URI syntax: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new GitBuildException("Could not save or get repository configuration: " + e.getMessage(), e);
    } catch (InvalidRemoteException e) {
        throw new GitBuildException("Invalid remote URI: " + e.getMessage(), e);
    } catch (TransportException e) {
        throw new GitBuildException("Communication error: " + e.getMessage(), e);
    } catch (GitAPIException e) {
        throw new GitBuildException("Unexpected exception: " + e.getMessage(), e);
    }
}

From source file:edu.tum.cs.mylyn.provisioning.git.GitProvisioningUtil.java

License:Open Source License

public static URIish getCloneUri(Config config, String branch) {
    String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch,
            ConfigConstants.CONFIG_KEY_REMOTE);
    String uri = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remote,
            ConfigConstants.CONFIG_KEY_URL);
    try {//ww w  .  ja  va2 s  .  com
        return new URIish(uri);
    } catch (URISyntaxException e) {
        return null;
    }
}

From source file:net.mobid.codetraq.runnables.GitChecker.java

License:Open Source License

private void clone(String path) {
    LogService.writeMessage("GitChecker is trying to do a clone from " + _server.getServerAddress());
    System.out.printf("GitChecker is trying to do a clone from %s%n", _server.getServerAddress());
    try {/* w  w w . jav a2  s.co  m*/
        File gitDir = new File(path);
        CloneCommand cloner = new CloneCommand();
        cloner.setBare(false);
        cloner.setDirectory(gitDir);
        cloner.setProgressMonitor(new TextProgressMonitor());
        cloner.setRemote("origin");
        cloner.setURI(_server.getServerAddress());
        mGit = cloner.call();
        // for some reason, repository cloned with jgit always has HEAD detached.
        // we need to create a "temporary" branch, then create a "master" branch.
        // we then merge the two...
        if (!isMasterBranchDefined(mGit.getRepository())) {
            // save the remote and merge config values
            mGit.getRepository().getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
                    _server.getServerBranch(), ConfigConstants.CONFIG_KEY_REMOTE, "origin");
            mGit.getRepository().getConfig().setString(ConfigConstants.CONFIG_BRANCH_SECTION,
                    _server.getServerBranch(), ConfigConstants.CONFIG_KEY_MERGE, _server.getServerBranch());
            mGit.getRepository().getConfig().save();
        }
        if (mGit.getRepository().getFullBranch() == null
                || Utilities.isHexString(mGit.getRepository().getFullBranch())) {
            // HEAD is detached and we need to reattach it
            attachHead(mGit, _server.getServerBranch());
        }
    } catch (Exception ex) {
        LogService.getLogger(GitChecker.class.getName()).log(Level.SEVERE, null, ex);
        LogService.writeLog(Level.SEVERE, ex);
    }
}

From source file:net.mobid.codetraq.runnables.GitChecker.java

License:Open Source License

private boolean isMasterBranchDefined(Repository r) {
    if (r.getConfig().getString(ConfigConstants.CONFIG_BRANCH_SECTION, _server.getServerBranch(),
            ConfigConstants.CONFIG_KEY_REMOTE) == null) {
        return false;
    }// ww  w  .j  av a2s.co  m
    return true;
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public PullResponse pull(PullRequest request) throws GitException, UnauthorizedException {
    String remoteName = request.getRemote();
    String remoteUri;/*from w  ww  .  j  av a 2  s  .  c o  m*/
    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 = request.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 = request.getTimeout();
        if (timeout > 0) {
            fetchCommand.setTimeout(timeout);
        }

        FetchResult fetchResult = (FetchResult) executeRemoteCommand(remoteUri, fetchCommand);

        Ref remoteBranchRef = fetchResult.getAdvertisedRef(remoteBranch);
        if (remoteBranchRef == null) {
            remoteBranchRef = fetchResult.getAdvertisedRef(Constants.R_HEADS + remoteBranch);
        }
        if (remoteBranchRef == null) {
            throw new GitException(String.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 = exception.getMessage();
        }
        throw new GitException(errorMessage, exception);
    }
    return newDto(PullResponse.class).withCommandOutput("Successfully pulled from " + remoteUri);
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@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 + "'");
    }//from   w  w w  .j  a  va  2 s.co m

    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(newDto(BranchListRequest.class).withListMode("r"));
            for (Branch remoteBranch : remoteBranches) {
                if (remoteBranch.getDisplayName().startsWith(name)) {
                    branchDelete(
                            newDto(BranchDeleteRequest.class).withName(remoteBranch.getName()).withForce(true));
                }
            }
        }
    }

    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public List<Remote> remoteList(RemoteListRequest request) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
    String remote = request.getRemote();

    if (remote != null && remoteNames.contains(remote)) {
        remoteNames.clear();/*from  w  ww  .j  av  a2 s .co m*/
        remoteNames.add(remote);
    }

    List<Remote> result = new ArrayList<>(remoteNames.size());
    for (String remoteName : remoteNames) {
        try {
            List<URIish> uris = new RemoteConfig(config, remoteName).getURIs();
            result.add(newDto(Remote.class).withName(remoteName)
                    .withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
        } catch (URISyntaxException exception) {
            throw new GitException(exception.getMessage(), exception);
        }
    }
    return result;
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void remoteUpdate(RemoteUpdateRequest request) throws GitException {
    String remoteName = request.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new IllegalArgumentException(ERROR_UPDATE_REMOTE_NAME_MISSING);
    }/*w  w  w  .  j  a v  a2  s.c  o  m*/

    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE);
    if (!remoteNames.contains(remoteName)) {
        throw new IllegalArgumentException("Remote " + remoteName + " not found. ");
    }

    RemoteConfig remoteConfig;
    try {
        remoteConfig = new RemoteConfig(config, remoteName);
    } catch (URISyntaxException e) {
        throw new GitException(e.getMessage(), e);
    }

    List<String> branches = request.getBranches();
    if (!branches.isEmpty()) {
        if (!request.isAddBranches()) {
            remoteConfig.setFetchRefSpecs(Collections.emptyList());
            remoteConfig.setPushRefSpecs(Collections.emptyList());
        } else {
            // Replace wildcard refSpec if any.
            remoteConfig.removeFetchRefSpec(
                    new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*")
                            .setForceUpdate(true));
            remoteConfig.removeFetchRefSpec(
                    new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*"));
        }

        // Add new refSpec.
        for (String branch : branches) {
            remoteConfig.addFetchRefSpec(new RefSpec(
                    Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch)
                            .setForceUpdate(true));
        }
    }

    // Remove URLs first.
    for (String url : request.getRemoveUrl()) {
        try {
            remoteConfig.removeURI(new URIish(url));
        } catch (URISyntaxException e) {
            LOG.debug(ERROR_REMOVING_INVALID_URL);
        }
    }

    // Add new URLs.
    for (String url : request.getAddUrl()) {
        try {
            remoteConfig.addURI(new URIish(url));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Remote url " + url + " is invalid. ");
        }
    }

    // Remove URLs for pushing.
    for (String url : request.getRemovePushUrl()) {
        try {
            remoteConfig.removePushURI(new URIish(url));
        } catch (URISyntaxException e) {
            LOG.debug(ERROR_REMOVING_INVALID_URL);
        }
    }

    // Add URLs for pushing.
    for (String url : request.getAddPushUrl()) {
        try {
            remoteConfig.addPushURI(new URIish(url));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Remote push url " + url + " is invalid. ");
        }
    }

    remoteConfig.update(config);

    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.core.GitProjectSetCapability.java

License:Open Source License

private String asReference(IProject project) throws TeamException {
    RepositoryMapping mapping = RepositoryMapping.getMapping(project);
    String branch;/*from  ww  w. j  a v  a2s  .  c  o m*/
    try {
        branch = mapping.getRepository().getBranch();
    } catch (IOException e) {
        throw new TeamException(
                NLS.bind(CoreText.GitProjectSetCapability_ExportCouldNotGetBranch, project.getName()));
    }
    StoredConfig config = mapping.getRepository().getConfig();
    String remote = config.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branch,
            ConfigConstants.CONFIG_KEY_REMOTE);
    String url = config.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remote,
            ConfigConstants.CONFIG_KEY_URL);
    if (url == null)
        throw new TeamException(NLS.bind(CoreText.GitProjectSetCapability_ExportNoRemote, project.getName()));

    String projectPath = mapping.getRepoRelativePath(project);
    if (projectPath.equals("")) //$NON-NLS-1$
        projectPath = "."; //$NON-NLS-1$

    return asReference(url, branch, projectPath);
}