@@ -44,7 +44,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
44
44
}
45
45
46
46
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
+ }
48
50
49
51
for (UnifiedDiffFile file : diff .getFiles ()) {
50
52
List <AbstractDelta <String >> patchDeltas = new ArrayList <>(
@@ -54,9 +56,9 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
54
56
if (file .getIndex () != null ) {
55
57
writer .accept ("index " + file .getIndex ());
56
58
}
57
- if ( file . getFromFile () != null ) {
58
- writer .accept ("--- " + file .getFromFile ());
59
- }
59
+
60
+ writer .accept ("--- " + file .getFromFile ());
61
+
60
62
if (file .getToFile () != null ) {
61
63
writer .accept ("+++ " + file .getToFile ());
62
64
}
@@ -83,7 +85,7 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
83
85
// if it isn't, output the current set,
84
86
// then create a new set and add the current Delta to
85
87
// it.
86
- processDeltas (writer , originalLines , deltas , contextSize );
88
+ processDeltas (writer , originalLines , deltas , contextSize , false );
87
89
deltas .clear ();
88
90
deltas .add (nextDelta );
89
91
}
@@ -92,7 +94,8 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
92
94
93
95
}
94
96
// 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 );
96
99
}
97
100
98
101
}
@@ -104,18 +107,23 @@ public static void write(UnifiedDiff diff, Function<String, List<String>> origin
104
107
105
108
private static void processDeltas (Consumer <String > writer ,
106
109
List <String > origLines , List <AbstractDelta <String >> deltas ,
107
- int contextSize ) {
110
+ int contextSize , boolean newFile ) {
108
111
List <String > buffer = new ArrayList <>();
109
112
int origTotal = 0 ; // counter for total lines output from Original
110
113
int revTotal = 0 ; // counter for total lines output from Original
111
114
int line ;
112
115
113
116
AbstractDelta <String > curDelta = deltas .get (0 );
114
117
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
+ }
119
127
}
120
128
121
129
int revStart = curDelta .getTarget ().getPosition () + 1 - contextSize ;
0 commit comments