Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cygwin git passing wrong path to my editor for commit messages

Tags:

git

cygwin

I'm using git under cygwin but it's not passing the correct path to my editor for commit messages.

I'm working in a test repository located on my drive at:

d:\X\git\myproject

in the cygwin terminal it shows this path as:

/cygdrive/d/X/git/myproject

When I commit without the -m flag (so that it opens up my editor for me to enter a message), my editor tries to write a file to the wrong path:

d:\cygdrive\d\x\git\myproject\.git\commit_editmsg

note the addition of "\cygdrive\d"

How can I make git pass the windows path (rather than the cygwin/unix path) to my editor?

like image 382
Abby Fichtner Avatar asked Aug 18 '15 20:08

Abby Fichtner


People also ask

How do I change the default git editor?

The command to do this is git config --global core. editor "nano" . You can change the highlighted section with your editor of choice!

How do I save and close git editor?

If you want to save your changes and quit, press Esc then type :wq and hit Enter or ↵ or on Macs, Return .


3 Answers

cygwin has a utility called cygpath which can be used to convert between cygwin and native Windows file paths. For instance:

$ cygpath --windows /cygdrive/d/X/git/myproject
D:\X\git\myproject

We're going to create a script which uses this utility to convert the path before passing it to your editor. We'll use emacs as an example, assuming it is installed at C:\emacs. Create a file called ~/bin/git-editor.sh:

#!/bin/sh
/cygdrive/c/emacs/bin/emacsclientw.exe $(cygpath --windows "${1}")

(since this is Windows, we don't need to set the executable flag on this file)

Now, set your git editor to point to this script:

$ git config --global core.editor "~/bin/git-editor.sh"
like image 102
itchimus Avatar answered Oct 24 '22 10:10

itchimus


#!/bin/dash -e
if [ "$1" ]
then k=$(cygpath -w "$1")
elif [ "$#" != 0 ]
then k=
fi
Notepad2 ${k+"$k"}
  1. If no path, pass no path

  2. If path is empty, pass empty path

  3. If path is not empty, convert to Windows format.

Then I set these variables:

export EDITOR=notepad2.sh
export GIT_EDITOR='dash /usr/local/bin/notepad2.sh'
  1. EDITOR allows script to work with Git

  2. GIT_EDITOR allows script to work with Hub commands

Source

like image 41
Zombo Avatar answered Oct 24 '22 10:10

Zombo


I had the same problem. Cygwin git would not pass the correct path to sublime 3 (It would not include the cygwin64 folder) I created the git-editor.sh folder, this is what I put in it:

#!/bin/sh
 /cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe $(cygpath --windows "${1}") -w

Then made this .sh file my core.editor

like image 1
Felipe C. Avatar answered Oct 24 '22 10:10

Felipe C.