Commit b5293bc4 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Filter out binary files in folder based projects + untracked git

Untracked git files can often contain binary files (for e.g temp build
dir). Similarly, folder based projects can easily have a lot of binary
files. Filter such stuff out for these 2 cases. This makes quick open
etc nicer to work with and reduces noise with small folder projects.

We can do this for all projects by default too as we are a plain text
editor and binary files aren't really useful for kate.
parent 9827a9cb
Pipeline #117809 passed with stage
in 4 minutes and 11 seconds
......@@ -441,10 +441,10 @@ QVector<QString> KateProjectWorker::filesFromGit(const QDir &dir, bool recursive
QStringLiteral(".")};
// ls-files + ls-files untracked
return gitFiles(dir, recursive, lsFilesArgs) << gitFiles(dir, recursive, lsFilesUntrackedArgs);
return gitFiles(dir, recursive, lsFilesArgs, false) << gitFiles(dir, recursive, lsFilesUntrackedArgs, true);
}
QVector<QString> KateProjectWorker::gitFiles(const QDir &dir, bool recursive, const QStringList &args)
QVector<QString> KateProjectWorker::gitFiles(const QDir &dir, bool recursive, const QStringList &args, bool ignoreBinaryFiles)
{
QProcess git;
setupGitProcess(git, dir.absolutePath(), args);
......@@ -465,6 +465,18 @@ QVector<QString> KateProjectWorker::gitFiles(const QDir &dir, bool recursive, co
}
files.append(QString::fromUtf8(byteArray));
}
// Filter out binary files
if (ignoreBinaryFiles) {
QtConcurrent::blockingMap(files, [dir](const QString &file) {
const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(dir.filePath(file));
if (!mimeType.inherits(QStringLiteral("text/plain"))) {
return QString();
}
return file;
});
}
return files;
}
......@@ -666,13 +678,25 @@ QVector<QString> KateProjectWorker::filesFromDirectory(const QDir &_dir, bool re
/**
* create iterator and collect all files
*/
QVector<QString> files;
QDirIterator dirIterator(dir, flags);
const QString dirPath = dir.path() + QLatin1Char('/');
std::vector<QFileInfo> fileInfos;
while (dirIterator.hasNext()) {
dirIterator.next();
// make it relative path
files.append(dirIterator.filePath().remove(dirPath));
fileInfos.push_back(dirIterator.fileInfo());
}
return files;
// Filter out binary files
std::function<QString(const QFileInfo &)> func = [dirPath](const QFileInfo &fi) {
// We only accept plainText files
const QMimeType mimeType = QMimeDatabase().mimeTypeForFile(fi);
if (!mimeType.inherits(QStringLiteral("text/plain"))) {
return QString();
}
return fi.filePath().remove(dirPath);
};
return QtConcurrent::blockingMapped<QVector<QString>>(fileInfos, func);
}
......@@ -68,7 +68,7 @@ private:
static QVector<QString> filesFromFossil(const QDir &dir, bool recursive);
static QVector<QString> filesFromDirectory(const QDir &dir, bool recursive, const QStringList &filters);
static QVector<QString> gitFiles(const QDir &dir, bool recursive, const QStringList &args);
static QVector<QString> gitFiles(const QDir &dir, bool recursive, const QStringList &args, bool ignoreBinaryFiles);
private:
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment