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