From 1d4cb9fb190fb83d7b5a401f02fd2e7a115765af Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Tue, 10 Oct 2023 13:32:43 +0100 Subject: [PATCH] .bashrc: Re-order git prompt, use local ref for upstream prefetch status. --- .bashrc | 60 +++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/.bashrc b/.bashrc index d02b3d2..25cc59c 100644 --- a/.bashrc +++ b/.bashrc @@ -7,11 +7,6 @@ __prompt_git_status() { # $1 The printf format string for the prompt. Must include %s. # Environment variables: # GIT_PROMPT_SHOW_TYPE=1 Show type of repository (Bare, Shallow). - # GIT_PROMPT_SHOW_IGNORED=1 Show a ! if the current directory is ignored. - # GIT_PROMPT_SHOW_UNSTAGED=1 Show a * if there are unstaged changes (superceeded by above). - # GIT_PROMPT_SHOW_UNCOMMITTED=1 Show a & if there are staged but uncommitted changes (superceeded by above). - # GIT_PROMPT_SHOW_UNTRACKED=1 Show a ? if there are untracked files in the working directory (superceeded by above). - # GIT_PROMPT_SHOW_STASH=1 Show a $ if there is a stash in this repository (superceeded by above). # GIT_PROMPT_SHOW_UPSTREAM=1 Show status of this repository compaired to upstream: # ?? - No upstream set. # == - Working tree is equal to upstream. @@ -20,19 +15,23 @@ __prompt_git_status() { # << or 5 + # S:branch_name >5 * # Returns: Exit code of the command line before entering this function. # shellcheck disable=SC2155 - local COUNT GIT_PROMPT GIT_REPO_INFO PWD=$(pwd -P) RET="$?" + local BRANCH COUNT GIT_PROMPT GIT_REPO_INFO PWD=$(pwd -P) RET="$?" # Bail out if there's no format argument given. (( $# != 1 )) && return "$RET" # Get some repository information. # shellcheck disable=SC2207 - IFS=$'\n' GIT_REPO_INFO=( $(git rev-parse --show-toplevel --git-dir --is-bare-repository --is-shallow-repository --is-inside-git-dir --is-inside-work-tree \ - --glob=prefetch 2>/dev/null) ) + IFS=$'\n' GIT_REPO_INFO=( $(git rev-parse --show-toplevel --git-dir --is-bare-repository --is-shallow-repository --is-inside-git-dir --is-inside-work-tree 2>/dev/null) ) ERR="$?" # Do nothing if there's an error. @@ -55,38 +54,22 @@ __prompt_git_status() { # Add the branch if there have been commits. if [[ -e "${GIT_REPO_INFO[1]}/refs/heads/master" ]]; then - GIT_PROMPT+="$(git describe --contains --all HEAD)" + BRANCH="$(git describe --contains --all HEAD)" + GIT_PROMPT+="$BRANCH" else GIT_PROMPT+="!NO COMMITS!" fi - # Add a marker if directory is ignored, there's unstaged files, uncommitted changes, untracked files or a stash. - if [[ -n "$GIT_PROMPT_SHOW_IGNORED" ]] && git check-ignore "${PWD#"${GIT_REPO_INFO[0]}/"}" >/dev/null 2>&1 ; then - GIT_PROMPT+=" !" - elif [[ -n "$GIT_PROMPT_SHOW_UNSTAGED" ]] && git ls-files --modified --exclude-standard --directory --error-unmatch -- ':/*' >/dev/null 2>&1; then - GIT_PROMPT+=" *" - elif [[ -n "$GIT_PROMPT_SHOW_UNCOMMITTED" ]] && ! git diff --name-only --cached --exit-code >/dev/null 2>&1; then - GIT_PROMPT+=" &" - elif [[ -n "$GIT_PROMPT_SHOW_UNTRACKED" ]] && git ls-files --others --exclude-standard --directory --error-unmatch -- ':/*' >/dev/null 2>&1; then - GIT_PROMPT+=" ?" - elif [[ -n "$GIT_PROMPT_SHOW_STASH" ]] && git rev-parse --verify --quiet refs/stash >/dev/null; then - GIT_PROMPT+=" $" - fi - # Add upstream status. [[ -n "$GIT_PROMPT_SHOW_UPSTREAM" ]] || [[ -n "$GIT_PROMPT_SHOW_UPSTREAM_EXTENDED" ]] && { # Whether to run prefetch tasks. [[ -n "$GIT_PROMPT_AUTO_PREFETCH" ]] && { # Only prefetch remotes if in the top-level working directory and if the repository is configured for maintenance work. [[ "$PWD" == "${GIT_REPO_INFO[0]}" ]] && git config --global --get --fixed-value maintenance.repo "$PWD" >/dev/null 2>&1 && { - if git maintenance run --task=prefetch 2>/dev/null; then - GIT_REPO_INFO[6]="$(git rev-parse --glob=prefetch)" - else - printf "\\033[1;31m%s\\033[39m\\n" "Git maintenance 'prefetch' task failed." >&2 - fi + git maintenance run --task=prefetch 2>/dev/null || printf "\\033[1;31m%s\\033[39m\\n" "Git maintenance 'prefetch' task failed." >&2 } } - COUNT="$(git rev-list --count --left-right "${GIT_REPO_INFO[6]:-@{upstream\}}...HEAD" 2>/dev/null | tr '[:blank:]' ' ')" + COUNT="$(git rev-list --count --left-right "${BRANCH:+"refs/prefetch/remotes/origin/$BRANCH"}${BRANCH:-@{upstream\}}...HEAD" 2>/dev/null | tr '[:blank:]' ' ')" case "$COUNT" in "") # No upstream. @@ -122,6 +105,19 @@ __prompt_git_status() { ;; esac } + + # Add a marker if directory is ignored, there's unstaged files, uncommitted changes, untracked files or a stash. + if [[ -n "$GIT_PROMPT_SHOW_IGNORED" ]] && git check-ignore "${PWD#"${GIT_REPO_INFO[0]}/"}" >/dev/null 2>&1 ; then + GIT_PROMPT+=" !" + elif [[ -n "$GIT_PROMPT_SHOW_UNSTAGED" ]] && git ls-files --modified --exclude-standard --directory --error-unmatch -- ':/*' >/dev/null 2>&1; then + GIT_PROMPT+=" *" + elif [[ -n "$GIT_PROMPT_SHOW_UNCOMMITTED" ]] && ! git diff --name-only --cached --exit-code >/dev/null 2>&1; then + GIT_PROMPT+=" &" + elif [[ -n "$GIT_PROMPT_SHOW_UNTRACKED" ]] && git ls-files --others --exclude-standard --directory --error-unmatch -- ':/*' >/dev/null 2>&1; then + GIT_PROMPT+=" +" + elif [[ -n "$GIT_PROMPT_SHOW_STASH" ]] && git rev-parse --verify --quiet refs/stash >/dev/null; then + GIT_PROMPT+=" $" + fi fi # Output the prompt. @@ -415,14 +411,14 @@ PROMPT_COMMAND="__nanorc_prompt_command; __ssh_agent_prompt_command" # Git prompt options. GIT_PROMPT_SHOW_TYPE=1 +GIT_PROMPT_SHOW_UPSTREAM=1 +GIT_PROMPT_SHOW_UPSTREAM_EXTENDED=1 +GIT_PROMPT_AUTO_PREFETCH=1 GIT_PROMPT_SHOW_IGNORED=1 GIT_PROMPT_SHOW_UNSTAGED=1 GIT_PROMPT_SHOW_UNCOMMITTED=1 GIT_PROMPT_SHOW_UNTRACKED=1 GIT_PROMPT_SHOW_STASH=1 -GIT_PROMPT_SHOW_UPSTREAM=1 -GIT_PROMPT_SHOW_UPSTREAM_EXTENDED=1 -GIT_PROMPT_AUTO_PREFETCH=1 # Version specific set up. if (( BASH_VERSINFO[0] >= 4 )); then