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

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

Introduction

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

Prototype

public void setExpectedOldObjectId(AnyObjectId id) 

Source Link

Document

Set the expected value of the ref after the lock is taken, but before update occurs.

Usage

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

License:Apache License

public void execute() throws IOException, ConcurrentRefUpdateException {
    final ObjectId headId = git.getLastCommit(Constants.R_HEADS + name);
    final RefUpdate ru = git.getRepository().updateRef(Constants.R_HEADS + name);
    if (headId == null) {
        ru.setExpectedOldObjectId(ObjectId.zeroId());
    } else {//  w  w w  .  j  a  va2 s.  c  o m
        ru.setExpectedOldObjectId(headId);
    }
    ru.setNewObjectId(commit.getId());
    ru.setRefLogMessage(commit.getShortMessage(), false);
    forceUpdate(ru, commit.getId());
}

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 {//from w  ww .  ja 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;
}

From source file:playRepository.BareCommit.java

License:Apache License

private void refUpdate(ObjectId commitId, String refName) throws IOException {
    RefUpdate ru = this.repository.updateRef(refName);
    ru.setForceUpdate(false);/*from  w w w  .jav  a  2s.c o  m*/
    ru.setRefLogIdent(getPersonIdent());
    ru.setNewObjectId(commitId);
    if (hasOldCommit(refName)) {
        ru.setExpectedOldObjectId(getCurrentMomentHeadObjectId());
    }
    ru.setRefLogMessage(getCommitMessage(), false);
    ru.update();
}