diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHunkParser.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHunkParser.java index b7594bb5d6..a65b9a6b02 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHunkParser.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitHunkParser.java @@ -79,7 +79,9 @@ final class GitHunkParser { ++oldLineCounter; break; default: - throw new IllegalStateException("cannot handle diff line: " + line); + if (!line.equals("\\ No newline at end of file")) { + throw new IllegalStateException("cannot handle diff line: " + line); + } } } diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitHunkParserTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitHunkParserTest.java index a58fae644d..e3f09ce5ef 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitHunkParserTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitHunkParserTest.java @@ -68,6 +68,17 @@ class GitHunkParserTest { " a\n" + "~illegal line\n"; + private static final String NO_NEWLINE_DIFF = "diff --git a/.editorconfig b/.editorconfig\n" + + "index ea2a3ba..2f02f32 100644\n" + + "--- a/.editorconfig\n" + + "+++ b/.editorconfig\n" + + "@@ -10,3 +10,4 @@\n" + + " indent_style = space\n" + + " indent_size = 2\n" + + " charset = utf-8\n" + + "+added line\n" + + "\\ No newline at end of file\n"; + @Test void shouldParseHunks() { List hunks = new GitHunkParser().parse(DIFF_001); @@ -127,6 +138,27 @@ class GitHunkParserTest { assertThrows(IllegalStateException.class, () -> new GitHunkParser().parse(ILLEGAL_DIFF)); } + @Test + void shouldIgnoreNoNewlineLine() { + List hunks = new GitHunkParser().parse(NO_NEWLINE_DIFF); + + Hunk hunk = hunks.get(0); + + Iterator lines = hunk.iterator(); + + DiffLine line1 = lines.next(); + assertThat(line1.getOldLineNumber()).hasValue(10); + assertThat(line1.getNewLineNumber()).hasValue(10); + assertThat(line1.getContent()).isEqualTo("indent_style = space"); + + lines.next(); + lines.next(); + DiffLine lastLine = lines.next(); + assertThat(lastLine.getOldLineNumber()).isEmpty(); + assertThat(lastLine.getNewLineNumber()).hasValue(13); + assertThat(lastLine.getContent()).isEqualTo("added line"); + } + private void assertHunk(Hunk hunk, int oldStart, int oldLineCount, int newStart, int newLineCount) { assertThat(hunk.getOldStart()).isEqualTo(oldStart); assertThat(hunk.getOldLineCount()).isEqualTo(oldLineCount);