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

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

Introduction

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

Prototype

String R_HEADS

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

Click Source Link

Document

Prefix for branch refs

Usage

From source file:org.chodavarapu.jgitaws.jgit.AmazonRepository.java

License:Eclipse Distribution License

@Override
public void create(boolean bare) throws IOException {
    if (exists())
        throw new IOException(MessageFormat.format(JGitText.get().repositoryAlreadyExists, ""));

    String master = Constants.R_HEADS + Constants.MASTER;
    RefUpdate.Result result = updateRef(Constants.HEAD, true).link(master);
    if (result != RefUpdate.Result.NEW)
        throw new IOException(result.name());
}

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  www . j  a v a 2  s . 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> getBranches() throws GitWrapException {
    return getRefs(Constants.R_HEADS);
}

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

License:Open Source License

public BareGitRepository setPushTarget(final String name, final String uri, final boolean heads,
        final boolean tags) throws GitWrapException {
    try {//from  w ww .ja v  a2  s.c om
        final RemoteConfig remote = new RemoteConfig(repository.getConfig(), name);

        final URIish uriish = new URIish(uri);

        final List<URIish> uris = remote.getURIs();
        if (uris == null || !uris.contains(uriish)) {
            remote.addURI(uriish);
        }

        final List<URIish> pushURIs = remote.getPushURIs();
        if (pushURIs == null || !pushURIs.contains(uriish)) {
            remote.addPushURI(uriish);
        }

        final List<RefSpec> pushRefSpecs = remote.getPushRefSpecs();
        if (heads) {
            final RefSpec headSpec = new RefSpec("+" + Constants.R_HEADS + "*:" + Constants.R_HEADS + "*");
            if (pushRefSpecs == null || !pushRefSpecs.contains(headSpec)) {
                remote.addPushRefSpec(headSpec);
            }
        }

        if (tags) {
            final RefSpec tagSpec = new RefSpec("+" + Constants.R_TAGS + "*:" + Constants.R_TAGS + "*");
            if (pushRefSpecs == null || !pushRefSpecs.contains(tagSpec)) {
                remote.addPushRefSpec(tagSpec);
            }
        }

        remote.update(repository.getConfig());
        repository.getConfig().save();
    } catch (final URISyntaxException e) {
        throw new GitWrapException("Invalid URI-ish: %s. Nested error: %s", e, uri, e.getMessage());
    } catch (final IOException e) {
        throw new GitWrapException("Failed to write Git config: %s", e, e.getMessage());
    }

    return this;
}

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

License:Open Source License

public BareGitRepository createBranch(final String source, final String name) throws GitWrapException {
    final String refName = (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) ? name
            : Constants.R_HEADS + name;/*from   w  w w .j a  v  a 2 s .c  om*/

    try {
        String src;
        final Ref from = repository.getRef(source);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Creating branch: " + refName + " from: " + from);
        }

        final ObjectId startAt = repository.resolve(source + "^0");
        if (from != null) {
            src = from.getName();
        } else {
            src = startAt.name();
        }
        src = repository.shortenRefName(src);

        if (!Repository.isValidRefName(refName)) {
            throw new GitWrapException("Invalid branch name: " + refName);
        }

        if (repository.resolve(refName) != null) {
            throw new GitWrapException("Branch: " + refName + " already exists!");
        }

        final RefUpdate updateRef = repository.updateRef(refName);
        updateRef.setNewObjectId(startAt);
        updateRef.setRefLogMessage("branch: Created from " + source, false);
        final Result updateResult = updateRef.update();

        if (updateResult == Result.REJECTED) {
            throw new GitWrapException("Branch creation rejected for: %s", refName);
        }
    } catch (final IOException e) {
        throw new GitWrapException("Failed to create branch: %s from: %s.\nReason: %s", e, refName, source,
                e.getMessage());
    }

    return this;
}

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

License:Open Source License

public boolean hasBranch(final String name) throws GitWrapException {
    final String refName = (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) ? name
            : Constants.R_HEADS + name;//from   w  w w . j  a v  a2 s.co  m

    try {
        return repository.resolve(refName) != null;
    } catch (final IOException e) {
        throw new GitWrapException("Failed to resolve branch: %s.\nReason: %s", e, refName, e.getMessage());
    }

}

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

License:Open Source License

public GitPathBuilder withHeadRef(final String head) {
    builder.append(Constants.R_HEADS).append(head);
    return this;
}

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

License:Open Source License

