Overriding the custom prompt
To overwrite the default bash prompt string, also known as PS1(prompt string one), create a file name at ~/.config/git/git-prompt.sh
e.g. C:/Users/ItsMe/.config/git/git-prompt.sh
You'll need to set a variable named PS1. Here's a very simple example of a custom prompt of "hello" followed by a newline and the classic '$' symbol:
PS1="hello\n$ "
Adding the repo detail
In my script I've written a function to get the repo text - this makes it easier to get dynamic data so the prompt will update when you move into a new repo.
getrepo()
{
INREPO=$(git rev-parse --is-inside-work-tree 2>/dev/null)
if [ "$INREPO" == "true" ]
then
TOPPATH=$(git rev-parse --show-toplevel)
TOPDIR=$(basename "$TOPPATH")
echo "$TOPDIR"
else
echo "not a git repo"
fi
}
PS1='`getrepo` '"\n$"
Here we're using "git rev-parse --is-inside-work-tree" to discover if the user is in a git repo at all. "2>/dev/null" redirects any error messages that would show when the user is not in a git repo.
If the user is in a git repo then we use "git rev-parse --show-toplevel" to collect the path of the current repo and set it to TOPPATH e.g. "C:\Users\MyName\Documents\my_repo"
Then we use basename to get the last section of the path in TOPPATH and set it to TOPDIR e.g. "my_repo"
Finally, we set PS1 to use our getrepo function
Custom colours for repos
My workplace uses a good many repos and it's sometimes easy to get my current repo confused. To solve this I want my repo string to have bold colours unique to each repo.
I do this by adding colour codes to my getrepo function. Find colour codes and more formatting here.
getrepo()
{
PINKPLE='\033[35m'
BLUEBG='\033[48;5;63;38;5;15m'
RESET='\033[0m'
INREPO=$(git rev-parse --is-inside-work-tree 2>/dev/null)
if [ "$INREPO" == "true" ]
then
TOPPATH=$(git rev-parse --show-toplevel)
TOPDIR=$(basename "$TOPPATH")
if [ $TOPDIR == "my_repo" ]
then
KMBLUE='\033[48;5;63;38;5;15m'
echo -e "$KMBLUE""MY REPO""$RESET"
else
echo -e $PINKPLE"$TOPDIR""$RESET"
fi
else
echo -e $PINKPLE"not a git repo""$RESET"
fi
}
In this addition we query the TOPDIR variable, and if it is "my_repo" we add a colour code to make its background blue.
Note the "-e" argument on the echos, this is required to make the colours show. Otherwise the colour code will simply show up as text.
Now the default colour used is a pink/purple colour, but when the user enters the my_repo git folder it changes to a blueish background. Now I'll never mistake which repo I'm in.
Finish your prompt
You'll probably want more than just your repo listed in the prompt. Now's the time to add the rest.
I found it useful to copy bits from the default git-prompt.sh which for windows users can be found here "C:\Program Files\Git\mingw64\share\git\completion\git-prompt.sh"
If it's useful: my full prompt file:
getrepo()
{
INREPO=$(git rev-parse --is-inside-work-tree 2>/dev/null)
PINKPLE='\033[35m'
if [ "$INREPO" == "true" ]
then
TOPPATH=$(git rev-parse --show-toplevel)
TOPDIR=$(basename "$TOPPATH")
RESET='\033[0m'
if [ $TOPDIR == "work_studio" ]
then
PINKBG='\033[48;5;125;38;5;230m'
echo -e "$PINKBG""WORK STUDIO""$RESET"
elif [ $TOPDIR == "work_modules" ]
then
BLUEBG='\033[48;5;63;38;5;15m'
echo -e "$BLUEBG""WORK_MODULES""$RESET"
else
echo -e "$PINKPLE""$TOPDIR""$RESET"
fi
else
echo -e "$PINKPLE""not a git repo""$RESET"
fi
}
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[32m\]' # change to green
PS1="$PS1"'\u@\h ' # user@host<space>
PS1="$PS1"'`getrepo` ' # show repo name. Well, the top folder name..
PS1="$PS1"'\[\033[33m\]' # change to yellow
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 -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
PS1="$PS1"'\[\033[36m\]' # change color to cyan
PS1="$PS1"'`__git_ps1`' # bash function
fi
fi
PS1="$PS1"'\[\033[0m\]' # change color
PS1="$PS1"'\n' # new line
PS1="$PS1"'$ ' # prompt: always $
No comments:
Post a Comment