dotfiles/config/fish/functions/prompt_vcs.fish (view raw)
| 1 | function prompt_vcs |
| 2 | set -l dir $PWD |
| 3 | set -l repo_root "" |
| 4 | set -l vcs_type "" |
| 5 | set -l depth 0 |
| 6 | |
| 7 | while test "$dir" != "/" -a $depth -lt 20 |
| 8 | if test -d "$dir/.jj" |
| 9 | set vcs_type jj |
| 10 | set repo_root $dir |
| 11 | break |
| 12 | end |
| 13 | if test -d "$dir/.git" |
| 14 | set vcs_type git |
| 15 | set repo_root $dir |
| 16 | break |
| 17 | end |
| 18 | set dir (path dirname $dir) |
| 19 | set depth (math $depth + 1) |
| 20 | end |
| 21 | |
| 22 | test -n "$vcs_type"; or return |
| 23 | |
| 24 | if test "$vcs_type" = jj; and command -sq jj |
| 25 | set -l head (jj log --no-graph -r "@" -T "change_id.shortest()" 2>/dev/null) |
| 26 | printf ' %s(jj: %s%s%s)%s' (set_color normal) (set_color blue) $head (set_color normal) (set_color normal) |
| 27 | return |
| 28 | end |
| 29 | |
| 30 | # git |
| 31 | set -l git_dir "$repo_root/.git" |
| 32 | if test -f "$git_dir" |
| 33 | read -l gitdir_line < "$git_dir" |
| 34 | set git_dir (string replace "gitdir: " "" -- $gitdir_line) |
| 35 | end |
| 36 | |
| 37 | set -l head_file "$git_dir/HEAD" |
| 38 | test -f "$head_file"; or return |
| 39 | |
| 40 | read -l head_content < "$head_file" |
| 41 | |
| 42 | if string match -qr '^ref: refs/heads/' -- $head_content |
| 43 | set -l branch (string replace 'ref: refs/heads/' '' -- $head_content) |
| 44 | printf ' %s(%s%s%s)%s' (set_color normal) (set_color blue) $branch (set_color normal) (set_color normal) |
| 45 | else |
| 46 | printf ' %s(%s%s%s)%s' (set_color normal) (set_color blue) (string sub -l 7 -- $head_content) (set_color normal) (set_color normal) |
| 47 | end |
| 48 | end |