tpope / vim-fugitive

fugitive.vim: A Git wrapper so awesome, it should be illegal
https://www.vim.org/scripts/script.php?script_id=2975
19.82k stars 1k forks source link

Partial staging does not work if patch contents contains end of a file with no newline #2327

Open zivarah opened 1 month ago

zivarah commented 1 month ago

This looks to be the same issue as #1609, but I'm opening a new one since that one is marked as completed.

Description

If a file contains no newline at the end of the file, selective staging of individual lines fails with patch does not apply so long as the end of the file appears in the patch.

It looks like this was also an issue in magit at some point, see https://github.com/magit/magit/issues/1139. However, adding "--ignore-space-change" to cmd in StageApply does not seem to address this issue for me so it looks like there's more to the fix.

Steps to reproduce

  1. Init the test repo and set up the problematic file:

    git init
    
    echo -n 'line 1
    line 2
    line 3' > a
    git add a
    git commit -m a
    
    echo 'line 1
    line 2
    line 2.2
    line 3' > a
  2. Open the status buffer:

    Head: main
    
    Unstaged (1)
    M a
    @@ -1,3 +1,4 @@
     line 1
     line 2
    -line 3
    \ No newline at end of file
    +line 2.2
    +line 3
  3. Try to stage only the "line 2.2" line (visually select and press s):

    fugitive: error: patch failed: a:1 error: a: patch does not apply

Details

The cmd being executed is:

['apply', '-p0', '--recount', '--cached', '--', '<patch file>']

The content of that patch file is:

diff --git a a
index 8cf2f17..58c1de1 100644
--- a
+++ a
@@ -1,3 +1,4 @@
 line 1
 line 2
 line 3
+line 2.2

Specs/setup

OS: MacOS Sonoma 14.5 (also reproduced in Windows 11) Fugitive: 0444df6 (Use nvim vim.ui.open as a fallback, 2024-07-18) Git: v2.45.2 Vim: Neovim v0.10.0