@@ -46,7 +46,10 @@ public final class UnifiedDiffParser {
4646 new UnifiedDiffLine (true , "^index\\ s[\\ da-zA-Z]+\\ .\\ .[\\ da-zA-Z]+(\\ s(\\ d+))?$" , this ::processIndex ),
4747 new UnifiedDiffLine (true , "^---\\ s" , this ::processFromFile ),
4848 new UnifiedDiffLine (true , "^\\ +\\ +\\ +\\ s" , this ::processToFile ),
49- new UnifiedDiffLine (true , UNIFIED_DIFF_CHUNK_REGEXP , this ::processChunk )
49+ new UnifiedDiffLine (false , UNIFIED_DIFF_CHUNK_REGEXP , this ::processChunk ),
50+ new UnifiedDiffLine ("^\\ s+" , this ::processNormalLine ),
51+ new UnifiedDiffLine ("^-" , this ::processDelLine ),
52+ new UnifiedDiffLine ("^+" , this ::processAddLine )
5053 };
5154
5255 private UnifiedDiffFile actualFile ;
@@ -62,20 +65,33 @@ public final class UnifiedDiffParser {
6265 private UnifiedDiff parse () throws IOException , UnifiedDiffParserException {
6366 boolean header = true ;
6467 String headerTxt = "" ;
68+ String tailTxt = "" ;
6569 while (READER .ready ()) {
6670 String line = READER .readLine ();
67- LOG .log (Level .INFO , "parsing line {0}" , line );
68- if (processLine (header , line ) == false ) {
69- if (header ) {
70- headerTxt += line + "\n " ;
71+ if (line .matches ("^\\ -\\ -\\ s+" )) {
72+ break ;
73+ } else {
74+ LOG .log (Level .INFO , "parsing line {0}" , line );
75+ if (processLine (header , line ) == false ) {
76+ if (header ) {
77+ headerTxt += line + "\n " ;
78+ } else {
79+ break ;
80+ }
7181 } else {
72- break ;
82+ header = false ;
83+ data .setHeader (headerTxt );
7384 }
74- } else {
75- header = false ;
76- data .setHeader (headerTxt );
7785 }
7886 }
87+
88+ finalizeChunk ();
89+
90+ while (READER .ready ()) {
91+ tailTxt += READER .readLine () + "\n " ;
92+ }
93+ data .setTailTxt (tailTxt );
94+
7995 return data ;
8096 }
8197
@@ -106,6 +122,10 @@ private boolean processLine(boolean header, String line) throws UnifiedDiffParse
106122 }
107123
108124 private void initFileIfNecessary () {
125+ if (!originalTxt .isEmpty () || !revisedTxt .isEmpty ()) {
126+ finalizeChunk ();
127+ actualFile = null ;
128+ }
109129 if (actualFile == null ) {
110130 actualFile = new UnifiedDiffFile ();
111131 data .addFile (actualFile );
@@ -121,60 +141,48 @@ public void processDiff(MatchResult match, String line) {
121141 actualFile .setDiffCommand (line );
122142 }
123143
124- public void processChunk (MatchResult _match , String chunkStart ) {
125- MatchResult match = _match ;
126- try {
127-
128- while (true ) {
129-
130- List <String > originalTxt = new ArrayList <>();
131- List <String > revisedTxt = new ArrayList <>();
132-
133- int old_ln = match .group (1 ) == null ? 1 : Integer .parseInt (match .group (1 ));
134- int new_ln = match .group (3 ) == null ? 1 : Integer .parseInt (match .group (3 ));
135- if (old_ln == 0 ) {
136- old_ln = 1 ;
137- }
138- if (new_ln == 0 ) {
139- new_ln = 1 ;
140- }
141-
142- while (this .READER .ready ()) {
143- String line = READER .readLine ();
144- LOG .log (Level .INFO , "processing chunk line {0}" , line );
144+ private List <String > originalTxt = new ArrayList <>();
145+ private List <String > revisedTxt = new ArrayList <>();
146+ private int old_ln ;
147+ private int new_ln ;
148+
149+ private void finalizeChunk () {
150+ if (!originalTxt .isEmpty () || !revisedTxt .isEmpty ()) {
151+ actualFile .getPatch ().addDelta (new ChangeDelta <>(new Chunk <>(
152+ old_ln - 1 , originalTxt ), new Chunk <>(
153+ new_ln - 1 , revisedTxt )));
154+ old_ln = 0 ;
155+ new_ln = 0 ;
156+ originalTxt .clear ();
157+ revisedTxt .clear ();
158+ }
159+ }
145160
146- if (line .startsWith (" " ) || line .startsWith ("+" )) {
147- revisedTxt .add (line .substring (1 ));
148- }
149- if (line .startsWith (" " ) || line .startsWith ("-" )) {
150- originalTxt .add (line .substring (1 ));
151- }
152- if (line .equals ("" ) || line .startsWith ("@@" ) || line .startsWith ("--" )) {
153- break ;
154- }
155- }
161+ public void processNormalLine (MatchResult match , String line ) {
162+ String cline = line .substring (1 );
163+ originalTxt .add (cline );
164+ revisedTxt .add (cline );
165+ }
156166
157- actualFile .getPatch ().addDelta (new ChangeDelta <>(new Chunk <>(
158- old_ln - 1 , originalTxt ), new Chunk <>(
159- new_ln - 1 , revisedTxt )));
167+ public void processAddLine (MatchResult match , String line ) {
168+ String cline = line .substring (1 );
169+ revisedTxt .add (cline );
170+ }
160171
161- if (READER .lastLine ().equals ("" )
162- || READER .lastLine ().startsWith ("--" )
163- || !READER .lastLine ().startsWith ("@@" )) {
164- break ;
165- } else {
166- Matcher m = UNIFIED_DIFF_CHUNK_REGEXP .matcher (READER .lastLine ());
167- if (m .find ()) {
168- match = m .toMatchResult ();
169- } else {
170- break ;
171- }
172- }
173- }
172+ public void processDelLine (MatchResult match , String line ) {
173+ String cline = line .substring (1 );
174+ originalTxt .add (cline );
175+ }
174176
175- } catch (IOException ex ) {
176- Logger .getLogger (UnifiedDiffParser .class .getName ()).log (Level .SEVERE , null , ex );
177- throw new UnifiedDiffParserException (ex );
177+ public void processChunk (MatchResult match , String chunkStart ) {
178+ finalizeChunk ();
179+ old_ln = match .group (1 ) == null ? 1 : Integer .parseInt (match .group (1 ));
180+ new_ln = match .group (3 ) == null ? 1 : Integer .parseInt (match .group (3 ));
181+ if (old_ln == 0 ) {
182+ old_ln = 1 ;
183+ }
184+ if (new_ln == 0 ) {
185+ new_ln = 1 ;
178186 }
179187 }
180188
0 commit comments