Skip to content

Instantly share code, notes, and snippets.

@schacon
Created July 23, 2025 15:43
Show Gist options
  • Save schacon/a3683d51175f0240b900d4c224dbc676 to your computer and use it in GitHub Desktop.
Save schacon/a3683d51175f0240b900d4c224dbc676 to your computer and use it in GitHub Desktop.
Claude Code Stop hook to auto-commit with the prompt
#!/usr/bin/env ruby
require 'json'
require 'tempfile'
# Read input from stdin
input_data = $stdin.read
# Parse JSON, exit non-zero if it fails
parsed_input = JSON.parse(input_data)
# Extract transcript path and working directory
transcript_path = parsed_input.dig('transcript_path')
cwd = parsed_input.dig('cwd')
# Change to the specified working directory
Dir.chdir(cwd) if cwd
# Read all entries and find the last user message with string content
last_user_entry = File.foreach(transcript_path)
.map { |line| JSON.parse(line.strip) }
.reverse
.find { |entry| entry.dig('message', 'role') == 'user' && entry.dig('message', 'content').is_a?(String) }
# Extract message content from the last user entry
message_content = last_user_entry&.dig('message', 'content')
# Create temporary file with the message content
temp_file = Tempfile.new('commit_message')
begin
temp_file.write(message_content)
temp_file.close
short_message = message_content.split("\n").first[0..50]
system("git", "add", "-A")
system("git", "commit", "-F", temp_file.path)
system("osascript", "-e", "display notification \"#{short_message}\" with title \"✅ Claude Commit\" sound name \"Glass\"")
ensure
temp_file.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment