Search in sources :

Example 1 with LockFailureException

use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.

the class StarredChangesUtil method deleteRef.

private void deleteRef(Repository repo, String refName, ObjectId oldObjectId) throws IOException {
    if (ObjectId.zeroId().equals(oldObjectId)) {
        // ref doesn't exist
        return;
    }
    try (TraceTimer traceTimer = TraceContext.newTimer("Delete star labels", Metadata.builder().noteDbRefName(refName).build())) {
        RefUpdate u = repo.updateRef(refName);
        u.setForceUpdate(true);
        u.setExpectedOldObjectId(oldObjectId);
        u.setRefLogIdent(serverIdent.get());
        u.setRefLogMessage("Unstar change", true);
        RefUpdate.Result result = u.delete();
        switch(result) {
            case FORCED:
                gitRefUpdated.fire(allUsers, u, null);
                return;
            case LOCK_FAILURE:
                throw new LockFailureException(String.format("Delete star ref %s failed", refName), u);
            case NEW:
            case NO_CHANGE:
            case FAST_FORWARD:
            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("Delete star ref %s failed: %s", refName, result.name()));
        }
    }
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) 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 2 with LockFailureException

use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.

the class StarredChangesUtil method unstarAllForChangeDeletion.

/**
 * Unstar the given change for all users.
 *
 * <p>Intended for use only when we're about to delete a change. For that reason, the change is
 * not reindexed.
 *
 * @param changeId change ID.
 * @throws IOException if an error occurred.
 */
public void unstarAllForChangeDeletion(Change.Id changeId) throws IOException {
    try (Repository repo = repoManager.openRepository(allUsers);
        RevWalk rw = new RevWalk(repo)) {
        BatchRefUpdate batchUpdate = repo.getRefDatabase().newBatchUpdate();
        batchUpdate.setAllowNonFastForwards(true);
        batchUpdate.setRefLogIdent(serverIdent.get());
        batchUpdate.setRefLogMessage("Unstar change " + changeId.get(), true);
        for (Account.Id accountId : byChangeFromIndex(changeId).keySet()) {
            String refName = RefNames.refsStarredChanges(changeId, accountId);
            Ref ref = repo.getRefDatabase().exactRef(refName);
            if (ref != null) {
                batchUpdate.addCommand(new ReceiveCommand(ref.getObjectId(), ObjectId.zeroId(), refName));
            }
        }
        batchUpdate.execute(rw, NullProgressMonitor.INSTANCE);
        for (ReceiveCommand command : batchUpdate.getCommands()) {
            if (command.getResult() != ReceiveCommand.Result.OK) {
                String message = String.format("Unstar change %d failed, ref %s could not be deleted: %s", changeId.get(), command.getRefName(), command.getResult());
                if (command.getResult() == ReceiveCommand.Result.LOCK_FAILURE) {
                    throw new LockFailureException(message, batchUpdate);
                }
                throw new GitUpdateFailureException(message, batchUpdate);
            }
        }
    }
}
Also used : Account(com.google.gerrit.entities.Account) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) GitUpdateFailureException(com.google.gerrit.git.GitUpdateFailureException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) LockFailureException(com.google.gerrit.git.LockFailureException)

Example 3 with LockFailureException

use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.

the class BaseCommitUtil method updateRef.

private static RevCommit updateRef(Repository repo, RevWalk rw, String refName, ObjectId autoMergeId, RevCommit merge) throws IOException {
    RefUpdate ru = repo.updateRef(refName);
    ru.setNewObjectId(autoMergeId);
    ru.disableRefLog();
    switch(ru.update()) {
        case FAST_FORWARD:
        case FORCED:
        case NEW:
        case NO_CHANGE:
            return rw.parseCommit(autoMergeId);
        case LOCK_FAILURE:
            throw new LockFailureException(String.format("Failed to create auto-merge of %s", merge.name()), ru);
        case IO_FAILURE:
        case NOT_ATTEMPTED:
        case REJECTED:
        case REJECTED_CURRENT_BRANCH:
        case REJECTED_MISSING_OBJECT:
        case REJECTED_OTHER_REASON:
        case RENAMED:
        default:
            throw new IOException(String.format("Failed to create auto-merge of %s: Cannot write %s (%s)", merge.name(), refName, ru.getResult()));
    }
}
Also used : IOException(java.io.IOException) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 4 with LockFailureException

