#!/usr/bin/env bash
# Open a GitLab merge request via glab, gated on green local verify
# and a non-default branch. Extra arguments are passed through to glab.
set -euo pipefail

if ! command -v glab >/dev/null 2>&1; then
  echo "error: glab CLI not found. See https://gitlab.com/gitlab-org/cli" >&2
  exit 127
fi

branch=$(git symbolic-ref --short HEAD)
if [[ "$branch" == "main" || "$branch" == "master" ]]; then
  echo "error: refusing to open MR from $branch" >&2
  exit 2
fi

echo "==> make verify"
make verify

exec glab mr create --fill --remove-source-branch "$@"
