Search in sources :

Example 11 with Result

use of org.eclipse.jgit.lib.RefUpdate.Result in project gerrit by GerritCodeReview.

the class CommitMsgHookTest method setHEAD.

private void setHEAD() throws Exception {
    try (ObjectInserter oi = repository.newObjectInserter()) {
        final CommitBuilder commit = new CommitBuilder();
        commit.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
        commit.setAuthor(author);
        commit.setCommitter(committer);
        commit.setMessage("test\n");
        ObjectId commitId = oi.insert(commit);
        final RefUpdate ref = repository.updateRef(Constants.HEAD);
        ref.setNewObjectId(commitId);
        Result result = ref.forceUpdate();
        assert_().withFailureMessage(Constants.HEAD + " did not change: " + ref.getResult()).that(result).isAnyOf(Result.FAST_FORWARD, Result.FORCED, Result.NEW, Result.NO_CHANGE);
    }
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 12 with Result

use of org.eclipse.jgit.lib.RefUpdate.Result in project gerrit by GerritCodeReview.

the class Schema_146 method rewriteUserBranch.

private void rewriteUserBranch(Repository repo, RevWalk rw, ObjectInserter oi, ObjectId emptyTree, Ref ref, Account account) throws IOException {
    ObjectId current = createInitialEmptyCommit(oi, emptyTree, account.getRegisteredOn());
    rw.reset();
    rw.sort(RevSort.TOPO);
    rw.sort(RevSort.REVERSE, true);
    rw.markStart(rw.parseCommit(ref.getObjectId()));
    RevCommit c;
    while ((c = rw.next()) != null) {
        if (isInitialEmptyCommit(emptyTree, c)) {
            return;
        }
        CommitBuilder cb = new CommitBuilder();
        cb.setParentId(current);
        cb.setTreeId(c.getTree());
        cb.setAuthor(c.getAuthorIdent());
        cb.setCommitter(c.getCommitterIdent());
        cb.setMessage(c.getFullMessage());
        cb.setEncoding(c.getEncoding());
        current = oi.insert(cb);
    }
    oi.flush();
    RefUpdate ru = repo.updateRef(ref.getName());
    ru.setExpectedOldObjectId(ref.getObjectId());
    ru.setNewObjectId(current);
    ru.setForceUpdate(true);
    ru.setRefLogIdent(serverIdent);
    ru.setRefLogMessage(getClass().getSimpleName(), true);
    Result result = ru.update();
    if (result != Result.FORCED) {
        throw new IOException(String.format("Failed to update ref %s: %s", ref.getName(), result.name()));
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 13 with Result

use of org.eclipse.jgit.lib.RefUpdate.Result in project gitblit by gitblit.

the class JGitUtils method commitIndex.

public static boolean commitIndex(Repository db, String branch, DirCache index, ObjectId parentId, boolean forceCommit, String author, String authorEmail, String message) throws IOException, ConcurrentRefUpdateException {
    boolean success = false;
    ObjectId headId = db.resolve(branch + "^{commit}");
    ObjectId baseId = parentId;
    if (baseId == null || headId == null) {
        return false;
    }
    ObjectInserter odi = db.newObjectInserter();
    try {
        // Create the in-memory index of the new/updated ticket
        ObjectId indexTreeId = index.writeTree(odi);
        // Create a commit object
        PersonIdent ident = new PersonIdent(author, authorEmail);
        if (forceCommit == false) {
            ThreeWayMerger merger = MergeStrategy.RECURSIVE.newMerger(db, true);
            merger.setObjectInserter(odi);
            merger.setBase(baseId);
            boolean mergeSuccess = merger.merge(indexTreeId, headId);
            if (mergeSuccess) {
                indexTreeId = merger.getResultTreeId();
            } else {
                //Manual merge required
                return false;
            }
        }
        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(ident);
        commit.setCommitter(ident);
        commit.setEncoding(com.gitblit.Constants.ENCODING);
        commit.setMessage(message);
        commit.setParentId(headId);
        commit.setTreeId(indexTreeId);
        // Insert the commit into the repository
        ObjectId commitId = odi.insert(commit);
        odi.flush();
        RevWalk revWalk = new RevWalk(db);
        try {
            RevCommit revCommit = revWalk.parseCommit(commitId);
            RefUpdate ru = db.updateRef(branch);
            ru.setForceUpdate(forceCommit);
            ru.setNewObjectId(commitId);
            ru.setExpectedOldObjectId(headId);
            ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
            Result rc = ru.update();
            switch(rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    success = true;
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, branch, commitId.toString(), rc));
            }
        } finally {
            revWalk.close();
        }
    } finally {
        odi.close();
    }
    return success;
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) PersonIdent(org.eclipse.jgit.lib.PersonIdent) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) FetchResult(org.eclipse.jgit.transport.FetchResult) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 14 with Result

use of org.eclipse.jgit.lib.RefUpdate.Result in project gitblit by gitblit.

