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

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

Introduction

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

Prototype

String CONFIG_REMOTE_SECTION

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

Click Source Link

Document

The "remote" section

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.// w ww  . j a  v  a 2  s  .c  o 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: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 {//from  ww  w.  j ava 2  s .c om
        return new URIish(uri);
    } catch (URISyntaxException e) {
        return null;
    }
}

From source file:net.polydawn.mdm.commands.MdmReleaseInitCommand.java

License:Open Source License

public void parse(Namespace args) {
    name = args.getString("name");
    path = args.getString("repo");

    // check if we're in a repo root.  we'll suggest slightly different default values if we are, as well as generate a gitlink in this repo to the new release repo.
    asSubmodule = isInRepoRoot();//from  w  w w . ja va2s.  c  om

    // pick out the name, if not given.
    if (name == null) {
        String prompt = "what's the name of this project";
        String nameSuggest = new File(System.getProperty("user.dir")).getName();
        if (args.getBoolean("use_defaults"))
            name = nameSuggest;
        else if (asSubmodule)
            prompt += " [default: " + nameSuggest + "] ";
        else
            nameSuggest = null;

        while (name == null) {
            name = inputPrompt(os, prompt + "?");
            if (name.equals("") && nameSuggest != null)
                name = nameSuggest;
            name = name.trim();
            if (name.equals(""))
                name = null;
        }
    }

    // the rest of args parsing is only valid if we're making releases repo that's a submodule.
    if (!asSubmodule)
        return;

    // ask for remote url.
    remotePublicUrl = args.getString("remote_url");
    if (remotePublicUrl == null)
        if (args.getBoolean("use_defaults")) {
            String parentRemote = repo.getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
                    ConfigConstants.CONFIG_KEY_URL);
            if (parentRemote == null)
                parentRemote = System.getProperty("user.dir");
            remotePublicUrl = "../" + name + "-releases.git";
            try {
                remotePublicUrl = new URI(parentRemote + "/").resolve(remotePublicUrl).toString();
            } catch (URISyntaxException e) {
            }
        } else
            remotePublicUrl = inputPrompt(os, "Configure a remote url where this repo will be accessible?\n"
                    + "This will be committed to the project's .gitmodules file, and so should be a publicly accessible url.\n"
                    + "remote url: ");
    remotePublicUrl = remotePublicUrl.trim();

    // and another.
    remotePublishUrl = args.getString("remote_publish_url");
    if (remotePublishUrl == null)
        if (args.getBoolean("use_defaults"))
            remotePublishUrl = remotePublicUrl;
        else
            remotePublishUrl = inputPrompt(os,
                    "Configure a remote url you'll use to push this repo when making releases?\n"
                            + "This will not be committed to the project; just set in your local config.\n"
                            + "remote url [leave blank to use the same public url]: ");
    remotePublishUrl = remotePublishUrl.trim();
    if (remotePublishUrl.equals(""))
        remotePublishUrl = remotePublicUrl;
}

From source file:net.polydawn.mdm.commands.MdmReleaseInitCommand.java

License:Open Source License

/**
 * Write origin and fetch config into the release module. Only performed when
 * operating in submodule mode, since otherwise we don't have any requirement to
 * request a value for {@link #remotePublishUrl}.
 *
 * @param releaserepo/*from   w  ww  . j  a va  2  s  .c o  m*/
 * @throws IOException
 */
void writeReleaseRepoConfig(Repository releaserepo) throws IOException {
    releaserepo.getConfig().setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
            ConfigConstants.CONFIG_KEY_URL, remotePublishUrl);
    releaserepo.getConfig().setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "fetch",
            "+refs/heads/*:refs/remotes/origin/*");
    releaserepo.getConfig().save();
}

From source file:net.polydawn.mdm.Plumbing.java

License:Open Source License

/**
 * Equivalent of calling `git remote add -t mdm/init origin [url]` &mdash; set up
 * the remote origin and use the "-t" option here to limit what can be
 * automatically dragged down from the network by a `git pull` (this is necessary
 * because even pulling in the parent project will recurse to fetching submodule
 * content as well).//  w ww. j a  va  2s  .  c  o  m
 * <p>
 * The url is taken from the parent repo's local .git/config entry for this
 * submodule (which should have already been initialized by a call to
 * {@link #initLocalConfig(Repository, MdmModule)}), and may be subject to some
 * transformations before it is saved to the submodule's .git/config (namely,
 * github urls may be altered to make sure we don't hit their user-agent-sensitive
 * http API).
 */
