List of usage examples for org.eclipse.jgit.diff RenameDetector compute
public List<DiffEntry> compute(ContentSource.Pair reader, ProgressMonitor pm) throws IOException, CancelledException
From source file:org.eclipse.che.git.impl.jgit.JGitDiffPage.java
License:Open Source License
/** * Show changes between index and working tree. * * @param formatter//from w w w . j a v a 2 s .c om * diff formatter * @return list of diff entries * @throws IOException * if any i/o errors occurs */ private List<DiffEntry> indexToWorkingTree(DiffFormatter formatter) throws IOException { DirCache dirCache = null; ObjectReader reader = repository.newObjectReader(); List<DiffEntry> diff; try { dirCache = repository.lockDirCache(); DirCacheIterator iterA = new DirCacheIterator(dirCache); FileTreeIterator iterB = new FileTreeIterator(repository); // Seems bug in DiffFormatter when work with working. Disable detect // renames by formatter and do it later. formatter.setDetectRenames(false); diff = formatter.scan(iterA, iterB); if (!request.isNoRenames()) { // Detect renames. RenameDetector renameDetector = createRenameDetector(); ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB)); renameDetector.addAll(diff); diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE); } } finally { reader.close(); if (dirCache != null) { dirCache.unlock(); } } return diff; }
From source file:org.eclipse.che.git.impl.jgit.JGitDiffPage.java
License:Open Source License
/** * Show changes between specified revision and working tree. * * @param commitId//from w w w. j a v a 2 s. c o m * id of commit * @param formatter * diff formatter * @return list of diff entries * @throws IOException * if any i/o errors occurs */ private List<DiffEntry> commitToWorkingTree(String commitId, DiffFormatter formatter) throws IOException { ObjectId commitA = repository.resolve(commitId); if (commitA == null) { File heads = new File(repository.getWorkTree().getPath() + "/.git/refs/heads"); if (heads.exists() && heads.list().length == 0) { return Collections.emptyList(); } throw new IllegalArgumentException("Invalid commit id " + commitId); } RevTree treeA; try (RevWalk revWalkA = new RevWalk(repository)) { treeA = revWalkA.parseTree(commitA); } List<DiffEntry> diff; try (ObjectReader reader = repository.newObjectReader()) { CanonicalTreeParser iterA = new CanonicalTreeParser(); iterA.reset(reader, treeA); FileTreeIterator iterB = new FileTreeIterator(repository); // Seems bug in DiffFormatter when work with working. Disable detect // renames by formatter and do it later. formatter.setDetectRenames(false); diff = formatter.scan(iterA, iterB); if (!request.isNoRenames()) { // Detect renames. RenameDetector renameDetector = createRenameDetector(); ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB)); renameDetector.addAll(diff); diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE); } } return diff; }
From source file:org.eclipse.egit.bc.BeyondCompareRepositoryActionHandler.java
License:Open Source License
protected String getPreviousPath(Repository repository, ObjectReader reader, RevCommit headCommit, RevCommit previousCommit, String path) throws IOException { TreeWalk walk = new TreeWalk(reader); walk.setRecursive(true);//w w w . jav a2 s .c om walk.addTree(previousCommit.getTree()); walk.addTree(headCommit.getTree()); List<DiffEntry> entries = DiffEntry.scan(walk); if (entries.size() < 2) return path; for (DiffEntry diff : entries) if (diff.getChangeType() == ChangeType.MODIFY && path.equals(diff.getNewPath())) return path; RenameDetector detector = new RenameDetector(repository); detector.addAll(entries); List<DiffEntry> renames = detector.compute(walk.getObjectReader(), NullProgressMonitor.INSTANCE); for (DiffEntry diff : renames) if (diff.getChangeType() == ChangeType.RENAME && path.equals(diff.getNewPath())) return diff.getOldPath(); return path; }
From source file:svnserver.repository.git.GitRepository.java
License:GNU General Public License
@NotNull private Map<String, String> collectRename(@NotNull GitFile oldTree, @NotNull GitFile newTree) throws IOException { if (!renameDetection) { return Collections.emptyMap(); }/*from w ww . ja v a 2 s . co m*/ final GitObject<ObjectId> oldTreeId = oldTree.getObjectId(); final GitObject<ObjectId> newTreeId = newTree.getObjectId(); if (oldTreeId == null || newTreeId == null || !Objects.equals(oldTreeId.getRepo(), newTreeId.getRepo())) { return Collections.emptyMap(); } final TreeWalk tw = new TreeWalk(repository); tw.setRecursive(true); tw.addTree(oldTree.getObjectId().getObject()); tw.addTree(newTree.getObjectId().getObject()); final RenameDetector rd = new RenameDetector(repository); rd.addAll(DiffEntry.scan(tw)); final Map<String, String> result = new HashMap<>(); for (DiffEntry diff : rd.compute(tw.getObjectReader(), null)) { if (diff.getScore() >= rd.getRenameScore()) { result.put(StringHelper.normalize(diff.getNewPath()), StringHelper.normalize(diff.getOldPath())); } } return result; }