0

I'm using gitbash as my main console on windows. Often I want to copy in a filename from explorer and use it for git commands, this often doesn't work as git sometimes expects filenames in the form /c/directory/directory/filename rather than c:\directory\directory\filename.

I usually have to go in an manually edit the filename/path to be in the correct format, which is anoying if the file is many directories deep.

I've written a sed command to modify the string to be of the correct format:

sed 's:\\:/:g' | sed 's:\(.\)::/\1:g'

Is there a way I can make this a function or something so that I can do something like...

git add convert("c:\blah\blah\myfile.txt")

and what actually gets run is

git add /c/blah/blah/myfile.txt
dan
  • 1,525
  • 1
  • 17
  • 35
  • possible duplicate of [Windows PATH to posix path conversion in bash](http://stackoverflow.com/questions/13701218/windows-path-to-posix-path-conversion-in-bash) – anishsane Jun 03 '14 at 12:48
  • It's not a duplicate of that question as I'm asking for a way to call the sed command from bash in a nice way. I'm not asking for a sed command like that question was – dan Jun 03 '14 at 13:12
  • [Opposite of this - convert bash path to windows](http://stackoverflow.com/questions/12015348/msys-path-conversion-or-cygpath-for-msys) – laggingreflex Apr 03 '15 at 18:37

2 Answers2

2

One way:

function git_add {
    git add "$(exec sed 's|\\|/|g; s|\(.\):|/\1|g' <<< "$1")"
}

and do:

git_add "c:\blah\blah\myfile.txt"

another way:

function convert {
    sed 's|\\|/|g; s|\(.\):|/\1|g' <<< "$1"
}

git add "$(convert "c:\blah\blah\myfile.txt")"
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • I want to do it for all things on command line, not just git, so would want something a bit more generic – dan Jun 03 '14 at 12:23
0

cygpath is in the gitbash distribution, so I use cygpath -u "c:\foo.xml"

I am currently running gitbash 2.31.1 on Win10

Bob Makowski
  • 305
  • 2
  • 8