From 1620b73d018bc7c0c1afb3fac0a51ab9fe592f5b Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Tue, 22 Aug 2023 12:29:52 +0200 Subject: [PATCH] Added sameFilesInDirs(): check for two different directories if the contain the same files (comparison by name and by git-blob). --- utils.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 3 ++ 2 files changed, 86 insertions(+) diff --git a/utils.cpp b/utils.cpp index 92bf620..1d3c7de 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1,5 +1,6 @@ #include "utils.h" #include "message_handler.h" +#include "git/git_client.h" #include #include @@ -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 i1(lst1); + while (i1.hasNext()) { + fileNameLst1 << i1.next().fileName(); + } + QListIterator 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 i3(lst1); + while (i3.hasNext()) { + gitBlobLst1 << GitClient::gitBlob(i3.next().fileName()); + } + QListIterator 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; +} diff --git a/utils.h b/utils.h index 79f838b..7f16616 100644 --- a/utils.h +++ b/utils.h @@ -7,6 +7,7 @@ #include #include #include +#include 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