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);
}
}
Aggregations