-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgit-context
More file actions
executable file
·120 lines (104 loc) · 3.33 KB
/
git-context
File metadata and controls
executable file
·120 lines (104 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
# git-context - Minimalist Git Context Generator for Commit Messages
# Generates essential git-related context to help LLMs create meaningful commit messages
# Get script directory (works even when called from other directories)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
# Default values
RECENT_COMMITS=3
PROMPT_FILE="$SCRIPT_DIR/prompts/commit_prompt.txt"
INCLUDE_PROMPT=true
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--recent-commits=*) RECENT_COMMITS="${1#*=}"; shift ;;
--prompt=*) PROMPT_FILE="${1#*=}"; shift ;;
--no-prompt) INCLUDE_PROMPT=false; shift ;;
--help|-h)
echo "Usage: git-context [options]"
echo ""
echo "Options:"
echo " --recent-commits=<num> Show most recent N commits for context (default: 3)"
echo " --prompt=<file> Use custom commit message prompt from file (default: prompts/commit_prompt.txt)"
echo " --no-prompt Don't include commit message prompt"
echo " --help, -h Show this help message"
exit 0
;;
*) echo "Unknown parameter: $1"; exit 1 ;;
esac
done
# Ensure we're in a git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Error: Not a git repository" >&2
exit 1
fi
# Begin output
echo "# Git Context for Commit Message Generation"
echo ""
# Show git status
status_output=$(git status --porcelain)
if [ -n "$status_output" ]; then
echo "## Git Status"
echo ""
echo '```'
git status -s
echo '```'
echo ""
fi
# Show changes using git diff HEAD
diff_output=$(git diff HEAD)
if [ -n "$diff_output" ]; then
echo "## Current Changes (Diff)"
echo ""
echo '```diff'
echo "$diff_output"
echo '```'
echo ""
fi
# Show summary of files changed
if [ -n "$status_output" ]; then
echo "## Files Changed"
echo ""
echo "$status_output" | while read -r line; do
# Extract the status code (first two characters)
status_code="${line:0:2}"
# Extract the filename (everything after the status code and whitespace)
file=${line#??}
file=${file#"${file%%[! ]*}"} # Remove leading whitespace
status_desc=""
# Determine the status description
case "$status_code" in
"M "|" M") status_desc="modified" ;;
"A ") status_desc="added" ;;
"D ") status_desc="deleted" ;;
"R ") status_desc="renamed" ;;
"C ") status_desc="copied" ;;
"??") status_desc="untracked" ;;
*) status_desc="$status_code" ;;
esac
echo "- $file ($status_desc)"
done
echo ""
fi
# Show recent commits
if [ "$RECENT_COMMITS" -gt 0 ]; then
echo "## Recent Commits (for reference)"
echo ""
git log -n "$RECENT_COMMITS" --pretty=format:"- %h: %s (%an, %ar)" | while read -r line; do
echo "$line"
done
echo ""
fi
# Include commit message prompt
if [ "$INCLUDE_PROMPT" = true ]; then
echo "## Commit Message Guidance"
echo ""
# Check if prompt file exists
if [ -f "$PROMPT_FILE" ]; then
cat "$PROMPT_FILE"
else
echo "Error: Prompt file not found: $PROMPT_FILE" >&2
exit 1
fi
echo ""
fi
exit 0