Search code examples
windowsgitcygwin

How to add split Git diff hunk from cygwin using Windows editor


I need to split a hunk while doing git add -p. But the e option during the interaction doesn't work. I will explain why it doesn't work and inquire what I could do to make it work.

So my hunk might look like the code below. From other research I have learned that I should use the e option, and then my editor will come up with the hunk's lines, and I can delete the ones I don't want.

+ virtual bool fat() const {
+    return true;
+ }
+ virtual bool lazy() const {
+    return true;
+ }

Clearly I would want to commit just the fat method and leave the lazy method for another commit.

The problem is that I'm running Git from Cygwin and I have editor set as follows:

export EDITOR=/cygdrive/c/csm/notepad2/Notepad2.exe

By the way when I do commit messages this starts the editor perfectly.

When I inspect the editor's commandline it looks like

c:\csm\notepad2\Notepad2.exe /home/csm/path/to/my/code/.git/addp-hunk-edit.diff

Clearly this is one of those times when Windows and Cygwin are mixing like two non-mixy things, but I would like a solution that does bridge this gap, rather than having to use some Cygwin-based editor.

I believe the question cygwin and windows git - path confusion is opposite to mine because I use the Git that comes with Cygwin and I want to use a Windows helper tool with it, while that question seeks to integrate a Git that is native to Windows with a Cygwin shell environment.


Solution

  • I would recommend to set Git's editor instead of global system's editor, but it should work for both:

    git config --global core.editor ~/bin/git-editor.sh
    

    and create ~/bin/git-editor.sh containing something like:

    /cygdrive/c/csm/notepad2/Notepad2.exe "`cygpath -w -a \"$*\"`"
    

    cygpath converts paths between Windows and Unix format.

    • -w - convert from Unix to Windows format
    • -a - output absolute path