Skip to content

Commit 7dec3fe

Browse files
committed
fixes #47
1 parent 3c6fa5f commit 7dec3fe

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

java-diff-utils/src/test/java/com/github/difflib/GenerateUnifiedDiffTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ public void testDiffWithHeaderLineInText() throws DiffException {
104104
original, patch, 10);
105105
UnifiedDiffUtils.parseUnifiedDiff(udiff);
106106
}
107+
108+
/**
109+
* Issue 47
110+
*/
111+
@Test
112+
public void testNewFileCreation() throws DiffException {
113+
List<String> original = new ArrayList<>();
114+
List<String> revised = new ArrayList<>();
115+
116+
revised.add("line1");
117+
revised.add("line2");
118+
119+
Patch<String> patch = DiffUtils.diff(original, revised);
120+
List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised",
121+
original, patch, 10);
122+
123+
assertEquals("@@ -0,0 +1,2 @@", udiff.get(2));
124+
125+
UnifiedDiffUtils.parseUnifiedDiff(udiff);
126+
}
107127

108128
private void verify(List<String> origLines, List<String> revLines,
109129
String originalFile, String revisedFile) throws DiffException {

0 commit comments

Comments
 (0)