Skip to content

Commit

Permalink
chore: test for bash >=4.1 in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
LangLangBart committed May 13, 2024
1 parent 7438a8f commit 69766f6
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions gh-find-code
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,42 @@ set -o allexport -o errexit -o errtrace -o nounset -o pipefail
###############################################################################

GHFC_DEBUG_MODE=${GHFC_DEBUG_MODE:-0}

die() {
echo ERROR: "$*" >&2
exit 1
}

if ((GHFC_DEBUG_MODE)); then
debug_directory=$(command mktemp -d)
store_all_debug="${debug_directory}/all_debug"
store_gh_api_debug="${debug_directory}/gh_api_debug"
store_gh_search_debug="${debug_directory}/gh_search_debug"
store_grep_extended_debug="${debug_directory}/grep_extended_debug"

# might not always be needed since it also captures all the escape sequences, disabled for now
# exec &> >(command tee -a "$store_all_debug")

# 'GH_DEBUG' is useful for understanding the reasons behind failed GitHub API calls.
export GH_DEBUG=api

# Redirect xtrace output to a file.
# Ensure Bash 4.1+ for BASH_XTRACEFD support.
if [[ ${BASH_VERSINFO[0]} -lt 4 || (${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 1) ]]; then
die "Bash 4.1 or newer is required for debugging. Current version: ${BASH_VERSION}"
fi

# Write xtrace output to file descriptor 6, appending to a file.
exec 6>>"$store_all_debug"
# Write the trace output to file descriptor 6.
BASH_XTRACEFD=6

# Use a more detailed execution trace prompt.
PS4='+$(date +%Y-%m-%d:%H:%M:%S) ${FUNCNAME[0]:-}:L${LINENO:-}: '
PS4='+ $(date +%Y-%m-%d:%H:%M:%S) [${BASH_SOURCE[0]:+${BASH_SOURCE[0]##*/}}:${FUNCNAME[0]:+${FUNCNAME[0]}():}${LINENO}]: '
set -o xtrace

# Ensure xtrace is enabled in all child processes started by 'fzf'.
execution_shell="$(which bash) -o xtrace -c"
# NOTE: To view errors while running in debug mode, display them in the preview window of fzf.
execution_shell="$(which bash) -o xtrace -o errexit -o errtrace -o nounset -o pipefail -c"
fi

die() {
echo ERROR: "$*" >&2
exit 1
}

BAT_THEME=${BAT_THEME:-Monokai Extended}
bat_executable=""
# Check for 'bat' early, as it is needed for the error_handler function.
Expand All @@ -48,7 +57,7 @@ for value in bat batcat; do
fi
done
builtin unset value
[[ -z $bat_executable ]] && die "'bat' was not found."
[[ -z $bat_executable ]] && die "The 'bat' command is required but was not found."

# Enable 'errtrace' to ensure the ERR trap is inherited by functions, command substitutions and
# commands executed in a subshell environment.
Expand Down Expand Up @@ -238,7 +247,7 @@ validate_environment() {
fi
done
# If no suitable python version was found, terminate the script
[[ -z $python_executable ]] && die "No suitable 'python' version found."
[[ -z $python_executable ]] && die "No suitable 'python' version found. Required: 'python >= $min_python_version'."

# Verify if there are at least two spaces between columns. The delimiter in 'fzf' is set to
# '\t' or '\s\s+' to separate fields. By default, the 'column' command should separate any
Expand Down Expand Up @@ -507,8 +516,6 @@ gh_query() {
# characters, such as hashtags (#).
sanitized_owner_repo_name=$(sanitize_input "$owner_repo_name")
sanitized_file_path=$(sanitize_input "$file_path")
command cp "$store_file_contents" "${store_file_contents}_${index}"
command cp "$store_file_contents" "${store_file_contents}_${index}_line_numbers"
(
# Run gh api commands with lower priority using nice
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_244
Expand Down Expand Up @@ -604,7 +611,7 @@ gh_query() {

# These lines are used as a mechanism to stop the loop from continuing to send POST
# requests, otherwise it will block the 'view_history_commands' function
while [[ $(<"$store_hold_gh_query_loop") == "hold" ]]; do
while [[ -s $store_hold_gh_query_loop && $(<"$store_hold_gh_query_loop") == "hold" ]]; do
command sleep 0.1
done
curl_custom "transform-header:printf '%b%s/%s of %s collected...%b' '$DARK_GRAY' \
Expand Down Expand Up @@ -722,10 +729,11 @@ view_contents() {
IFS=' ' read -ra line_numbers <"${store_file_contents}_${index}_line_numbers"

# NOTE: The '--line-range' in 'bat' overrides preceding flags. However, the
# '--highlight-line' attribute can be utilized multiple times.
# '-H, --highlight-line' attribute can be utilized multiple times.
# https://github.com/sharkdp/bat/pull/162#pullrequestreview-125072252
for number in "${line_numbers[@]}"; do
bat_args+=("--highlight-line=${number}")
# use the short form to avoid making the command unnecessarily long
bat_args+=("-H=${number}")
done
file_name=$(command basename "$file_path")

Expand Down

0 comments on commit 69766f6

Please sign in to comment.