ApplyCommand: handle completely empty context lines in text patches

C git treats completely empty lines as empty context lines (which
traditionally have a single blank). Apparently newer GNU diff may
produce such lines; see [1]. ("Newer" meaning "since 2006"...)

[1] https://github.com/git/git/commit/b507b465f7831

Change-Id: I80c1f030edb17a46289b1dabf11a2648d2660d38
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
Thomas Wolf 2021-03-10 18:04:25 +01:00 committed by Matthias Sohn
parent 76b76a6048
commit 2a0295ccfd
5 changed files with 22 additions and 2 deletions

View File

@ -295,6 +295,14 @@ public void testEncodingChange() throws Exception {
checkBinary("umlaut", true);
}
@Test
public void testEmptyLine() throws Exception {
// C git accepts completely empty lines as empty context lines.
// According to comments in the C git sources (apply.c), newer GNU diff
// may produce such diffs.
checkBinary("emptyLine", true);
}
@Test
public void testAddA1() throws Exception {
ApplyResult result = init("A1", false, true);

View File

@ -640,6 +640,11 @@ && canApplyAt(hunkLines, newLines, 0)) {
int sz = hunkLines.size();
for (int j = 1; j < sz; j++) {
ByteBuffer hunkLine = hunkLines.get(j);
if (!hunkLine.hasRemaining()) {
// Completely empty line; accept as empty context line
applyAt++;
continue;
}
switch (hunkLine.array()[hunkLine.position()]) {
case ' ':
applyAt++;
@ -676,8 +681,7 @@ && canApplyAt(hunkLines, newLines, 0)) {
// Must be the marker for the final newline
break;
}
out.write(line.array(), line.position(),
line.limit() - line.position());
out.write(line.array(), line.position(), line.remaining());
if (l.hasNext()) {
out.write('\n');
}
@ -703,6 +707,14 @@ private boolean canApplyAt(List<ByteBuffer> hunkLines,
int pos = line;
for (int j = 1; j < sz; j++) {
ByteBuffer hunkLine = hunkLines.get(j);
if (!hunkLine.hasRemaining()) {
// Empty line. Accept as empty context line.
if (pos >= limit || newLines.get(pos).hasRemaining()) {
return false;
}
pos++;
continue;
}
switch (hunkLine.array()[hunkLine.position()]) {
case ' ':
case '-':