use of org.eclipse.jgit.diff.DiffEntry in project OpenGrok by OpenGrok.
the class GitRepository method getFiles.
/**
* Assemble list of files that changed between 2 commits.
* @param repository repository object
* @param oldCommit parent commit
* @param newCommit new commit (the mehotd assumes oldCommit is its parent)
* @param files set of files that changed (excludes renamed files)
* @param renamedFiles set of renamed files (if renamed handling is enabled)
* @throws IOException on I/O problem
*/
private void getFiles(org.eclipse.jgit.lib.Repository repository, RevCommit oldCommit, RevCommit newCommit, Set<String> files, Set<String> renamedFiles) throws IOException {
OutputStream outputStream = NullOutputStream.INSTANCE;
try (DiffFormatter formatter = new DiffFormatter(outputStream)) {
formatter.setRepository(repository);
if (isHandleRenamedFiles()) {
formatter.setDetectRenames(true);
}
List<DiffEntry> diffs = formatter.scan(prepareTreeParser(repository, oldCommit), prepareTreeParser(repository, newCommit));
for (DiffEntry diff : diffs) {
if (diff.getChangeType() != DiffEntry.ChangeType.DELETE) {
if (files != null) {
files.add(getNativePath(getDirectoryNameRelative()) + File.separator + getNativePath(diff.getNewPath()));
}
}
if (diff.getChangeType() == DiffEntry.ChangeType.RENAME && isHandleRenamedFiles()) {
renamedFiles.add(getNativePath(getDirectoryNameRelative()) + File.separator + getNativePath(diff.getNewPath()));
}
}
}
}
use of org.eclipse.jgit.diff.DiffEntry in project omegat by omegat-org.
the class GITRemoteRepository2 method getRecentlyDeletedFiles.
@Override
public String[] getRecentlyDeletedFiles() throws Exception {
final ArrayList<String> deleted = new ArrayList<>();
ObjectId head = repository.getRefDatabase().findRef("HEAD").getObjectId();
String settingKey = "lastDeleteCheckForName" + localDirectory.getName();
String sinceRevisionString = projectTeamSettings.get(settingKey);
ObjectId sinceRevision;
if (sinceRevisionString == null) {
sinceRevision = head;
} else {
sinceRevision = ObjectId.fromString(sinceRevisionString);
}
Git git = new Git(repository);
AbstractTreeIterator startTreeIterator = getTreeIterator(git, sinceRevision);
AbstractTreeIterator headTreeIterator = new FileTreeIterator(git.getRepository());
List<DiffEntry> diffEntries = git.diff().setOldTree(startTreeIterator).setNewTree(headTreeIterator).call();
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getChangeType().equals(DiffEntry.ChangeType.DELETE)) {
deleted.add(diffEntry.getOldPath().replace('/', File.separatorChar));
}
}
projectTeamSettings.set(settingKey, head.getName());
String[] result = new String[deleted.size()];
return deleted.toArray(result);
}
use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.
the class JGitConnection method getCommitDiffFiles.
private List<DiffCommitFile> getCommitDiffFiles(RevCommit revCommit, String pattern) throws IOException {
List<DiffEntry> diffs;
TreeFilter filter = null;
if (!isNullOrEmpty(pattern)) {
filter = AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(pattern)), TreeFilter.ANY_DIFF);
}
List<DiffCommitFile> commitFilesList = new ArrayList<>();
try (TreeWalk tw = new TreeWalk(repository)) {
tw.setRecursive(true);
// and to get the list of DiffEntry.
if (revCommit.getParentCount() > 0) {
RevCommit parent = parseCommit(revCommit.getParent(0));
tw.reset(parent.getTree(), revCommit.getTree());
if (filter != null) {
tw.setFilter(filter);
} else {
tw.setFilter(TreeFilter.ANY_DIFF);
}
diffs = DiffEntry.scan(tw);
} else {
// list of DiffEntry.
try (RevWalk rw = new RevWalk(repository);
DiffFormatter diffFormat = new DiffFormatter(NullOutputStream.INSTANCE)) {
diffFormat.setRepository(repository);
if (filter != null) {
diffFormat.setPathFilter(filter);
}
diffs = diffFormat.scan(new EmptyTreeIterator(), new CanonicalTreeParser(null, rw.getObjectReader(), revCommit.getTree()));
}
}
}
if (diffs != null) {
commitFilesList.addAll(diffs.stream().map(diff -> newDto(DiffCommitFile.class).withOldPath(diff.getOldPath()).withNewPath(diff.getNewPath()).withChangeType(diff.getChangeType().name())).collect(Collectors.toList()));
}
return commitFilesList;
}
use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.
the class JGitDiffPage method commitToIndex.
/**
* Show changes between specified revision and index. If
* <code>commitId == null</code> then view changes between HEAD and index.
*
* @param commitId
* id of commit, pass <code>null</code> is the same as pass HEAD
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
if (commitId == null) {
commitId = Constants.HEAD;
}
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
DirCache dirCache = null;
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
dirCache = repository.lockDirCache();
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
DirCacheIterator iterB = new DirCacheIterator(dirCache);
if (!params.isNoRenames()) {
// Use embedded RenameDetector it works well with index and
// revision history.
formatter.setDetectRenames(true);
int renameLimit = params.getRenameLimit();
if (renameLimit > 0) {
formatter.getRenameDetector().setRenameLimit(renameLimit);
}
}
diff = formatter.scan(iterA, iterB);
} finally {
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
use of org.eclipse.jgit.diff.DiffEntry in project che by eclipse.
the class JGitDiffPage method indexToWorkingTree.
/**
* Show changes between index and working tree.
*
* @param formatter
* 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 (!params.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;
}
Aggregations