From 70602c72f07d6ed23f3e7a4219a1a1eab3f299b4 Mon Sep 17 00:00:00 2001 From: Erwin Jansen Date: Fri, 28 Aug 2020 12:45:08 -0700 Subject: [PATCH] lib/omb-prompt-base: Set a timeout on git commands The calculate of git_summary_status can be extremely expensive for large repos. This results in the shell becoming unresponsive. We now set a timeout of 0.2 seconds to make sure we always have a responsive shell. Fixes #172 --- lib/omb-prompt-base.sh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/omb-prompt-base.sh b/lib/omb-prompt-base.sh index 5c25620d5..47796d2de 100644 --- a/lib/omb-prompt-base.sh +++ b/lib/omb-prompt-base.sh @@ -36,6 +36,7 @@ SCM_GIT_SHOW_CURRENT_USER=${SCM_GIT_SHOW_CURRENT_USER:=false} SCM_GIT_SHOW_MINIMAL_INFO=${SCM_GIT_SHOW_MINIMAL_INFO:=false} SCM_GIT='git' + SCM_GIT_CHAR='±' SCM_GIT_DETACHED_CHAR='⌿' SCM_GIT_AHEAD_CHAR="↑" @@ -43,6 +44,7 @@ SCM_GIT_BEHIND_CHAR="↓" SCM_GIT_UNTRACKED_CHAR="?:" SCM_GIT_UNSTAGED_CHAR="U:" SCM_GIT_STAGED_CHAR="S:" +SCM_GIT_TIMEOUT="0.2" SCM_HG='hg' SCM_HG_CHAR='☿' @@ -105,7 +107,24 @@ function _omb_prompt_format { } function _omb_prompt_git { - command git "$@" + run_with_timeout $SCM_GIT_TIMEOUT command git "$@" +} + +function run_with_timeout() { + # Runs the given command with the given timeout + # $1 Timeout in seconds + # $@ Command to be executed. + local TIME=$1 + shift + local CMD=$@ + local TIMEOUT= + if which timeout &> /dev/null; then + TIMEOUT=timeout + elif which gtimeout &> /dev/null; then + TIMEOUT=gtimeout + fi + + $TIMEOUT ${TIME} ${CMD} } function scm {