Be Genius


Bo Jeanes

I am an software engineer who has lived and worked in New York, Brisbane, Chicago, San Francisco, and now Melbourne. I primarily work in Ruby though am a functional programmer at heart and a fan of programming languages in general. In particular, I love Rust and Clojure but keep my eye on many langauges all the time.

Fish Shell version of __git_ps1 Function That is Bundled with Git

Today at ActionHack, I was showing off Fish Shell to "`geoffreyd": I was showing off all the cool fish prompt features I had, but pointed out that my git branch portion needed some beefing up, as it didn't show the current mode nor commit shas when not in a branch.

Knowing that git came with a `_*git*ps1 ()@ function for Bash that achieves this, I decided to port it to Fish tonight. My shell scripting fu is pretty weak but as far as I can tell it works great and I am now using it in my shell.

Here is the Fish function (I’ve kept the function name the same as the bash one):

function __git_ps1
  set -l g (git rev-parse --git-dir ^/dev/null)
  if [ -n "$g" ]
    set -l r ""
    set -l b ""

    if [ -d "$g/../.dotest" ]
      if [ -f "$g/../.dotest/rebasing" ]
        set r "|REBASE"
      elseif [ -f "$g/../.dotest/applying" ]
        set r "|AM"
        set r "|AM/REBASE"

      set b (git symbolic-ref HEAD ^/dev/null)
    elseif [ -f "$g/.dotest-merge/interactive" ]
      set r "|REBASE-i"
      set b (cat "$g/.dotest-merge/head-name")
    elseif [ -d "$g/.dotest-merge" ]
      set r "|REBASE-m"
      set b (cat "$g/.dotest-merge/head-name")
    elseif [ -f "$g/MERGE_HEAD" ]
      set r "|MERGING"
      set b (git symbolic-ref HEAD ^/dev/null)
      if [ -f "$g/BISECT_LOG" ]
        set r "|BISECTING"

      set b (git symbolic-ref HEAD ^/dev/null)
      if [ -z $b ]
        set b (git describe --exact-match HEAD ^/dev/null)
        if [ -z $b ]
          set b (cut -c1-7 "$g/HEAD")
          set b "$b..."

    if not test $argv
        set argv " (%s)"

    set b (echo $b | sed -e 's|^refs/heads/||')

    printf $argv "$b$r" ^/dev/null

I have also pushed it to my dot-files repository here.

Enjoy :)

UPDATE: Here it is in action:

__git_ps1 for Fish


  1. I made an updated version of the __git_ps1 function that works with fish 2 here:

    by Niklas Hambüchen on

You need to login with GitHub in order to comment.