Search in sources :

Example 26 with BatchRefUpdate

use of org.eclipse.jgit.lib.BatchRefUpdate 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 27 with BatchRefUpdate

use of org.eclipse.jgit.lib.BatchRefUpdate 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)

Example 28 with BatchRefUpdate

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

the class NoteDbUpdateManagerTest method newBatchRefUpdate.

@SafeVarargs
private static BatchRefUpdate newBatchRefUpdate(Consumer<ReceiveCommand>... resultSetters) {
    try (Repository repo = new InMemoryRepository(new DfsRepositoryDescription("repo"))) {
        BatchRefUpdate bru = repo.getRefDatabase().newBatchUpdate();
        for (int i = 0; i < resultSetters.length; i++) {
            ReceiveCommand cmd = new ReceiveCommand(ObjectId.fromString(String.format("%039x1", i)), ObjectId.fromString(String.format("%039x2", i)), "refs/heads/branch" + i);
            bru.addCommand(cmd);
            resultSetters[i].accept(cmd);
        }
        return bru;
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) Repository(org.eclipse.jgit.lib.Repository) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) DfsRepositoryDescription(org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 29 with BatchRefUpdate

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

the class AccountsUpdate method commit.

private void commit(Repository allUsersRepo, List<UpdatedAccount> updatedAccounts) throws IOException {
    if (updatedAccounts.isEmpty()) {
        return;
    }
    beforeCommit.run();
    BatchRefUpdate batchRefUpdate = allUsersRepo.getRefDatabase().newBatchUpdate();
    String externalIdUpdateMessage = updatedAccounts.size() == 1 ? Iterables.getOnlyElement(updatedAccounts).message : "Batch update for " + updatedAccounts.size() + " accounts";
    for (UpdatedAccount updatedAccount : updatedAccounts) {
        // These updates are all for different refs (because batches never update the same account
        // more than once), so there can be multiple commits in the same batch, all with the same base
        // revision in their AccountConfig.
        commitAccountConfig(updatedAccount.message, allUsersRepo, batchRefUpdate, updatedAccount.accountConfig, updatedAccount.created);
        // When creating a new account we must allow empty commits so that the user branch gets
        // created with an empty commit when no account properties are set and hence no
        // 'account.config' file will be created.
        // These update the same ref, so they need to be stacked on top of one another using the same
        // ExternalIdNotes instance.
        commitExternalIdUpdates(externalIdUpdateMessage, allUsersRepo, batchRefUpdate);
    }
    RefUpdateUtil.executeChecked(batchRefUpdate, allUsersRepo);
    Set<Account.Id> accountsToSkipForReindex = getUpdatedAccountIds(batchRefUpdate);
    extIdNotesLoader.updateExternalIdCacheAndMaybeReindexAccounts(externalIdNotes, accountsToSkipForReindex);
    gitRefUpdated.fire(allUsersName, batchRefUpdate, currentUser.map(IdentifiedUser::state).orElse(null));
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 30 with BatchRefUpdate

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

the class NoteDbUpdateManager method execute.

private BatchRefUpdate execute(OpenRepo or, boolean dryrun, @Nullable PushCertificate pushCert) throws IOException {
    if (or == null || or.cmds.isEmpty()) {
        return null;
    }
    if (!dryrun) {
        or.flush();
    } else {
        // OpenRepo buffers objects separately; caller may assume that objects are available in the
        // inserter it previously passed via setChangeRepo.
        or.flushToFinalInserter();
    }
    BatchRefUpdate bru = or.repo.getRefDatabase().newBatchUpdate();
    bru.setPushCertificate(pushCert);
    if (refLogMessage != null) {
        bru.setRefLogMessage(refLogMessage, false);
    } else {
        bru.setRefLogMessage(firstNonNull(NoteDbUtil.guessRestApiHandler(), "Update NoteDb refs"), false);
    }
    bru.setRefLogIdent(refLogIdent != null ? refLogIdent : serverIdent.get());
    bru.setAtomic(true);
    or.cmds.addTo(bru);
    bru.setAllowNonFastForwards(allowNonFastForwards(or.cmds));
    for (BatchUpdateListener listener : batchUpdateListeners) {
        bru = listener.beforeUpdateRefs(bru);
    }
    if (!dryrun) {
        RefUpdateUtil.executeChecked(bru, or.rw);
    }
    return bru;
}
Also used : BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) BatchUpdateListener(com.google.gerrit.server.update.BatchUpdateListener)

Aggregations

BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)36 ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)20 IOException (java.io.IOException)16 Repository (org.eclipse.jgit.lib.Repository)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)15 OrmException (com.google.gwtorm.server.OrmException)8 Map (java.util.Map)7 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)7 Ref (org.eclipse.jgit.lib.Ref)7 Account (com.google.gerrit.reviewdb.client.Account)6 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)6 ObjectId (org.eclipse.jgit.lib.ObjectId)5 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)5 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)4 ResultSet (java.sql.ResultSet)4 Statement (java.sql.Statement)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)3 NullProgressMonitor (org.eclipse.jgit.lib.NullProgressMonitor)3 RefUpdate (org.eclipse.jgit.lib.RefUpdate)3