News for hackers. Stuff that matters.
Re:Bash PS1 Generator by MichaelMoser123 on Thursday June 24, 2021 @09:15AM (#27616949)
current git branch is missing (helps me to avoid working on the wrong branch). (tried to do it with colors, but things got too messy). I never managed to work with colors in bash propmts; they look different on each environment.

  parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
  }

  PS1="[\u@\h \W\$(parse_git_branch)]\$ "
Re:Bash PS1 Generator by sirn on Thursday June 24, 2021 @09:53AM (#27617410)
You might want to use `git rev-parse --abbrev-ref HEAD` or `git name-rev --name-only HEAD` instead of `git branch`. Plumbing commands (such as rev-parse or name-ref) are generally more interface-stable to use in a script than porcelain commands (such as git branch) which are designed for interactive use where output may change depending on user environment and configuration.
Re:Bash PS1 Generator by NegativeLatency on Thursday June 24, 2021 @11:25AM (#27618963)
I’ve heard this advice given before and initially followed it but ‘git branch’ and parsing the output works in a few cases (I don’t recall what they are at this time) that less hackey approaches don’t.
Re:Bash PS1 Generator by sirn on Thursday June 24, 2021 @12:45PM (#27620185)
I think that might be `git symbolic-ref` which doesn't work in detached HEAD state (e.g. when checking out a commit) whereas `git rev-parse --abbrev-ref` will print "HEAD". On the other hand, `git name-rev` will try to find the nearest named ref (including tags) and how far HEAD is from that named ref (e.g. master~1).

Internally, symbolic-ref resolve the ref in the same way as git branch, just without handling detached HEAD. __git_ps1 use a combination of rev-parse, symbolic-ref and git describe to provide a more interesting output for displaying state of the working directory.

To have the similar output for detached HEAD as git-status (e.g. "HEAD detached at fa0c4e1") then probably `git rev-parse --abbrev-rev` and `git rev-parse --short` should be used together. Unfortunately, while rev-parse can print multiple output in a single command, abbrev-rev and short cannot be used together. In such case, parsing `git status` might indeed be more suitable as it spawn only one executable instead of two.

Some porcelain commands do have --porcelain flag to ensure the stability of the interface, though (e.g. `git status --porcelain=v1`).

Re:Bash PS1 Generator by MichaelMoser123 on Thursday June 24, 2021 @10:10AM (#27617705)
makes sense. thanks!
Re:Bash PS1 Generator by ryenus on Thursday June 24, 2021 @11:48AM (#27619312)
Nowadays there's the `--show-current` option:

    git branch --show-current
See https://git-scm.com/docs/git-branch
Re:Bash PS1 Generator by paulmac_ie on Thursday June 24, 2021 @10:11AM (#27617735)
This is my prompt that shows my git branch, AWS_PROFILE etc:

    if [ "$color_prompt" = yes ]; then
        PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\ 
        [\033[0;36m\] @ \[\033[0;36m\]\h \w\ 
        [\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\ 
        [\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] 
        AWS:${AWS_PROFILE} ▶\[\033[0m\] '
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
Re:Bash PS1 Generator by res0nat0r on Thursday June 24, 2021 @12:17PM (#27619759)
This is the PS1 I've been using for a long time:

export PS1="\n\$(kube_ps1)\n\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;31m\]\$(__git_ps1)\n\$\[\033[00m\] "

Note it needs the git-prompt.sh script (usually comes with the bash-completion package), and the kube-ps1 script: https://github.com/jonmosco/kube-ps1

It will show you the current kubernetes profile and namespace, user@host, current directory and git branch if you're in a git repo.

Re:Bash PS1 Generator by dllthomas on Thursday June 24, 2021 @02:41PM (#27622047)
I recently added a count of stashes (in red) following the branch name, and I've been liking it.