Added sameFilesInDirs(): check for two different directories if the contain the

same files (comparison by name and by git-blob).
This commit is contained in:
Gerhard Hoffmann 2023-08-22 12:29:52 +02:00
parent 4ebdcf56a0
commit 1620b73d01
2 changed files with 86 additions and 0 deletions

View File

@ -1,5 +1,6 @@
#include "utils.h"
#include "message_handler.h"
#include "git/git_client.h"
#include <QFile>
#include <QTextStream>
@ -86,3 +87,85 @@ QString Utils::rstrip(QString const &str) {
}
return "";
}
bool Utils::sameFilesInDirs(QDir const &dir1, QDir const &dir2,
QStringList const &nameFilters) {
if (!dir1.exists()) {
printCriticalErrorMsg(dir1.dirName() + " DOES NOT EXIST");
return false;
}
if (!dir2.exists()) {
printCriticalErrorMsg(dir2.dirName() + " DOES NOT EXIST");
return false;
}
if (dir1.absolutePath() == dir2.absolutePath()) {
printCriticalErrorMsg(dir1.dirName() + " AND "+ dir2.dirName() + " HAVE SAME PATH");
return false;
}
// files, sorted by name
QFileInfoList const &lst1 = dir1.entryInfoList(nameFilters, QDir::Files, QDir::Name);
QFileInfoList const &lst2 = dir2.entryInfoList(nameFilters, QDir::Files, QDir::Name);
QStringList fileNameLst1{};
QStringList fileNameLst2{};
QListIterator<QFileInfo> i1(lst1);
while (i1.hasNext()) {
fileNameLst1 << i1.next().fileName();
}
QListIterator<QFileInfo> i2(lst2);
while (i2.hasNext()) {
fileNameLst2 << i2.next().fileName();
}
if (fileNameLst1.isEmpty()) {
qCritical() << "DIR1" << dir1.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
return false;
}
if (fileNameLst2.isEmpty()) {
qCritical() << "DIR1" << dir2.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
return false;
}
if (fileNameLst1 != fileNameLst2) {
printCriticalErrorMsg(dir1.dirName() + " AND " + dir2.dirName()
+ " DIFFER: [" + fileNameLst1.join(',') + "],["
+ fileNameLst2.join(',') + "]");
return false;
} else {
printInfoMsg(dir1.dirName() + " AND " + dir2.dirName()
+ " ARE EQUAL: [" + fileNameLst1.join(',') + "]");
}
QStringList gitBlobLst1{};
QStringList gitBlobLst2{};
QListIterator<QFileInfo> i3(lst1);
while (i3.hasNext()) {
gitBlobLst1 << GitClient::gitBlob(i3.next().fileName());
}
QListIterator<QFileInfo> i4(lst2);
while (i4.hasNext()) {
gitBlobLst2 << GitClient::gitBlob(i4.next().fileName());
}
if (gitBlobLst1.isEmpty()) {
qCritical() << "DIR1" << dir1.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
return false;
}
if (gitBlobLst2.isEmpty()) {
qCritical() << "DIR1" << dir2.dirName() << " DOES NOT CONTAIN EXPECTED FILES";
return false;
}
if (gitBlobLst1 != gitBlobLst2) {
printCriticalErrorMsg(dir1.dirName() + " AND " + dir2.dirName()
+ " DIFFER: [" + gitBlobLst1.join(',') + "],["
+ gitBlobLst2.join(',') + "]");
return false;
} else {
printInfoMsg(dir1.dirName() + " AND " + dir2.dirName()
+ " CONTAIN SAME GIT-BLOBS FOR FILES: [" + fileNameLst1.join(',') + "]");
}
return true;
}

View File

@ -7,6 +7,7 @@
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <QDir>
namespace Utils {
int read1stLineOfFile(QString fileName);
@ -16,6 +17,8 @@ namespace Utils {
void printLineEditInfo(QStringList const &lines);
QString getTariffLoadTime(QString fileName);
QString rstrip(QString const &str);
bool sameFilesInDirs(QDir const &dir1, QDir const &dir2,
QStringList const &nameFilters = {"*.json"});
}
#endif // UTILS_H_INCLUDED