--- /dev/null
+#!/bin/sh
+
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2023 Hugo Villeneuve <hugo@hugovil.com>
+
+# Common functions for hvutilities scripts
+
+# Define to 1 to display debug messages:
+#debug=1
+
+# Define to 1 to display messages via syslog:
+#syslog=1
+#syslog_tag="my_script"
+
+COLOR_NONE="\033[0m"
+COLOR_RED="\033[0;31m"
+COLOR_GREEN="\033[0;32m"
+COLOR_YELLOW="\033[0;33m"
+COLOR_BLUE="\033[0;34m"
+COLOR_LIGHT_BLUE="\033[1;34m"
+
+COLOR_ERR="${COLOR_RED}"
+COLOR_WARN="${COLOR_YELLOW}"
+COLOR_DEBUG="${COLOR_LIGHT_BLUE}"
+
+# When using /bin/sh, echo needs to be prefixed with its path to avoid
+# printing "-e".
+ECHO="/bin/echo -e"
+
+# Arg1: facility: err, warning, info, debug
+# Remaining arguments: text to log
+log_common() {
+ local facility="${1}"
+ local color=""
+
+ # Must use descriptor number for POSIX:
+ # 1: /dev/stdout
+ # 2 /dev/stderr
+ local dest=""
+
+ # Remove first argument
+ shift 1
+
+ if [ "${syslog}" != "1" ]; then
+ case ${facility} in
+ debug)
+ color=${COLOR_DEBUG}
+ dest=1
+ ;;
+ info)
+ color=${COLOR_NONE}
+ dest=1
+ ;;
+ warning)
+ color=${COLOR_WARN}
+ dest=2
+ ;;
+ err)
+ color=${COLOR_ERR}
+ dest=2
+ ;;
+ *)
+ echo "error: invalid facility: ${facility}"
+ exit 1
+ ;;
+ esac
+
+ ${ECHO} >&${dest} "${color}${*}${COLOR_NONE}"
+ else
+ if [ "${syslog_tag}" != "" ]; then
+ tag_opt="-t ${syslog_tag}"
+ fi
+ logger -p syslog.${facility} ${tag_opt} "${*}"
+ fi
+}
+
+log_info() {
+ log_common info "${*}"
+}
+
+log_dbg() {
+ if [ "${debug}" = "1" ]; then
+ log_common debug "${*}"
+ fi
+}
+
+log_warn() {
+ log_common warning "${*}"
+}
+
+log_err() {
+ log_common err "${*}"
+}
+
+# Arg1: label
+# Arg2: detected exit code
+# Arg3: expected exit code
+test_result() {
+ local label
+ local detected
+ local expected
+ local color
+
+ label="Test \"${1}\" result: "
+ detected="${2}"
+ expected="${3}"
+
+ if [ "${detected}" = "${expected}" ]; then
+ result="PASS"
+ color="${COLOR_GREEN}"
+ else
+ result="FAIL"
+ color="${COLOR_RED}"
+ fi
+
+ ${ECHO} "${label}[${color}${result}${COLOR_NONE}]"
+}
+
+# Arg1: commit message title
+# Arg2: branch (optional)
+git_find_commit_by_title()
+{
+ if [ $# -lt 1 -o $# -gt 2 ]; then
+ log_err "Wrong number of arguments: ${#}"
+ exit 1
+ fi
+
+ branch=""
+
+ if [ $# -eq 2 ]; then
+ branch="${2}"
+
+ if ! git branch | grep -q -e "${branch}$"; then
+ log_err "Missing source branch: ${branch}"
+ exit 1
+ fi
+ fi
+
+ echo $(git --no-pager log --no-abbrev-commit --oneline -F --pretty=oneline --max-count=1 --since=1.year --grep "${1}" ${branch} | awk {'print $1'})
+}
+
+# Find SHA1 of preceding commit:
+# First arg: commit message title
+git_find_preceding_commit_by_title()
+{
+ local sha1_end
+
+ sha1_end="$(git_find_commit_by_title "${1}")"
+ echo $(git log --oneline ${sha1_end}~2..${sha1_end}~1 | awk {'print $1'})
+}
PROG_NAME="`readlink -e $0`"
PROG_PATH=$(dirname ${PROG_NAME})
+source ${PROG_PATH}/hvutilities.sh
trap 'catch $?' EXIT
base_commit="auto"
fi
-# First arg: commit message
-find_commit_by_log()
-{
- echo $(git --no-pager log --no-abbrev-commit --oneline -F --pretty=oneline --max-count=1 --since=1.year --grep "${1}" | awk {'print $1'})
-}
-
-# Find SHA1 of preceding commit:
-# First arg: commit message
-find_preceding_commit_by_log()
-{
- local sha1_end
-
- sha1_end="$(find_commit_by_log "${1}")"
- echo $(git log --oneline ${sha1_end}~2..${sha1_end}~1 | awk {'print $1'})
-}
-
# Remove "Name" if present in email address. Needed because of a bug in
# get_maintainer.pl even if we specify the "--non" option.
# Arg #1: "Name <email>" or "email"
done
}
-COMMIT_START_SHA1=$(find_commit_by_log "${commit_start}")
+COMMIT_START_SHA1=$(git_find_commit_by_title "${commit_start}")
if [ x"${COMMIT_START_SHA1}" = x"" ]; then
echo "Cannot find start commit identified by: \"${commit_start}\""
exit 1
fi
-COMMIT_END_SHA1=$(find_commit_by_log "${commit_end}")
+COMMIT_END_SHA1=$(git_find_commit_by_title "${commit_end}")
if [ x"${COMMIT_END_SHA1}" = x"" ]; then
echo "Cannot find end commit identified by: \"${commit_end}\""
if echo "${commit_end}" | grep -q "^end"; then
# Take commit just before end commit:
- COMMIT_END_SHA1=$(find_preceding_commit_by_log "${commit_end}")
+ COMMIT_END_SHA1=$(git_find_preceding_commit_by_title "${commit_end}")
fi
rm -f /tmp/b4-cover.patch