Search in sources :

Example 16 with Result

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

the class BranchTicketService method getTicketsBranch.

/**
	 * Returns a RefModel for the refs/meta/gitblit/tickets branch in the repository.
	 * If the branch can not be found, null is returned.
	 *
	 * @return a refmodel for the gitblit tickets branch or null
	 */
private RefModel getTicketsBranch(Repository db) {
    List<RefModel> refs = JGitUtils.getRefs(db, "refs/");
    Ref oldRef = null;
    for (RefModel ref : refs) {
        if (ref.reference.getName().equals(BRANCH)) {
            return ref;
        } else if (ref.reference.getName().equals("refs/gitblit/tickets")) {
            oldRef = ref.reference;
        }
    }
    if (oldRef != null) {
        // rename old ref to refs/meta/gitblit/tickets
        RefRename cmd;
        try {
            cmd = db.renameRef(oldRef.getName(), BRANCH);
            cmd.setRefLogIdent(new PersonIdent("Gitblit", "gitblit@localhost"));
            cmd.setRefLogMessage("renamed " + oldRef.getName() + " => " + BRANCH);
            Result res = cmd.rename();
            switch(res) {
                case RENAMED:
                    log.info(db.getDirectory() + " " + cmd.getRefLogMessage());
                    return getTicketsBranch(db);
                default:
                    log.error("failed to rename " + oldRef.getName() + " => " + BRANCH + " (" + res.name() + ")");
            }
        } catch (IOException e) {
            log.error("failed to rename tickets branch", 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)

Example 17 with Result

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

the class ProjectConfigTest method update.

private void update(RevCommit rev) throws Exception {
    RefUpdate u = db.updateRef(RefNames.REFS_CONFIG);
    u.disableRefLog();
    u.setNewObjectId(rev);
    Result result = u.forceUpdate();
    assert_().withFailureMessage("Cannot update ref for test: " + result).that(result).isAnyOf(Result.FAST_FORWARD, Result.FORCED, Result.NEW, Result.NO_CHANGE);
}
Also used : RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 18 with Result

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

the class AccountsUpdate method createUserBranch.

public static void createUserBranch(Repository repo, ObjectInserter oi, PersonIdent committerIdent, PersonIdent authorIdent, Account account) throws IOException {
    ObjectId id = createInitialEmptyCommit(oi, committerIdent, authorIdent, account.getRegisteredOn());
    String refName = RefNames.refsUsers(account.getId());
    RefUpdate ru = repo.updateRef(refName);
    ru.setExpectedOldObjectId(ObjectId.zeroId());
    ru.setNewObjectId(id);
    ru.setForceUpdate(true);
    ru.setRefLogIdent(committerIdent);
    ru.setRefLogMessage("Create Account", true);
    Result result = ru.update();
    if (result != Result.NEW) {
        throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name()));
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 19 with Result

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

the class NotesBranchUtil method updateRef.

private void updateRef(String notesBranch) throws IOException, MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, ConcurrentRefUpdateException {
    if (baseCommit != null && oursCommit.getTree().equals(baseCommit.getTree())) {
        // Avoid saving this commit as it has no new information.
        return;
    }
    int remainingLockFailureCalls = MAX_LOCK_FAILURE_CALLS;
    RefUpdate refUpdate = createRefUpdate(notesBranch, oursCommit, baseCommit);
    for (; ; ) {
        Result result = refUpdate.update();
        if (result == Result.LOCK_FAILURE) {
            if (--remainingLockFailureCalls > 0) {
                try {
                    Thread.sleep(SLEEP_ON_LOCK_FAILURE_MS);
                } catch (InterruptedException e) {
                // ignore
                }
            } else {
                throw new ConcurrentRefUpdateException("Failed to lock the ref: " + notesBranch, refUpdate.getRef(), result);
            }
        } else if (result == Result.REJECTED) {
            RevCommit theirsCommit = revWalk.parseCommit(refUpdate.getOldObjectId());
            NoteMap theirs = NoteMap.read(revWalk.getObjectReader(), theirsCommit);
            NoteMapMerger merger = new NoteMapMerger(db, getNoteMerger(), MergeStrategy.RESOLVE);
            NoteMap merged = merger.merge(base, ours, theirs);
            RevCommit mergeCommit = createCommit(merged, gerritIdent, "Merged note commits\n", theirsCommit, oursCommit);
            refUpdate = createRefUpdate(notesBranch, mergeCommit, theirsCommit);
            remainingLockFailureCalls = MAX_LOCK_FAILURE_CALLS;
        } else if (result == Result.IO_FAILURE) {
            throw new IOException("Couldn't update " + notesBranch + ". " + result.name());
        } else {
            gitRefUpdated.fire(project, refUpdate, null);
            break;
        }
    }
}
Also used : NoteMap(org.eclipse.jgit.notes.NoteMap) IOException(java.io.IOException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) NoteMapMerger(org.eclipse.jgit.notes.NoteMapMerger) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

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