Search in sources :

Example 36 with RevWalk

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

the class CreateChangeIT method createNewCommitWithoutChangeId.

private RevCommit createNewCommitWithoutChangeId() throws Exception {
    try (Repository repo = repoManager.openRepository(project);
        RevWalk walk = new RevWalk(repo)) {
        Ref ref = repo.exactRef("refs/heads/master");
        RevCommit tip = null;
        if (ref != null) {
            tip = walk.parseCommit(ref.getObjectId());
        }
        TestRepository<?> testSrcRepo = new TestRepository<>(repo);
        TestRepository<?>.BranchBuilder<?> builder = testSrcRepo.branch("refs/heads/master");
        RevCommit revCommit = tip == null ? builder.commit().message("commit 1").add("a.txt", "content").create() : builder.commit().parent(tip).message("commit 1").add("a.txt", "content").create();
        assertThat(GitUtil.getChangeId(testSrcRepo, revCommit)).isEmpty();
        return revCommit;
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) RefNames.changeMetaRef(com.google.gerrit.reviewdb.client.RefNames.changeMetaRef) Ref(org.eclipse.jgit.lib.Ref) TestRepository(org.eclipse.jgit.junit.TestRepository) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 37 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 38 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 39 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 40 with RevWalk

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

the class GetArchive method apply.

@Override
public BinaryResult apply(RevisionResource rsrc) throws BadRequestException, IOException, MethodNotAllowedException {
    if (Strings.isNullOrEmpty(format)) {
        throw new BadRequestException("format is not specified");
    }
    final ArchiveFormat f = allowedFormats.extensions.get("." + format);
    if (f == null) {
        throw new BadRequestException("unknown archive format");
    }
    if (f == ArchiveFormat.ZIP) {
        throw new MethodNotAllowedException("zip format is disabled");
    }
    boolean close = true;
    final Repository repo = repoManager.openRepository(rsrc.getControl().getProject().getNameKey());
    try {
        final RevCommit commit;
        String name;
        try (RevWalk rw = new RevWalk(repo)) {
            commit = rw.parseCommit(ObjectId.fromString(rsrc.getPatchSet().getRevision().get()));
            name = name(f, rw, commit);
        }
        BinaryResult bin = new BinaryResult() {

            @Override
            public void writeTo(OutputStream out) throws IOException {
                try {
                    new ArchiveCommand(repo).setFormat(f.name()).setTree(commit.getTree()).setOutputStream(out).call();
                } catch (GitAPIException e) {
                    throw new IOException(e);
                }
            }

            @Override
            public void close() throws IOException {
                repo.close();
            }
        };
        bin.disableGzip().setContentType(f.getMimeType()).setAttachmentName(name);
        close = false;
        return bin;
    } finally {
        if (close) {
            repo.close();
        }
    }
}
Also used : MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ArchiveCommand(org.eclipse.jgit.api.ArchiveCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult)

Aggregations

RevWalk (org.eclipse.jgit.revwalk.RevWalk)487 RevCommit (org.eclipse.jgit.revwalk.RevCommit)292 Repository (org.eclipse.jgit.lib.Repository)205 ObjectId (org.eclipse.jgit.lib.ObjectId)199 IOException (java.io.IOException)160 Ref (org.eclipse.jgit.lib.Ref)124 Test (org.junit.Test)76 ObjectReader (org.eclipse.jgit.lib.ObjectReader)69 ArrayList (java.util.ArrayList)61 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)55 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)47 File (java.io.File)39 RevTree (org.eclipse.jgit.revwalk.RevTree)39 TestRepository (org.eclipse.jgit.junit.TestRepository)35 Git (org.eclipse.jgit.api.Git)32 PersonIdent (org.eclipse.jgit.lib.PersonIdent)32 RevObject (org.eclipse.jgit.revwalk.RevObject)32 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)31 HashMap (java.util.HashMap)30 Map (java.util.Map)27