From 67e0e2643ee8ca8608eaa2e12a32b9b52bd9695f 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 the git command 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. * lib/omb-prompt-base: Set a timeout on git commands * lib/omb-prompt-base: Fix timeout bug * lib/omb-prompt-base: Rename function "{run_with => _omb_prompt}_timeout" * lib/omb-prompt-base(_omb_prompt_timeout): Fix coding style * lib/omb-prompt-base(_omb_prompt_timeout): Check timeout commands on startup Fixes https://github.com/ohmybash/oh-my-bash/issues/172 Co-authored-by: Koichi Murase --- 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..84a42df0a 100644 --- a/lib/omb-prompt-base.sh +++ b/lib/omb-prompt-base.sh @@ -43,6 +43,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,9 +106,27 @@ function _omb_prompt_format { } function _omb_prompt_git { - command git "$@" + _omb_prompt_timeout "$SCM_GIT_TIMEOUT" command git "$@" } +## @fn _omb_prompt_timeout +## Runs the given command with the given timeout +## @param $1 Timeout in seconds +## @param $@ Command to be executed. +if _omb_util_command_exists timeout; then + function _omb_prompt_timeout { + timeout "$@" + } +elif _omb_util_command_exists gtimeout; then + function _omb_prompt_timeout { + gtimeout "$@" + } +else + function _omb_prompt_timeout { + "${@:2}" + } +fi + function scm { if [[ "$SCM_CHECK" = false ]]; then SCM=$SCM_NONE elif [[ -f .git/HEAD ]]; then SCM=$SCM_GIT