简体   繁体   中英

retrieve the path of a repo from the “git bash” from windows with right format

I am trying to retrieve the path of a repository from the "git bash" shell in windows, as follows

user@CND7293ZVV MINGW64 /c/Work/git/repository/subfolder (master)
$ git rev-parse --show-toplevel
C:/Work/git/repository

The problem is that I want to use the ouptut path to use it from a bash script, but it is not in bash format. I want to get /c/Work/git/repository . Is there any way to get the path in a way that can be used directly in the git bash shell?

extra information: The target is to store that path in a variable to be used inside a bash script, independently of whether I am running bash from a linux terminal, or when I am running from the git bash.


Update :

To be able to use the command inside the git-bash environment, and also inside native linux bash, we can use the following:

REPODIR=$(git rev-parse --show-toplevel) 
# If we are inside mingw* environment, then we update the path to proper format
if [[ $(uname) == MINGW* ]] ; then REPODIR=$(cygpath -u "${REPODIR}"); fi; echo ${REPODIR}

When running on the Git bash command line, you have an buildin utility with the name cygpath that does this:

$ cygpath -u "C:/Work/git/repository"
/c/Work/git/repository

This can be combined with your existing command to do everything on 1 line:

$ cygpath -u "$(git rev-parse --show-toplevel)"
/c/Work/git/repository

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM