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);
}
}
}
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());
}
}
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;
}
}
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));
}
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;
}
Aggregations