Skip to content

Diff created with java-diff-utils cannot be applied with git apply if file is shorter than context #119

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

Closed
lgrammel opened this issue Apr 4, 2021 · 2 comments

Comments

@lgrammel
Copy link

lgrammel commented Apr 4, 2021

Describe the bug
Diff cannot be applied with git apply

To Reproduce

  1. original content
const world: string = 'world',
      p: number | undefined = 42;

console.log(`Hello, ${world}!`);
  1. new content
const world: string = 'world';
const p: number | undefined = 42;

console.log(`Hello, ${world}!`);
  1. code to create diff (Kotlin)
/**
 * @param contextSize
 *        How many lines of context around each changed line should be included.
 */
class DiffFactory(private val contextSize: Int) {

    companion object {

        private const val LINE_SEPARATOR = "\n"
    }

    /**
     * Creates a git-apply compatible diff.
     */
    fun computeDiff(
        filename: String,
        originalContent: String,
        newContent: String
    ): String = UnifiedDiffUtils.generateUnifiedDiff(
        "a/$filename", // a/ and b/ required for git-apply compatibility
        "b/$filename",
        originalContent.split(LINE_SEPARATOR),
        DiffUtils.diff(
            originalContent, newContent,
            NullDiffAlgorithmListener
        ),
        contextSize
    ).joinToString(LINE_SEPARATOR)
}
  1. With contextSize = 3 the following diff gets created:
--- a/examples/unchain-variable-declaration.js
+++ b/examples/unchain-variable-declaration.js
@@ -1,5 +1,5 @@
-const world: string = 'world',
-      p: number | undefined = 42;
+const world: string = 'world';
+const p: number | undefined = 42;
 
 console.log(`Hello, ${world}!`);
 

(important: line with 1 space at the end - I've also added an empty newline to support git apply call from file instead of system in)
5. git apply fails

> git apply example.diff
error: patch failed: examples/unchain-variable-declaration.js:1
error: examples/unchain-variable-declaration.js: patch does not apply

Expected behavior
Git apply works with the following patch:

--- a/examples/unchain-variable-declaration.js
+++ b/examples/unchain-variable-declaration.js
@@ -1,4 +1,4 @@
-const world: string = 'world',
-      p: number | undefined = 42;
+const world: string = 'world';
+const p: number | undefined = 42;
 
 console.log(`Hello, ${world}!`);

The difference is that the index is limited to 1,4 (original input is 4 lines long).

System

  • Kotlin 1.4
@wumpz wumpz pinned this issue Apr 12, 2021
@wumpz
Copy link
Collaborator

wumpz commented Apr 28, 2021

I created a test from your sources and I got the correct result (GenerateunifiedDiffTest.testWrongContextLength):

--- a/$filename
+++ b/$filename
@@ -1,4 +1,4 @@
-const world: string = 'world',
-      p: number | undefined = 42;
+const world: string = 'world';
+const p: number | undefined = 42;
 
 console.log(`Hello, ${world}!`);

So I must assume, there is something different in your setup.

@wumpz wumpz closed this as completed in 94674a0 Apr 28, 2021
@wumpz wumpz unpinned this issue Apr 28, 2021
@lgrammel
Copy link
Author

Thanks for taking a look. I'll see if I can find out what is different in my setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants