dandavison / delta

A syntax-highlighting pager for git, diff, grep, and blame output
https://dandavison.github.io/delta/
MIT License
21.34k stars 359 forks source link

🐛 Delta as `diffFilter` breaks keyboard input with `git add -p` in Git CMD #1650

Open imbrish opened 3 months ago

imbrish commented 3 months ago

With the following configuration in git config:

[interactive]
    diffFilter = delta --color-only
[alias]
    ap = "!git add -p"

The keyboard input does not work at all when git ap asks:

(1/1) Stage this hunk [y,n,q,a,d,e,?]?

Even Ctrl + C does nothing, the only way to stop is with Ctrl + Z.

This happens when using git-cmd but not when using git-bash.

The keyboard works normally with either of the changes below:

[interactive]
    diffFilter = diff-so-fancy --patch
[interactive]
    diffFilter = delta --color-only | cat
[alias]
    ap = add -p

This seems like some race condition. Not sure if the problem is specific to delta, but then diff-so-fancy works fine.

Is there something else I could try to debug this?

hintron commented 3 months ago

Same here, when I start Git Bash under Windows Terminal 1.19.10573.0. I have Delta 0.16.5 (also happens with 0.17.0) and Git Bash 2.40.1.1. However, when I run git-cmd or git-bash directly, and not under Windows Terminal, I don't hit the issue.

hintron commented 3 months ago

When I upgraded to Git 2.44.0.1, it mostly behaves the same, except if I hold ctrl and hit enter, I can get it to re-display the prompt, and sometimes a ctrl-C goes through. Also, the line number vertical bars are replaced with Γï« and Γöé (but they show up normal as : and | when I do git diff standalone).

BTW my .gitconfig contains

[core]
        pager = delta

[interactive]
        diffFilter = delta --color-only

[delta]
        features = mgh_decorations
        line-numbers = true
        whitespace-error-style = 22 reverse

[delta "mgh_decorations"]
        commit-decoration-style = bold green box
        file-style = bold yellow
        file-decoration-style = bold yellow ul
        hunk-header-decoration-style = purple box
Bouska commented 3 months ago

I have also the same issue (except is replaced by Ôï« and by Ôöé) using delta 0.17.0 (copied from MSYS2/MINGW64), Windows Terminal 1.19.10573.0 and git 2.44.0.windows.1 (Git Bash). But using the same configuration with git version 2.44.0 (from MSYS2/MINGW64) works as expected. So I guess the git exe from Git Bash doesn't like the delta exe (copied from MSYS2/MINGW64) when executed under Windows Terminal (really a weird race condition)

Bouska commented 3 months ago

After some investigation, I'm not so sure this is a bug in delta. Under the hood, git calls an equivalent to git diff-files --color --ignore-submodules=dirty -p -- | delta --color-only and this command works perfectly fine.

imbrish commented 3 months ago

I am not sure your problem is the same as mine, it seems like some encoding issue. Have you tried running chcp 65001 (or chcp.com 65001) prior to the command you have trouble with?

I have this in my .bashrc and have not seen any such problems, also with the same git version you reference.

Bouska commented 3 months ago

I still think we have the same problem, the main issue I have is that (1/1) Stage this hunk [y,n,q,a,d,e,?]? doesn't accept any input, ctrl-c doesn't work either, only ctrl-z works (and the next hunk is shown before I get the prompt back). The encoding issue seems to be only a side effect.

Bouska commented 3 months ago

@imbrish chcp.com 65001 fixes my encoding issue 👍 but still have the main issue

imbrish commented 3 months ago

Glad it helped 👍

I think the broken keyboard input is mostly a problem with Git for Windows and how it interacts with Command Prompt or Terminal. Actually, it seems it is not the input that is broken, but the output, because I am still able to go through the git add --patch in blind, and the changes do get staged. This is made even more apparent when using ctrl + enter.

I have also experienced similar issues before I started to use Delta. For example, using a shell command (with !) for textconv would break mouse scrolling in diffs paged with less. I am not entirely sure what happens there, but it seems to have something to do with registering of mouse and keyboard hooks.

Anyway, I have also tried the following in git config:

[interactive]
    diffFilter = delta --color-only --paging=never

But it does not change anything, the problem is exactly the same as originally described. Only piping through cat helps. I have no clue why that is.

@dandavison possibly you could point us where to look? What exactly does delta do differently when its output is piped and when it is not? I would imagine delta --color-only --paging=never should be equivalent to delta --color-only | cat, but apparently it is not. May it be that git detects pipe in the diffFilter, and then runs it differently?

Bouska commented 3 months ago

Here is the Git code that interest us: add-patch.c#L403 So basically what git does in our case is:

As I said before, git diff-files --color --ignore-submodules=dirty -p -- | delta --color-only works fine, so I'm guessing its finding the equivalent hunks in the colored buffer that triggers the issue.

imbrish commented 3 months ago

Investigating at the source 😉

If the problem lies in matching the plain and colored hunks, then why would ... | cat fix it? The output stays the same.

I wonder if this works only in Command Prompt. Have you tried diffFilter = delta --color-only | cat in Windows Terminal?

Bouska commented 3 months ago

I tried diffFilter = delta --color-only | cat yesterday and it would still freeze, but I tried today and it works (but not exactly in the same conditions) 🤷

adriencarbonaro commented 2 months ago

Hello, I also have the same issue. I think it is a git for windows or windows terminal issue, as git bash works as intended. I also tested with same version of delta on another (older windows) computer and it works. The only change is the version of windows itself (10 vs 11) and git for windows (2.30 vs 2.44)

stefan-muc commented 1 month ago

I have a somehow related issue: I experience the freeze on git add -p with VSCode/VSCodium Git bash (MINGW64) terminal, but not in native Git CMD/Bash started through start menu / context menu in explorer. Can confirm imbrish discovery that if I continue "blind" the changes get indeed added.

Versions: VSCode/VSCodium 1.89.1 (system setup) Git: git version 2.45.1.windows.1 Windows: Win 10 22H2 Build 19045.4412 (VSCode and VSCodium), Win 11 23H2 Build 22631.3593 (only VSCode tested) .gitconfig: Default example as in Get Started - without delta there's no freeze.