]> Untitled Git - hvutilities.git/commitdiff
Add common hvutilities.sh
authorHugo Villeneuve <hugo@hugovil.com>
Fri, 27 Mar 2026 14:11:39 +0000 (10:11 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Wed, 1 Apr 2026 17:11:29 +0000 (13:11 -0400)
Signed-off-by: Hugo Villeneuve <hugo@hugovil.com>
scripts/Makefile.am
scripts/hvutilities.sh [new file with mode: 0644]
scripts/kernel-send-patches.sh

index 477ed2738d6453481bbddad7b47491f92851058b..91648ea3e46adb97cb505b0f60948aac6e1c7cdb 100644 (file)
@@ -28,6 +28,7 @@ dist_bin_SCRIPTS = \
     hvk-select.sh \
     hvk-stack \
     hvk-x86.sh \
+    hvutilities.sh \
     iso8859-to-utf8.sh \
     kernel-send-patches.sh \
     mail-files mail-if-fail mail-statistics \
diff --git a/scripts/hvutilities.sh b/scripts/hvutilities.sh
new file mode 100644 (file)
index 0000000..ee53d81
--- /dev/null
@@ -0,0 +1,150 @@
+#!/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'})
+}
index 828c87a06e82772cbe70e3c3314c0d08c7c6d624..d48e33d9e8204bd18956196deaeac1098bc76a43 100755 (executable)
@@ -12,6 +12,7 @@ set -e
 
 PROG_NAME="`readlink -e $0`"
 PROG_PATH=$(dirname ${PROG_NAME})
+source ${PROG_PATH}/hvutilities.sh
 
 trap 'catch $?' EXIT
 
@@ -201,22 +202,6 @@ if [ x"${base_commit}" = x"" ]; then
     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"
@@ -272,14 +257,14 @@ add_cc_list_addresses()
     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}\""
@@ -288,7 +273,7 @@ fi
 
 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