Skip to content

Issue 16 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/diffparser.iml
/build
/.idea
/.gradle
/diffparser.iml
/build
/.idea
/.gradle
/.nb-gradle/
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ext{
dependencies {
compile('org.slf4j:slf4j-api:1.7.25')
testCompile('org.slf4j:slf4j-log4j12:1.7.25')
testCompile('org.testng:testng:6.8.7')
testCompile('org.testng:testng:6.10')
}

task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down Expand Up @@ -137,4 +137,7 @@ bintray {
publish = true
}


//Setup the test task
test {
useTestNG()
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.2-bin.zip
6 changes: 3 additions & 3 deletions gradlew
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

warn ( ) {
warn () {
echo "$*"
}

die ( ) {
die () {
echo
echo "$*"
echo
Expand Down Expand Up @@ -155,7 +155,7 @@ if $cygwin ; then
fi

# Escape application args
save ( ) {
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
Expand Down
168 changes: 84 additions & 84 deletions gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windows variants
if not "%OS%" == "Windows_NT" goto win9xME_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
24 changes: 12 additions & 12 deletions src/main/java/io/reflectoring/diffparser/api/UnifiedDiffParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
*/
package io.reflectoring.diffparser.api;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.reflectoring.diffparser.api.model.Diff;
import io.reflectoring.diffparser.api.model.Hunk;
import io.reflectoring.diffparser.api.model.Line;
import io.reflectoring.diffparser.api.model.Range;
import io.reflectoring.diffparser.unified.ParserState;
import io.reflectoring.diffparser.unified.ResizingParseWindow;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A parser that parses a unified diff from text into a {@link Diff} data structure.
* <p/>
Expand Down Expand Up @@ -101,7 +105,7 @@ public List<Diff> parse(InputStream in) {
}

private void parseNeutralLine(Diff currentDiff, String currentLine) {
Line line = new Line(Line.LineType.NEUTRAL, currentLine);
Line line = new Line(Line.LineType.NEUTRAL, currentLine.substring(1));
currentDiff.getLatestHunk().getLines().add(line);
}

Expand Down Expand Up @@ -169,12 +173,8 @@ public List<Diff> parse(byte[] bytes) {

@Override
public List<Diff> parse(File file) throws IOException {
FileInputStream in = new FileInputStream(file);
try{
try(FileInputStream in = new FileInputStream(file)) {
return parse(in);
} finally {
in.close();
}
}

}
85 changes: 44 additions & 41 deletions src/test/java/io/reflectoring/diffparser/unified/GitDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,54 @@
*/
package io.reflectoring.diffparser.unified;

import io.reflectoring.diffparser.api.DiffParser;
import io.reflectoring.diffparser.api.UnifiedDiffParser;
import io.reflectoring.diffparser.api.model.Diff;
import io.reflectoring.diffparser.api.model.Hunk;
import io.reflectoring.diffparser.api.model.Line;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import java.io.InputStream;
import java.util.List;

import org.testng.annotations.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

public class GitDiffTest {

@Test
public void testParse() {
// given
DiffParser parser = new UnifiedDiffParser();
InputStream in = getClass().getResourceAsStream("git.diff");

// when
List<Diff> diffs = parser.parse(in);

// then
assertNotNull(diffs);
assertEquals(3, diffs.size());

Diff diff1 = diffs.get(0);
assertEquals("a/diffparser/pom.xml", diff1.getFromFileName());
assertEquals("b/diffparser/pom.xml", diff1.getToFileName());
assertEquals(2, diff1.getHunks().size());

List<String> headerLines = diff1.getHeaderLines();
assertEquals(2, headerLines.size());

Hunk hunk1 = diff1.getHunks().get(0);
assertEquals(6, hunk1.getFromFileRange().getLineStart());
assertEquals(7, hunk1.getFromFileRange().getLineCount());
assertEquals(6, hunk1.getToFileRange().getLineStart());
assertEquals(7, hunk1.getToFileRange().getLineCount());
import io.reflectoring.diffparser.api.DiffParser;
import io.reflectoring.diffparser.api.UnifiedDiffParser;
import io.reflectoring.diffparser.api.model.Diff;
import io.reflectoring.diffparser.api.model.Hunk;
import io.reflectoring.diffparser.api.model.Line;

List<Line> lines = hunk1.getLines();
assertEquals(8, lines.size());
assertEquals(Line.LineType.FROM, lines.get(3).getLineType());
assertEquals(Line.LineType.TO, lines.get(4).getLineType());
}
public class GitDiffTest
{

@Test
public void testParse()
{
// given
DiffParser parser = new UnifiedDiffParser();
InputStream in = getClass().getResourceAsStream("git.diff");

// when
List<Diff> diffs = parser.parse(in);

// then
assertNotNull(diffs);
assertEquals(diffs.size(), 3);

Diff diff1 = diffs.get(0);
assertEquals("a/diffparser/pom.xml", diff1.getFromFileName());
assertEquals("b/diffparser/pom.xml", diff1.getToFileName());
assertEquals(diff1.getHunks().size(), 2);

List<String> headerLines = diff1.getHeaderLines();
assertEquals(headerLines.size(), 2);

Hunk hunk1 = diff1.getHunks().get(0);
assertEquals(hunk1.getFromFileRange().getLineStart(), 6);
assertEquals(hunk1.getFromFileRange().getLineCount(), 7);
assertEquals(hunk1.getToFileRange().getLineStart(), 6);
assertEquals(hunk1.getToFileRange().getLineCount(), 7);

List<Line> lines = hunk1.getLines();
assertEquals(lines.size(), 8);
assertEquals(lines.get(3).getLineType(), Line.LineType.FROM);
assertEquals(lines.get(4).getLineType(), Line.LineType.TO);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.reflectoring.diffparser.unified;

import java.io.InputStream;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.Test;

import io.reflectoring.diffparser.api.DiffParser;
import io.reflectoring.diffparser.api.UnifiedDiffParser;
import junit.framework.Assert;
import org.testng.annotations.Test;
import io.reflectoring.diffparser.api.model.Diff;
import io.reflectoring.diffparser.api.model.Hunk;
import io.reflectoring.diffparser.api.model.Line;

import java.io.InputStream;
import java.util.List;

/**
* Tests the DiffParser with a diff created by the "svn diff" command.
*/
Expand Down
Loading