# # # patch "guitone/src/util/DiffParser.cpp" # from [962e00c66f7011d084cedabb9762b93973907403] # to [561160a37a57636159d2300bc72d80205d470bfc] # # patch "guitone/src/util/DiffParser.h" # from [568c7594ae3f23b95c1834b466fbd44b513f0eed] # to [09081b3461bf1b5a588c4288f25db08bb1d1af80] # ============================================================ --- guitone/src/util/DiffParser.cpp 962e00c66f7011d084cedabb9762b93973907403 +++ guitone/src/util/DiffParser.cpp 561160a37a57636159d2300bc72d80205d470bfc @@ -34,8 +34,13 @@ void DiffParser::parse(const QString & i { if (input.length() == 0) return; - QStringList lines = input.split(QRegExp("[\\n\\r]+")); + // remove last newline character + QString in(input); + in.chop(1); + // split the output into lines + QStringList lines = in.split(QChar('\n')); + QString curFile; Diff * curDiff; @@ -47,11 +52,7 @@ void DiffParser::parse(const QString & i { QString line(lines.at(i)); Q_ASSERT(line.length() > 0); - QChar curChar = line.at(0); - QChar lastChar = ' ', nextChar = ' '; - if (i > 0) lastChar = lines.at(i-1).at(0); - if (i < s-1) nextChar = lines.at(i+1).at(0); // // parse file diff separators, setup new DiffFile object @@ -77,6 +78,7 @@ void DiffParser::parse(const QString & i // create a new diff curDiff = new Diff(); + fileDiffs[curFile] = curDiff; // skip the next line as we don't care about the content id's here i++; @@ -125,141 +127,110 @@ void DiffParser::parse(const QString & i continue; } - // - // Possible cases to handle - // - // " basic" - // "+new" - // - // and - // - // "-old" - // "+new" - // - // and - // - // "+new" - // "+new" - // + qDebug("Processing line %s", qPrintable(line)); + + curGroup = new DiffLineGroup(); + curGroup->changeLen = 1; + curGroup->contents.push_back(line.mid(1)); + if (curChar == '+') { - qDebug("a plusline"); - // create linked dummy item - if (lastChar == ' ') + QChar lastChar; + if (i > 0) { - DiffLineGroup * lastGroup = new DiffLineGroup(); - lastGroup->startPos = leftLinesCount; - lastGroup->changeLen = 0; - lastGroup->state = DiffLineGroup::Removed; - - curHunk->lineGroups.push_back(lastGroup); - curGroup = new DiffLineGroup(); - curHunk->lineGroups.push_back(curGroup); - - curGroup->startPos = rightLinesCount; - curGroup->changeLen = 1; - curGroup->contents.push_back(line.mid(1)); - - lastGroup->friendGroup = curGroup; - curGroup->friendGroup = lastGroup; - - leftLinesCount++; - continue; + lastChar = lines.at(i-1).at(0); } - if (lastChar == '+') + qDebug("Last char %c", lastChar.toLatin1()); + + // check for any last group + if (lastChar == ' ') { - curGroup->contents.push_back(line.mid(1)); - curGroup->changeLen++; - leftLinesCount++; - continue; + // insert dummy group + DiffLineGroup * dummy = new DiffLineGroup(); + dummy->changeLen = 0; + dummy->startPos = leftLinesCount; + dummy->state = DiffLineGroup::Removed; + dummy->friendGroup = curGroup; + curGroup->friendGroup = dummy; + curHunk->lineGroups.push_back(dummy); } - - if (lastChar == '-') + else + if (lastChar == '-') { - curGroup = new DiffLineGroup(); - curHunk->lineGroups.push_back(curGroup); - - curGroup->startPos = rightLinesCount; - curGroup->changeLen = 1; - curGroup->state = DiffLineGroup::Added; - curGroup->contents.push_back(line.mid(1)); - + // connect the last group with this group DiffLineGroup * lastGroup = curHunk->lineGroups.last(); Q_ASSERT(lastGroup); curGroup->friendGroup = lastGroup; lastGroup->friendGroup = curGroup; - - leftLinesCount++; - continue; } - qWarning("unknown last char %c", lastChar.toLatin1()); + curGroup->startPos = leftLinesCount; + curGroup->state = DiffLineGroup::Added; } - - // - // Possible cases to handle - // - // "-old" - // " basic" - // - // and - // - // "-old" - // "+new" (already handled above) - // - // and - // - // "-old" - // "-old" - // - if (curChar == '-') + else { - qDebug("a minusline followed by %c", nextChar.toLatin1()); - - // create linked dummy item - if (nextChar == ' ') + curGroup->startPos = rightLinesCount; + curGroup->state = DiffLineGroup::Removed; + } + + curHunk->lineGroups.push_back(curGroup); + + // "eat" as much lines with the same change type + QChar nextChar; + while (++i < s) + { + nextChar = lines.at(i).at(0); + if (nextChar == curChar) { - DiffLineGroup * nextGroup = new DiffLineGroup(); - nextGroup->startPos = rightLinesCount; - nextGroup->changeLen = 0; - nextGroup->state = DiffLineGroup::Added; - - curGroup = new DiffLineGroup(); - curHunk->lineGroups.push_back(curGroup); - - curHunk->lineGroups.push_back(nextGroup); - curGroup->startPos = leftLinesCount; - curGroup->changeLen = 1; - curGroup->contents.push_back(line.mid(1)); - - nextGroup->friendGroup = curGroup; - curGroup->friendGroup = nextGroup; - - rightLinesCount++; - continue; - } - - if (nextChar == '+') - { - // this case will be handled in the '+' section - rightLinesCount++; - continue; - } - - if (nextChar == '-') - { - // FIXME: this case is NOT handled above curGroup->changeLen++; - rightLinesCount++; + QString nextLine(lines.at(i)); + curGroup->contents.push_back(nextLine.mid(1)); continue; } - - qWarning("unknown next char %c", lastChar.toLatin1()); + i--; + break; } - qWarning("unknown current char %c", curChar.toLatin1()); + Q_ASSERT(nextChar != curChar); + + // we just processed a plusline, the connections should already be there + if (curChar == '+') + { + Q_ASSERT(curGroup->friendGroup); + continue; + } + + // if the next line is a plusline, the connection happens in the + // next round, otherwise we need to do it by hand + if (curChar == '-' && nextChar == '+') + { + continue; + } + + qDebug("Current Group: %s", qPrintable(curGroup->contents.join("\n"))); + qDebug("Next line: %s", qPrintable(lines.at(i+1))); + + // if we either reached the last line or the next line is a + // unchanged line, insert dummy group + DiffLineGroup * dummy = new DiffLineGroup(); + dummy->changeLen = 0; + dummy->startPos = rightLinesCount; + dummy->state = DiffLineGroup::Added; + dummy->friendGroup = curGroup; + curGroup->friendGroup = dummy; + curHunk->lineGroups.push_back(dummy); } + + qDebug("Diff hunks: %d", curDiff->hunks.size()); + for (int i=0, j=curDiff->hunks.size(); ihunks.at(i)->lineGroups.size()); + for (int k=0, l=curDiff->hunks.at(i)->lineGroups.size(); khunks.at(i)->lineGroups.at(k)->state, curDiff->hunks.at(i)->lineGroups.at(k)->changeLen); + } + } } Diff* DiffParser::getDiff(const QString & filename) ============================================================ --- guitone/src/util/DiffParser.h 568c7594ae3f23b95c1834b466fbd44b513f0eed +++ guitone/src/util/DiffParser.h 09081b3461bf1b5a588c4288f25db08bb1d1af80 @@ -25,13 +25,14 @@ #include #include #include +#include struct DiffLineGroup { enum State {Added, Removed} state; int startPos; int changeLen; - QList contents; + QStringList contents; DiffLineGroup * friendGroup; };