Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7dec3fe

Browse files
committed
fixes#47
1 parent3c6fa5f commit7dec3fe

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 representingthe 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
returnret;
196196
}
197197
returnnewArrayList<>();
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
privatestaticList<String>processDeltas(List<String>origLines,
210-
List<AbstractDelta<String>>deltas,intcontextSize) {
211+
List<AbstractDelta<String>>deltas,intcontextSize,booleannewFile) {
211212
List<String>buffer =newArrayList<>();
212213
intorigTotal =0;// counter for total lines output from Original
213214
intrevTotal =0;// counter for total lines output from Original
214215
intline;
215216

216217
AbstractDelta<String>curDelta =deltas.get(0);
217-
218-
// NOTE: +1 to overcome the 0-offset Position
219-
intorigStart =curDelta.getSource().getPosition() +1 -contextSize;
220-
if (origStart <1) {
221-
origStart =1;
218+
intorigStart;
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
intrevStart =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+
publicvoidtestNewFileCreation()throwsDiffException {
113+
List<String>original =newArrayList<>();
114+
List<String>revised =newArrayList<>();
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
privatevoidverify(List<String>origLines,List<String>revLines,
109129
StringoriginalFile,StringrevisedFile)throwsDiffException {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp