use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.
the class JGitDiffPage method writeNames.
private void writeNames(List<DiffEntry> diff, OutputStream out) throws IOException {
PrintWriter writer = new PrintWriter(out);
for (DiffEntry de : diff) {
writer.print((de.getChangeType() == ChangeType.DELETE ? de.getOldPath() : de.getNewPath()) + (diff.size() != diff.indexOf(de) + 1 ? lineSeparator() : ""));
}
writer.flush();
}
use of org.eclipse.jgit.diff.DiffEntry in project gerrit by GerritCodeReview.
the class PatchListLoader method readPatchList.
public PatchList readPatchList(Repository repo, RevWalk rw, ObjectInserter ins) throws IOException, PatchListNotAvailableException {
ObjectReader reader = rw.getObjectReader();
checkArgument(reader.getCreatedFromInserter() == ins);
RawTextComparator cmp = comparatorFor(key.getWhitespace());
try (DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
RevCommit b = rw.parseCommit(key.getNewId());
RevObject a = aFor(key, repo, rw, ins, b);
if (a == null) {
// TODO(sop) Remove this case.
// This is an octopus merge commit which should be compared against the
// auto-merge. However since we don't support computing the auto-merge
// for octopus merge commits, we fall back to diffing against the first
// parent, even though this wasn't what was requested.
//
ComparisonType comparisonType = ComparisonType.againstParent(1);
PatchListEntry[] entries = new PatchListEntry[2];
entries[0] = newCommitMessage(cmp, reader, null, b);
entries[1] = newMergeList(cmp, reader, null, b, comparisonType);
return new PatchList(a, b, true, comparisonType, entries);
}
ComparisonType comparisonType = getComparisonType(a, b);
RevCommit aCommit = a instanceof RevCommit ? (RevCommit) a : null;
RevTree aTree = rw.parseTree(a);
RevTree bTree = b.getTree();
df.setReader(reader, repo.getConfig());
df.setDiffComparator(cmp);
df.setDetectRenames(true);
List<DiffEntry> diffEntries = df.scan(aTree, bTree);
Set<String> paths = null;
if (key.getOldId() != null && b.getParentCount() == 1) {
PatchListKey newKey = PatchListKey.againstDefaultBase(key.getNewId(), key.getWhitespace());
PatchListKey oldKey = PatchListKey.againstDefaultBase(key.getOldId(), key.getWhitespace());
paths = Stream.concat(patchListCache.get(newKey, project).getPatches().stream(), patchListCache.get(oldKey, project).getPatches().stream()).map(PatchListEntry::getNewName).collect(toSet());
}
int cnt = diffEntries.size();
List<PatchListEntry> entries = new ArrayList<>();
entries.add(newCommitMessage(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b));
boolean isMerge = b.getParentCount() > 1;
if (isMerge) {
entries.add(newMergeList(cmp, reader, comparisonType.isAgainstParentOrAutoMerge() ? null : aCommit, b, comparisonType));
}
for (int i = 0; i < cnt; i++) {
DiffEntry e = diffEntries.get(i);
if (paths == null || paths.contains(e.getNewPath()) || paths.contains(e.getOldPath())) {
FileHeader fh = toFileHeader(key, df, e);
long oldSize = getFileSize(reader, e.getOldMode(), e.getOldPath(), aTree);
long newSize = getFileSize(reader, e.getNewMode(), e.getNewPath(), bTree);
entries.add(newEntry(aTree, fh, newSize, newSize - oldSize));
}
}
return new PatchList(a, b, isMerge, comparisonType, entries.toArray(new PatchListEntry[entries.size()]));
}
}
use of org.eclipse.jgit.diff.DiffEntry in project indy by Commonjava.
the class GitManager method verifyChangesExist.
private boolean verifyChangesExist(final Collection<String> paths) throws GitSubsystemException {
return lockAnd(me -> {
try {
final DiffFormatter formatter = new DiffFormatter(System.out);
formatter.setRepository(repo);
final ObjectId oid = repo.resolve(Constants.HEAD);
if (oid == null) {
return true;
}
final RevWalk walk = new RevWalk(repo);
final RevCommit commit = walk.parseCommit(oid);
final RevTree treeWalk = walk.parseTree(commit);
final List<TreeFilter> filters = new ArrayList<>();
for (final String path : paths) {
filters.add(PathFilter.create(path));
}
filters.add(TreeFilter.ANY_DIFF);
walk.setTreeFilter(AndTreeFilter.create(filters));
final CanonicalTreeParser tree = new CanonicalTreeParser();
final ObjectReader oldReader = repo.newObjectReader();
try {
tree.reset(oldReader, treeWalk.getId());
} finally {
oldReader.release();
}
walk.dispose();
final FileTreeIterator files = new FileTreeIterator(repo);
final List<DiffEntry> entries = formatter.scan(tree, files);
return entries != null && !entries.isEmpty();
} catch (final IOException e) {
throw new GitSubsystemException("Failed to scan for actual changes among: %s. Reason: %s", e, paths, e.getMessage());
}
});
}
use of org.eclipse.jgit.diff.DiffEntry in project searchcode-server by boyter.
the class IndexGitRepoJob method updateGitRepository.
/**
* Update a git repository and return if it has changed and the differences
*/
public RepositoryChanged updateGitRepository(String repoName, String repoRemoteLocation, String repoUserName, String repoPassword, String repoLocations, String branch, boolean useCredentials) {
boolean changed = false;
List<String> changedFiles = new ArrayList<>();
List<String> deletedFiles = new ArrayList<>();
Singleton.getLogger().info("Attempting to pull latest from " + repoRemoteLocation + " for " + repoName);
Repository localRepository = null;
Git git = null;
try {
localRepository = new FileRepository(new File(repoLocations + "/" + repoName + "/.git"));
Ref head = localRepository.getRef("HEAD");
git = new Git(localRepository);
git.reset();
git.clean();
PullCommand pullCmd = git.pull();
if (useCredentials) {
pullCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoUserName, repoPassword));
}
pullCmd.call();
Ref newHEAD = localRepository.getRef("HEAD");
if (!head.toString().equals(newHEAD.toString())) {
changed = true;
// Get the differences between the the heads which we updated at
// and use these to just update the differences between them
ObjectId oldHead = localRepository.resolve(head.getObjectId().getName() + "^{tree}");
ObjectId newHead = localRepository.resolve(newHEAD.getObjectId().getName() + "^{tree}");
ObjectReader reader = localRepository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, newHead);
List<DiffEntry> entries = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
for (DiffEntry entry : entries) {
if ("DELETE".equals(entry.getChangeType().name())) {
deletedFiles.add(FilenameUtils.separatorsToUnix(entry.getOldPath()));
} else {
changedFiles.add(FilenameUtils.separatorsToUnix(entry.getNewPath()));
}
}
}
} catch (IOException | GitAPIException | InvalidPathException ex) {
changed = false;
Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " updateGitRepository for " + repoName + "\n with message: " + ex.getMessage());
} finally {
Singleton.getHelpers().closeQuietly(localRepository);
Singleton.getHelpers().closeQuietly(git);
}
return new RepositoryChanged(changed, changedFiles, deletedFiles);
}
use of org.eclipse.jgit.diff.DiffEntry 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));
}
Aggregations