@todo The subversion library returns borked diffs, where the headers are at the end. This function takes those headers, and moves them into the correct place to create a valid working diff. Find the source of this problem.
| 29 | /// takes those headers, and moves them into the correct place to create a valid working diff. |
| 30 | /// Find the source of this problem. |
| 31 | QString repairDiff(const QString& diff) { |
| 32 | qCDebug(PLUGIN_SVN) << "diff before repair:" << diff; |
| 33 | QStringList lines = diff.split(QLatin1Char('\n')); |
| 34 | QMap<QString, QString> headers; |
| 35 | for(int a = 0; a < lines.size()-1; ++a) { |
| 36 | const QLatin1String indexLineBegin("Index: "); |
| 37 | if (lines.at(a).startsWith(indexLineBegin) && lines.at(a + 1).startsWith(QLatin1String("====="))) { |
| 38 | const auto fileName = QStringView{lines.at(a)}.sliced(indexLineBegin.size()).trimmed().toString(); |
| 39 | headers[fileName] = lines.at(a); |
| 40 | qCDebug(PLUGIN_SVN) << "found header for" << fileName; |
| 41 | lines[a] = QString(); |
| 42 | if (lines.at(a + 1).startsWith(QLatin1String("======"))) { |
| 43 | headers[fileName] += QLatin1Char('\n') + lines.at(a + 1); |
| 44 | lines[a + 1] = QString(); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static const QRegularExpression whitespaceRegex(QStringLiteral("\\s")); |
| 50 | for(int a = 0; a < lines.size()-1; ++a) { |
| 51 | const QLatin1String threeDashLineBegin("--- "); |
| 52 | if (lines.at(a).startsWith(threeDashLineBegin)) { |
| 53 | const auto tail = lines.at(a).mid(threeDashLineBegin.size()); |
| 54 | if (const auto whitespaceIndex = tail.indexOf(whitespaceRegex); whitespaceIndex != -1) { |
| 55 | const auto file = tail.left(whitespaceIndex); |
| 56 | qCDebug(PLUGIN_SVN) << "checking for" << file; |
| 57 | const auto headerIt = headers.constFind(file); |
| 58 | if (headerIt != headers.constEnd()) { |
| 59 | qCDebug(PLUGIN_SVN) << "adding header for" << file << ":" << *headerIt; |
| 60 | lines[a] = *headerIt + QLatin1Char('\n') + lines.at(a); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | QString ret = lines.join(QLatin1Char('\n')); |
| 66 | qCDebug(PLUGIN_SVN) << "repaired diff:" << ret; |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | } // unnamed namespace |
| 71 |