the class JGitUtils method merge.

/**
	 * Tries to merge a commit into a branch.  If there are conflicts, the merge
	 * will fail.
	 *
	 * @param repository
	 * @param src
	 * @param toBranch
	 * @param mergeType
	 *            Defines the integration strategy to use for merging.
	 * @param committer
	 * @param message
	 * @return the merge result
	 */
public static MergeResult merge(Repository repository, String src, String toBranch, MergeType mergeType, PersonIdent committer, String message) {
    if (!toBranch.startsWith(Constants.R_REFS)) {
        // branch ref doesn't start with ref, assume this is a branch head
        toBranch = Constants.R_HEADS + toBranch;
    }
    IntegrationStrategy strategy = IntegrationStrategyFactory.create(mergeType, repository, src, toBranch);
    MergeResult mergeResult = strategy.merge(committer, message);
    if (mergeResult.status != MergeStatus.MERGED) {
        return mergeResult;
    }
    try {
        // Update the integration branch ref
        RefUpdate mergeRefUpdate = repository.updateRef(toBranch);
        mergeRefUpdate.setNewObjectId(strategy.getMergeCommit());
        mergeRefUpdate.setRefLogMessage(strategy.getRefLogMessage(), false);
        mergeRefUpdate.setExpectedOldObjectId(strategy.branchTip);
        RefUpdate.Result rc = mergeRefUpdate.update();
        switch(rc) {
            case FAST_FORWARD:
                // successful, clean merge
                break;
            default:
                mergeResult = new MergeResult(MergeStatus.FAILED, null);
                throw new GitBlitException(MessageFormat.format("Unexpected result \"{0}\" when {1} in {2}", rc.name(), strategy.getOperationMessage(), repository.getDirectory()));
        }
    } catch (IOException e) {
        LOGGER.error("Failed to merge", e);
    }
    return mergeResult;
}
Also used : Result(org.eclipse.jgit.lib.RefUpdate.Result) GitBlitException(com.gitblit.GitBlitException) IOException(java.io.IOException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 15 with Result

use of org.eclipse.jgit.lib.RefUpdate.Result in project gitblit by gitblit.

the class RefLogUtils method getRefLogBranch.

/**
	 * Returns a RefModel for the reflog branch in the repository. If the
	 * branch can not be found, null is returned.
	 *
	 * @param repository
	 * @return a refmodel for the reflog branch or null
	 */
public static RefModel getRefLogBranch(Repository repository) {
    List<RefModel> refs = JGitUtils.getRefs(repository, "refs/");
    Ref oldRef = null;
    for (RefModel ref : refs) {
        if (ref.reference.getName().equals(GB_REFLOG)) {
            return ref;
        } else if (ref.reference.getName().equals("refs/gitblit/reflog")) {
            oldRef = ref.reference;
        } else if (ref.reference.getName().equals("refs/gitblit/pushes")) {
            oldRef = ref.reference;
        }
    }
    if (oldRef != null) {
        // rename old ref to refs/meta/gitblit/reflog
        RefRename cmd;
        try {
            cmd = repository.renameRef(oldRef.getName(), GB_REFLOG);
            cmd.setRefLogIdent(new PersonIdent("Gitblit", "gitblit@localhost"));
            cmd.setRefLogMessage("renamed " + oldRef.getName() + " => " + GB_REFLOG);
            Result res = cmd.rename();
            switch(res) {
                case RENAMED:
                    LOGGER.info(repository.getDirectory() + " " + cmd.getRefLogMessage());
                    return getRefLogBranch(repository);
                default:
                    LOGGER.error("failed to rename " + oldRef.getName() + " => " + GB_REFLOG + " (" + res.name() + ")");
            }
        } catch (IOException e) {
            LOGGER.error("failed to rename reflog", e);
        }
    }
    return null;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) RefModel(com.gitblit.models.RefModel) PersonIdent(org.eclipse.jgit.lib.PersonIdent) IOException(java.io.IOException) RefRename(org.eclipse.jgit.lib.RefRename) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Aggregations

Result (org.eclipse.jgit.lib.RefUpdate.Result)19 RefUpdate (org.eclipse.jgit.lib.RefUpdate)17 IOException (java.io.IOException)11 ObjectId (org.eclipse.jgit.lib.ObjectId)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)7 PersonIdent (org.eclipse.jgit.lib.PersonIdent)7 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)6 Ref (org.eclipse.jgit.lib.Ref)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 FetchResult (org.eclipse.jgit.transport.FetchResult)4 RefModel (com.gitblit.models.RefModel)3 DirCache (org.eclipse.jgit.dircache.DirCache)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 GitException (org.eclipse.che.api.git.exception.GitException)2 MergeResult (org.eclipse.che.api.git.shared.MergeResult)2 RebaseResult (org.eclipse.jgit.api.RebaseResult)2 ConcurrentRefUpdateException (org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)2 RefRename (org.eclipse.jgit.lib.RefRename)2