# git commit with optional AI-generated commit message

___x_cmd_git_commit(){
    local msg=""
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)
                    ___x_cmd help -m git commit; return ;;
            -m|--message)
                    msg="$2"; arg:2:shift ;;
            *)      break ;;
        esac
    done

    if [ -z "$msg" ]; then
        git:info "generating commit message via agent ..."
        msg="$( ___x_cmd ask --quick "
You are a git commit message generator. Generate a commit message following Conventional Commits convention.

Format:
<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert

Rules:
- Use imperative mood in the description (\"add feature\" not \"added feature\")
- Keep the description under 72 characters
- Reference issues/PRs in the footer when applicable
- The commit message MUST be strictly following Conventional Commits

The git diff output of this commit is:
$(___x_cmd___git_origin diff --cached --no-color)
"
)"      || {
            git:error "failed to generate commit message"
            return 1
        }

        [ -n "$msg" ] || {
            git:error "commit message is empty"
            return 1
        }
        git:info --m:message "$msg" "committing with message"
    fi
    ___x_cmd___git_origin commit -m "${msg}" "$@"
}
