@@ -127,8 +127,8 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
127127 }
128128
129129 /**
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.
132132 *
133133 * @param originalFileName - Filename of the original (unrevised file)
134134 * @param revisedFileName - Filename of the revised file
@@ -179,7 +179,7 @@ public static List<String> generateUnifiedDiff(String originalFileName,
179179 // then create a new set and add the current Delta to
180180 // it.
181181 List <String > curBlock = processDeltas (originalLines ,
182- deltas , contextSize );
182+ deltas , contextSize , false );
183183 ret .addAll (curBlock );
184184 deltas .clear ();
185185 deltas .add (nextDelta );
@@ -190,15 +190,16 @@ public static List<String> generateUnifiedDiff(String originalFileName,
190190 }
191191 // don't forget to process the last set of Deltas
192192 List <String > curBlock = processDeltas (originalLines , deltas ,
193- contextSize );
193+ contextSize , patchDeltas . size () == 1 && originalFileName == null );
194194 ret .addAll (curBlock );
195195 return ret ;
196196 }
197197 return new ArrayList <>();
198198 }
199199
200200 /**
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.
202203 *
203204 * @param origLines - the lines of the original file
204205 * @param deltas - the Deltas to be output as a single block
@@ -207,18 +208,22 @@ public static List<String> generateUnifiedDiff(String originalFileName,
207208 * @author Bill James (tankerbay@gmail.com)
208209 */
209210 private static List <String > processDeltas (List <String > origLines ,
210- List <AbstractDelta <String >> deltas , int contextSize ) {
211+ List <AbstractDelta <String >> deltas , int contextSize , boolean newFile ) {
211212 List <String > buffer = new ArrayList <>();
212213 int origTotal = 0 ; // counter for total lines output from Original
213214 int revTotal = 0 ; // counter for total lines output from Original
214215 int line ;
215216
216217 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+ }
222227 }
223228
224229 int revStart = curDelta .getTarget ().getPosition () + 1 - contextSize ;
0 commit comments