From 337a4d7d1c347f7296d7267af5333a58a691d7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Mon, 5 Sep 2022 14:45:46 +0200 Subject: [PATCH] hooks: fix newline injection on MacOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because MacOS `sed` just needs newlines to be escaped differently. Signed-off-by: Jakub SokoĊ‚owski --- scripts/hooks/prepare-commit-msg | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/hooks/prepare-commit-msg b/scripts/hooks/prepare-commit-msg index 32e908d71b..9a3dc5f00c 100755 --- a/scripts/hooks/prepare-commit-msg +++ b/scripts/hooks/prepare-commit-msg @@ -1,10 +1,13 @@ #!/usr/bin/env bash -set -euo pipefail +set -eo pipefail if [[ -z $(git diff --cached --name-only -r | grep status-go-version.json) ]]; then exit 0 # No status-go-version.json changes found. fi +# MacOS sed is garbage and interprets newlines differently. +[[ "$(uname)" -eq "Darwin" ]] && NL=$'\\\n' || NL="\n" + MERGE_BASE=$(git merge-base -a develop "$(git rev-parse --abbrev-ref HEAD)" develop) GO_COMMIT_MERGE_BASE=$(git show "${MERGE_BASE}":status-go-version.json | awk -F'"' '/commit-sha1/{print $4}') GO_COMMIT_CURRENT=$(awk -F'"' '/commit-sha1/{print $4}' status-go-version.json) @@ -16,7 +19,7 @@ COMMIT_MSG_FILE=$1 # Check if the commit message already contains the link (rebase) and update otherwise insert into line2 if ! grep -qF "${GITHUB_LINK_PREFIX}" "${COMMIT_MSG_FILE}" >/dev/null; then sed -in'' -e "2i\\ -\n${GITHUB_LINK}" "${COMMIT_MSG_FILE}" +${NL}${GITHUB_LINK}" "${COMMIT_MSG_FILE}" else sed -in'' -e "s;^${GITHUB_LINK_PREFIX}.*$;${GITHUB_LINK};" "${COMMIT_MSG_FILE}" fi