Search in sources :

Example 71 with RefUpdate

use of org.eclipse.jgit.lib.RefUpdate in project egit by eclipse.

the class GitRepositoriesViewTestBase method createStableBranch.

protected static void createStableBranch(Repository myRepository) throws IOException {
    // let's create a stable branch temporarily so
    // that we push two branches to remote
    String newRefName = "refs/heads/stable";
    RefUpdate updateRef = myRepository.updateRef(newRefName);
    Ref sourceBranch = myRepository.exactRef("refs/heads/master");
    ObjectId startAt = sourceBranch.getObjectId();
    String startBranch = Repository.shortenRefName(sourceBranch.getName());
    updateRef.setNewObjectId(startAt);
    updateRef.setRefLogMessage("branch: Created from " + startBranch, // $NON-NLS-1$
    false);
    updateRef.update();
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 72 with RefUpdate

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

the class StarredChangesUtil method updateLabels.

private void updateLabels(Repository repo, String refName, ObjectId oldObjectId, Collection<String> labels) throws IOException, InvalidLabelsException {
    try (TraceTimer traceTimer = TraceContext.newTimer("Update star labels", Metadata.builder().noteDbRefName(refName).resourceCount(labels.size()).build());
        RevWalk rw = new RevWalk(repo)) {
        RefUpdate u = repo.updateRef(refName);
        u.setExpectedOldObjectId(oldObjectId);
        u.setForceUpdate(true);
        u.setNewObjectId(writeLabels(repo, labels));
        u.setRefLogIdent(serverIdent.get());
        u.setRefLogMessage("Update star labels", true);
        RefUpdate.Result result = u.update(rw);
        switch(result) {
            case NEW:
            case FORCED:
            case NO_CHANGE:
            case FAST_FORWARD:
                gitRefUpdated.fire(allUsers, u, null);
                return;
            case LOCK_FAILURE:
                throw new LockFailureException(String.format("Update star labels on ref %s failed", refName), u);
            case IO_FAILURE:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case RENAMED:
            case REJECTED_MISSING_OBJECT:
            case REJECTED_OTHER_REASON:
            default:
                throw new StorageException(String.format("Update star labels on ref %s failed: %s", refName, result.name()));
        }
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) RevWalk(org.eclipse.jgit.revwalk.RevWalk) StorageException(com.google.gerrit.exceptions.StorageException) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 73 with RefUpdate

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

the class ConsistencyChecker method fixPatchSetRef.

private void fixPatchSetRef(ProblemInfo p, PatchSet ps) {
    try {
        RefUpdate ru = repo.updateRef(ps.id().toRefName());
        ru.setForceUpdate(true);
        ru.setNewObjectId(ps.commitId());
        ru.setRefLogIdent(newRefLogIdent());
        ru.setRefLogMessage("Repair patch set ref", true);
        RefUpdate.Result result = ru.update();
        switch(result) {
            case NEW:
            case FORCED:
            case FAST_FORWARD:
            case NO_CHANGE:
                p.status = Status.FIXED;
                p.outcome = "Repaired patch set ref";
                return;
            case IO_FAILURE:
            case LOCK_FAILURE:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case RENAMED:
            case REJECTED_MISSING_OBJECT:
            case REJECTED_OTHER_REASON:
            default:
                p.status = Status.FIX_FAILED;
                p.outcome = "Failed to update patch set ref: " + result;
                return;
        }
    } catch (IOException e) {
        String msg = "Error fixing patch set ref";
        logger.atWarning().withCause(e).log("%s %s", msg, ps.id().toRefName());
        p.status = Status.FIX_FAILED;
        p.outcome = msg;
    }
}
Also used : IOException(java.io.IOException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 74 with RefUpdate

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

the class IntBlob method tryStore.

public static RefUpdate tryStore(Repository repo, RevWalk rw, Project.NameKey projectName, String refName, @Nullable ObjectId oldId, int val, GitReferenceUpdated gitRefUpdated) throws IOException {
    ObjectId newId;
    try (ObjectInserter ins = repo.newObjectInserter()) {
        logger.atFine().log("storing value %d on %s in %s (oldId: %s)", val, refName, projectName, oldId == null ? "null" : oldId.name());
        newId = ins.insert(OBJ_BLOB, Integer.toString(val).getBytes(UTF_8));
        ins.flush();
        logger.atFine().log("successfully stored %d on %s as %s in %s", val, refName, newId.name(), projectName);
    }
    RefUpdate ru = repo.updateRef(refName);
    if (oldId != null) {
        ru.setExpectedOldObjectId(oldId);
    }
    ru.disableRefLog();
    ru.setNewObjectId(newId);
    // Required for non-commitish updates.
    ru.setForceUpdate(true);
    RefUpdate.Result result = ru.update(rw);
    if (refUpdated(result)) {
        gitRefUpdated.fire(projectName, ru, null);
    }
    return ru;
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 75 with RefUpdate

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

the class RepoSequence method acquire.

/**
 * Updates the next available sequence number in NoteDb in order to have a batch of sequence
 * numbers available that can be handed out. {@link #counter} stores the next sequence number that
 * can be handed out. When {@link #limit} is reached a new batch of sequence numbers needs to be
 * retrieved by calling this method.
 *
 * <p><strong>Note:</strong> Callers are required to acquire the {@link #counterLock} before
 * calling this method.
 *
 * @param count the number of sequence numbers which should be retrieved
 */
private void acquire(int count) {
    try (Repository repo = repoManager.openRepository(projectName);
        RevWalk rw = new RevWalk(repo)) {
        logger.atFine().log("acquire %d ids on %s in %s", count, refName, projectName);
        Optional<IntBlob> blob = IntBlob.parse(repo, refName, rw);
        afterReadRef.run();
        ObjectId oldId;
        int next;
        if (!blob.isPresent()) {
            oldId = ObjectId.zeroId();
            next = seed.get();
        } else {
            oldId = blob.get().id();
            next = blob.get().value();
        }
        next = Math.max(floor, next);
        RefUpdate refUpdate = IntBlob.tryStore(repo, rw, projectName, refName, oldId, next + count, gitRefUpdated);
        RefUpdateUtil.checkResult(refUpdate);
        counter = next;
        limit = counter + count;
        acquireCount++;
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) StorageException(com.google.gerrit.exceptions.StorageException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

RefUpdate (org.eclipse.jgit.lib.RefUpdate)110 Repository (org.eclipse.jgit.lib.Repository)51 ObjectId (org.eclipse.jgit.lib.ObjectId)45 IOException (java.io.IOException)34 Test (org.junit.Test)32 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)27 TestRepository (org.eclipse.jgit.junit.TestRepository)26 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 Result (org.eclipse.jgit.lib.RefUpdate.Result)22 RevCommit (org.eclipse.jgit.revwalk.RevCommit)20 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)18 Ref (org.eclipse.jgit.lib.Ref)17 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)17 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)15 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)14 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)12 LockFailureException (com.google.gerrit.git.LockFailureException)10 PersonIdent (org.eclipse.jgit.lib.PersonIdent)9 DirCache (org.eclipse.jgit.dircache.DirCache)8