-
Notifications
You must be signed in to change notification settings - Fork 0
169 lines (137 loc) · 5.72 KB
/
Copy pathcleanup-history.yml
File metadata and controls
169 lines (137 loc) · 5.72 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
name: Cleanup gh-pages History
on:
schedule:
# Run on the 1st of every month at 3:00 AM UTC
- cron: '0 3 1 * *'
workflow_dispatch:
inputs:
keep_days:
description: 'Keep commits from last N days (default: 30)'
required: false
type: number
default: 30
dry_run:
description: 'Dry run (show what would be done without pushing)'
required: false
type: boolean
default: false
permissions:
contents: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
fetch-depth: 0 # Full history needed for squashing
- name: Cleanup old history
shell: bash
run: |
set -e
KEEP_DAYS="${{ github.event.inputs.keep_days || 30 }}"
DRY_RUN="${{ github.event.inputs.dry_run || 'false' }}"
echo "=== gh-pages History Cleanup ==="
echo "Keep commits from last: ${KEEP_DAYS} days"
echo "Dry run: ${DRY_RUN}"
echo ""
# Get current branch info
CURRENT_COMMITS=$(git rev-list --count HEAD)
CURRENT_SIZE=$(du -sh .git | cut -f1)
echo "Current state:"
echo " Commits: ${CURRENT_COMMITS}"
echo " .git size: ${CURRENT_SIZE}"
echo ""
# Find the cutoff date
CUTOFF_DATE=$(date -d "-${KEEP_DAYS} days" +%Y-%m-%d 2>/dev/null || date -v-${KEEP_DAYS}d +%Y-%m-%d)
echo "Cutoff date: ${CUTOFF_DATE}"
# Find commits to keep (after cutoff date)
COMMITS_TO_KEEP=$(git rev-list --after="${CUTOFF_DATE}" HEAD | wc -l | tr -d ' ')
echo "Commits after cutoff: ${COMMITS_TO_KEEP}"
if [ "${COMMITS_TO_KEEP}" -eq 0 ]; then
echo "No recent commits found, keeping at least the latest commit"
COMMITS_TO_KEEP=1
fi
# If we have very few commits, nothing to clean
if [ "${CURRENT_COMMITS}" -le "${COMMITS_TO_KEEP}" ]; then
echo ""
echo "Nothing to clean up - all commits are within retention period"
exit 0
fi
COMMITS_TO_SQUASH=$((CURRENT_COMMITS - COMMITS_TO_KEEP))
echo "Commits to squash: ${COMMITS_TO_SQUASH}"
echo ""
# Show what will be removed
echo "=== Commits to be squashed ==="
git log --oneline --skip=${COMMITS_TO_KEEP} -20 || true
if [ "${COMMITS_TO_SQUASH}" -gt 20 ]; then
echo "... and $((COMMITS_TO_SQUASH - 20)) more"
fi
echo ""
if [ "${DRY_RUN}" = "true" ]; then
echo "=== DRY RUN - No changes made ==="
exit 0
fi
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Create a new orphan branch with squashed history
echo "=== Squashing history ==="
# Get the commit hash to start from (first commit to keep)
FIRST_KEPT_COMMIT=$(git rev-list --after="${CUTOFF_DATE}" HEAD | tail -1)
if [ -z "${FIRST_KEPT_COMMIT}" ]; then
FIRST_KEPT_COMMIT=$(git rev-parse HEAD)
fi
# Create orphan branch from the state before our retention period
# with all current files
git checkout --orphan temp-cleanup
# Stage all current files
git add -A
# Create initial squashed commit
git commit -m "chore: squash history (commits before ${CUTOFF_DATE})
This commit contains the squashed history of gh-pages branch.
Original commits before ${CUTOFF_DATE} have been combined to reduce repository size.
Squashed ${COMMITS_TO_SQUASH} commits.
🤖 Generated by cleanup-history workflow"
# Now cherry-pick the recent commits on top
if [ "${COMMITS_TO_KEEP}" -gt 0 ]; then
echo "Cherry-picking ${COMMITS_TO_KEEP} recent commits..."
RECENT_COMMITS=$(git rev-list --reverse --after="${CUTOFF_DATE}" gh-pages)
for commit in ${RECENT_COMMITS}; do
# Skip if this would result in empty commit
git cherry-pick --allow-empty "${commit}" || {
echo " Skipping commit ${commit} (likely empty or conflict)"
git cherry-pick --abort 2>/dev/null || true
}
done
fi
# Force update gh-pages
git branch -D gh-pages
git branch -m gh-pages
# Garbage collect
echo ""
echo "=== Running garbage collection ==="
git reflog expire --expire=now --all
git gc --prune=now --aggressive
NEW_SIZE=$(du -sh .git | cut -f1)
NEW_COMMITS=$(git rev-list --count HEAD)
echo ""
echo "=== Cleanup complete ==="
echo "Before: ${CURRENT_COMMITS} commits, ${CURRENT_SIZE}"
echo "After: ${NEW_COMMITS} commits, ${NEW_SIZE}"
- name: Push cleaned history
if: ${{ github.event.inputs.dry_run != 'true' }}
run: |
git push origin gh-pages --force
- name: Summary
run: |
if [ "${{ github.event.inputs.dry_run }}" = "true" ]; then
echo "## Dry Run Complete" >> $GITHUB_STEP_SUMMARY
echo "No changes were pushed." >> $GITHUB_STEP_SUMMARY
else
echo "## History Cleanup Complete" >> $GITHUB_STEP_SUMMARY
echo "The gh-pages branch history has been squashed." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Old commits have been combined into a single commit to reduce repository size." >> $GITHUB_STEP_SUMMARY
fi