@@ -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.
181181List <String >curBlock =processDeltas (originalLines ,
182- deltas ,contextSize );
182+ deltas ,contextSize , false );
183183ret .addAll (curBlock );
184184deltas .clear ();
185185deltas .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
192192List <String >curBlock =processDeltas (originalLines ,deltas ,
193- contextSize );
193+ contextSize , patchDeltas . size () == 1 && originalFileName == null );
194194ret .addAll (curBlock );
195195return ret ;
196196 }
197197return 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 */
209210private static List <String >processDeltas (List <String >origLines ,
210- List <AbstractDelta <String >>deltas ,int contextSize ) {
211+ List <AbstractDelta <String >>deltas ,int contextSize , boolean newFile ) {
211212List <String >buffer =new ArrayList <>();
212213int origTotal =0 ;// counter for total lines output from Original
213214int revTotal =0 ;// counter for total lines output from Original
214215int line ;
215216
216217AbstractDelta <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
224229int revStart =curDelta .getTarget ().getPosition () +1 -contextSize ;