Search in sources :

Example 46 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gerrit by GerritCodeReview.

the class NoteDbUpdateManager method checkResults.

/**
   * Check results of all commands in the update batch, reducing to a single exception if there was
   * a failure.
   *
   * <p>Throws {@link LockFailureException} if at least one command failed with {@code
   * LOCK_FAILURE}, and the entire transaction was aborted, i.e. any non-{@code LOCK_FAILURE}
   * results, if there were any, failed with "transaction aborted".
   *
   * <p>In particular, if the underlying ref database does not {@link
   * org.eclipse.jgit.lib.RefDatabase#performsAtomicTransactions() perform atomic transactions},
   * then a combination of {@code LOCK_FAILURE} on one ref and {@code OK} or another result on other
   * refs will <em>not</em> throw {@code LockFailureException}.
   *
   * @param bru batch update; should already have been executed.
   * @throws LockFailureException if the transaction was aborted due to lock failure.
   * @throws IOException if any result was not {@code OK}.
   */
@VisibleForTesting
static void checkResults(BatchRefUpdate bru) throws LockFailureException, IOException {
    int lockFailure = 0;
    int aborted = 0;
    int failure = 0;
    for (ReceiveCommand cmd : bru.getCommands()) {
        if (cmd.getResult() != ReceiveCommand.Result.OK) {
            failure++;
        }
        if (cmd.getResult() == ReceiveCommand.Result.LOCK_FAILURE) {
            lockFailure++;
        } else if (cmd.getResult() == ReceiveCommand.Result.REJECTED_OTHER_REASON && JGitText.get().transactionAborted.equals(cmd.getMessage())) {
            aborted++;
        }
    }
    if (lockFailure + aborted == bru.getCommands().size()) {
        throw new LockFailureException("Update aborted with one or more lock failures: " + bru);
    } else if (failure > 0) {
        throw new IOException("Update failed: " + bru);
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) IOException(java.io.IOException) LockFailureException(com.google.gerrit.server.git.LockFailureException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 47 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gerrit by GerritCodeReview.

the class NoteDbUpdateManager method stage.

/**
   * Stage updates in the manager's internal list of commands.
   *
   * @return map of the state that would get written to the applicable repo(s) for each affected
   *     change.
   * @throws OrmException if a database layer error occurs.
   * @throws IOException if a storage layer error occurs.
   */
public Map<Change.Id, StagedResult> stage() throws OrmException, IOException {
    if (staged != null) {
        return staged;
    }
    try (Timer1.Context timer = metrics.stageUpdateLatency.start(CHANGES)) {
        staged = new HashMap<>();
        if (isEmpty()) {
            return staged;
        }
        initChangeRepo();
        if (!draftUpdates.isEmpty() || !toDelete.isEmpty()) {
            initAllUsersRepo();
        }
        checkExpectedState();
        addCommands();
        Table<Change.Id, Account.Id, ObjectId> allDraftIds = getDraftIds();
        Set<Change.Id> changeIds = new HashSet<>();
        for (ReceiveCommand cmd : changeRepo.getCommandsSnapshot()) {
            Change.Id changeId = Change.Id.fromRef(cmd.getRefName());
            if (changeId == null || !cmd.getRefName().equals(RefNames.changeMetaRef(changeId))) {
                // Not a meta ref update, likely due to a repo update along with the change meta update.
                continue;
            }
            changeIds.add(changeId);
            Optional<ObjectId> metaId = Optional.of(cmd.getNewId());
            staged.put(changeId, StagedResult.create(changeId, NoteDbChangeState.Delta.create(changeId, metaId, allDraftIds.rowMap().remove(changeId)), changeRepo, allUsersRepo));
        }
        for (Map.Entry<Change.Id, Map<Account.Id, ObjectId>> e : allDraftIds.rowMap().entrySet()) {
            // If a change remains in the table at this point, it means we are
            // updating its drafts but not the change itself.
            StagedResult r = StagedResult.create(e.getKey(), NoteDbChangeState.Delta.create(e.getKey(), Optional.empty(), e.getValue()), changeRepo, allUsersRepo);
            checkState(r.changeCommands().isEmpty(), "should not have change commands when updating only drafts: %s", r);
            staged.put(r.id(), r);
        }
        return staged;
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Account(com.google.gerrit.reviewdb.client.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) ObjectId(org.eclipse.jgit.lib.ObjectId) Timer1(com.google.gerrit.metrics.Timer1) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 48 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gerrit by GerritCodeReview.

the class ChangeRebuilderImpl method deleteChangeMetaRef.

private void deleteChangeMetaRef(Change change, ChainedReceiveCommands cmds) throws IOException {
    String refName = changeMetaRef(change.getId());
    Optional<ObjectId> old = cmds.get(refName);
    if (old.isPresent()) {
        cmds.add(new ReceiveCommand(old.get(), ObjectId.zeroId(), refName));
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 49 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gerrit by GerritCodeReview.

the class ChangeEditModifier method updateReferenceWithNameChange.

private void updateReferenceWithNameChange(Repository repository, String currentRefName, ObjectId currentObjectId, String newRefName, ObjectId targetObjectId, Timestamp timestamp) throws IOException {
    BatchRefUpdate batchRefUpdate = repository.getRefDatabase().newBatchUpdate();
    batchRefUpdate.addCommand(new ReceiveCommand(ObjectId.zeroId(), targetObjectId, newRefName));
    batchRefUpdate.addCommand(new ReceiveCommand(currentObjectId, ObjectId.zeroId(), currentRefName));
    batchRefUpdate.setRefLogMessage("rebase edit", false);
    batchRefUpdate.setRefLogIdent(getRefLogIdent(timestamp));
    try (RevWalk revWalk = new RevWalk(repository)) {
        batchRefUpdate.execute(revWalk, NullProgressMonitor.INSTANCE);
    }
    for (ReceiveCommand cmd : batchRefUpdate.getCommands()) {
        if (cmd.getResult() != ReceiveCommand.Result.OK) {
            throw new IOException("failed: " + cmd);
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 50 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gerrit by GerritCodeReview.

the class DeleteRef method deleteMultipleRefs.

private void deleteMultipleRefs(Repository r) throws OrmException, IOException, ResourceConflictException, PermissionBackendException {
    BatchRefUpdate batchUpdate = r.getRefDatabase().newBatchUpdate();
    batchUpdate.setAtomic(false);
    List<String> refs = prefix == null ? refsToDelete : refsToDelete.stream().map(ref -> ref.startsWith(prefix) ? ref : prefix + ref).collect(toList());
    for (String ref : refs) {
        batchUpdate.addCommand(createDeleteCommand(resource, r, ref));
    }
    try (RevWalk rw = new RevWalk(r)) {
        batchUpdate.execute(rw, NullProgressMonitor.INSTANCE);
    }
    StringBuilder errorMessages = new StringBuilder();
    for (ReceiveCommand command : batchUpdate.getCommands()) {
        if (command.getResult() == Result.OK) {
            postDeletion(resource, command);
        } else {
            appendAndLogErrorMessage(errorMessages, command);
        }
    }
    if (errorMessages.length() > 0) {
        throw new ResourceConflictException(errorMessages.toString());
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Aggregations

ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)55 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)14 RepositoryModel (com.gitblit.models.RepositoryModel)13 ObjectId (org.eclipse.jgit.lib.ObjectId)13 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)12 RevWalk (org.eclipse.jgit.revwalk.RevWalk)12 Repository (org.eclipse.jgit.lib.Repository)9 Date (java.util.Date)8 Ref (org.eclipse.jgit.lib.Ref)7 OrmException (com.google.gwtorm.server.OrmException)6 Map (java.util.Map)6 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)5 Change (com.google.gerrit.reviewdb.client.Change)5 PersonIdent (org.eclipse.jgit.lib.PersonIdent)5 TicketModel (com.gitblit.models.TicketModel)4 Change (com.gitblit.models.TicketModel.Change)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4 Account (com.google.gerrit.reviewdb.client.Account)4