From 200eeffeee2fb535b326fb2a7e17d87b49ab83ea Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 2 Mar 2025 11:21:14 +0100 Subject: [PATCH] sample_prompts/acidhub: fix regression showing all branches Fix the accidental "git branch" output leaking while making sure we support: 1. unborn branch, where HEAD does not exist (`git init`) 2. detached head (`git checkout --detach`) Notably computing the branch name should be independent of computing a diff against HEAD. In scenario 1 there is a branch but no HEAD, while in scenario 2 it's the other way round. Hence we need a separate check to see if we're in a git repo. "git rev-parse" seems to work. Not sure what's best pracitce. Also remove the ahead/behind logic, it was broken because it misspelled @{upstream}. Fixes #11179 (cherry picked from commit 7b7e7443538a741474c33c8d8c2d6b0f4c950001) --- .../web_config/sample_prompts/acidhub.fish | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/share/tools/web_config/sample_prompts/acidhub.fish b/share/tools/web_config/sample_prompts/acidhub.fish index 61a5078d2..1a692209e 100644 --- a/share/tools/web_config/sample_prompts/acidhub.fish +++ b/share/tools/web_config/sample_prompts/acidhub.fish @@ -5,18 +5,15 @@ function fish_prompt -d "Write out the prompt" set -l laststatus $status set -l git_info - if set -l git_branch (command git branch --format=%\(refname:lstrip=2\) 2> /dev/null) + if git rev-parse 2>/dev/null + set -l git_branch ( + { command git symbolic-ref HEAD 2>/dev/null || echo - } | + string replace refs/heads/ '') + set git_branch (set_color -o blue)"$git_branch" set -l git_status - if test -n "$git_branch"; and not command git diff-index --quiet HEAD -- - if set -l count (command git rev-list --count --left-right $upstream...HEAD 2>/dev/null) - echo $count | read -l ahead behind - if test "$ahead" -gt 0 - set git_status "$git_status"(set_color red)⬆ - end - if test "$behind" -gt 0 - set git_status "$git_status"(set_color red)⬇ - end - end + if git rev-parse --quiet --verify HEAD >/dev/null + and not command git diff-index --quiet HEAD -- + for i in (git status --porcelain | string sub -l 2 | sort | uniq) switch $i case "." @@ -35,11 +32,8 @@ function fish_prompt -d "Write out the prompt" end else set git_status (set_color green): - if test -z "$git_branch" - set git_branch - - end end - set git_info "(git$git_status"(set_color -o blue)"$git_branch"(set_color white)")" + set git_info "(git$git_status$git_branch"(set_color white)")" end # Disable PWD shortening by default.