Search in sources :

Example 81 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class CommentsUtil method deleteAllDraftsFromAllUsers.

public void deleteAllDraftsFromAllUsers(Change.Id changeId) throws IOException {
    try (Repository repo = repoManager.openRepository(allUsers);
        RevWalk rw = new RevWalk(repo)) {
        BatchRefUpdate bru = repo.getRefDatabase().newBatchUpdate();
        for (Ref ref : getDraftRefs(repo, changeId)) {
            bru.addCommand(new ReceiveCommand(ref.getObjectId(), ObjectId.zeroId(), ref.getName()));
        }
        bru.setRefLogMessage("Delete drafts from NoteDb", false);
        bru.execute(rw, NullProgressMonitor.INSTANCE);
        for (ReceiveCommand cmd : bru.getCommands()) {
            if (cmd.getResult() != ReceiveCommand.Result.OK) {
                throw new IOException(String.format("Failed to delete draft comment ref %s at %s: %s (%s)", cmd.getRefName(), cmd.getOldId(), cmd.getResult(), cmd.getMessage()));
            }
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate)

Example 82 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class Rebase method applyImpl.

@Override
protected ChangeInfo applyImpl(BatchUpdate.Factory updateFactory, RevisionResource rsrc, RebaseInput input) throws EmailException, OrmException, UpdateException, RestApiException, IOException, NoSuchChangeException, PermissionBackendException {
    rsrc.permissions().database(dbProvider).check(ChangePermission.REBASE);
    ChangeControl control = rsrc.getControl();
    Change change = rsrc.getChange();
    try (Repository repo = repoManager.openRepository(change.getProject());
        ObjectInserter oi = repo.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = new RevWalk(reader);
        BatchUpdate bu = updateFactory.create(dbProvider.get(), change.getProject(), rsrc.getUser(), TimeUtil.nowTs())) {
        if (!change.getStatus().isOpen()) {
            throw new ResourceConflictException("change is " + ChangeUtil.status(change));
        } else if (!hasOneParent(rw, rsrc.getPatchSet())) {
            throw new ResourceConflictException("cannot rebase merge commits or commit with no ancestor");
        }
        bu.setRepository(repo, rw, oi);
        bu.addOp(change.getId(), rebaseFactory.create(control, rsrc.getPatchSet(), findBaseRev(repo, rw, rsrc, input)).setForceContentMerge(true).setFireRevisionCreated(true));
        bu.execute();
    }
    return json.create(OPTIONS).format(change.getProject(), change.getId());
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ChangeControl(com.google.gerrit.server.project.ChangeControl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Change(com.google.gerrit.reviewdb.client.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Example 83 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class Rebase method getDescription.

@Override
public UiAction.Description getDescription(RevisionResource resource) {
    PatchSet patchSet = resource.getPatchSet();
    Change change = resource.getChange();
    Branch.NameKey dest = change.getDest();
    boolean visible = change.getStatus().isOpen() && resource.isCurrent() && resource.permissions().database(dbProvider).testOrFalse(ChangePermission.REBASE);
    boolean enabled = true;
    if (visible) {
        try (Repository repo = repoManager.openRepository(dest.getParentKey());
            RevWalk rw = new RevWalk(repo)) {
            visible = hasOneParent(rw, resource.getPatchSet());
            enabled = rebaseUtil.canRebase(patchSet, dest, repo, rw);
        } catch (IOException e) {
            log.error("Failed to check if patch set can be rebased: " + resource.getPatchSet(), e);
            visible = false;
        }
    }
    return new UiAction.Description().setLabel("Rebase").setTitle("Rebase onto tip of branch or parent change").setVisible(visible).setEnabled(enabled);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Branch(com.google.gerrit.reviewdb.client.Branch) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 84 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class DeleteChangeOp method isPatchSetMerged.

private boolean isPatchSetMerged(ChangeContext ctx, PatchSet patchSet) throws IOException {
    Optional<ObjectId> destId = ctx.getRepoView().getRef(ctx.getChange().getDest().get());
    if (!destId.isPresent()) {
        return false;
    }
    RevWalk revWalk = ctx.getRevWalk();
    ObjectId objectId = ObjectId.fromString(patchSet.getRevision().get());
    return revWalk.isMergedInto(revWalk.parseCommit(objectId), revWalk.parseCommit(destId.get()));
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 85 with RevWalk

use of org.eclipse.jgit.revwalk.RevWalk in project gerrit by GerritCodeReview.

the class FileContentUtil method getContent.

/**
   * Get the content of a file at a specific commit or one of it's parent commits.
   *
   * @param project A {@code Project} that this request refers to.
   * @param revstr An {@code ObjectId} specifying the commit.
   * @param path A string specifying the filepath.
   * @param parent A 1-based parent index to get the content from instead. Null if the content
   *     should be obtained from {@code revstr} instead.
   * @return Content of the file as {@code BinaryResult}.
   * @throws ResourceNotFoundException
   * @throws IOException
   */
public BinaryResult getContent(ProjectState project, ObjectId revstr, String path, @Nullable Integer parent) throws BadRequestException, ResourceNotFoundException, IOException {
    try (Repository repo = openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        if (parent != null) {
            RevCommit revCommit = rw.parseCommit(revstr);
            if (revCommit == null) {
                throw new ResourceNotFoundException("commit not found");
            }
            if (parent > revCommit.getParentCount()) {
                throw new BadRequestException("invalid parent");
            }
            revstr = rw.parseCommit(revstr).getParent(Integer.max(0, parent - 1)).toObjectId();
        }
        return getContent(repo, project, revstr, path);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RevWalk (org.eclipse.jgit.revwalk.RevWalk)217 RevCommit (org.eclipse.jgit.revwalk.RevCommit)114 Repository (org.eclipse.jgit.lib.Repository)100 ObjectId (org.eclipse.jgit.lib.ObjectId)92 IOException (java.io.IOException)67 Ref (org.eclipse.jgit.lib.Ref)35 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)33 Test (org.junit.Test)33 Change (com.google.gerrit.reviewdb.client.Change)31 ObjectReader (org.eclipse.jgit.lib.ObjectReader)30 ArrayList (java.util.ArrayList)25 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)24 OrmException (com.google.gwtorm.server.OrmException)24 RevTree (org.eclipse.jgit.revwalk.RevTree)24 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)21 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)16 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)15 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)15 PersonIdent (org.eclipse.jgit.lib.PersonIdent)15 Project (com.google.gerrit.reviewdb.client.Project)14