1
0
mirror of synced 2026-01-22 10:50:53 +00:00
Interlisp.medley/scripts/medley/medley_utils.sh
Frank Halasz d6cb62a354
Response to Issue #2198: loadup and medley scripts can now store/retrieve loadup files to/from named subdirectories of MEDLEYDIR/loadups (#2200)
* Add to loadup and medley scripts a --branch argument whereby which loadup outputs are stored in MEDLEYDIR/loadups/branches/BRANCH, where BRANCH is the name of the currently active git branch of MEDLEYDIR (for loadup) or the name of a BRANCH whereby which a loadup had been run with the --branch option (for medley).  Man pages updated accordingly.

* Removed --git-branch synonym for --branch from both medley and loadup scripts.  Added optional BRANCH argument to --branch in loadup script. If BRANCH is specified iot is used as the (pseudo)branch name instead of the current active git branch of MEDLEYDIR.  If BRANCH is "-", the current git branch name is used.

* When doing loadups, a file called gitinfo is created in the LOADUP_OUTDIR that contains the git commit, git branch and git status of MEDLEYDIR at the time of the loadup.  Branch names in loadup and medley scripts now contain only alphanumerics, dahes, underscores, periods.  Any other character is coerced to underscore.  The branch name is now included in the window title.

* Incorporate changes from PR#2208: Use the loadup step script names as the --id string.

* Add loadups/gitinfo and loadups/branches to .gitignore
2025-07-14 10:28:26 -07:00

191 lines
4.6 KiB
Bash

#!only-to-be-sourced
# shellcheck shell=sh
###############################################################################
#
# medley_utils.sh - script containing various useful functions for medley.sh script.
#
# !!!! This script is meant to be SOURCEd from the scripts/medley.sh script.
# !!!! It should not be run as a standlone script.
#
# 2023-01-23 Frank Halasz
#
# Copyright 2023 Interlisp.org
#
###############################################################################
is_tput="$(command -v tput)"
export EOL="
"
output_error_msg() {
local_oem_file="${TMPDIR:-/tmp}"/oem_$$
echo "$1" >"${local_oem_file}"
while read -r line
do
if [ -n "${is_tput}" ];
then
echo "$(${is_tput} setab 1)$(${is_tput} setaf 7)${line}$(${is_tput} sgr0)"
else
echo "${line}"
fi
done <"${local_oem_file}"
rm -f "${local_oem_file}"
}
output_warn_msg() {
local_oem_file="${TMPDIR:-/tmp}"/oem_$$
echo "$1" >"${local_oem_file}"
while read -r line
do
echo "$(${is_tput} setab 3)$(${is_tput} setaf 4)${line}$(${is_tput} sgr0)"
done <"${local_oem_file}"
rm -f "${local_oem_file}"
}
check_for_dash_or_end() {
local_err_msg="";
if [ -z "$2" ] || [ "$2" = "--" ]
then
local_err_msg="Error: the flag \"$1\" requires a value.
Value is missing."
usage "${local_err_msg}"
else
case "$2" in
-*)
local_err_msg="Error: either the value for flag \"${1}\" is missing OR
the value begins with a \"-\", which is not allowed."
usage "${local_err_msg}"
;;
esac
fi
}
check_file_writeable_or_creatable() {
local_msg_core="\"$2\" given as the value of the \"$1\" flag"
local_err_msg=""
if [ -e "$2" ]
then
if [ ! -f "$2" ]
then
local_err_msg="Error: File ${local_msg_core} is not a regular file.
It is either a directory or a device file of some sort.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
elif [ ! -w "$2" ]
then
local_err_msg="Error: File ${local_msg_core} exists but is not writeable
Exiting"
output_error_msg "${local_err_msg}"
exit 1
fi
else
if [ ! -w "$(dirname -- "$2")" ]
then
local_err_msg="Error: File ${local_msg_core} cannot be created because
its directory either doen't exist or is not writeable.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
fi
fi
}
check_dir_writeable_or_creatable() {
local_msg_core="\"$2\" given as the value of the \"$1\" flag"
local_err_msg=""
if [ -e "$2" ]
then
if [ ! -d "$2" ]
then
local_err_msg="Error: ${local_msg_core} exists but is not a directory.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
elif [ ! -w "$2" ]
then
local_err_msg="Error: Directory ${local_msg_core} exists but is not writeable
Exiting"
output_error_msg "${local_err_msg}"
exit 1
fi
else
if [ ! -w "$(dirname -- "$2")" ]
then
local_err_msg="Error: Directory ${local_msg_core} cannot be created because
its directory either doesn't exist or is not writeable.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
fi
fi
}
check_file_readable() {
local_msg_core="\"$2\" given as the value of the \"$1\" flag"
if [ ! -r "$2" ]
then
local_err_msg="Error: File ${local_msg_core}
either doesn't exist or is not readable.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
fi
}
check_dir_exists() {
local_msg_core="\"$2\" given as the value of the \"$1\" flag"
if [ -e "$2" ]
then
if [ ! -d "$2" ]
then
local_err_msg="Error: Pathname ${local_msg_core} exists but is not a directory.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
elif [ ! -r "$2" ]
then
local_err_msg="Error: Directory ${local_msg_core} exists but is not readable.
Exiting"
output_error_msg "${local_err_msg}"
exit 1
fi
fi
}
parse_nethub_data() {
nh_host=""
nh_port=""
nh_mac=""
nh_debug=""
#
x="${1%:}:"
nh_host="${x%%:*}"
x="${x#"${nh_host}":*}"
nh_port="${x%%:*}"
if [ "${nh_port}" = "${x}" ]; then nh_port=""; return 0; fi
x="${x#"${nh_port}":*}"
nh_mac="${x%%:*}"
if [ "${nh_mac}" = "${x}" ]; then nh_mac=""; return 0; fi
nh_debug="${x#"${nh_mac}":*}"
if [ "${nh_debug}" = "${x}" ]; then nh_debug=""; return 0; fi
nh_debug="${nh_debug%:}"
return 0
}
git_commit_info () {
if [ -f "$(command -v git)" ] && [ -x "$(command -v git)" ]
then
if git -C "$1" rev-parse >/dev/null 2>/dev/null
then
# This does NOT indicate if there are any modified files!
COMMIT_ID="$(git -C "$1" rev-parse --short HEAD)"
BRANCH="$(git -C "$1" rev-parse --abbrev-ref HEAD)"
fi
fi
}