#!/usr/bin/env python
# coding=utf-8

import re
import sys

CONSOLE_COLORS = {
  'RED': '\x1b[0;31m',
  'RESET': '\x1b[0m'
}

VALID_MSG_PATTERN = r'^(build|chore|ci|docs|feat|fix|perf|refactor|release|revert|style|test|tests)(\(?.+\)?!?)?:\s.*$'

commit_msg_path = sys.argv[1]
commit_msg: str

with open(commit_msg_path, encoding="utf8") as f:
  commit_msg = f.readlines()[0]

if not re.match(VALID_MSG_PATTERN, commit_msg):
  print(CONSOLE_COLORS["RED"] + "Invalid format of commit message, please refer to: https://www.conventionalcommits.org/zh-hans/v1.0.0/")
  print(CONSOLE_COLORS['RESET'])
  exit(1)