public static void setMdmRemote(MdmModule module) {
    String url = module.getUrlLocal();
    if (isGithubHttpUrl(url) && !url.endsWith(".git")) {
        // Github 404's unknown user agents only from some urls, so in order to have jgit accept the same urls that cgit will accept, we rewrite to the url that always responds correctly.
        url += ".git";
    }
    module.getRepo().getConfig().setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin",
            ConfigConstants.CONFIG_KEY_URL, url);
    module.getRepo().getConfig().setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "fetch",
            "+refs/heads/mdm/init:refs/remotes/origin/mdm/init");
}

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

License:Open Source License

@Override
public void fetch(FetchRequest request) throws GitException, UnauthorizedException {
    String remoteName = request.getRemote();
    String remoteUri;//w w w. jav  a  2s.co  m
    try {
        List<RefSpec> fetchRefSpecs;
        List<String> refSpec = request.getRefSpec();
        if (!refSpec.isEmpty()) {
            fetchRefSpecs = new ArrayList<>(refSpec.size());
            for (String refSpecItem : refSpec) {
                RefSpec fetchRefSpec = (refSpecItem.indexOf(':') < 0) //
                        ? new RefSpec(Constants.R_HEADS + refSpecItem + ":") //
                        : new RefSpec(refSpecItem);
                fetchRefSpecs.add(fetchRefSpec);
            }
        } else {
            fetchRefSpecs = Collections.emptyList();
        }

        FetchCommand fetchCommand = getGit().fetch();

        // If this an unknown remote with no refspecs given, put HEAD
        // (otherwise JGit fails)
        if (remoteName != null && refSpec.isEmpty()) {
            boolean found = false;
            List<Remote> configRemotes = remoteList(newDto(RemoteListRequest.class));
            for (Remote configRemote : configRemotes) {
                if (remoteName.equals(configRemote.getName())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                fetchRefSpecs = Collections
                        .singletonList(new RefSpec(Constants.HEAD + ":" + Constants.FETCH_HEAD));
            }
        }

        if (remoteName == null) {
            remoteName = Constants.DEFAULT_REMOTE_NAME;
        }
        fetchCommand.setRemote(remoteName);
        remoteUri = getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName,
                ConfigConstants.CONFIG_KEY_URL);
        fetchCommand.setRefSpecs(fetchRefSpecs);

        int timeout = request.getTimeout();
        if (timeout > 0) {
            fetchCommand.setTimeout(timeout);
        }
        fetchCommand.setRemoveDeletedRefs(request.isRemoveDeletedRefs());

        executeRemoteCommand(remoteUri, fetchCommand);
    } catch (GitException | GitAPIException exception) {
        String errorMessage;
        if (exception.getMessage().contains("Invalid remote: ")) {
            errorMessage = ERROR_NO_REMOTE_REPOSITORY;
        } else if ("Nothing to fetch.".equals(exception.getMessage())) {
            return;
        } else {
            errorMessage = exception.getMessage();
        }
        throw new GitException(errorMessage, exception);
    }
}

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;//  w  ww  . ja  v 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 PushResponse push(PushRequest request) throws GitException, UnauthorizedException {
    String remoteName = request.getRemote();
    String remoteUri = getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, remoteName,
            ConfigConstants.CONFIG_KEY_URL);
    PushResponse pushResponseDto = newDto(PushResponse.class);
    try {/*www . j  a v a2 s. c  om*/
        PushCommand pushCommand = getGit().push();

        if (request.getRemote() != null) {
            pushCommand.setRemote(remoteName);
        }
        List<String> refSpec = request.getRefSpec();
        if (!refSpec.isEmpty()) {
            List<RefSpec> refSpecInst = new ArrayList<>(refSpec.size());
            refSpecInst.addAll(refSpec.stream().map(RefSpec::new).collect(Collectors.toList()));
            pushCommand.setRefSpecs(refSpecInst);
        }

        pushCommand.setForce(request.isForce());

        int timeout = request.getTimeout();
        if (timeout > 0) {
            pushCommand.setTimeout(timeout);
        }

        @SuppressWarnings("unchecked")
        Iterable<PushResult> pushResults = (Iterable<PushResult>) executeRemoteCommand(remoteUri, pushCommand);
        PushResult pushResult = pushResults.iterator().next();
        return addCommandOutputUpdates(pushResponseDto, request, pushResult);
    } catch (GitAPIException exception) {
        if ("origin: not found.".equals(exception.getMessage())) {
            throw new GitException(ERROR_NO_REMOTE_REPOSITORY, exception);
        } else {
            throw new GitException(exception.getMessage(), exception);
        }
    }
}

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 + "'");
    }//w  ww .j  av a2s.c om

    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.egit.core.GitProjectSetCapability.java

License:Open Source License

private String asReference(IProject project) throws TeamException {
    RepositoryMapping mapping = RepositoryMapping.getMapping(project);
    String branch;//  ww w .j a va2 s  . c  om
    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);
}