| 661 | } |
| 662 | |
| 663 | void UIDiffView::loadFromPatch( const std::string& patchText, |
| 664 | const std::string& originalFilePath ) { |
| 665 | mLines.clear(); |
| 666 | auto lines = String::split( patchText, '\n', true ); |
| 667 | |
| 668 | std::string fileText; |
| 669 | bool hasCompleteFile = false; |
| 670 | std::vector<std::string> fileLines; |
| 671 | if ( !originalFilePath.empty() && FileSystem::fileExists( originalFilePath ) ) { |
| 672 | FileSystem::fileGet( originalFilePath, fileText ); |
| 673 | fileLines = String::split( fileText, '\n', true ); |
| 674 | hasCompleteFile = true; |
| 675 | } |
| 676 | |
| 677 | Int64 oldLineNum = 0; |
| 678 | Int64 newLineNum = 0; |
| 679 | Int64 expectedOldLineNum = 1; |
| 680 | Int64 expectedNewLineNum = 1; |
| 681 | std::string filename; |
| 682 | |
| 683 | for ( const auto& line : lines ) { |
| 684 | if ( String::startsWith( line, "diff " ) || String::startsWith( line, "index " ) || |
| 685 | String::startsWith( line, "--- " ) || String::startsWith( line, "+++ " ) ) { |
| 686 | if ( String::startsWith( line, "+++ " ) ) { |
| 687 | filename = line.substr( 4 ); |
| 688 | if ( String::startsWith( filename, "b/" ) ) |
| 689 | filename = filename.substr( 2 ); |
| 690 | filename = String::trim( filename ); |
| 691 | } |
| 692 | continue; |
| 693 | } |
| 694 | |
| 695 | if ( String::startsWith( line, "@@ " ) ) { |
| 696 | size_t minusPos = line.find( "-" ); |
| 697 | size_t plusPos = line.find( "+" ); |
| 698 | if ( minusPos != std::string::npos && plusPos != std::string::npos ) { |
| 699 | size_t commaPos = line.find( ",", minusPos ); |
| 700 | size_t spacePos = line.find( " ", minusPos ); |
| 701 | if ( commaPos != std::string::npos && commaPos < spacePos ) { |
| 702 | oldLineNum = |
| 703 | std::stoll( line.substr( minusPos + 1, commaPos - minusPos - 1 ) ) - 1; |
| 704 | } else if ( spacePos != std::string::npos ) { |
| 705 | oldLineNum = |
| 706 | std::stoll( line.substr( minusPos + 1, spacePos - minusPos - 1 ) ) - 1; |
| 707 | } |
| 708 | |
| 709 | commaPos = line.find( ",", plusPos ); |
| 710 | spacePos = line.find( " ", plusPos ); |
| 711 | if ( commaPos != std::string::npos && commaPos < spacePos ) { |
| 712 | newLineNum = |
| 713 | std::stoll( line.substr( plusPos + 1, commaPos - plusPos - 1 ) ) - 1; |
| 714 | } else if ( spacePos != std::string::npos ) { |
| 715 | newLineNum = |
| 716 | std::stoll( line.substr( plusPos + 1, spacePos - plusPos - 1 ) ) - 1; |
| 717 | } |
| 718 | |
| 719 | if ( hasCompleteFile ) { |
| 720 | while ( expectedNewLineNum < newLineNum + 1 && |