public GitRepository checkoutBranch(final String name) throws GitWrapException {
    final String refName;
    if (name == null) {
        refName = Constants.HEAD;/*from w  ww.j ava2s.c o  m*/
    } else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) {
        refName = name;
    } else {
        refName = Constants.R_HEADS + name;
    }

    if (hasBranch(refName)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Checking out: " + refName);
        }

        final FileRepository repository = getRepository();
        final boolean detach = !refName.startsWith(Constants.R_HEADS);

        try {
            final RevWalk walk = new RevWalk(repository);
            final RevCommit newCommit = walk.parseCommit(repository.resolve(refName));
            final RevCommit oldCommit = walk.parseCommit(repository.resolve(Constants.HEAD));
            final GitIndex index = repository.getIndex();
            final RevTree newTree = newCommit.getTree();
            final RevTree oldTree = oldCommit.getTree();

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Checking out: " + newCommit + " (resolved from: " + refName + ")");
            }

            final WorkDirCheckout checkout = new WorkDirCheckout(repository, repository.getWorkTree(),
                    repository.mapTree(oldTree), index, repository.mapTree(newTree));

            checkout.checkout();

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Writing index...");
            }

            index.write();

            final RefUpdate u = repository.updateRef(Constants.HEAD, detach);
            Result result;
            if (detach) {
                u.setNewObjectId(newCommit.getId());
                u.setRefLogMessage("Switching to detached branch: " + refName, false);

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Updating head ref to point to: " + newCommit.getId());
                }

                result = u.forceUpdate();
            } else {
                u.setRefLogMessage("Switching to linked branch: " + refName, false);

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Linking head ref to: " + refName);
                }

                result = u.link(refName);
            }

            switch (result) {
            case NEW:
            case FORCED:
            case NO_CHANGE:
            case FAST_FORWARD:
                break;
            default:
                throw new GitWrapException("Error checking out branch: %s. Result: %s", refName,
                        u.getResult().name());
            }
        } catch (final IOException e) {
            throw new GitWrapException("Failed to checkout branch: %s.\nReason: %s", e, refName,
                    e.getMessage());
        }
    } else {
        throw new GitWrapException(
                "Cannot checkout non-existent branch: %s. Perhaps you meant to call createBranch(..)?",
                refName);
    }

    return this;
}

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;//from  w  w w .  j  a v a2 s  .  c o  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 MergeResult merge(MergeRequest request) throws GitException {
    org.eclipse.jgit.api.MergeResult jGitMergeResult;
    MergeResult.MergeStatus status;/*from   w  w  w .j  a  va2  s  .  com*/
    try {
        Ref ref = repository.findRef(request.getCommit());
        if (ref == null) {
            throw new IllegalArgumentException("Invalid reference to commit for merge " + request.getCommit());
        }
        // Shorten local branch names by removing '/refs/heads/' from the beginning
        String name = ref.getName();
        if (name.startsWith(Constants.R_HEADS)) {
            name = name.substring(Constants.R_HEADS.length());
        }
        jGitMergeResult = getGit().merge().include(name, ref.getObjectId()).call();
    } catch (CheckoutConflictException exception) {
        jGitMergeResult = new org.eclipse.jgit.api.MergeResult(exception.getConflictingPaths());
    } catch (IOException | GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }

    switch (jGitMergeResult.getMergeStatus()) {
    case ALREADY_UP_TO_DATE:
        status = MergeResult.MergeStatus.ALREADY_UP_TO_DATE;
        break;
    case CONFLICTING:
        status = MergeResult.MergeStatus.CONFLICTING;
        break;
    case FAILED:
        status = MergeResult.MergeStatus.FAILED;
        break;
    case FAST_FORWARD:
        status = MergeResult.MergeStatus.FAST_FORWARD;
        break;
    case MERGED:
        status = MergeResult.MergeStatus.MERGED;
        break;
    case NOT_SUPPORTED:
        status = MergeResult.MergeStatus.NOT_SUPPORTED;
        break;
    case CHECKOUT_CONFLICT:
        status = MergeResult.MergeStatus.CONFLICTING;
        break;
    default:
        throw new IllegalStateException("Unknown merge status " + jGitMergeResult.getMergeStatus());
    }

    ObjectId[] jGitMergedCommits = jGitMergeResult.getMergedCommits();
    List<String> mergedCommits = new ArrayList<>();
    if (jGitMergedCommits != null) {
        for (ObjectId commit : jGitMergedCommits) {
            mergedCommits.add(commit.getName());
        }
    }

    List<String> conflicts;
    if (org.eclipse.jgit.api.MergeResult.MergeStatus.CHECKOUT_CONFLICT
            .equals(jGitMergeResult.getMergeStatus())) {
        conflicts = jGitMergeResult.getCheckoutConflicts();
    } else {
        Map<String, int[][]> jGitConflicts = jGitMergeResult.getConflicts();
        conflicts = jGitConflicts != null ? new ArrayList<>(jGitConflicts.keySet()) : Collections.emptyList();
    }

    Map<String, ResolveMerger.MergeFailureReason> jGitFailing = jGitMergeResult.getFailingPaths();
    ObjectId newHead = jGitMergeResult.getNewHead();

    return newDto(MergeResult.class)
            .withFailed(jGitFailing != null ? new ArrayList<>(jGitFailing.keySet()) : Collections.emptyList())
            .withNewHead(newHead != null ? newHead.getName() : null).withMergeStatus(status)
            .withConflicts(conflicts).withMergedCommits(mergedCommits);
}