Example usage for org.apache.commons.io.filefilter DirectoryFileFilter DirectoryFileFilter

List of usage examples for org.apache.commons.io.filefilter DirectoryFileFilter DirectoryFileFilter

Introduction

In this page you can find the example usage for org.apache.commons.io.filefilter DirectoryFileFilter DirectoryFileFilter.

Prototype

protected DirectoryFileFilter() 

Source Link

Document

Restrictive consructor.

Usage

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

@Override
public void cloneWithSparseCheckout(String directory, String remoteUrl)
        throws GitException, UnauthorizedException {
    //TODO rework this code when jgit will support sparse-checkout. Tracked issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=383772
    if (directory == null) {
        throw new GitException("Subdirectory for sparse-checkout is not specified");
    }//  w  w  w  .j  a  v a  2s .  c om
    clone(CloneParams.create(remoteUrl));
    final String sourcePath = getWorkingDir().getPath();
    final String keepDirectoryPath = sourcePath + "/" + directory;
    IOFileFilter folderFilter = new DirectoryFileFilter() {
        public boolean accept(File dir) {
            String directoryPath = dir.getPath();
            return !(directoryPath.startsWith(keepDirectoryPath)
                    || directoryPath.startsWith(sourcePath + "/.git"));
        }
    };
    Collection<File> files = org.apache.commons.io.FileUtils.listFilesAndDirs(getWorkingDir(),
            TrueFileFilter.INSTANCE, folderFilter);
    try {
        DirCache index = getRepository().lockDirCache();
        int sourcePathLength = sourcePath.length() + 1;
        files.stream().filter(File::isFile).forEach(
                file -> index.getEntry(file.getPath().substring(sourcePathLength)).setAssumeValid(true));
        index.write();
        index.commit();
        for (File file : files) {
            if (keepDirectoryPath.startsWith(file.getPath())) {
                continue;
            }
            if (file.exists()) {
                FileUtils.delete(file, FileUtils.RECURSIVE);
            }
        }
    } catch (IOException exception) {
        String message = generateExceptionMessage(exception);
        throw new GitException(message, exception);
    }
}