Example usage for org.eclipse.jgit.lib RefUpdate getRef

List of usage examples for org.eclipse.jgit.lib RefUpdate getRef

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib RefUpdate getRef.

Prototype

public Ref getRef() 

Source Link

Document

Get the reference this update will create or modify.

Usage

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/*w  w w .  j  ava2s .co 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:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static void commit(final Git git, final String branchName, final String name, final String email,
        final String message, final TimeZone timeZone, final Date when, final Map<String, File> content) {

    final PersonIdent author = buildPersonIdent(git, name, email, timeZone, when);

    try {// ww w .  j  a va2  s. c  o  m
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");
            final DirCache index = createTemporaryIndex(git, headId, content);
            final ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            final CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage(message);
            //headId can be null if the repository has no commit yet
            if (headId != null) {
                commit.setParentId(headId);
            }
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            final ObjectId commitId = odi.insert(commit);
            odi.flush();

            final RevWalk revWalk = new RevWalk(git.getRepository());
            try {
                final RevCommit revCommit = revWalk.parseCommit(commitId);
                final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                if (headId == null) {
                    ru.setExpectedOldObjectId(ObjectId.zeroId());
                } else {
                    ru.setExpectedOldObjectId(headId);
                }
                ru.setNewObjectId(commitId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                final RefUpdate.Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            Constants.HEAD, commitId.toString(), rc));
                }

            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

private static void updateHead(final Git git, final StringBuilder refLogMessage, final ObjectId newHeadId,
        final ObjectId oldHeadID) throws java.io.IOException, ConcurrentRefUpdateException {
    RefUpdate refUpdate = git.getRepository().updateRef(Constants.HEAD);
    refUpdate.setNewObjectId(newHeadId);
    refUpdate.setRefLogMessage(refLogMessage.toString(), false);
    refUpdate.setExpectedOldObjectId(oldHeadID);
    RefUpdate.Result rc = refUpdate.update();
    switch (rc) {
    case NEW://from w  ww . j a v a  2 s .c  om
    case FAST_FORWARD:
        return;
    case REJECTED:
    case LOCK_FAILURE:
        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
    default:
        throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD,
                newHeadId.toString(), rc));
    }
}

From source file:org.moxie.utils.JGitUtils.java

License:Apache License

public static void updateGhPages(File repositoryFolder, File sourceFolder, boolean obliterate) {
    String ghpages = "refs/heads/gh-pages";
    try {//from ww  w .  j a va 2s  .c o  m
        File gitDir = FileKey.resolve(repositoryFolder, FS.DETECTED);
        Repository repository = new FileRepository(gitDir);

        ObjectId objectId = repository.resolve(ghpages);
        if (objectId == null) {
            JGitUtils.createOrphanBranch(repository, "gh-pages", null);
        }

        System.out.println("Updating gh-pages branch...");
        ObjectId headId = repository.resolve(ghpages + "^{commit}");
        ObjectInserter odi = repository.newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            DirCache index = createIndex(repository, headId, sourceFolder, obliterate);
            ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            PersonIdent author = new PersonIdent("Moxie", "moxie@localhost");
            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage("updated pages");
            commit.setParentId(headId);
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevWalk revWalk = new RevWalk(repository);
            try {
                RevCommit revCommit = revWalk.parseCommit(commitId);
                RefUpdate ru = repository.updateRef(ghpages);
                ru.setNewObjectId(commitId);
                ru.setExpectedOldObjectId(headId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            ghpages, commitId.toString(), rc));
                }
            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
        System.out.println("gh-pages updated.");
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.SimpleRefUpdateCommand.java

License:Apache License

private void forceUpdate(final RefUpdate ru, final ObjectId id)
        throws java.io.IOException, ConcurrentRefUpdateException {
    final RefUpdate.Result rc = ru.forceUpdate();
    switch (rc) {
    case NEW:/*from  w w w. j av  a2 s .co  m*/
    case FORCED:
    case FAST_FORWARD:
        break;
    case REJECTED:
    case LOCK_FAILURE:
        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
    default:
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, id.toString(), rc));
    }
}

From source file:org.uberfire.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static boolean commit(final Git git, final String branchName, final CommitInfo commitInfo,
        final boolean amend, final ObjectId _originId, final CommitContent content) {
    boolean hadEffecitiveCommit = true;
    final PersonIdent author = buildPersonIdent(git, commitInfo.getName(), commitInfo.getEmail(),
            commitInfo.getTimeZone(), commitInfo.getWhen());

    try {// w ww .j a va  2 s  .  c o m
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");

            final ObjectId originId;
            if (_originId == null) {
                originId = git.getRepository().resolve(branchName + "^{commit}");
            } else {
                originId = _originId;
            }

            final DirCache index;
            if (content instanceof DefaultCommitContent) {
                index = createTemporaryIndex(git, originId, (DefaultCommitContent) content);
            } else if (content instanceof MoveCommitContent) {
                index = createTemporaryIndex(git, originId, (MoveCommitContent) content);
            } else if (content instanceof CopyCommitContent) {
                index = createTemporaryIndex(git, originId, (CopyCommitContent) content);
            } else if (content instanceof RevertCommitContent) {
                index = createTemporaryIndex(git, originId);
            } else {
                index = null;
            }

            if (index != null) {
                final ObjectId indexTreeId = index.writeTree(odi);

                // Create a commit object
                final CommitBuilder commit = new CommitBuilder();
                commit.setAuthor(author);
                commit.setCommitter(author);
                commit.setEncoding(Constants.CHARACTER_ENCODING);
                commit.setMessage(commitInfo.getMessage());
                //headId can be null if the repository has no commit yet
                if (headId != null) {
                    if (amend) {
                        final List<ObjectId> parents = new LinkedList<ObjectId>();
                        final RevCommit previousCommit = new RevWalk(git.getRepository()).parseCommit(headId);
                        final RevCommit[] p = previousCommit.getParents();
                        for (final RevCommit revCommit : p) {
                            parents.add(0, revCommit.getId());
                        }
                        commit.setParentIds(parents);
                    } else {
                        commit.setParentId(headId);
                    }
                }
                commit.setTreeId(indexTreeId);

                // Insert the commit into the repository
                final ObjectId commitId = odi.insert(commit);
                odi.flush();

                final RevWalk revWalk = new RevWalk(git.getRepository());
                try {
                    final RevCommit revCommit = revWalk.parseCommit(commitId);
                    final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                    if (headId == null) {
                        ru.setExpectedOldObjectId(ObjectId.zeroId());
                    } else {
                        ru.setExpectedOldObjectId(headId);
                    }
                    ru.setNewObjectId(commitId);
                    ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                    final RefUpdate.Result rc = ru.forceUpdate();
                    switch (rc) {
                    case NEW:
                    case FORCED:
                    case FAST_FORWARD:
                        break;
                    case REJECTED:
                    case LOCK_FAILURE:
                        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(),
                                rc);
                    default:
                        throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                                Constants.HEAD, commitId.toString(), rc));
                    }

                } finally {
                    revWalk.release();
                }
            } else {
                hadEffecitiveCommit = false;
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
    return hadEffecitiveCommit;
}