@@ -127,8 +127,8 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
127
127
}
128
128
129
129
/**
130
- * generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing
131
- * the Patch.
130
+ * generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format
131
+ * text representing the Patch.
132
132
*
133
133
* @param originalFileName - Filename of the original (unrevised file)
134
134
* @param revisedFileName - Filename of the revised file
@@ -179,7 +179,7 @@ public static List<String> generateUnifiedDiff(String originalFileName,
179
179
// then create a new set and add the current Delta to
180
180
// it.
181
181
List <String > curBlock = processDeltas (originalLines ,
182
- deltas , contextSize );
182
+ deltas , contextSize , false );
183
183
ret .addAll (curBlock );
184
184
deltas .clear ();
185
185
deltas .add (nextDelta );
@@ -190,15 +190,16 @@ public static List<String> generateUnifiedDiff(String originalFileName,
190
190
}
191
191
// don't forget to process the last set of Deltas
192
192
List <String > curBlock = processDeltas (originalLines , deltas ,
193
- contextSize );
193
+ contextSize , patchDeltas . size () == 1 && originalFileName == null );
194
194
ret .addAll (curBlock );
195
195
return ret ;
196
196
}
197
197
return new ArrayList <>();
198
198
}
199
199
200
200
/**
201
- * processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text.
201
+ * processDeltas takes a list of Deltas and outputs them together in a single block of
202
+ * Unified-Diff-format text.
202
203
*
203
204
* @param origLines - the lines of the original file
204
205
* @param deltas - the Deltas to be output as a single block
@@ -207,18 +208,22 @@ public static List<String> generateUnifiedDiff(String originalFileName,
207
208
* @author Bill James (tankerbay@gmail.com)
208
209
*/
209
210
private static List <String > processDeltas (List <String > origLines ,
210
- List <AbstractDelta <String >> deltas , int contextSize ) {
211
+ List <AbstractDelta <String >> deltas , int contextSize , boolean newFile ) {
211
212
List <String > buffer = new ArrayList <>();
212
213
int origTotal = 0 ; // counter for total lines output from Original
213
214
int revTotal = 0 ; // counter for total lines output from Original
214
215
int line ;
215
216
216
217
AbstractDelta <String > curDelta = deltas .get (0 );
217
-
218
- // NOTE: +1 to overcome the 0-offset Position
219
- int origStart = curDelta .getSource ().getPosition () + 1 - contextSize ;
220
- if (origStart < 1 ) {
221
- origStart = 1 ;
218
+ int origStart ;
219
+ if (newFile ) {
220
+ origStart = 0 ;
221
+ } else {
222
+ // NOTE: +1 to overcome the 0-offset Position
223
+ origStart = curDelta .getSource ().getPosition () + 1 - contextSize ;
224
+ if (origStart < 1 ) {
225
+ origStart = 1 ;
226
+ }
222
227
}
223
228
224
229
int revStart = curDelta .getTarget ().getPosition () + 1 - contextSize ;
0 commit comments