Hacker News new | ask | show | jobs
by thamer 979 days ago
I use conditional includes for this, but I also add a single letter describing which Git identity I'm currently using to my PS1 so that it appears before $ in my shell prompt. This prevents me from committing code with the wrong identity, in case I'm using a git checkout that's anywhere not covered by the conditional include rules.

I use Starship (https://starship.rs) to manage my prompt, and wrote a short script that only runs if I'm somewhere in a git repo, and if so finds my Git user's email and looks up the corresponding letter in an associative array declared in my ~/.config/starship-zsh/.zshenv:

    git_email=$(git config --get user.email | perl -pe 'chomp if eof')
    letter=$STARSHIP_GIT_USERS[$git_email]
    echo -n $letter

It's installed like this:

    [custom.git_user]
    command = "ZDOTDIR=~/.config/starship-zsh /path/to/the-script-above.sh" # set ZDOTDIR, makes zsh load the .zshenv file
    when = "git rev-parse --git-dir >/dev/null 2>/dev/null" # only run if we're in a git repo
    format = ' $output'

The .zshenv file contains the STARSHIP_GIT_USERS variable and its values, with a `declare -A` since it's an associative array.