Example usage for org.eclipse.jgit.lib Repository shortenRefName

List of usage examples for org.eclipse.jgit.lib Repository shortenRefName

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository shortenRefName.

Prototype

@NonNull
public static String shortenRefName(String refName) 

Source Link

Document

Get a shortened more user friendly ref name

Usage

From source file:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public Ref checkout(final Git git, final Ref localRef, final SetupUpstreamMode mode, final boolean force)
        throws GitAPIException {
    CheckoutCommand checkout = git.checkout();
    checkout.setName(Repository.shortenRefName(localRef.getName()));
    checkout.setForce(force);/*w  w  w  .j  a v  a 2  s  . co  m*/
    checkout.setUpstreamMode(mode);
    return checkout.call();
}

From source file:org.jboss.forge.git.GitUtils.java

License:Open Source License

public static Ref checkout(final Git git, final Ref localRef, final boolean createBranch,
        final SetupUpstreamMode mode, final boolean force) throws GitAPIException {
    CheckoutCommand checkout = git.checkout();
    checkout.setName(Repository.shortenRefName(localRef.getName()));
    checkout.setForce(force);/*from  w  ww .  j  a v  a2  s.  com*/
    checkout.setUpstreamMode(mode);
    return checkout.call();
}

From source file:org.jfrog.bamboo.release.scm.git.ResetCommand.java

License:Eclipse Distribution License

/**
 * Executes the {@code Reset} command. Each instance of this class should only be used for one invocation of the
 * command. Don't call this method twice on an instance.
 *
 * @return the Ref after reset/*from  w w w . ja  va2 s  . c  o  m*/
 */
@Override
public Ref call() throws GitAPIException {
    checkCallable();

    Ref r;
    RevCommit commit;

    try {
        boolean merging = false;
        if (repo.getRepositoryState().equals(RepositoryState.MERGING)
                || repo.getRepositoryState().equals(RepositoryState.MERGING_RESOLVED)) {
            merging = true;
        }

        // resolve the ref to a commit
        final ObjectId commitId;
        try {
            commitId = repo.resolve(ref);
        } catch (IOException e) {
            throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotRead, ref), e);
        }
        RevWalk rw = new RevWalk(repo);
        try {
            commit = rw.parseCommit(commitId);
        } catch (IOException e) {
            throw new JGitInternalException(
                    MessageFormat.format(JGitText.get().cannotReadCommit, commitId.toString()), e);
        } finally {
            rw.release();
        }

        // write the ref
        final RefUpdate ru = repo.updateRef(Constants.HEAD);
        ru.setNewObjectId(commitId);

        String refName = Repository.shortenRefName(ref);
        String message = "reset --" //$NON-NLS-1$
                + mode.toString().toLowerCase() + " " + refName; //$NON-NLS-1$
        ru.setRefLogMessage(message, false);
        if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) {
            throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotLock, ru.getName()));
        }

        switch (mode) {
        case HARD:
            checkoutIndex(commit);
            break;
        case MIXED:
            resetIndex(commit);
            break;
        case SOFT: // do nothing, only the ref was changed
            break;
        }

        if (mode != ResetType.SOFT && merging) {
            resetMerge();
        }

        setCallable(false);
        r = ru.getRef();
    } catch (IOException e) {
        throw new JGitInternalException("Error while executing reset command");
    }

    return r;
}

From source file:util.ChkoutCmd.java

License:Eclipse Distribution License

/**
 * @throws RefAlreadyExistsException/*from w  w w. j av  a  2 s .  c o m*/
 *             when trying to create (without force) a branch with a name
 *             that already exists
 * @throws RefNotFoundException
 *             if the start point or branch can not be found
 * @throws InvalidRefNameException
 *             if the provided name is <code>null</code> or otherwise
 *             invalid
 * @throws CheckoutConflictException
 *             if the checkout results in a conflict
 * @return the newly created branch
 */
public Ref call() throws GitAPIException, RefAlreadyExistsException, RefNotFoundException,
        InvalidRefNameException, CheckoutConflictException {
    checkCallable();
    processOptions();
    try {
        if (checkoutAllPaths || !paths.isEmpty()) {
            checkoutPaths();
            status = new CheckoutResult(Status.OK, paths);
            setCallable(false);
            return null;
        }

        if (createBranch) {
            Git git = new Git(repo);
            CreateBranchCommand command = git.branchCreate();
            command.setName(name);
            command.setStartPoint(getStartPoint().name());
            if (upstreamMode != null)
                command.setUpstreamMode(upstreamMode);
            command.call();
        }

        Ref headRef = repo.getRef(Constants.HEAD);
        String shortHeadRef = getShortBranchName(headRef);
        String refLogMessage = "checkout: moving from " + shortHeadRef; //$NON-NLS-1$
        ObjectId branch = repo.resolve(name);
        if (branch == null)
            throw new RefNotFoundException(MessageFormat.format(JGitText.get().refNotResolved, name));

        RevWalk revWalk = new RevWalk(repo);
        AnyObjectId headId = headRef.getObjectId();
        RevCommit headCommit = headId == null ? null : revWalk.parseCommit(headId);
        RevCommit newCommit = revWalk.parseCommit(branch);
        RevTree headTree = headCommit == null ? null : headCommit.getTree();
        DirCacheCheckout dco;
        DirCache dc = repo.lockDirCache();
        try {
            dco = new DirCacheCheckout(repo, headTree, dc, newCommit.getTree());
            dco.setFailOnConflict(false);
            try {
                dco.checkout();
            } catch (org.eclipse.jgit.errors.CheckoutConflictException e) {
                status = new CheckoutResult(Status.CONFLICTS, dco.getConflicts());
                throw new CheckoutConflictException(dco.getConflicts(), e);
            }
        } finally {
            dc.unlock();
        }
        Ref ref = repo.getRef(name);
        if (ref != null && !ref.getName().startsWith(Constants.R_HEADS))
            ref = null;
        String toName = Repository.shortenRefName(name);
        RefUpdate refUpdate = repo.updateRef(Constants.HEAD, ref == null);
        refUpdate.setForceUpdate(force);
        refUpdate.setRefLogMessage(refLogMessage + " to " + toName, false); //$NON-NLS-1$
        Result updateResult;
        if (ref != null)
            updateResult = refUpdate.link(ref.getName());
        else {
            refUpdate.setNewObjectId(newCommit);
            updateResult = refUpdate.forceUpdate();
        }

        setCallable(false);

        boolean ok = false;
        switch (updateResult) {
        case NEW:
            ok = true;
            break;
        case NO_CHANGE:
        case FAST_FORWARD:
        case FORCED:
            ok = true;
            break;
        default:
            break;
        }

        if (!ok)
            throw new JGitInternalException(
                    MessageFormat.format(JGitText.get().checkoutUnexpectedResult, updateResult.name()));

        if (!dco.getToBeDeleted().isEmpty()) {
            status = new CheckoutResult(Status.NONDELETED, dco.getToBeDeleted());
        } else
            status = new CheckoutResult(new ArrayList<String>(dco.getUpdated().keySet()), dco.getRemoved());

        return ref;
    } catch (IOException ioe) {
        throw new JGitInternalException(ioe.getMessage(), ioe);
    } finally {
        if (status == null)
            status = CheckoutResult.ERROR_RESULT;
    }
}

From source file:util.ChkoutCmd.java

License:Eclipse Distribution License

private String getShortBranchName(Ref headRef) {
    if (headRef.getTarget().getName().equals(headRef.getName()))
        return headRef.getTarget().getObjectId().getName();
    return Repository.shortenRefName(headRef.getTarget().getName());
}