Skip to content

Commit

Permalink
A rough draft for a fix for concatenated diffs. A concatenated diff i…
Browse files Browse the repository at this point in the history
…s produced

by simply concatenating the diff for each file, so it lacks the expected header
for each file. Normally this would not be needed, but it's helpful for
programmatically generated JGit diffs. This breaks test TortoiseDiffTest because
it fails to parse the second hunk in the first file for tortoise.diff.
  • Loading branch information
selliott512 committed Jul 5, 2019
1 parent 08302eb commit 6efdc37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ public List<Diff> parse(InputStream in) {
Diff currentDiff = new Diff();
String currentLine;
while ((currentLine = window.slideForward()) != null) {
ParserState lastState = state;
state = state.nextState(window);
switch (state) {
case INITIAL:
// nothing to do
break;
case HEADER:
// The goal of the following is to make sure a diff is not
// lost when a new header is encountered suddenly due to
// diffs being concatenated.
if ((lastState != ParserState.INITIAL) &&
(lastState != ParserState.HEADER) &&
(lastState != ParserState.END))
{
parsedDiffs.add(currentDiff);
currentDiff = new Diff();
}
parseHeader(currentDiff, currentLine);
break;
case FROM_FILE:
Expand Down Expand Up @@ -97,6 +108,13 @@ public List<Diff> parse(InputStream in) {
}
}

// Something like that may be needed to make sure no diffs are lost.
if (currentDiff.getHunks().size() > 0)
{
parsedDiffs.add(currentDiff);
currentDiff = new Diff();
}

return parsedDiffs;
}

Expand Down
19 changes: 16 additions & 3 deletions src/main/java/io/reflectoring/diffparser/unified/ParserState.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public ParserState nextState(ParseWindow window) {
}
},

// TODO: At least FROM_LINE and TO_LINE should have a common method. Seems
// like NEUTRAL_LINE could as well, but that's a special case.

/**
* The parser is in this state if it is currently parsing a line containing a line that is in the first file,
* but not the second (a "from" line).
Expand All @@ -145,9 +148,12 @@ public ParserState nextState(ParseWindow window) {
} else if (matchesHunkStartPattern(line)) {
logTransition(line, FROM_LINE, HUNK_START);
return HUNK_START;
} else {
logTransition(line, FROM_LINE, NEUTRAL_LINE);
} else if (matchesNeutralPattern(line)) {
logTransition(line, TO_LINE, NEUTRAL_LINE);
return NEUTRAL_LINE;
} else {
logTransition(line, TO_LINE, HEADER);
return HEADER;
}
}
},
Expand Down Expand Up @@ -175,9 +181,12 @@ public ParserState nextState(ParseWindow window) {
} else if (matchesHunkStartPattern(line)) {
logTransition(line, TO_LINE, HUNK_START);
return HUNK_START;
} else {
} else if (matchesNeutralPattern(line)) {
logTransition(line, TO_LINE, NEUTRAL_LINE);
return NEUTRAL_LINE;
} else {
logTransition(line, TO_LINE, HEADER);
return HEADER;
}
}
},
Expand Down Expand Up @@ -249,6 +258,10 @@ protected boolean matchesFromLinePattern(String line) {
return line.startsWith("-");
}

protected boolean matchesNeutralPattern(String line) {
return line.startsWith(" ") || line.startsWith("\\");
}

protected boolean matchesToLinePattern(String line) {
return line.startsWith("+");
}
Expand Down

0 comments on commit 6efdc37

Please sign in to comment.