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