#!/bin/sh
"$(dirname "$0")/_/husky.sh"

MATCH_PATH='chaoscenter/web/'

BRANCH=$(git rev-parse --abbrev-ref HEAD)

echo "ON_BRANCH ⟶ $BRANCH"

REMOTE_EXISTS=$(git ls-remote --heads origin "$BRANCH" | wc -l)

CHECK_AGAINST_COMMIT=""

if [ "$REMOTE_EXISTS" -gt 0 ];
  then
    # remote exists, compare changes from last pushed commit
    LAST_PUSHED_COMMIT_ID=$(git rev-parse --short @{push})
    echo "LAST_PUSHED_COMMIT ⟶ $LAST_PUSHED_COMMIT_ID"
    CHECK_AGAINST_COMMIT="$LAST_PUSHED_COMMIT_ID"
  else
    # remote doesn't exists, compare changes from branched off commit
    BRANCHED_COMMIT_ID=$(git rev-parse --short "$(git merge-base main "$BRANCH")")
    echo "BRANCHED_COMMIT_ID ⟶ $BRANCHED_COMMIT_ID"
    CHECK_AGAINST_COMMIT="$BRANCHED_COMMIT_ID"
fi

CHAOS_UI_FILE_CHANGES=$(git diff --name-only -r --stat --oneline "$CHECK_AGAINST_COMMIT" HEAD | grep "$MATCH_PATH" -c)

echo "CHAOS_UI_FILE_CHANGES ⟶ $CHAOS_UI_FILE_CHANGES"

if [ "$CHAOS_UI_FILE_CHANGES" -gt 0 ];
  then
    cd ./chaoscenter/web && yarn test:silent --changedSince="$CHECK_AGAINST_COMMIT"
  else
    echo "Skipping husky pre-push hook in $MATCH_PATH folder"
fi