| 419 | } |
| 420 | |
| 421 | VcsDiff VcsDiff::subDiff(const uint startLine, const uint endLine, DiffDirection dir) const |
| 422 | { |
| 423 | // Code adapted from cola/diffparse.py |
| 424 | enum LineType { |
| 425 | ADD = '+', |
| 426 | DEL = '-', |
| 427 | CTX = ' ', |
| 428 | NO_NEWLINE = '\\', |
| 429 | }; |
| 430 | |
| 431 | VcsDiff ret; |
| 432 | ret.setBaseDiff(baseDiff()); |
| 433 | ret.setDepth(depth()); |
| 434 | |
| 435 | QStringList lines; |
| 436 | for (const auto &hunk : d->hunks) { |
| 437 | // Skip hunks before the first line |
| 438 | if (hunk < startLine) |
| 439 | continue; |
| 440 | |
| 441 | // Skip hunks after the last line |
| 442 | if (hunk > endLine) |
| 443 | break; |
| 444 | |
| 445 | int addCount = 0; |
| 446 | int delCount = 0; |
| 447 | int ctxCount = 0; |
| 448 | QStringList filteredLines; |
| 449 | |
| 450 | // Will be set if the previous line in a hunk was |
| 451 | // skipped because it was not in the selected range |
| 452 | bool prevSkipped = false; |
| 453 | |
| 454 | uint lnIdx = hunk.headingLineIdx; |
| 455 | |
| 456 | // Store the number of skipped lines which start the hunk |
| 457 | // (i.e. lines before a first deletion (addition in case of reverse) |
| 458 | // so that we can adjust the start appropriately |
| 459 | int startOffset = 0; |
| 460 | const auto _lines = QStringList(hunk.lines.constBegin(), hunk.lines.constEnd()); |
| 461 | for (const auto &line : _lines) { |
| 462 | lnIdx++; |
| 463 | LineType tp = line.length() > 0 ? (LineType)line[0].toLatin1() : (LineType)0; |
| 464 | QString content = line.mid(1); |
| 465 | |
| 466 | if (dir == Reverse) { |
| 467 | if (tp == ADD) |
| 468 | tp = DEL; |
| 469 | else if (tp == DEL) |
| 470 | tp = ADD; |
| 471 | } |
| 472 | |
| 473 | if (lnIdx < startLine || endLine < lnIdx) { |
| 474 | // skip additions (or deletions if reverse) that are not in range |
| 475 | if (tp == ADD) { |
| 476 | prevSkipped = true; |
| 477 | // If we are before the range and |
| 478 | // so far we only encountered ADD (or DEL, in case of reverse) lines |
no test coverage detected