-
-
Save Dan-Q/c4317d36354cc13989f415d8aaa8ea54 to your computer and use it in GitHub Desktop.
Post-commit hook that writes logs to .agent-logs/ "as if" an AI agent had written the code. There is no good reason to use this, but you can read the story of why it exists at https://danq.me/ai-agent-logging/
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
| #!/bin/sh | |
| ################################################################################ | |
| # Half-joking post-commit hook that adds an .agent-log/ entry to each commit, # | |
| # identifying the AI agent that modified the code... even if an AI agent was # | |
| # not actually used! # | |
| # # | |
| # Place in .git/hooks/post-commit. Probably do not check-in to your repo! # | |
| # # | |
| # More information: # | |
| # https://danq.me/ai-agent-logging/ # | |
| ################################################################################ | |
| # Avoid infinite recursion: | |
| if [ "$GIT_AGENT_POST_COMMIT_AMEND" = "1" ]; then | |
| exit 0 | |
| fi | |
| # Determine the repository root and agent logs directory: | |
| REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" | |
| LOG_DIR="$REPO_ROOT/.agent-logs" | |
| # Use the current branch name as the log filename (fallback to 'HEAD' if detached) | |
| BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo HEAD)" | |
| LOG_FILE="$LOG_DIR/$BRANCH_NAME" | |
| # Get the first line (subject) of the last commit message | |
| FIRST_LINE="$(git log -1 --pretty=%s)" | |
| if [ -n "$FIRST_LINE" ]; then | |
| # Ensure the log directory exists: | |
| mkdir -p "$LOG_DIR" | |
| # List of all possible AI agents to 'simulate': | |
| AI_AGENT_LIST="agent stardust frantic" | |
| # Choose one at random: | |
| set -- $AI_AGENT_LIST | |
| WORD_COUNT=$# | |
| INDEX=$((RANDOM % WORD_COUNT + 1)) | |
| i=1 | |
| for w in "$@"; do | |
| if [ "$i" -eq "$INDEX" ]; then | |
| PREFIX="$w" | |
| break | |
| fi | |
| i=$((i + 1)) | |
| done | |
| # Log what the AI agent "did" in this format: | |
| echo "[$PREFIX] $FIRST_LINE" >> "$LOG_FILE" | |
| # Stage the updated log file and amend the just-created commit with it: | |
| # (we set GIT_AGENT_POST_COMMIT_AMEND=1 to avoid infinite recursion) | |
| GIT_AGENT_POST_COMMIT_AMEND=1 git add "$LOG_FILE" && \ | |
| GIT_AGENT_POST_COMMIT_AMEND=1 git commit --amend --no-edit | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment