Sync changes with master branch.

This commit is contained in:
Darren 'Tadgy' Austin 2023-11-03 22:02:08 +00:00
commit df39384f22
9 changed files with 293 additions and 68 deletions

200
.bashrc
View file

@ -21,20 +21,16 @@ __prompt_git_status() {
# GIT_PROMPT_SHOW_STASH=1 Show a $ if there is a stash in this repository (superceeded by above).
# Displays: The printf formatted git prompt based upon $1 and the environment vaiables above, for example:
# S:branch_name >5 *
# Returns: Exit code of the command line before entering this function.
# shellcheck disable=SC2155
local RET="$?" BRANCH COUNT GIT_PROMPT GIT_PROMPT_MARKER_SET GIT_REPO_INFO PWD=$(pwd -P)
# Returns: 0 = Produced a prompt successfully.
# 1 = An error occured.
local BRANCH COUNT GIT_PROMPT GIT_PROMPT_MARKER_SET GIT_REPO_INFO IFS=$'\n'
# Bail out if there's no format argument given.
(( $# != 1 )) && return "$RET"
# Bail out if there's no format argument given, or it doesn't contain %s
(( $# != 1 )) || [[ "$1" != *%s* ]] && return 1
# Get some repository information.
# shellcheck disable=SC2207
IFS=$'\n' GIT_REPO_INFO=( $( git rev-parse --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.
(( ERR >= 1 )) && return "$RET"
GIT_REPO_INFO=( $( git rev-parse --is-bare-repository --is-shallow-repository --is-inside-git-dir --is-inside-work-tree 2>/dev/null) ) || return 1
# Generate the prompt.
if [[ "${GIT_REPO_INFO[2]}" == "true" ]]; then
@ -55,7 +51,11 @@ __prompt_git_status() {
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"
if [[ "$BRANCH" == "HEAD" ]]; then
GIT_PROMPT+="?NO COMMITS?"
unset BRANCH
# Output the prompt and escape early.
# shellcheck disable=SC2059
printf -- "$1" "$GIT_PROMPT"
return 0
else
GIT_PROMPT+="$BRANCH"
fi
@ -140,13 +140,11 @@ __prompt_git_status() {
# shellcheck disable=SC2059
printf -- "$1" "$GIT_PROMPT"
# Return the original error code.
return "$RET"
return 0
}
__prompt_user_colour() {
# Determine the colour of the username in the prompt.
local RET="$?"
if [[ "$LOGNAME" == "root" ]]; then
printf "%s" "1;31m" # Bright Red.
@ -156,29 +154,78 @@ __prompt_user_colour() {
printf "%s" "1;36m" # Bright Cyan.
fi
return "$RET"
return 0
}
__git_prompt_command() {
# Perform git actions.
# Environment variables:
# GIT_DISABLE_PROMPT_PREFETCH=1 Disable automatic 'prefetch' of upstream refs.
# This can also be disabled on a per repository basis using:
# git config --add --local --bool script.DisablePromptPrefetch true
# Returns: Exit code of the command line before entering this function.
local RET="$?"
# GIT_DISABLE_PROMPT_PREFETCH=1 Disable automatic 'prefetch' of upstream refs.
# This can also be disabled on a per repository basis using:
# git config ---local -replace-all --type bool script.DisablePromptPrefetch true
# Returns: 0 = Tasks completed successfully.
# 1 = An error occured.
local GIT_REPO_INFO LC_ALL="C" NOW REPO_TIMESTAMP TIMESTAMP_VAR
# Run prefetch tasks if inside a work directory.
[[ -z "$GIT_DISABLE_PROMPT_PREFETCH" ]] && [[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]] && \
[[ "$(git config --get --local --bool script.DisablePromptPrefetch 2>/dev/null)" != "true" ]] && {
git maintenance run --task=prefetch 2>/dev/null || printf "\\033[1;31m%s\\033[39m\\n" "Git maintenance 'prefetch' task failed." >&2
# shellcheck disable=SC2207
GIT_REPO_INFO=( $( git rev-parse --is-inside-work-tree --show-toplevel 2>/dev/null) ) || return 1
# Only process if in a work directory.
[[ "${GIT_REPO_INFO[0]}" == "true" ]] && {
# Run prefetch tasks if not disabled.
[[ -z "$GIT_DISABLE_PROMPT_PREFETCH" ]] && [[ "$(git config --local --get --type bool script.DisablePromptPrefetch 2>/dev/null)" != "true" ]] && {
git maintenance run --task=prefetch 2>/dev/null || {
printf "\\033[1;31m%s\\033[0m\\n" "Git maintenance 'prefetch' task failed." >&2
return 1
}
}
# The time now.
if [[ "$PLATFORM" == "Linux" ]]; then
NOW="$(date +'%s%3N')"
elif [[ "$PLATFORM" == "Darwin" ]]; then
NOW="$(perl -e 'use Time::HiRes; printf "%.3f", Time::HiRes::time();')"
NOW="${NOW/.}"
fi
# Determine the timestamp variable name depending on bash version.
if (( BASH_VERSINFO[0] >= 4 )); then
TIMESTAMP_VAR="GIT_REPO_TIMESTAMP[${GIT_REPO_INFO[1]//[^[:alnum:]]/_}]"
else
# This is going to pollute the environment, but Darwin is a PITA.
TIMESTAMP_VAR="GIT_REPO_TIMESTAMP_${GIT_REPO_INFO[1]//[^[:alnum:]]/_}"
fi
if [[ -n "${!TIMESTAMP_VAR}" ]]; then
# Monitor the git repo.
REPO_TIMESTAMP="$(git config --local --get --type int script.AutoMergeLast)"
(( ${!TIMESTAMP_VAR:-0} < REPO_TIMESTAMP )) && {
# Display message depending on status.
if [[ "$(git config --local --get --type int script.AutoMergeSuccess)" == "true" ]]; then
printf "\\033[1;32m%s" "Git auto-merge succeeded for this repo."
if [[ "${GIT_REPO_INFO[1]}" == "$HOME" ]]; then
printf " %s\\033[0m\\n" "Re-source .bash* files."
else
printf "\\033[0m\\n"
fi
# Update the timestamp in the environment.
declare -g "$TIMESTAMP_VAR"="$NOW"
else
printf "\\033[1;31m%s\\033[0m\\n" "Git auto-merge failed for this repo - correct manually." >&2
fi
}
else
# Just set the timestamp in the environment.
declare -g "$TIMESTAMP_VAR"="$NOW"
fi
}
return "$RET"
return 0
}
__nanorc_prompt_command() {
# Dynamically handle .nanorc file versions.
hash nano >/dev/null 2>&1 && {
# shellcheck disable=SC2155
local NANO_VER="$(command nano --version | awk '/version/ { print $4 }' | cut -d. -f1)"
@ -199,15 +246,17 @@ __nanorc_prompt_command() {
else
unalias nano 2>/dev/null
fi
printf "\\033[1;31m%s\\033[39m\\n" "No .nanorc for version '$NANO_VER'." >&2
printf "\\033[1;31m%s\\033[0m\\n" "No .nanorc for version '$NANO_VER'." >&2
fi
}
return 0
}
__ssh_agent_prompt_command() {
# If necessary, find and activate a new ssh agent socket before each prompt is displayed.
# Returns: 0 = All is good.
# 1 = An error occured.
# Returns: 0 = All is good.
# 1 = An error occured.
local ERR
if [[ -z "$SSH_AUTH_SOCK" ]]; then
@ -225,28 +274,29 @@ __ssh_agent_prompt_command() {
# Find a new socket to use.
if __find_ssh_agent_sock; then
printf "\\033[1;33m%s\\033[39m\\n" "Connected to existing ssh-agent socket."
printf "\\033[1;33m%s\\033[0m\\n" "Connected to existing ssh-agent socket."
sleep 0.5
else
# Start a new agent.
eval "$(ssh-agent -s 2>/dev/null | grep -v 'echo'; printf "%s" "ERR=${PIPESTATUS[0]}")"
(( ERR > 0 )) && {
printf "\\033[1;31m%s\\033[39m\\n" "Failed to start new ssh-agent - continuing with no agent."
printf "\\033[1;31m%s\\033[0m\\n" "Failed to start new ssh-agent - continuing with no agent."
sleep 0.5
return 1
}
printf "\\033[1;32m%s\\033[39m\\n" "Started new ssh-agent."
printf "\\033[1;32m%s\\033[0m\\n" "Started new ssh-agent."
__write_ssh_agents
sleep 0.5
fi
}
return 0
}
__find_ssh_agent_sock() {
# Find an *active* ssh agent socket.
# Returns: 0 = Found an active socket.
# 1 = Did not find a viable socket.
# Returns: 0 = Found an active socket.
# 1 = Did not find a viable socket.
local I
# Search the SSH_AUTH_SOCKS array for a viable socket.
@ -265,8 +315,8 @@ __find_ssh_agent_sock() {
__read_ssh_agents() {
# Read all the known ssh agent sockets into an array.
# Returns: 0 = Processed and read the agents file without issue.
# 1 = Error processing/reading the agents file.
# Returns: 0 = Processed and read the agents file without issue.
# 1 = Error processing/reading the agents file.
local ERR I SOCK
[[ ! -e "$HOME/.ssh/agents" ]] && touch "$HOME/.ssh/agents"
@ -277,10 +327,10 @@ __read_ssh_agents() {
exec 9<"$HOME/.ssh/agents" && flock -E 10 -e -w 0.5 9
ERR=$?
if (( ERR == 10 )); then
printf "\\033[1;31m%s\\033[39m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
return 1
elif (( ERR > 0 )); then
printf "\\033[1;31m%s\\033[39m\\n" "Flock usage error." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Flock usage error." >&2
return 1
fi
@ -301,11 +351,11 @@ __read_ssh_agents() {
fi
done
(( ERR != 0 )) && {
printf "\\033[1;31m%s\\033[39m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
return 1
}
else
printf "\\033[1;31m%s\\033[39m\\n" "File locking unsupported on '$PLATFORM'." >&2
printf "\\033[1;31m%s\\033[0m\\n" "File locking unsupported on '$PLATFORM'." >&2
return 1
fi
@ -326,18 +376,19 @@ __read_ssh_agents() {
# Error out if the data couldn't be read.
(( ERR != 0 )) && {
printf "\\033[1;31m%s\\033[39m\\n" "Failed to read ssh-agent socket list." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Failed to read ssh-agent socket list." >&2
unset SSH_AUTH_SOCKS SSH_AGENTS_MTIME
return 1
}
return 0
}
__write_ssh_agents() {
# Write all unique ssh agent sockets into the ~/.ssh/agents file.
# Returns: 0 = Processed and wrote the agents file without issue.
# 1 = Error processing/writing the agents file.
# 2 = The SSH_AUTH_SOCKS array may be out of date as the agents file'a mtime has changed.
# Returns: 0 = Processed and wrote the agents file without issue.
# 1 = Error processing/writing the agents file.
# 2 = The SSH_AUTH_SOCKS array may be out of date as the agents file'a mtime has changed.
local ERR I J MTIME SOCKS
# Add the current agent socket to the sockets array.
@ -364,10 +415,10 @@ __write_ssh_agents() {
exec 9<"$HOME/.ssh/agents" && flock -E 10 -e -w 0.5 9
ERR=$?
if (( ERR == 10 )); then
printf "\\033[1;31m%s\\033[39m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
return 1
elif (( ERR > 0 )); then
printf "\\033[1;31m%s\\033[39m\\n" "Flock usage error." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Flock usage error." >&2
return 1
fi
elif [[ "$(uname -s)" == "Darwin" ]]; then
@ -387,11 +438,11 @@ __write_ssh_agents() {
fi
done
(( ERR != 0 )) && {
printf "\\033[1;31m%s\\033[39m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Failed to obtain lock on ~/.ssh/agents." >&2
return 1
}
else
printf "\\033[1;31m%s\\033[39m\\n" "File locking unsupported on '$PLATFORM'." >&2
printf "\\033[1;31m%s\\033[0m\\n" "File locking unsupported on '$PLATFORM'." >&2
return 1
fi
@ -408,14 +459,17 @@ __write_ssh_agents() {
rm -f "$HOME/.ssh/agents" 2>/dev/null
elif (( ERR >= 1 )); then
rm -f "$HOME/.ssh/agents" 2>/dev/null
printf "\\033[1;31m%s\\033[39m\\n" "Failed to write ssh-agent socket list." >&2
printf "\\033[1;31m%s\\033[0m\\n" "Failed to write ssh-agent socket list." >&2
return 1
fi
return 0
}
imagebin() {
[[ -z "$1" ]] && {
# Throw an image file into an imagebin.
[[ -z "$1" ]] || [[ ! -e "$1" ]] && {
printf "%s: %s\\n" "Usage" "${FUNCNAME[0]} <filename>" >&2
return 1
}
@ -448,7 +502,6 @@ PROMPT_COMMAND="__nanorc_prompt_command; __ssh_agent_prompt_command; __git_promp
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
@ -463,31 +516,25 @@ if (( BASH_VERSINFO[0] >= 4 )); then
# Trim the path in the prompt.
PROMPT_DIRTRIM=2
# Coloured username + host + directory:
PS1='[\[\033[$(__prompt_user_colour)\]\u\[\033[39m\]@\[\033[1;33m\]\h\[\033[39m\]] \[\033[1;34m\]\w\[\033[39m\]$(__prompt_git_status "\[\\033[1;35m\] (%s)\[\\033[39m\]") ->'
PS1='[\[\033[$(__prompt_user_colour)\]\u\[\033[0m\]@\[\033[1;33m\]\h\[\033[0m\]] \[\033[1;34m\]\w\[\033[0m\]$(__prompt_git_status "\[\\033[1;35m\] (%s)\[\\033[0m\]") ->'
else
# Set the prompts.
# Coloured username + host + directory:
# shellcheck disable=SC2154
PS1='[\[\033[$(__prompt_user_colour)\]\u\[\033[39m\]@\[\033[1;33m\]\h\[\033[39m\]] \[\033[1;34m\]$(printf "%s" "${PWD/#$HOME/~}" | awk -F/ '\''{if (NF>3) {printf ".../" $(NF-1) "/" $NF} else {printf $0}}'\'')\[\033[39m\]$(__prompt_git_status "\[\\033[1;35m\] (%s)\[\\033[39m\]") ->'
PS1='[\[\033[$(__prompt_user_colour)\]\u\[\033[0m\]@\[\033[1;33m\]\h\[\033[0m\]] \[\033[1;34m\]$(printf "%s" "${PWD/#$HOME/~}" | awk -F/ '\''{if (NF>3) {printf ".../" $(NF-1) "/" $NF} else {printf $0}}'\'')\[\033[0m\]$(__prompt_git_status "\[\\033[1;35m\] (%s)\[\\033[0m\]") ->'
fi
# Set the debugger prompt.
# shellcheck disable=SC2155
export PS4='+(\[\033[1;33m\]$?\[\033[39m\]) \[\033[1;34m\]${BASH_SOURCE##*/}\[\033[39m\]${FUNCNAME[0]:+(\[\033[1;32m\]${FUNCNAME[0]}\[\033[39m\])}:\[\033[1;31m\]$LINENO\[\033[39m\]: '
export PS4='+(\[\033[1;33m\]$?\[\033[0m\]) \[\033[1;34m\]${BASH_SOURCE##*/}\[\033[0m\]${FUNCNAME[0]:+(\[\033[1;32m\]${FUNCNAME[0]}\[\033[0m\])}:\[\033[1;31m\]$LINENO\[\033[0m\]: '
# Common aliases.
hash bc >/dev/null 2>&1 && alias bc='bc -lq'
hash diff >/dev/null 2>&1 && alias diff='diff --color=auto -u'
hash gpg2 >/dev/null 2>&1 && {
alias gpg='command gpg2'
alias gpg2='gpg2 --pinentry-mode=loopback'
}
hash grep >/dev/null 2>&1 && {
alias egrep='grep -E --color=auto'
alias fgrep='grep -F --color=auto'
alias grep='grep --color=auto'
}
hash nc >/dev/null 2>&1 && alias pastebin='nc termbin.com 9999'
# Auto start the ssh agent and add keys for scp/sftp/ssh.
__ssh_agent_prompt_command
@ -511,6 +558,26 @@ hash ssh ssh-add >/dev/null 2>&1 && ssh() {
command "${_EXEC:-${FUNCNAME[0]}}" "$@"
}
# Handle the ~/.gitconfig link.
if [[ -e "$HOME/.gitconfig-$USER@$HOSTNAME" ]]; then
FILENAME=".gitconfig-$USER@$HOSTNAME"
elif [[ -e "$HOME/.gitconfig-$USER@*.${HOSTNAME#*.}" ]]; then
FILENAME=".gitconfig-$USER@*.${HOSTNAME#*.}"
elif [[ -e "$HOME/.gitconfig-$USER@*" ]]; then
FILENAME=".gitconfig-$USER@*"
elif [[ -e "$HOME/.gitconfig-*@$HOSTNAME" ]]; then
FILENAME=".gitconfig-*@$HOSTNAME"
elif [[ -e "$HOME/.gitconfig-*.${HOSTNAME#*.}" ]]; then
FILENAME=".gitconfig-*.${HOSTNAME#*.}"
elif [[ -e "$HOME/.gitconfig-default" ]]; then
FILENAME=".gitconfig-default"
else
(cd "$HOME" && [[ -L ".gitconfig" ]] && rm -f ".gitconfig")
printf "%s: %s\\n" "${BASH_SOURCE##*/}" "failed to update .gitconfig symlink" >&2
fi
[[ -n "$FILENAME" ]] && (cd "$HOME" && ln -sf "$FILENAME" ".gitconfig")
unset FILENAME
# Platform specific set up.
if [[ "$PLATFORM" = "Linux" ]]; then
# Linux specific functions.
@ -525,7 +592,9 @@ if [[ "$PLATFORM" = "Linux" ]]; then
}
# Linux specific aliases.
hash diff >/dev/null 2>&1 && alias diff='diff --color=auto -u'
hash ftpwho >/dev/null 2>&1 && alias ftpwho='ftpwho -v'
hash gpg2 >/dev/null 2>&1 && alias gpg='command gpg2' && alias gpg2='gpg2 --pinentry-mode=loopback'
hash iftop >/dev/null 2>&1 && alias iftop='TERM=vt100 iftop'
hash iotop >/dev/null 2>&1 && alias iotop='TERM=linux iotop'
hash ip >/dev/null 2>&1 && alias ip='ip -color=auto'
@ -534,12 +603,19 @@ if [[ "$PLATFORM" = "Linux" ]]; then
hash minicom >/dev/null 2>&1 && alias minicom='minicom -m -c on'
hash mkpasswd >/dev/null 2>&1 && alias mkpasswd='mkpasswd -m sha512crypt'
hash mkpasswd >/dev/null 2>&1 && alias pwgen='mkpasswd -m sha512crypt'
hash nc >/dev/null 2>&1 && alias pastebin='nc termbin.com 9999'
hash pinfo >/dev/null 2>&1 && alias info='pinfo'
hash ping >/dev/null 2>&1 && alias ping='ping -b'
elif [[ "$PLATFORM" = "Darwin" ]]; then
# Darwin specific aliases.
hash df >/dev/null 2>&1 && alias df='df -P'
hash ls >/dev/null 2>&1 && alias ls='ls -bFG'
# Darwin specific aliases (some dependant on macports)
[[ ! -e "/opt/local/libexec/gnubin/df" ]] && alias df='df -kP'
# shellcheck disable=SC2015
[[ ! -e "/opt/local/libexec/gnubin//diff" ]] && alias diff='diff -u' || alias diff='diff --color=auto -u'
hash last less >/dev/null 2>&1 && alias laston='last | less'
# shellcheck disable=SC2015
[[ ! -e "/opt/local/libexec/gnubin/ls" ]] && alias ls='ls -bFGO' || alias ls='ls -bFv --color=auto'
[[ -e "/opt/local/libexec/gnubin/nc" ]] && alias pastebin='nc termbin.com 9999'
[[ -e "/opt/local/bin/pinfo" ]] && alias info='pinfo'
hash top >/dev/null 2>&1 && alias top='top -o cpu -S'
else
printf "%s: %s\\n" "${BASH_SOURCE##*/}" "unsupported platform: $PLATFORM" >&2