use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.

the class ProjectCreator method createEmptyCommits.

private void createEmptyCommits(Repository repo, Project.NameKey project, List<String> refs) throws IOException {
    try (ObjectInserter oi = repo.newObjectInserter()) {
        CommitBuilder cb = new CommitBuilder();
        cb.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
        cb.setAuthor(metaDataUpdateFactory.getUserPersonIdent());
        cb.setCommitter(serverIdent.get());
        cb.setMessage("Initial empty repository\n");
        ObjectId id = oi.insert(cb);
        oi.flush();
        for (String ref : refs) {
            RefUpdate ru = repo.updateRef(ref);
            ru.setNewObjectId(id);
            Result result = ru.update();
            switch(result) {
                case NEW:
                    referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE, identifiedUser.get().state());
                    break;
                case LOCK_FAILURE:
                    throw new LockFailureException(String.format("Failed to create ref \"%s\"", ref), ru);
                case FAST_FORWARD:
                case FORCED:
                case IO_FAILURE:
                case NOT_ATTEMPTED:
                case NO_CHANGE:
                case REJECTED:
                case REJECTED_CURRENT_BRANCH:
                case RENAMED:
                case REJECTED_MISSING_OBJECT:
                case REJECTED_OTHER_REASON:
                default:
                    {
                        throw new IOException(String.format("Failed to create ref \"%s\": %s", ref, result.name()));
                    }
            }
        }
    } catch (IOException e) {
        logger.atSevere().withCause(e).log("Cannot create empty commit for %s", project.get());
        throw e;
    }
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Result(org.eclipse.jgit.lib.RefUpdate.Result)

Example 5 with LockFailureException

use of com.google.gerrit.git.LockFailureException in project gerrit by GerritCodeReview.

the class SetHead method apply.

@Override
public Response<String> apply(ProjectResource rsrc, HeadInput input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException, PermissionBackendException {
    if (input == null || Strings.isNullOrEmpty(input.ref)) {
        throw new BadRequestException("ref required");
    }
    String ref = RefNames.fullName(input.ref);
    permissionBackend.user(rsrc.getUser()).project(rsrc.getNameKey()).ref(ref).check(RefPermission.SET_HEAD);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
        if (!cur.containsKey(ref)) {
            throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
        }
        final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
        final String newHead = ref;
        if (!oldHead.equals(newHead)) {
            final RefUpdate u = repo.updateRef(Constants.HEAD, true);
            u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
            RefUpdate.Result res = u.link(newHead);
            switch(res) {
                case NO_CHANGE:
                case RENAMED:
                case FORCED:
                case NEW:
                    break;
                case LOCK_FAILURE:
                    throw new LockFailureException("Setting HEAD failed", u);
                case FAST_FORWARD:
                case IO_FAILURE:
                case NOT_ATTEMPTED:
                case REJECTED:
                case REJECTED_CURRENT_BRANCH:
                case REJECTED_MISSING_OBJECT:
                case REJECTED_OTHER_REASON:
                default:
                    throw new IOException("Setting HEAD failed with " + res);
            }
            fire(rsrc.getNameKey(), oldHead, newHead);
        }
        return Response.ok(ref);
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(rsrc.getName(), e);
    }
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

LockFailureException (com.google.gerrit.git.LockFailureException)13 RefUpdate (org.eclipse.jgit.lib.RefUpdate)10 IOException (java.io.IOException)7 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5 Repository (org.eclipse.jgit.lib.Repository)5 StorageException (com.google.gerrit.exceptions.StorageException)4 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)3 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)3 BranchNameKey (com.google.gerrit.entities.BranchNameKey)2 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)2 Ref (org.eclipse.jgit.lib.Ref)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 Attempt (com.github.rholder.retry.Attempt)1 RetryListener (com.github.rholder.retry.RetryListener)1 AutoValue (com.google.auto.value.AutoValue)1 Joiner (com.google.common.base.Joiner)1 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1