9

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?

Abby Fichtner
  • 2,598
  • 2
  • 17
  • 20
  • Interesting question- Cygwin's path handling has vexed others as well as myself! May I assume you're trying to open a Windows based editor as opposed to a Linux-ish terminal editor? I've not had problems with the latter. – rholmes Aug 18 '15 at 21:36
  • @rholmes - I am. It's an emacs clone for windows called Epsilon. After posting this, I found that epsilon actually has a setting to handle cygwin style paths, which thankfully lets me use it! But am still really curious how you'd do this in other editors... – Abby Fichtner Aug 19 '15 at 04:32

3 Answers3

8

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"
itchimus
  • 96
  • 1
  • 4
  • oh sweet, super helpful. Thank you! – Abby Fichtner Oct 23 '15 at 23:15
  • Figured out how to do this using sublime editor and not having to resort to external scripts. `git config --global core.editor '/cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe --new-window --wait \`cygpath --windows ${1}\` && set -` – smac89 Apr 25 '16 at 23:13
  • In case it wasn't obvious, you can test all of the above is working by typing git config -e. If your config file is opened in your editor of choice you are good to go. – Jason D May 09 '23 at 20:25
2
#!/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

Zombo
  • 1
  • 62
  • 391
  • 407
1

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

Felipe C.
  • 171
  • 1
  • 5