use of org.eclipse.jgit.lib.ObjectReader in project gerrit by GerritCodeReview.
the class ReceiveCommits method autoCloseChanges.
private void autoCloseChanges(final ReceiveCommand cmd) {
logDebug("Starting auto-closing of changes");
String refName = cmd.getRefName();
checkState(!MagicBranch.isMagicBranch(refName), "shouldn't be auto-closing changes on magic branch %s", refName);
// insertChangesAndPatchSets.
try (BatchUpdate bu = batchUpdateFactory.create(db, projectControl.getProject().getNameKey(), user, TimeUtil.nowTs());
ObjectInserter ins = repo.newObjectInserter();
ObjectReader reader = ins.newReader();
RevWalk rw = new RevWalk(reader)) {
bu.setRepository(repo, rw, ins).updateChangesInParallel();
bu.setRequestId(receiveId);
// TODO(dborowitz): Teach BatchUpdate to ignore missing changes.
RevCommit newTip = rw.parseCommit(cmd.getNewId());
Branch.NameKey branch = new Branch.NameKey(project.getNameKey(), refName);
rw.reset();
rw.markStart(newTip);
if (!ObjectId.zeroId().equals(cmd.getOldId())) {
rw.markUninteresting(rw.parseCommit(cmd.getOldId()));
}
ListMultimap<ObjectId, Ref> byCommit = changeRefsById();
Map<Change.Key, ChangeNotes> byKey = null;
List<ReplaceRequest> replaceAndClose = new ArrayList<>();
int existingPatchSets = 0;
int newPatchSets = 0;
COMMIT: for (RevCommit c; (c = rw.next()) != null; ) {
rw.parseBody(c);
for (Ref ref : byCommit.get(c.copy())) {
existingPatchSets++;
PatchSet.Id psId = PatchSet.Id.fromRef(ref.getName());
bu.addOp(psId.getParentKey(), mergedByPushOpFactory.create(requestScopePropagator, psId, refName));
continue COMMIT;
}
for (String changeId : c.getFooterLines(CHANGE_ID)) {
if (byKey == null) {
byKey = openChangesByBranch(branch);
}
ChangeNotes onto = byKey.get(new Change.Key(changeId.trim()));
if (onto != null) {
newPatchSets++;
// Hold onto this until we're done with the walk, as the call to
// req.validate below calls isMergedInto which resets the walk.
ReplaceRequest req = new ReplaceRequest(onto.getChangeId(), c, cmd, false);
req.notes = onto;
replaceAndClose.add(req);
continue COMMIT;
}
}
}
for (final ReplaceRequest req : replaceAndClose) {
Change.Id id = req.notes.getChangeId();
if (!req.validate(true)) {
logDebug("Not closing {} because validation failed", id);
continue;
}
req.addOps(bu, null);
bu.addOp(id, mergedByPushOpFactory.create(requestScopePropagator, req.psId, refName).setPatchSetProvider(new Provider<PatchSet>() {
@Override
public PatchSet get() {
return req.replaceOp.getPatchSet();
}
}));
bu.addOp(id, new ChangeProgressOp(closeProgress));
}
logDebug("Auto-closing {} changes with existing patch sets and {} with new patch sets", existingPatchSets, newPatchSets);
bu.execute();
} catch (RestApiException e) {
logError("Can't insert patchset", e);
} catch (IOException | OrmException | UpdateException | PermissionBackendException e) {
logError("Can't scan for changes to close", e);
}
}
use of org.eclipse.jgit.lib.ObjectReader in project repairnator by Spirals-Team.
the class TestCheckoutBuggyBuildSourceCode method testCheckoutPreviousBuildSourceCodeNoPR2.
@Test
public void testCheckoutPreviousBuildSourceCodeNoPR2() throws IOException, GitAPIException, RepairnatorConfigException {
// alibaba/fastjson
int buildId = 222020421;
int previousBuildId = 222016611;
ScannedBuildStatus status = ScannedBuildStatus.PASSING_AND_PASSING_WITH_TEST_CHANGES;
Build build = BuildHelper.getBuildFromId(buildId, null);
assertThat(build, notNullValue());
assertThat(buildId, is(build.getId()));
assertThat(build.isPullRequest(), is(false));
Build previousBuild = BuildHelper.getBuildFromId(previousBuildId, null);
assertThat(previousBuild, notNullValue());
assertThat(previousBuild.getId(), is(previousBuildId));
assertThat(previousBuild.isPullRequest(), is(false));
Path tmpDirPath = Files.createTempDirectory("test_checkoutprevious");
File tmpDir = tmpDirPath.toFile();
tmpDir.deleteOnExit();
BuildToBeInspected toBeInspected = new BuildToBeInspected(build, previousBuild, status, "");
ProjectInspector inspector = mock(ProjectInspector.class);
when(inspector.getWorkspace()).thenReturn(tmpDir.getAbsolutePath());
when(inspector.getRepoLocalPath()).thenReturn(tmpDir.getAbsolutePath() + "/repo");
when(inspector.getRepoToPushLocalPath()).thenReturn(tmpDir.getAbsolutePath() + "/repotopush");
when(inspector.getBuildToBeInspected()).thenReturn(toBeInspected);
when(inspector.getPatchedBuild()).thenReturn(build);
when(inspector.getBuggyBuild()).thenReturn(previousBuild);
when(inspector.getGitHelper()).thenReturn(new GitHelper());
JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath() + "/repo");
when(inspector.getJobStatus()).thenReturn(jobStatus);
CloneRepository cloneStep = new CloneRepository(inspector);
CheckoutBuggyBuildSourceCode checkoutBuild = new CheckoutBuggyBuildSourceCode(inspector);
cloneStep.setNextStep(checkoutBuild);
cloneStep.execute();
assertThat(checkoutBuild.getPipelineState(), is(PipelineState.PREVIOUSBUILDCODECHECKEDOUT));
assertThat(jobStatus.getPipelineState(), is(PipelineState.PREVIOUSBUILDCODECHECKEDOUT));
assertThat(checkoutBuild.isShouldStop(), is(false));
Git gitDir = Git.open(new File(tmpDir, "repo"));
Iterable<RevCommit> logs = gitDir.log().call();
Iterator<RevCommit> iterator = logs.iterator();
boolean foundRightCommitAfterRepairCommits = false;
boolean foundUndoSourceCodeCommit = false;
boolean stopSearch = false;
while (iterator.hasNext() && !stopSearch) {
RevCommit revCommit = iterator.next();
if (revCommit.getShortMessage().equals("Undo changes on source code")) {
foundUndoSourceCodeCommit = true;
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
ObjectReader reader = gitDir.getRepository().newObjectReader();
RevCommit prevCommit = iterator.next();
oldTreeIter.reset(reader, prevCommit.getTree());
newTreeIter.reset(reader, revCommit.getTree());
List<DiffEntry> diff = gitDir.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();
for (DiffEntry entry : diff) {
assertThat(entry.getOldPath(), startsWith("src/main/java/"));
}
revCommit = prevCommit;
}
if (revCommit.getName().equals(build.getCommit().getSha())) {
foundRightCommitAfterRepairCommits = true;
}
if (!revCommit.getShortMessage().contains("repairnator")) {
stopSearch = true;
}
if (foundRightCommitAfterRepairCommits && foundUndoSourceCodeCommit) {
stopSearch = true;
}
}
assertThat(foundRightCommitAfterRepairCommits, is(true));
assertThat(foundUndoSourceCodeCommit, is(true));
}
use of org.eclipse.jgit.lib.ObjectReader in project repairnator by Spirals-Team.
the class TestCheckoutBuggyBuildSourceCode method testCheckoutPreviousBuildSourceCodeNoPR.
@Test
public void testCheckoutPreviousBuildSourceCodeNoPR() throws IOException, GitAPIException, RepairnatorConfigException {
// INRIA/spoon
int buildId = 221992429;
int previousBuildId = 218213030;
ScannedBuildStatus status = ScannedBuildStatus.PASSING_AND_PASSING_WITH_TEST_CHANGES;
Build build = BuildHelper.getBuildFromId(buildId, null);
assertThat(build, notNullValue());
assertThat(buildId, is(build.getId()));
assertThat(build.isPullRequest(), is(false));
Build previousBuild = BuildHelper.getBuildFromId(previousBuildId, null);
assertThat(previousBuild, notNullValue());
assertThat(previousBuild.getId(), is(previousBuildId));
assertThat(previousBuild.isPullRequest(), is(false));
Path tmpDirPath = Files.createTempDirectory("test_checkoutprevious");
File tmpDir = tmpDirPath.toFile();
tmpDir.deleteOnExit();
BuildToBeInspected toBeInspected = new BuildToBeInspected(previousBuild, build, status, "");
ProjectInspector inspector = mock(ProjectInspector.class);
when(inspector.getWorkspace()).thenReturn(tmpDir.getAbsolutePath());
when(inspector.getRepoLocalPath()).thenReturn(tmpDir.getAbsolutePath() + "/repo");
when(inspector.getRepoToPushLocalPath()).thenReturn(tmpDir.getAbsolutePath() + "/repotopush");
when(inspector.getBuildToBeInspected()).thenReturn(toBeInspected);
when(inspector.getPatchedBuild()).thenReturn(build);
when(inspector.getBuggyBuild()).thenReturn(previousBuild);
when(inspector.getGitHelper()).thenReturn(new GitHelper());
JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath() + "/repo");
when(inspector.getJobStatus()).thenReturn(jobStatus);
CloneRepository cloneStep = new CloneRepository(inspector);
CheckoutBuggyBuildSourceCode checkoutBuild = new CheckoutBuggyBuildSourceCode(inspector);
cloneStep.setNextStep(checkoutBuild);
cloneStep.execute();
assertThat(checkoutBuild.getPipelineState(), is(PipelineState.PREVIOUSBUILDCODECHECKEDOUT));
assertThat(jobStatus.getPipelineState(), is(PipelineState.PREVIOUSBUILDCODECHECKEDOUT));
assertThat(checkoutBuild.isShouldStop(), is(false));
Git gitDir = Git.open(new File(tmpDir, "repo"));
Iterable<RevCommit> logs = gitDir.log().call();
Iterator<RevCommit> iterator = logs.iterator();
boolean foundRightCommitAfterRepairCommits = false;
boolean foundUndoSourceCodeCommit = false;
boolean stopSearch = false;
while (iterator.hasNext() && !stopSearch) {
RevCommit revCommit = iterator.next();
if (revCommit.getShortMessage().equals("Undo changes on source code")) {
foundUndoSourceCodeCommit = true;
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
ObjectReader reader = gitDir.getRepository().newObjectReader();
RevCommit prevCommit = iterator.next();
oldTreeIter.reset(reader, prevCommit.getTree());
newTreeIter.reset(reader, revCommit.getTree());
List<DiffEntry> diff = gitDir.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();
for (DiffEntry entry : diff) {
assertThat(entry.getOldPath(), startsWith("src/main/java/"));
}
revCommit = prevCommit;
}
if (revCommit.getName().equals(build.getCommit().getSha())) {
foundRightCommitAfterRepairCommits = true;
}
if (!revCommit.getShortMessage().contains("repairnator")) {
stopSearch = true;
}
if (foundRightCommitAfterRepairCommits && foundUndoSourceCodeCommit) {
stopSearch = true;
}
}
assertThat(foundRightCommitAfterRepairCommits, is(true));
assertThat(foundUndoSourceCodeCommit, is(true));
}
use of org.eclipse.jgit.lib.ObjectReader in project gitiles by GerritCodeReview.
the class RevisionServlet method doGetText.
@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
GitilesView view = ViewFilter.getView(req);
Repository repo = ServletUtils.getRepository(req);
try (ObjectReader reader = repo.newObjectReader()) {
ObjectLoader loader = reader.open(view.getRevision().getId());
if (loader.getType() != OBJ_COMMIT) {
res.setStatus(SC_NOT_FOUND);
} else {
PathServlet.setTypeHeader(res, loader.getType());
try (Writer writer = startRenderText(req, res);
OutputStream out = BaseEncoding.base64().encodingStream(writer)) {
loader.copyTo(out);
}
}
}
}
use of org.eclipse.jgit.lib.ObjectReader in project gitiles by GerritCodeReview.
the class PathServlet method showTree.
private void showTree(HttpServletRequest req, HttpServletResponse res, WalkResult wr) throws IOException {
GitilesView view = ViewFilter.getView(req);
Config cfg = getAccess(req).getConfig();
List<String> autodive = view.getParameters().get(AUTODIVE_PARAM);
if (autodive.size() != 1 || !NO_AUTODIVE_VALUE.equals(autodive.get(0))) {
byte[] path = Constants.encode(view.getPathPart());
ObjectReader reader = wr.getObjectReader();
CanonicalTreeParser child = getOnlyChildSubtree(reader, wr.id, path);
if (child != null) {
while (true) {
path = new byte[child.getEntryPathLength()];
System.arraycopy(child.getEntryPathBuffer(), 0, path, 0, child.getEntryPathLength());
CanonicalTreeParser next = getOnlyChildSubtree(reader, child.getEntryObjectId(), path);
if (next == null) {
break;
}
child = next;
}
res.sendRedirect(GitilesView.path().copyFrom(view).setPathPart(RawParseUtils.decode(child.getEntryPathBuffer(), 0, child.getEntryPathLength())).toUrl());
return;
}
}
// TODO(sop): Allow caching trees by SHA-1 when no S cookie is sent.
renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", !view.getPathPart().isEmpty() ? view.getPathPart() : "/", "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.TREE.toString(), "data", new TreeSoyData(wr.getObjectReader(), view, cfg, wr.root, req.getRequestURI()).setArchiveFormat(getArchiveFormat(getAccess(req))).toSoyData(wr.id, wr.tw)));
}
Aggregations