Skip to content

Trim Core and Package History #4

Trim Core and Package History

Trim Core and Package History #4

name: Trim Core and Package History
on:
schedule:
- cron: "0 18 * * 3"
workflow_dispatch:
jobs:
trim:
runs-on: ubuntu-latest
strategy:
matrix:
branch: [core, package]
steps:
- name: Apt Update
env:
DEBIAN_FRONTEND: noninteractive
run: |
sudo apt-get update
sudo apt-get -y install git
- name: Trim History
env:
REPO_URL: https://github.com/vernesong/OpenClash.git
BRANCH: ${{ matrix.branch }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
if [ "${BRANCH}" = "core" ]; then
MAX_COMMITS=20
LIMIT_COMMITS=10
else
MAX_COMMITS=50
LIMIT_COMMITS=30
fi
echo "Start full clone for history check (branch: ${BRANCH})..."
mkdir -p ../full_clone_tmp
cd ../full_clone_tmp
git clone --branch "${BRANCH}" --single-branch --no-tags --progress --depth=1 "$REPO_URL" .
MAX_FETCH_RETRIES=3
FETCH_OK=false
for i in $(seq 1 $MAX_FETCH_RETRIES); do
if git fetch --unshallow; then
FETCH_OK=true
break
else
echo "git fetch --unshallow failed (attempt $i/$MAX_FETCH_RETRIES), retrying..." >&2
sleep 2
fi
done
if [ "$FETCH_OK" = false ]; then
echo "Warning: git fetch --unshallow failed after $MAX_FETCH_RETRIES attempts, proceeding with available history." >&2
fi
count=$(git rev-list --count HEAD 2>/dev/null || echo 0)
echo "Current commit count on ${BRANCH} branch: $count"
if [ "$count" -le "$MAX_COMMITS" ]; then
echo "No trimming needed (commit count: $count <= $MAX_COMMITS)."
cd ..
rm -rf full_clone_tmp
exit 0
fi
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
echo "Collecting recent commits..."
mapfile -t recent_commits < <(git rev-list --max-count=$LIMIT_COMMITS HEAD)
echo "Looking for sync commit (grep 'Release: Auto sync')..."
sync_commit=$(git log --grep='Release: Auto sync' --format='%H' | tail -n 1 || true)
keep_commits=("${recent_commits[@]}")
if [ -n "$sync_commit" ]; then
found=false
for c in "${recent_commits[@]}"; do
if [ "$c" = "$sync_commit" ]; then
found=true
break
fi
done
if [ "$found" = false ]; then
keep_commits+=("$sync_commit")
fi
fi
echo "Rebuilding trimmed branch from kept commits (no extra commits)..."
ORIG_HEAD=$(git rev-parse HEAD)
LAST_COMMIT=""
for c in $(printf "%s\n" "${keep_commits[@]}" | tac); do
echo "Processing commit $c ..."
git checkout -- . || true
git checkout "$c" -- . || true
git add -A || true
TREE_ID=$(git write-tree)
AUTHOR_NAME=$(git show -s --format='%an' $c)
AUTHOR_EMAIL=$(git show -s --format='%ae' $c)
COMMIT_DATE=$(git show -s --format='%ad' --date=iso-strict $c)
COMMIT_MSG=$(git log --format=%B -n 1 $c)
echo "Commit message for $c:"
printf "%s\n" "$COMMIT_MSG"
MSG_FILE=$(mktemp)
printf "%s\n" "$COMMIT_MSG" > "$MSG_FILE"
if [ -z "$LAST_COMMIT" ]; then
NEW_COMMIT=$(GIT_AUTHOR_NAME="$AUTHOR_NAME" GIT_AUTHOR_EMAIL="$AUTHOR_EMAIL" GIT_AUTHOR_DATE="$COMMIT_DATE" git commit-tree $TREE_ID -F "$MSG_FILE")
else
NEW_COMMIT=$(GIT_AUTHOR_NAME="$AUTHOR_NAME" GIT_AUTHOR_EMAIL="$AUTHOR_EMAIL" GIT_AUTHOR_DATE="$COMMIT_DATE" git commit-tree $TREE_ID -p "$LAST_COMMIT" -F "$MSG_FILE")
fi
rm -f "$MSG_FILE"
LAST_COMMIT=$NEW_COMMIT
done
if [ -n "$LAST_COMMIT" ]; then
git update-ref refs/heads/__tmp_history_trim $LAST_COMMIT
echo "Built commit: $LAST_COMMIT"
if [ -n "${GITHUB_TOKEN:-}" ]; then
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/vernesong/OpenClash.git"
fi
git push --force --quiet origin "$LAST_COMMIT:refs/heads/${BRANCH}" || true
echo "Trim complete. New commit count (remote unchanged locally until fetched): $(git rev-list --count HEAD)"
else
echo "No kept commits; skipping trim." >&2
fi
cd ..
rm -rf full_clone_tmp