Last active
October 27, 2025 19:13
-
-
Save bestdan/9fe5254ab13fe074d1d3cf63761f243e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env bash | |
| # Create a git commit message using claude | |
| gcaim() { | |
| local staged_diff | |
| staged_diff=$(git diff --cached 2>/dev/null) | |
| if [[ -n "$staged_diff" ]]; then | |
| local prompt | |
| read -r -d '' prompt <<'EOF' | |
| Generate a commit message from the git diff provided via stdin. | |
| Output format: Plain text only. No markdown. No code blocks. No explanations. | |
| Start immediately with the commit message in Conventional Commits format: | |
| <type>(<scope>): <description> | |
| <optional body> | |
| Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore | |
| Do not ask questions. Do not add commentary. Output only the commit message. | |
| EOF | |
| local output | |
| local error_output | |
| error_output=$(mktemp) | |
| output=$(echo "$staged_diff" | claude -p "$prompt" 2>"$error_output") | |
| local claude_exit=$? | |
| if [[ $claude_exit -ne 0 ]]; then | |
| echo "Error: Failed to generate commit message with Claude (exit code: $claude_exit)" >&2 | |
| if [[ -s "$error_output" ]]; then | |
| echo "Error details: Check logs for more information" >&2 | |
| fi | |
| rm -f "$error_output" | |
| return 1 | |
| fi | |
| rm -f "$error_output" | |
| local filtered_output=$(command grep -vE '^(diff --git|new file mode|deleted file mode|index )' <<< "$output" || true) | |
| echo "${filtered_output:-$output}" | |
| return 0 | |
| else | |
| echo "No changes staged for commit. Use 'git add' first." >&2 | |
| return 1 | |
| fi | |
| } | |
| # Create a git commit message using claude, and ask for approval before committing it | |
| gcai() { | |
| local commit_msg | |
| commit_msg=$(gcaim) | |
| local exit_code=$? | |
| if [[ $exit_code -ne 0 ]]; then | |
| return $exit_code | |
| fi | |
| echo "Generated commit message:" | |
| echo "------------------------" | |
| echo "$commit_msg" | |
| echo "------------------------" | |
| echo -n "Use this commit message? (y/n): " | |
| if ! read -r -t 30 response; then | |
| echo "" | |
| echo "Error: Timeout or EOF while waiting for response" >&2 | |
| return 1 | |
| fi | |
| if [[ -z "$response" ]]; then | |
| echo "Commit cancelled." | |
| return 1 | |
| fi | |
| if [[ "$response" =~ ^[Yy]$ ]]; then | |
| git commit -m "$commit_msg" | |
| else | |
| echo "Commit cancelled." | |
| fi | |
| } | |
| # Generate a GitHub PR description using claude based on changes between main and current branch | |
| prdai() { | |
| # Check if we're in a git repository | |
| if ! git rev-parse --git-dir > /dev/null 2>&1; then | |
| echo "Error: Not in a git repository" >&2 | |
| return 1 | |
| fi | |
| # Get the current branch name | |
| local current_branch | |
| current_branch=$(git branch --show-current) | |
| if [[ -z "$current_branch" ]]; then | |
| echo "Error: Unable to determine current branch" >&2 | |
| return 1 | |
| fi | |
| # Check if we're on main branch | |
| if [[ "$current_branch" == "main" ]]; then | |
| echo "Error: Currently on main branch. Switch to a feature branch first." >&2 | |
| return 1 | |
| fi | |
| # Get the diff between main and current branch | |
| local branch_diff | |
| branch_diff=$(git diff main...HEAD 2>/dev/null) | |
| if [[ -z "$branch_diff" ]]; then | |
| echo "Error: No changes found between main and current branch ($current_branch)" >&2 | |
| return 1 | |
| fi | |
| local prompt | |
| read -r -d '' prompt <<'EOF' | |
| Generate a GitHub pull request description from the git diff provided via stdin. | |
| Output format: Plain text with markdown formatting. Use standard GitHub PR description structure. | |
| Structure: | |
| ## Summary | |
| Brief overview of what this PR accomplishes (2-3 sentences max) | |
| ## Changes | |
| - Bullet point list of key changes | |
| - Focus on what changed and why | |
| - Group related changes together | |
| ## Testing | |
| Brief notes on how to test these changes or what was tested | |
| Start immediately with the PR description. Do not ask questions. Do not add meta-commentary about the PR itself. | |
| EOF | |
| local output | |
| local error_output | |
| error_output=$(mktemp) | |
| output=$(echo "$branch_diff" | claude -p "$prompt" 2>"$error_output") | |
| local claude_exit=$? | |
| if [[ $claude_exit -ne 0 ]]; then | |
| echo "Error: Failed to generate PR description with Claude (exit code: $claude_exit)" >&2 | |
| if [[ -s "$error_output" ]]; then | |
| echo "Error details: Check logs for more information" >&2 | |
| fi | |
| rm -f "$error_output" | |
| return 1 | |
| fi | |
| rm -f "$error_output" | |
| echo "$output" | |
| return 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment