@@ -53,7 +53,7 @@ public final class UnifiedDiffReader {
5353 private final UnifiedDiffLine RENAME_TO = new UnifiedDiffLine (true , "^rename\\ sto\\ s(.+)$" , this ::processRenameTo );
5454
5555 private final UnifiedDiffLine NEW_FILE_MODE = new UnifiedDiffLine (true , "^new\\ sfile\\ smode\\ s(\\ d+)" , this ::processNewFileMode );
56-
56+
5757 private final UnifiedDiffLine DELETED_FILE_MODE = new UnifiedDiffLine (true , "^deleted\\ sfile\\ smode\\ s(\\ d+)" , this ::processDeletedFileMode );
5858
5959 private final UnifiedDiffLine CHUNK = new UnifiedDiffLine (false , UNIFIED_DIFF_CHUNK_REGEXP , this ::processChunk );
@@ -94,9 +94,9 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
9494 if (!CHUNK .validLine (line )) {
9595 initFileIfNecessary ();
9696 while (line != null && !CHUNK .validLine (line )) {
97- if (!processLine (line , DIFF_COMMAND , SIMILARITY_INDEX , INDEX ,
98- FROM_FILE , TO_FILE ,
99- RENAME_FROM , RENAME_TO ,
97+ if (!processLine (line , DIFF_COMMAND , SIMILARITY_INDEX , INDEX ,
98+ FROM_FILE , TO_FILE ,
99+ RENAME_FROM , RENAME_TO ,
100100 NEW_FILE_MODE , DELETED_FILE_MODE )) {
101101 throw new UnifiedDiffParserException ("expected file start line not found" );
102102 }
@@ -117,8 +117,8 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
117117 }
118118 }
119119 line = READER .readLine ();
120-
121- if ("\\ No newline at end of file" .equals (line )) {
120+
121+ if ("\\ No newline at end of file" .equals (line )) {
122122 actualFile .setNoNewLineAtTheEndOfTheFile (true );
123123 line = READER .readLine ();
124124 }
@@ -153,11 +153,12 @@ static String[] parseFileNames(String line) {
153153 private static final Logger LOG = Logger .getLogger (UnifiedDiffReader .class .getName ());
154154
155155 /**
156- * To parse a diff file use this method.
156+ * To parse a diff file use this method.
157+ *
157158 * @param stream This is the diff file data.
158159 * @return In a UnifiedDiff structure this diff file data is returned.
159160 * @throws IOException
160- * @throws UnifiedDiffParserException
161+ * @throws UnifiedDiffParserException
161162 */
162163 public static UnifiedDiff parseUnifiedDiff (InputStream stream ) throws IOException , UnifiedDiffParserException {
163164 UnifiedDiffReader parser = new UnifiedDiffReader (new BufferedReader (new InputStreamReader (stream )));
@@ -198,44 +199,58 @@ private void processDiff(MatchResult match, String line) {
198199 actualFile .setToFile (fromTo [1 ]);
199200 actualFile .setDiffCommand (line );
200201 }
201-
202+
202203 private void processSimilarityIndex (MatchResult match , String line ) {
203204 actualFile .setSimilarityIndex (Integer .valueOf (match .group (1 )));
204205 }
205206
206207 private List <String > originalTxt = new ArrayList <>();
207208 private List <String > revisedTxt = new ArrayList <>();
209+ private List <Integer > addLineIdxList = new ArrayList <>();
210+ private List <Integer > delLineIdxList = new ArrayList <>();
208211 private int old_ln ;
209212 private int old_size ;
210213 private int new_ln ;
211214 private int new_size ;
215+ private int delLineIdx = 0 ;
216+ private int addLineIdx = 0 ;
212217
213218 private void finalizeChunk () {
214219 if (!originalTxt .isEmpty () || !revisedTxt .isEmpty ()) {
215220 actualFile .getPatch ().addDelta (new ChangeDelta <>(new Chunk <>(
216- old_ln - 1 , originalTxt ), new Chunk <>(
217- new_ln - 1 , revisedTxt )));
221+ old_ln - 1 , originalTxt , delLineIdxList ), new Chunk <>(
222+ new_ln - 1 , revisedTxt , addLineIdxList )));
218223 old_ln = 0 ;
219224 new_ln = 0 ;
220225 originalTxt .clear ();
221226 revisedTxt .clear ();
227+ addLineIdxList .clear ();
228+ delLineIdxList .clear ();
229+ delLineIdx = 0 ;
230+ addLineIdx = 0 ;
222231 }
223232 }
224233
225234 private void processNormalLine (MatchResult match , String line ) {
226235 String cline = line .substring (1 );
227236 originalTxt .add (cline );
228237 revisedTxt .add (cline );
238+ delLineIdx ++;
239+ addLineIdx ++;
229240 }
230241
231242 private void processAddLine (MatchResult match , String line ) {
232243 String cline = line .substring (1 );
233244 revisedTxt .add (cline );
245+ addLineIdx ++;
246+ addLineIdxList .add (new_ln - 1 + addLineIdx );
234247 }
235248
236249 private void processDelLine (MatchResult match , String line ) {
237250 String cline = line .substring (1 );
238251 originalTxt .add (cline );
252+ delLineIdx ++;
253+ delLineIdxList .add (old_ln - 1 + delLineIdx );
239254 }
240255
241256 private void processChunk (MatchResult match , String chunkStart ) {
@@ -273,7 +288,7 @@ private void processToFile(MatchResult match, String line) {
273288 actualFile .setToFile (extractFileName (line ));
274289 actualFile .setToTimestamp (extractTimestamp (line ));
275290 }
276-
291+
277292 private void processRenameFrom (MatchResult match , String line ) {
278293 actualFile .setRenameFrom (match .group (1 ));
279294 }
@@ -286,7 +301,7 @@ private void processNewFileMode(MatchResult match, String line) {
286301 //initFileIfNecessary();
287302 actualFile .setNewFileMode (match .group (1 ));
288303 }
289-
304+
290305 private void processDeletedFileMode (MatchResult match , String line ) {
291306 //initFileIfNecessary();
292307 actualFile .setDeletedFileMode (match .group (1 ));
0 commit comments