Example usage for org.eclipse.jgit.lib Constants R_REMOTES

List of usage examples for org.eclipse.jgit.lib Constants R_REMOTES

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants R_REMOTES.

Prototype

String R_REMOTES

To view the source code for org.eclipse.jgit.lib Constants R_REMOTES.

Click Source Link

Document

Prefix for remotes refs

Usage

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

License:Open Source License

String abbreviateRef(String dst, boolean abbreviateRemote) {
    if (dst.startsWith(Constants.R_HEADS))
        dst = dst.substring(Constants.R_HEADS.length());
    else if (dst.startsWith(Constants.R_TAGS))
        dst = dst.substring(Constants.R_TAGS.length());
    else if (abbreviateRemote && dst.startsWith(Constants.R_REMOTES))
        dst = dst.substring(Constants.R_REMOTES.length());
    return dst;//from ww w.  ja v a 2  s .co  m
}

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

protected final void doClone(final String remoteUrl, final String remoteName, final String branch)
        throws GitWrapException {
    final FileRepository repository = getRepository();

    final String branchRef = Constants.R_HEADS + (branch == null ? Constants.MASTER : branch);

    try {/*from  w ww .  j  a  va2s .c  o  m*/
        final RefUpdate head = repository.updateRef(Constants.HEAD);
        head.disableRefLog();
        head.link(branchRef);

        final RemoteConfig remoteConfig = new RemoteConfig(repository.getConfig(), remoteName);
        remoteConfig.addURI(new URIish(remoteUrl));

        final String remoteRef = Constants.R_REMOTES + remoteName;

        RefSpec spec = new RefSpec();
        spec = spec.setForceUpdate(true);
        spec = spec.setSourceDestination(Constants.R_HEADS + "*", remoteRef + "/*");

        remoteConfig.addFetchRefSpec(spec);

        remoteConfig.update(repository.getConfig());

        repository.getConfig().setString("branch", branch, "remote", remoteName);
        repository.getConfig().setString("branch", branch, "merge", branchRef);

        repository.getConfig().save();

        fetch(remoteName);
        postClone(remoteUrl, branchRef);
    } catch (final IOException e) {
        throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage());
    } catch (final URISyntaxException e) {
        throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage());
    }
}

From source file:org.commonjava.gitwrap.BareGitRepository.java

License:Open Source License

public Set<String> getRemotes() throws GitWrapException {
    return getRefs(Constants.R_REMOTES);
}

From source file:org.commonjava.gitwrap.GitPathBuilder.java

License:Open Source License

public GitPathBuilder withRemoteRef(final String remoteName) {
    builder.append(Constants.R_REMOTES).append(remoteName);
    return this;
}

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

License:Open Source License

@Override
public void remoteAdd(RemoteAddRequest request) throws GitException {
    String remoteName = request.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new IllegalArgumentException(ERROR_ADD_REMOTE_NAME_MISSING);
    }//  w ww .  jav a 2s .  c o  m

    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections("remote");
    if (remoteNames.contains(remoteName)) {
        throw new IllegalArgumentException(String.format(ERROR_REMOTE_NAME_ALREADY_EXISTS, remoteName));
    }

    String url = request.getUrl();
    if (isNullOrEmpty(url)) {
        throw new IllegalArgumentException(ERROR_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 IllegalArgumentException("Remote url " + url + " is invalid. ");
    }

    List<String> branches = request.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);
    }
}

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);
    }//from w  ww  .  j  ava2s . c  om

    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.op.CloneOperation.java

License:Open Source License

private void doInit(final IProgressMonitor monitor) throws URISyntaxException, IOException {
    monitor.setTaskName(CoreText.CloneOperation_initializingRepository);

    local = new FileRepository(gitdir);
    local.create();/*from  w w w .j a  v a2s.co m*/

    final RefUpdate head = local.updateRef(Constants.HEAD);
    head.disableRefLog();
    head.link(branch);

    remoteConfig = new RemoteConfig(local.getConfig(), remoteName);
    remoteConfig.addURI(uri);

    final String dst = Constants.R_REMOTES + remoteConfig.getName();
    RefSpec wcrs = new RefSpec();
    wcrs = wcrs.setForceUpdate(true);
    wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$

    if (allSelected) {
        remoteConfig.addFetchRefSpec(wcrs);
    } else {
        for (final Ref ref : selectedBranches)
            if (wcrs.matchSource(ref))
                remoteConfig.addFetchRefSpec(wcrs.expandFromSource(ref));
    }

    // we're setting up for a clone with a checkout
    local.getConfig().setBoolean("core", null, "bare", false); //$NON-NLS-1$ //$NON-NLS-2$

    remoteConfig.update(local.getConfig());

    // branch is like 'Constants.R_HEADS + branchName', we need only
    // the 'branchName' part
    String branchName = branch.substring(Constants.R_HEADS.length());

    // setup the default remote branch for branchName
    local.getConfig().setString("branch", branchName, "remote", remoteName); //$NON-NLS-1$ //$NON-NLS-2$
    local.getConfig().setString("branch", branchName, "merge", branch); //$NON-NLS-1$ //$NON-NLS-2$

    local.getConfig().save();
}

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

License:Open Source License

