post-commit.bash
1#!/bin/bash
2# pgit post-commit hook
3# Automatically closes issues referenced in commit messages using the bug CLI
4#
5# Usage: Install as .git/hooks/post-commit
6
7set -e
8
9# Check if bug is available
10if ! command -v bug &> /dev/null; then
11 echo "Warning: bug not found in PATH, skipping issue updates" >&2
12 exit 0
13fi
14
15# Get the commit hash (now correct - post-commit runs after commit)
16COMMIT_HASH=$(git rev-parse HEAD)
17if [ -z "$COMMIT_HASH" ]; then
18 echo "Warning: Could not determine commit hash, skipping issue updates" >&2
19 exit 0
20fi
21
22# Get the commit message from the just-created commit
23COMMIT_MSG=$(git log -1 --pretty=%B)
24
25# Extract issue references using regex
26# Matches: fixes #872a52d, close #abc1234, resolved #def5678, etc.
27# Case insensitive, captures the 7-char hex ID
28ISSUE_REFS=$(echo "$COMMIT_MSG" | grep -oEi '\b(fix|fixes|fixed|close|closes|closed|resolve|resolves|resolved)\b[[:space:]]*#[a-f0-9]{7}\b' || true)
29
30if [ -z "$ISSUE_REFS" ]; then
31 # No issue references found
32 exit 0
33fi
34
35# Process each reference
36echo "$ISSUE_REFS" | while read -r ref; do
37 # Extract the issue ID (the hex part after #)
38 issue_id=$(echo "$ref" | grep -oE '#[a-f0-9]{7}\b' | sed 's/^#//i' | tr '[:upper:]' '[:lower:]')
39
40 if [ -z "$issue_id" ]; then
41 continue
42 fi
43
44 echo "Processing issue #$issue_id..." >&2
45
46 # Add comment with commit reference
47 comment="Fixed in commit $COMMIT_HASH"
48
49 # Try to add comment - don't fail the commit if this fails
50 if bug agent comment "$issue_id" --message "$comment" 2>/dev/null; then
51 echo " Added comment to issue #$issue_id" >&2
52 else
53 echo " Warning: Failed to add comment to issue #$issue_id" >&2
54 fi
55
56 # Try to close the issue - don't fail the commit if this fails
57 if bug agent close "$issue_id" 2>/dev/null; then
58 echo " Closed issue #$issue_id" >&2
59 else
60 echo " Warning: Failed to close issue #$issue_id" >&2
61 fi
62done
63
64exit 0