gitpod/dev/preview/util/preview-name-from-branch.sh
2022-03-11 13:50:18 +05:30

22 lines
806 B
Bash
Executable File

#!/usr/bin/env bash
#
# Based on the name of the current branch this will compute the name of the associated
# preview environment.
#
# NOTE: This needs to produce the same result as the function in .werft/util/preview.ts
# See the file for implementation notes.
#
function preview-name-from-branch {
branch_name=$(git symbolic-ref HEAD 2>&1) || error "Cannot get current branch"
sanitizedd_branch_name=$(echo "$branch_name" | awk '{ sub(/^refs\/heads\//, ""); $0 = tolower($0); gsub(/[^-a-z0-9]/, "-"); print }')
length=$(echo -n "$sanitizedd_branch_name" | wc -c)
if [ "$length" -gt 20 ]; then
hashed=$(echo -n "${sanitizedd_branch_name}" | sha256sum)
echo "${sanitizedd_branch_name:0:10}${hashed:0:10}"
else
echo "${sanitizedd_branch_name}"
fi
}