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