I'm trying to edit my git bash terminal on Windows in the C:/Program Files/Git/etc/profile.d/git-prompt.sh
directory and I want to check if my current working directory is a Git repository.
Below is my current code:
if test -f /etc/profile.d/git-sdk.sh
then
TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
TITLEPREFIX=$MSYSTEM
fi
if test -f ~/.config/git/git-prompt.sh
then
. ~/.config/git/git-prompt.sh
else
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\[\033[1;32m\]' # change to green
PS1="$PS1"'➜ ' # arrow
PS1="$PS1"'\[\033[1;36m\]' # change color to bold cyan
PS1="$PS1"'\W' # current working directory
if test -z "$WINELOADERNOEXEC"
then
GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
# if test -z "$COMPLETION_PATH/git-prompt.sh"
# then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
# if test -n `git branch --show-current`
# then
PS1="$PS1"'\[\033[1;34m\]' # change color to cyan
PS1="$PS1"' git:(' # bash function
PS1="$PS1"'\[\033[1;31m\]' # change color to cyan
PS1="$PS1"'`git branch --show-current`' # get current working branch
PS1="$PS1"'\[\033[1;34m\]' # change color to bold blue
PS1="$PS1"') ' # close brackets
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[1;33m\]' # change color to bold yellow
PS1="$PS1"'$ ' # prompt: always $
PS1="$PS1"'\[\033[0m\]' # change color to default
# fi
# fi
fi
fi
MSYS2_PS1="$PS1" # for detection by MSYS2 SDK's bash.basrc
# Evaluate all user-specific Bash completion scripts (if any)
if test -z "$WINELOADERNOEXEC"
then
for c in "$HOME"/bash_completion.d/*.bash
do
# Handle absence of any scripts (or the folder) gracefully
test ! -f "$c" ||
. "$c"
done
fi
I'm trying to create a conditional statement to test if my current directory is a Git repository and if so, display the correct format.
If I open my terminal in a Git repository:
➜ test-repo git:(main)
$
If I open my terminal without Git repository:
fatal: not a git repository (or any of the parent directories): .git
➜ test-repo git:()
$
The intension is to remove fatal: not a git repository (or any of the parent directories): .git
and git:()
if I'm not in a Git repository.
CodePudding user response:
How about
if git status -s 1>&2 2>/dev/null
then
: You are in a git repo
else
: You are not
fi
UPDATE : A faster alternative to git status -s
would be
git rev-parse -q --short HEAD