Search in sources :

Example 16 with BatchRefUpdate

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

the class Schema_120 method allowSubmoduleSubscription.

private void allowSubmoduleSubscription(Branch.NameKey subbranch, Branch.NameKey superBranch) throws OrmException {
    try (Repository git = mgr.openRepository(subbranch.getParentKey());
        RevWalk rw = new RevWalk(git)) {
        BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
        try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, subbranch.getParentKey(), git, bru)) {
            md.getCommitBuilder().setAuthor(serverUser);
            md.getCommitBuilder().setCommitter(serverUser);
            md.setMessage("Added superproject subscription during upgrade");
            ProjectConfig pc = ProjectConfig.read(md);
            SubscribeSection s = null;
            for (SubscribeSection s1 : pc.getSubscribeSections(subbranch)) {
                if (s1.getProject().equals(superBranch.getParentKey())) {
                    s = s1;
                }
            }
            if (s == null) {
                s = new SubscribeSection(superBranch.getParentKey());
                pc.addSubscribeSection(s);
            }
            RefSpec newRefSpec = new RefSpec(subbranch.get() + ":" + superBranch.get());
            if (!s.getMatchingRefSpecs().contains(newRefSpec)) {
                // For the migration we use only exact RefSpecs, we're not trying to
                // generalize it.
                s.addMatchingRefSpec(newRefSpec);
            }
            pc.commit(md);
        }
        bru.execute(rw, NullProgressMonitor.INSTANCE);
    } catch (ConfigInvalidException | IOException e) {
        throw new OrmException(e);
    }
}
Also used : ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Repository(org.eclipse.jgit.lib.Repository) RefSpec(org.eclipse.jgit.transport.RefSpec) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OrmException(com.google.gwtorm.server.OrmException) SubscribeSection(com.google.gerrit.common.data.SubscribeSection) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 17 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);
    bru.setRefLogMessage(firstNonNull(refLogMessage, "Update NoteDb refs"), false);
    bru.setRefLogIdent(refLogIdent != null ? refLogIdent : serverIdent.get());
    or.cmds.addTo(bru);
    bru.setAllowNonFastForwards(true);
    if (!dryrun) {
        bru.execute(or.rw, NullProgressMonitor.INSTANCE);
        checkResults(bru);
    }
    return bru;
}
Also used : BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 18 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 19 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)

Aggregations

BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)19 IOException (java.io.IOException)13 RevWalk (org.eclipse.jgit.revwalk.RevWalk)12 Repository (org.eclipse.jgit.lib.Repository)11 ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)11 OrmException (com.google.gwtorm.server.OrmException)9 Map (java.util.Map)7 Account (com.google.gerrit.reviewdb.client.Account)6 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)6 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)6 ResultSet (java.sql.ResultSet)4 Statement (java.sql.Statement)4 Ref (org.eclipse.jgit.lib.Ref)4 NullProgressMonitor (org.eclipse.jgit.lib.NullProgressMonitor)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 TicketModel (com.gitblit.models.TicketModel)2 Change (com.gitblit.models.TicketModel.Change)2 TicketLink (com.gitblit.models.TicketModel.TicketLink)2 Nullable (com.google.gerrit.common.Nullable)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2