@@ -44,7 +44,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
4444 }
4545
4646 public static void write (UnifiedDiff diff , Function <String , List <String >> originalLinesProvider , Consumer <String > writer , int contextSize ) throws IOException {
47- writer .accept (diff .getHeader ());
47+ if (diff .getHeader () != null ) {
48+ writer .accept (diff .getHeader ());
49+ }
4850
4951 for (UnifiedDiffFile file : diff .getFiles ()) {
5052 List <AbstractDelta <String >> patchDeltas = new ArrayList <>(
@@ -54,9 +56,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
5456 if (file .getIndex () != null ) {
5557 writer .accept ("index " + file .getIndex ());
5658 }
57- if ( file . getFromFile () != null ) {
58- writer .accept ("--- " + file .getFromFile ());
59- }
59+
60+ writer .accept ("--- " + file .getFromFile ());
61+
6062 if (file .getToFile () != null ) {
6163 writer .accept ("+++ " + file .getToFile ());
6264 }
@@ -83,7 +85,7 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
8385 // if it isn't, output the current set,
8486 // then create a new set and add the current Delta to
8587 // it.
86- processDeltas (writer , originalLines , deltas , contextSize );
88+ processDeltas (writer , originalLines , deltas , contextSize , false );
8789 deltas .clear ();
8890 deltas .add (nextDelta );
8991 }
@@ -92,7 +94,8 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
9294
9395 }
9496 // don't forget to process the last set of Deltas
95- processDeltas (writer , originalLines , deltas , contextSize );
97+ processDeltas (writer , originalLines , deltas , contextSize ,
98+ patchDeltas .size () == 1 && file .getFromFile () == null );
9699 }
97100
98101 }
@@ -104,18 +107,23 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
104107
105108 private static void processDeltas (Consumer <String > writer ,
106109 List <String > origLines , List <AbstractDelta <String >> deltas ,
107- int contextSize ) {
110+ int contextSize , boolean newFile ) {
108111 List <String > buffer = new ArrayList <>();
109112 int origTotal = 0 ; // counter for total lines output from Original
110113 int revTotal = 0 ; // counter for total lines output from Original
111114 int line ;
112115
113116 AbstractDelta <String > curDelta = deltas .get (0 );
114117
115- // NOTE: +1 to overcome the 0-offset Position
116- int origStart = curDelta .getSource ().getPosition () + 1 - contextSize ;
117- if (origStart < 1 ) {
118- origStart = 1 ;
118+ int origStart ;
119+ if (newFile ) {
120+ origStart = 0 ;
121+ } else {
122+ // NOTE: +1 to overcome the 0-offset Position
123+ origStart = curDelta .getSource ().getPosition () + 1 - contextSize ;
124+ if (origStart < 1 ) {
125+ origStart = 1 ;
126+ }
119127 }
120128
121129 int revStart = curDelta .getTarget ().getPosition () + 1 - contextSize ;
0 commit comments