/**
 * Tries to map a commit to a symbolic reference.
 * <p>/*from   w  w w.  j  a v a 2s.  c  o  m*/
 * This value will be cached for the given commit ID unless refresh is
 * specified. The return value will be the full name, e.g.
 * "refs/remotes/someBranch", "refs/tags/v.1.0"
 * <p>
 * Since this mapping is not unique, the following precedence rules are
 * used:
 * <ul>
 * <li>Tags take precedence over branches</li>
 * <li>Local branches take preference over remote branches</li>
 * <li>Newer references take precedence over older ones where time stamps
 * are available</li>
 * <li>If there are still ambiguities, the reference name with the highest
 * lexicographic value will be returned</li>
 * </ul>
 *
 * @param repository
 *            the {@link Repository}
 * @param commitId
 *            a commit
 * @param refresh
 *            if true, the cache will be invalidated
 * @return the symbolic reference, or <code>null</code> if no such reference
 *         can be found
 */
public String mapCommitToRef(Repository repository, String commitId, boolean refresh) {
    synchronized (commitMappingCache) {

        if (!ObjectId.isId(commitId)) {
            return null;
        }

        Map<String, String> cacheEntry = commitMappingCache.get(repository.getDirectory().toString());
        if (!refresh && cacheEntry != null && cacheEntry.containsKey(commitId)) {
            // this may be null in fact
            return cacheEntry.get(commitId);
        }
        if (cacheEntry == null) {
            cacheEntry = new HashMap<String, String>();
            commitMappingCache.put(repository.getDirectory().getPath(), cacheEntry);
        } else {
            cacheEntry.clear();
        }

        Map<String, Date> tagMap = new HashMap<String, Date>();
        try {
            RevWalk rw = new RevWalk(repository);
            Map<String, Ref> tags = repository.getRefDatabase().getRefs(Constants.R_TAGS);
            for (Ref tagRef : tags.values()) {
                RevTag tag = rw.parseTag(repository.resolve(tagRef.getName()));
                if (tag.getObject().name().equals(commitId)) {
                    Date timestamp;
                    if (tag.getTaggerIdent() != null) {
                        timestamp = tag.getTaggerIdent().getWhen();
                    } else {
                        timestamp = null;
                    }
                    tagMap.put(tagRef.getName(), timestamp);
                }
            }
        } catch (IOException e) {
            // ignore here
        }

        String cacheValue = null;

        if (!tagMap.isEmpty()) {
            // we try to obtain the "latest" tag
            Date compareDate = new Date(0);
            for (Map.Entry<String, Date> tagEntry : tagMap.entrySet()) {
                if (tagEntry.getValue() != null && tagEntry.getValue().after(compareDate)) {
                    compareDate = tagEntry.getValue();
                    cacheValue = tagEntry.getKey();
                }
            }
            // if we don't have time stamps, we sort
            if (cacheValue == null) {
                String compareString = ""; //$NON-NLS-1$
                for (String tagName : tagMap.keySet()) {
                    if (tagName.compareTo(compareString) >= 0) {
                        cacheValue = tagName;
                        compareString = tagName;
                    }
                }
            }
        }

        if (cacheValue == null) {
            // we didnt't find a tag, so let's look for local branches
            Set<String> branchNames = new TreeSet<String>();
            // put this into a sorted set
            try {
                Map<String, Ref> remoteBranches = repository.getRefDatabase().getRefs(Constants.R_HEADS);
                for (Ref branch : remoteBranches.values()) {
                    if (branch.getObjectId().name().equals(commitId)) {
                        branchNames.add(branch.getName());
                    }
                }
            } catch (IOException e) {
                // ignore here
            }
            if (!branchNames.isEmpty()) {
                // get the last (sorted) entry
                cacheValue = branchNames.toArray(new String[branchNames.size()])[branchNames.size() - 1];
            }
        }

        if (cacheValue == null) {
            // last try: remote branches
            Set<String> branchNames = new TreeSet<String>();
            // put this into a sorted set
            try {
                Map<String, Ref> remoteBranches = repository.getRefDatabase().getRefs(Constants.R_REMOTES);
                for (Ref branch : remoteBranches.values()) {
                    if (branch.getObjectId().name().equals(commitId)) {
                        branchNames.add(branch.getName());
                    }
                }
                if (!branchNames.isEmpty()) {
                    // get the last (sorted) entry
                    cacheValue = branchNames.toArray(new String[branchNames.size()])[branchNames.size() - 1];
                }
            } catch (IOException e) {
                // ignore here
            }
        }
        cacheEntry.put(commitId, cacheValue);
        return cacheValue;
    }
}

From source file:org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataTest.java

License:Open Source License

@Test
public void shouldReturnSourceMergeForRemoteBranch() throws Exception {
    // given//from  www .  j a  v  a 2  s. c o m
    Git git = new Git(repo);
    git.branchCreate().setName("test3").setStartPoint("refs/heads/master")
            .setUpstreamMode(SetupUpstreamMode.TRACK).call();
    git.checkout().setName("test3").call();
    repo.renameRef(R_HEADS + "test3", Constants.R_REMOTES + "origin/master").rename();
    GitSynchronizeData gsd = new GitSynchronizeData(repo, "refs/remotes/origin/master", HEAD, false);

    // when
    String srcMerge = gsd.getSrcMerge();

    // then
    assertThat(srcMerge, is("refs/heads/master"));
}

From source file:org.eclipse.egit.core.test.RevUtilsTest.java

License:Open Source License

private boolean isContainedInAnyRemoteRef(RevCommit commit) throws IOException {
    Collection<Ref> remoteRefs = repository.getRefDatabase().getRefs(Constants.R_REMOTES).values();
    return RevUtils.isContainedInAnyRef(repository, commit, remoteRefs);
}