Search in sources :

Example 1 with DirectoryFileFilter

use of org.apache.commons.io.filefilter.DirectoryFileFilter in project che by eclipse.

the class JGitConnection method cloneWithSparseCheckout.

@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");
    }
    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);
    }
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) GitException(org.eclipse.che.api.git.exception.GitException) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) IOException(java.io.IOException) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File) DirectoryFileFilter(org.apache.commons.io.filefilter.DirectoryFileFilter)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 DirectoryFileFilter (org.apache.commons.io.filefilter.DirectoryFileFilter)1 IOFileFilter (org.apache.commons.io.filefilter.IOFileFilter)1 GitException (org.eclipse.che.api.git.exception.GitException)1 DiffCommitFile (org.eclipse.che.api.git.shared.DiffCommitFile)1 DirCache (org.eclipse.jgit.dircache.DirCache)1