From 9f60a6fde1b3ffae451ed402c56f7f3a3e65f12c Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Fri, 20 Oct 2023 20:15:03 +0100 Subject: [PATCH] Use timeout to limit possibly long running git commands in some repos. --- .bashrc | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/.bashrc b/.bashrc index c1febf8..95aadc5 100644 --- a/.bashrc +++ b/.bashrc @@ -26,7 +26,7 @@ __prompt_git_status() { # S:branch_name >5 * # Returns: Exit code of the command line before entering this function. # shellcheck disable=SC2155 - local BRANCH COUNT GIT_PROMPT GIT_REPO_INFO PWD=$(pwd -P) RET="$?" + local RET="$?" BRANCH COUNT GIT_PROMPT GIT_PROMPT_MARKER_SET GIT_REPO_INFO PWD=$(pwd -P) # Bail out if there's no format argument given. (( $# != 1 )) && return "$RET" @@ -108,17 +108,40 @@ __prompt_git_status() { } # 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 + [[ -n "$GIT_PROMPT_SHOW_IGNORED" ]] && git check-ignore "${PWD#"${GIT_REPO_INFO[0]}/"}" >/dev/null 2>&1 && { 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_MARKER_SET=1 + } + [[ -z "$GIT_PROMPT_MARKER_SET" ]] && [[ -n "$GIT_PROMPT_SHOW_UNSTAGED" ]] && { + timeout 2s git ls-files --modified --exclude-standard --directory --error-unmatch -- ':/*' >/dev/null 2>&1 + ERR=$? + if (( ERR == 124 )); then + GIT_PROMPT+=" _" + GIT_PROMPT_MARKER_SET=1 + elif (( ERR == 0 )); then + GIT_PROMPT+=" *" + GIT_PROMPT_MARKER_SET=1 + fi + } + [[ -z "$GIT_PROMPT_MARKER_SET" ]] && [[ -n "$GIT_PROMPT_SHOW_UNCOMMITTED" ]] && ! git diff --name-only --cached --exit-code >/dev/null 2>&1 && { 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_MARKER_SET=1 + } + [[ -z "$GIT_PROMPT_MARKER_SET" ]] && [[ -n "$GIT_PROMPT_SHOW_UNTRACKED" ]] && { + timeout 2s git ls-files git ls-files --others --exclude-standard --directory --error-unmatch -- ':/*' >/dev/null 2>&1 + ERR=$? + if (( ERR == 124 )); then + GIT_PROMPT+=" _" + GIT_PROMPT_MARKER_SET=1 + elif (( ERR == 0 )); then + GIT_PROMPT+=" +" + GIT_PROMPT_MARKER_SET=1 + fi + } + [[ -z "$GIT_PROMPT_MARKER_SET" ]] && [[ -n "$GIT_PROMPT_SHOW_STASH" ]] && git rev-parse --verify --quiet refs/stash >/dev/null && { GIT_PROMPT+=" $" - fi + GIT_PROMPT_MARKER_SET=1 + } fi # Output the prompt.