use of org.eclipse.jgit.treewalk.filter.NotIgnoredFilter in project egit by eclipse.
the class GitMergeEditorInput method buildDiffContainer.
private IDiffContainer buildDiffContainer(Repository repository, RevCommit headCommit, RevCommit ancestorCommit, List<String> filterPaths, RevWalk rw, IProgressMonitor monitor) throws IOException, InterruptedException {
monitor.setTaskName(UIText.GitMergeEditorInput_CalculatingDiffTaskName);
IDiffContainer result = new DiffNode(Differencer.CONFLICTING);
try (TreeWalk tw = new TreeWalk(repository)) {
int dirCacheIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));
int fileTreeIndex = tw.addTree(new FileTreeIterator(repository));
int repositoryTreeIndex = tw.addTree(rw.parseTree(repository.resolve(Constants.HEAD)));
// skip ignored resources
NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(fileTreeIndex);
// filter by selected resources
if (filterPaths.size() > 1) {
List<TreeFilter> suffixFilters = new ArrayList<>();
for (String filterPath : filterPaths) suffixFilters.add(PathFilter.create(filterPath));
TreeFilter otf = OrTreeFilter.create(suffixFilters);
tw.setFilter(AndTreeFilter.create(otf, notIgnoredFilter));
} else if (filterPaths.size() > 0) {
String path = filterPaths.get(0);
if (path.length() == 0)
tw.setFilter(notIgnoredFilter);
else
tw.setFilter(AndTreeFilter.create(PathFilter.create(path), notIgnoredFilter));
} else
tw.setFilter(notIgnoredFilter);
tw.setRecursive(true);
while (tw.next()) {
if (monitor.isCanceled())
throw new InterruptedException();
String gitPath = tw.getPathString();
monitor.setTaskName(gitPath);
FileTreeIterator fit = tw.getTree(fileTreeIndex, FileTreeIterator.class);
if (fit == null)
continue;
DirCacheIterator dit = tw.getTree(dirCacheIndex, DirCacheIterator.class);
final DirCacheEntry dirCacheEntry = dit == null ? null : dit.getDirCacheEntry();
boolean conflicting = dirCacheEntry != null && dirCacheEntry.getStage() > 0;
AbstractTreeIterator rt = tw.getTree(repositoryTreeIndex, AbstractTreeIterator.class);
// compare local file against HEAD to see if it was modified
boolean modified = rt != null && !fit.getEntryObjectId().equals(rt.getEntryObjectId());
// if this is neither conflicting nor changed, we skip it
if (!conflicting && !modified)
continue;
ITypedElement right;
if (conflicting) {
GitFileRevision revision = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_3);
String encoding = CompareCoreUtils.getResourceEncoding(repository, gitPath);
right = new FileRevisionTypedElement(revision, encoding);
} else
right = CompareUtils.getFileRevisionTypedElement(gitPath, headCommit, repository);
// can this really happen?
if (right instanceof EmptyTypedElement)
continue;
IFileRevision rev;
// if the file is not conflicting (as it was auto-merged)
// we will show the auto-merged (local) version
Path repositoryPath = new Path(repository.getWorkTree().getAbsolutePath());
IPath location = repositoryPath.append(fit.getEntryPathString());
IFile file = ResourceUtil.getFileForLocation(location, false);
if (!conflicting || useWorkspace) {
if (file != null)
rev = new LocalFileRevision(file);
else
rev = new WorkingTreeFileRevision(location.toFile());
} else {
rev = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_2);
}
IRunnableContext runnableContext = getContainer();
if (runnableContext == null)
runnableContext = PlatformUI.getWorkbench().getProgressService();
EditableRevision leftEditable;
if (file != null)
leftEditable = new ResourceEditableRevision(rev, file, runnableContext);
else
leftEditable = new LocationEditableRevision(rev, location, runnableContext);
// make sure we don't need a round trip later
try {
leftEditable.cacheContents(monitor);
} catch (CoreException e) {
throw new IOException(e.getMessage());
}
int kind = Differencer.NO_CHANGE;
if (conflicting)
kind = Differencer.CONFLICTING;
else if (modified)
kind = Differencer.PSEUDO_CONFLICT;
IDiffContainer fileParent = getFileParent(result, repositoryPath, file, location);
ITypedElement anc;
if (ancestorCommit != null)
anc = CompareUtils.getFileRevisionTypedElement(gitPath, ancestorCommit, repository);
else
anc = null;
// instead of null
if (anc instanceof EmptyTypedElement)
anc = null;
// create the node as child
new DiffNode(fileParent, kind, anc, leftEditable, right);
}
return result;
}
}
use of org.eclipse.jgit.treewalk.filter.NotIgnoredFilter in project egit by eclipse.
the class GitSyncCache method loadDataFromGit.
static boolean loadDataFromGit(GitSynchronizeData gsd, TreeFilter filter, GitSyncObjectCache repoCache) {
Repository repo = gsd.getRepository();
try (TreeWalk tw = new TreeWalk(repo)) {
if (filter != null)
tw.setFilter(filter);
// setup local tree
FileTreeIterator fti = null;
if (gsd.shouldIncludeLocal()) {
fti = new FileTreeIterator(repo);
tw.addTree(fti);
if (filter != null)
tw.setFilter(AndTreeFilter.create(filter, new NotIgnoredFilter(0)));
else
tw.setFilter(new NotIgnoredFilter(0));
} else if (gsd.getSrcRevCommit() != null)
tw.addTree(gsd.getSrcRevCommit().getTree());
else
tw.addTree(new EmptyTreeIterator());
// setup base tree
if (gsd.getCommonAncestorRev() != null)
tw.addTree(gsd.getCommonAncestorRev().getTree());
else
tw.addTree(new EmptyTreeIterator());
// setup remote tree
if (gsd.getDstRevCommit() != null)
tw.addTree(gsd.getDstRevCommit().getTree());
else
tw.addTree(new EmptyTreeIterator());
DirCacheIterator dci = null;
if (fti != null) {
dci = new DirCacheIterator(DirCache.read(repo));
tw.addTree(dci);
fti.setDirCacheIterator(tw, 3);
}
List<ThreeWayDiffEntry> diffEntrys = ThreeWayDiffEntry.scan(tw, gsd);
for (ThreeWayDiffEntry diffEntry : diffEntrys) repoCache.addMember(diffEntry);
} catch (Exception e) {
Activator.logError(e.getMessage(), e);
return false;
}
return true;
}
use of org.eclipse.jgit.treewalk.filter.NotIgnoredFilter in project egit by eclipse.
the class CompareTreeView method buildMaps.
private void buildMaps(Repository repository, RevCommit baseCommit, RevCommit compareCommit, IProgressMonitor monitor) throws InterruptedException, IOException {
monitor.beginTask(UIText.CompareTreeView_AnalyzingRepositoryTaskText, IProgressMonitor.UNKNOWN);
long previousTimeMilliseconds = System.currentTimeMillis();
boolean useIndex = compareVersion.equals(INDEX_VERSION);
fileNodes.clear();
containerNodes.clear();
boolean checkIgnored = false;
try (TreeWalk tw = new TreeWalk(repository)) {
int baseTreeIndex;
if (baseCommit == null) {
checkIgnored = true;
baseTreeIndex = tw.addTree(new FileTreeIterator(repository));
} else
baseTreeIndex = tw.addTree(new CanonicalTreeParser(null, repository.newObjectReader(), baseCommit.getTree()));
int compareTreeIndex;
if (!useIndex)
compareTreeIndex = tw.addTree(new CanonicalTreeParser(null, repository.newObjectReader(), compareCommit.getTree()));
else
compareTreeIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));
if (input instanceof IResource[]) {
IResource[] resources = (IResource[]) input;
List<TreeFilter> orFilters = new ArrayList<>(resources.length);
for (IResource resource : resources) {
String relPath = repositoryMapping.getRepoRelativePath(resource);
if (relPath != null && relPath.length() > 0) {
orFilters.add(PathFilter.create(relPath));
}
}
if (checkIgnored) {
if (orFilters.size() > 1) {
TreeFilter andFilter = AndTreeFilter.create(new NotIgnoredFilter(baseTreeIndex), OrTreeFilter.create(orFilters));
tw.setFilter(andFilter);
} else if (orFilters.size() == 1) {
TreeFilter andFilter = AndTreeFilter.create(new NotIgnoredFilter(baseTreeIndex), orFilters.get(0));
tw.setFilter(andFilter);
} else
tw.setFilter(new NotIgnoredFilter(baseTreeIndex));
} else if (orFilters.size() > 1)
tw.setFilter(OrTreeFilter.create(orFilters));
else if (orFilters.size() == 1)
tw.setFilter(orFilters.get(0));
}
tw.setRecursive(true);
if (monitor.isCanceled())
throw new InterruptedException();
while (tw.next()) {
if (monitor.isCanceled())
throw new InterruptedException();
AbstractTreeIterator compareVersionIterator = tw.getTree(compareTreeIndex, AbstractTreeIterator.class);
AbstractTreeIterator baseVersionIterator = tw.getTree(baseTreeIndex, AbstractTreeIterator.class);
IFileRevision left = null;
IFileRevision right = null;
String repoRelativePath = baseVersionIterator != null ? baseVersionIterator.getEntryPathString() : compareVersionIterator.getEntryPathString();
IPath currentPath = new Path(repoRelativePath);
// Updating the progress bar is slow, so just sample it. To
// make sure slow compares are reflected in the progress
// monitor also update before comparing large files.
long currentTimeMilliseconds = System.currentTimeMillis();
long size1 = -1;
long size2 = -1;
if (compareVersionIterator != null && baseVersionIterator != null) {
size1 = getEntrySize(tw, compareVersionIterator);
size2 = getEntrySize(tw, baseVersionIterator);
}
final long REPORTSIZE = 100000;
if (size1 > REPORTSIZE || size2 > REPORTSIZE || currentTimeMilliseconds - previousTimeMilliseconds > 500) {
monitor.setTaskName(currentPath.toString());
previousTimeMilliseconds = currentTimeMilliseconds;
}
Type type = null;
if (compareVersionIterator != null && baseVersionIterator != null) {
boolean equalContent = compareVersionIterator.getEntryObjectId().equals(baseVersionIterator.getEntryObjectId());
type = equalContent ? Type.FILE_BOTH_SIDES_SAME : Type.FILE_BOTH_SIDES_DIFFER;
} else if (compareVersionIterator != null && baseVersionIterator == null) {
type = Type.FILE_DELETED;
} else if (compareVersionIterator == null && baseVersionIterator != null) {
type = Type.FILE_ADDED;
}
IFile file = null;
if (type != Type.FILE_BOTH_SIDES_SAME) {
file = ResourceUtil.getFileForLocation(repository, repoRelativePath, false);
}
if (baseVersionIterator != null) {
if (baseCommit == null) {
if (file != null)
left = new LocalFileRevision(file);
else {
IPath path = getRepositoryPath().append(repoRelativePath);
left = new WorkingTreeFileRevision(path.toFile());
}
} else {
left = GitFileRevision.inCommit(repository, baseCommit, repoRelativePath, tw.getObjectId(baseTreeIndex));
}
}
if (compareVersionIterator != null) {
if (!useIndex)
right = GitFileRevision.inCommit(repository, compareCommit, repoRelativePath, tw.getObjectId(compareTreeIndex));
else
right = GitFileRevision.inIndex(repository, repoRelativePath);
}
IPath containerPath = currentPath.removeLastSegments(1);
ContainerNode containerNode = getOrCreateContainerNode(containerPath, type);
FileNode fileNode = new FileNode(currentPath, file, type, left, right);
containerNode.addChild(fileNode);
fileNodes.put(currentPath, fileNode);
// change of the "only equal content" flag.
if (type != Type.FILE_BOTH_SIDES_SAME) {
IPath path = currentPath;
while (path.segmentCount() > 0) {
path = path.removeLastSegments(1);
ContainerNode node = containerNodes.get(path);
node.setOnlyEqualContent(false);
}
}
}
} finally {
monitor.done();
}
}
Aggregations