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

Commit7da2973

Browse files
committed
fixes#110 support for similarity index, rename from, rename to
1 parentfb0837f commit7da2973

File tree

6 files changed

+319
-16
lines changed

6 files changed

+319
-16
lines changed

‎java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffFile.java‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
importcom.github.difflib.patch.Patch;
1919

2020
/**
21-
*
21+
* Data structure for one patched file from a unified diff file.
22+
*
2223
* @author Tobias Warneke (t.warneke@gmx.net)
2324
*/
2425
publicfinalclassUnifiedDiffFile {
@@ -27,12 +28,15 @@ public final class UnifiedDiffFile {
2728
privateStringfromFile;
2829
privateStringfromTimestamp;
2930
privateStringtoFile;
31+
privateStringrenameFrom;
32+
privateStringrenameTo;
3033
privateStringtoTimestamp;
3134
privateStringindex;
3235
privateStringnewFileMode;
3336
privateStringdeletedFileMode;
3437
privatePatch<String>patch =newPatch<>();
3538
privatebooleannoNewLineAtTheEndOfTheFile =false;
39+
privateIntegersimilarityIndex;
3640

3741
publicStringgetDiffCommand() {
3842
returndiffCommand;
@@ -85,8 +89,30 @@ public String getToTimestamp() {
8589
publicvoidsetToTimestamp(StringtoTimestamp) {
8690
this.toTimestamp =toTimestamp;
8791
}
88-
89-
92+
93+
publicIntegergetSimilarityIndex() {
94+
returnsimilarityIndex;
95+
}
96+
97+
publicvoidsetSimilarityIndex(IntegersimilarityIndex) {
98+
this.similarityIndex =similarityIndex;
99+
}
100+
101+
publicStringgetRenameFrom() {
102+
returnrenameFrom;
103+
}
104+
105+
publicvoidsetRenameFrom(StringrenameFrom) {
106+
this.renameFrom =renameFrom;
107+
}
108+
109+
publicStringgetRenameTo() {
110+
returnrenameTo;
111+
}
112+
113+
publicvoidsetRenameTo(StringrenameTo) {
114+
this.renameTo =renameTo;
115+
}
90116

91117
publicstaticUnifiedDiffFilefrom(StringfromFile,StringtoFile,Patch<String>patch) {
92118
UnifiedDiffFilefile =newUnifiedDiffFile();

‎java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ public final class UnifiedDiffReader {
4545
privatefinalUnifiedDiffdata =newUnifiedDiff();
4646

4747
privatefinalUnifiedDiffLineDIFF_COMMAND =newUnifiedDiffLine(true,"^diff\\s",this::processDiff);
48+
privatefinalUnifiedDiffLineSIMILARITY_INDEX =newUnifiedDiffLine(true,"^similarity index (\\d+)%$",this::processSimilarityIndex);
4849
privatefinalUnifiedDiffLineINDEX =newUnifiedDiffLine(true,"^index\\s[\\da-zA-Z]+\\.\\.[\\da-zA-Z]+(\\s(\\d+))?$",this::processIndex);
4950
privatefinalUnifiedDiffLineFROM_FILE =newUnifiedDiffLine(true,"^---\\s",this::processFromFile);
5051
privatefinalUnifiedDiffLineTO_FILE =newUnifiedDiffLine(true,"^\\+\\+\\+\\s",this::processToFile);
52+
privatefinalUnifiedDiffLineRENAME_FROM =newUnifiedDiffLine(true,"^rename\\sfrom\\s(.+)$",this::processRenameFrom);
53+
privatefinalUnifiedDiffLineRENAME_TO =newUnifiedDiffLine(true,"^rename\\sto\\s(.+)$",this::processRenameTo);
5154

5255
privatefinalUnifiedDiffLineNEW_FILE_MODE =newUnifiedDiffLine(true,"^new\\sfile\\smode\\s(\\d+)",this::processNewFileMode);
5356

@@ -91,7 +94,10 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
9194
if (!CHUNK.validLine(line)) {
9295
initFileIfNecessary();
9396
while (line !=null && !CHUNK.validLine(line)) {
94-
if (!processLine(line,DIFF_COMMAND,INDEX,FROM_FILE,TO_FILE,NEW_FILE_MODE,DELETED_FILE_MODE)) {
97+
if (!processLine(line,DIFF_COMMAND,SIMILARITY_INDEX,INDEX,
98+
FROM_FILE,TO_FILE,
99+
RENAME_FROM,RENAME_TO,
100+
NEW_FILE_MODE,DELETED_FILE_MODE)) {
95101
thrownewUnifiedDiffParserException("expected file start line not found");
96102
}
97103
line =READER.readLine();
@@ -146,6 +152,13 @@ static String[] parseFileNames(String line) {
146152

147153
privatestaticfinalLoggerLOG =Logger.getLogger(UnifiedDiffReader.class.getName());
148154

155+
/**
156+
* To parse a diff file use this method.
157+
* @param stream This is the diff file data.
158+
* @return In a UnifiedDiff structure this diff file data is returned.
159+
* @throws IOException
160+
* @throws UnifiedDiffParserException
161+
*/
149162
publicstaticUnifiedDiffparseUnifiedDiff(InputStreamstream)throwsIOException,UnifiedDiffParserException {
150163
UnifiedDiffReaderparser =newUnifiedDiffReader(newBufferedReader(newInputStreamReader(stream)));
151164
returnparser.parse();
@@ -185,6 +198,10 @@ private void processDiff(MatchResult match, String line) {
185198
actualFile.setToFile(fromTo[1]);
186199
actualFile.setDiffCommand(line);
187200
}
201+
202+
privatevoidprocessSimilarityIndex(MatchResultmatch,Stringline) {
203+
actualFile.setSimilarityIndex(Integer.valueOf(match.group(1)));
204+
}
188205

189206
privateList<String>originalTxt =newArrayList<>();
190207
privateList<String>revisedTxt =newArrayList<>();
@@ -256,6 +273,14 @@ private void processToFile(MatchResult match, String line) {
256273
actualFile.setToFile(extractFileName(line));
257274
actualFile.setToTimestamp(extractTimestamp(line));
258275
}
276+
277+
privatevoidprocessRenameFrom(MatchResultmatch,Stringline) {
278+
actualFile.setRenameFrom(match.group(1));
279+
}
280+
281+
privatevoidprocessRenameTo(MatchResultmatch,Stringline) {
282+
actualFile.setRenameTo(match.group(1));
283+
}
259284

260285
privatevoidprocessNewFileMode(MatchResultmatch,Stringline) {
261286
//initFileIfNecessary();

‎java-diff-utils/src/main/java/com/github/difflib/unifieddiff/package-info.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
* limitations under the License.
1515
*/
1616
/**
17-
* This is the first test version of a multifile diff parser. The Api is still subject
18-
* of change.
17+
* This is the new implementation of UnifiedDiff Tools. This version is multi file aware.
18+
* <p/>
19+
* To read a unified diff file you should use {@link UnifiedDiffReader#parseUnifiedDiff}.
20+
* You will get a {@link UnifiedDiff} that holds all informations about the
21+
* diffs and the files.
22+
* <p/>
23+
* To process the UnifiedDiff use {@link UnifiedDiffWriter#write}.
1924
*/
2025
packagecom.github.difflib.unifieddiff;

‎java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void testParseIssue51() throws IOException {
155155
UnifiedDiffFilefile1 =diff.getFiles().get(0);
156156
assertThat(file1.getFromFile()).isEqualTo("f1");
157157
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
158-
158+
159159
UnifiedDiffFilefile2 =diff.getFiles().get(1);
160160
assertThat(file2.getFromFile()).isEqualTo("f2");
161161
assertThat(file2.getPatch().getDeltas().size()).isEqualTo(1);
@@ -220,7 +220,7 @@ public void testParseIssue85() throws IOException {
220220
publicvoidtestTimeStampRegexp() {
221221
assertThat("2019-04-18 13:49:39.516149751 +0200").matches(UnifiedDiffReader.TIMESTAMP_REGEXP);
222222
}
223-
223+
224224
@Test
225225
publicvoidtestParseIssue98()throwsIOException {
226226
UnifiedDiffdiff =UnifiedDiffReader.parseUnifiedDiff(
@@ -236,7 +236,7 @@ public void testParseIssue98() throws IOException {
236236
assertEquals("src/test/java/se/bjurr/violations/lib/model/ViolationTest.java",file1.getFromFile());
237237
assertThat(diff.getTail()).isEqualTo("2.25.1");
238238
}
239-
239+
240240
@Test
241241
publicvoidtestParseIssue104()throwsIOException {
242242
UnifiedDiffdiff =UnifiedDiffReader.parseUnifiedDiff(
@@ -247,12 +247,12 @@ public void testParseIssue104() throws IOException {
247247
finalUnifiedDiffFilefile =diff.getFiles().get(2);
248248
assertThat(file.getFromFile()).isEqualTo("/dev/null");
249249
assertThat(file.getToFile()).isEqualTo("doc/samba_data_tool_path.xml.in");
250-
250+
251251
assertThat(file.getPatch().toString()).isEqualTo("Patch{deltas=[[ChangeDelta, position: 0, lines: [] to [@SAMBA_DATA_TOOL@]]]}");
252252

253-
assertThat(diff.getTail()).isEqualTo("2.14.4");
253+
assertThat(diff.getTail()).isEqualTo("2.14.4");
254254
}
255-
255+
256256
@Test
257257
publicvoidtestParseIssue107BazelDiff()throwsIOException {
258258
UnifiedDiffdiff =UnifiedDiffReader.parseUnifiedDiff(
@@ -263,13 +263,13 @@ public void testParseIssue107BazelDiff() throws IOException {
263263
finalUnifiedDiffFilefile =diff.getFiles().get(0);
264264
assertThat(file.getFromFile()).isEqualTo("./src/main/java/com/amazonaws/AbortedException.java");
265265
assertThat(file.getToFile()).isEqualTo("/home/greg/projects/bazel/third_party/aws-sdk-auth-lite/src/main/java/com/amazonaws/AbortedException.java");
266-
266+
267267
assertThat(diff.getFiles().stream()
268268
.filter(f ->f.isNoNewLineAtTheEndOfTheFile())
269269
.count())
270-
.isEqualTo(48);
270+
.isEqualTo(48);
271271
}
272-
272+
273273
@Test
274274
publicvoidtestParseIssue107_2()throwsIOException {
275275
UnifiedDiffdiff =UnifiedDiffReader.parseUnifiedDiff(
@@ -281,6 +281,22 @@ public void testParseIssue107_2() throws IOException {
281281
UnifiedDiffFilefile1 =diff.getFiles().get(0);
282282
assertThat(file1.getFromFile()).isEqualTo("Main.java");
283283
assertThat(file1.getPatch().getDeltas().size()).isEqualTo(1);
284+
285+
}
286+
287+
@Test
288+
publicvoidtestParseIssue110()throwsIOException {
289+
UnifiedDiffdiff =UnifiedDiffReader.parseUnifiedDiff(
290+
UnifiedDiffReaderTest.class.getResourceAsStream("0001-avahi-python-Use-the-agnostic-DBM-interface.patch"));
291+
292+
assertThat(diff.getFiles().size()).isEqualTo(5);
293+
294+
finalUnifiedDiffFilefile =diff.getFiles().get(4);
295+
assertThat(file.getSimilarityIndex()).isEqualTo(87);
296+
assertThat(file.getRenameFrom()).isEqualTo("service-type-database/build-db.in");
297+
assertThat(file.getRenameTo()).isEqualTo("service-type-database/build-db");
284298

299+
assertThat(file.getFromFile()).isEqualTo("service-type-database/build-db.in");
300+
assertThat(file.getToFile()).isEqualTo("service-type-database/build-db");
285301
}
286302
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp