hooks: fix newline injection on MacOS

Because MacOS `sed` just needs newlines to be escaped differently.

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2022-09-05 14:45:46 +02:00
parent 8fe5e5f73c
commit 337a4d7d1c
No known key found for this signature in database
GPG Key ID: 09AA5403E54D9931
1 changed files with 5 additions and 2 deletions

View File

@ -1,10 +1,13 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -eo pipefail
if [[ -z $(git diff --cached --name-only -r | grep status-go-version.json) ]]; then if [[ -z $(git diff --cached --name-only -r | grep status-go-version.json) ]]; then
exit 0 # No status-go-version.json changes found. exit 0 # No status-go-version.json changes found.
fi 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) 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_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) 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 # 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 if ! grep -qF "${GITHUB_LINK_PREFIX}" "${COMMIT_MSG_FILE}" >/dev/null; then
sed -in'' -e "2i\\ sed -in'' -e "2i\\
\n${GITHUB_LINK}" "${COMMIT_MSG_FILE}" ${NL}${GITHUB_LINK}" "${COMMIT_MSG_FILE}"
else else
sed -in'' -e "s;^${GITHUB_LINK_PREFIX}.*$;${GITHUB_LINK};" "${COMMIT_MSG_FILE}" sed -in'' -e "s;^${GITHUB_LINK_PREFIX}.*$;${GITHUB_LINK};" "${COMMIT_MSG_FILE}"
fi fi