Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
@lgrammel

Description

@lgrammel

Describe the bug
Diff cannot be applied withgit apply

To Reproduce

  1. original content
constworld:string='world',p:number|undefined=42;console.log(`Hello,${world}!`);
  1. new content
constworld:string='world';constp: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.*/classDiffFactory(privatevalcontextSize:Int) {companionobject {privateconstvalLINE_SEPARATOR="\n"    }/**     * Creates a git-apply compatible diff.*/funcomputeDiff(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. WithcontextSize = 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.differror: patch failed: examples/unchain-variable-declaration.js:1error: 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp