-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsavepatch
More file actions
executable file
·62 lines (49 loc) · 1.63 KB
/
savepatch
File metadata and controls
executable file
·62 lines (49 loc) · 1.63 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
#!/bin/bash
# Check if commit hash is provided
if [ $# -ne 1 ]; then
echo "Usage: save-patch COMMIT_HASH"
echo "Example: savepatch 43184d474a753ede9c2ccecb1396e24604bd613e"
exit 1
fi
COMMIT="$1"
# Validate that we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Error: Not in a git repository"
exit 1
fi
# Validate commit exists
if ! git rev-parse --verify "$COMMIT" > /dev/null 2>&1; then
echo "Error: Commit '$COMMIT' not found"
exit 1
fi
# Create patches directory if it doesn't exist
mkdir -p patches
# Find the highest numbered patch file
highest_num=0
if ls patches/*.patch > /dev/null 2>&1; then
for patch_file in patches/*.patch; do
# Extract number from filename (assuming format NNNN-*.patch)
filename=$(basename "$patch_file")
if [[ $filename =~ ^([0-9]{4})- ]]; then
num=${BASH_REMATCH[1]}
# Remove leading zeros for comparison
num=$((10#$num))
if [ $num -gt $highest_num ]; then
highest_num=$num
fi
fi
done
fi
# Next number is highest + 1
next_num=$((highest_num + 1))
# Get commit subject line and make it filename-safe
subject=$(git log -1 --pretty=format:%s "$COMMIT" | tr '[:upper:]' '[:lower:]' | tr -cs 'a-z0-9' '-')
# Remove trailing dashes
subject=$(echo "$subject" | sed 's/-*$//')
# Format number prefix (4 digits with leading zeros)
num_prefix=$(printf "%04d" "$next_num")
# Generate patch filename
patch_filename="patches/${num_prefix}-${subject}.patch"
# Create the patch file
git diff "${COMMIT}^!" > "$patch_filename"
echo "Saved $patch_filename"