|
| 1 | +name: Trim Core and Package History |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 18 * * 3" |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + trim: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + strategy: |
| 12 | + matrix: |
| 13 | + branch: [core, package] |
| 14 | + steps: |
| 15 | + - name: Apt Update |
| 16 | + env: |
| 17 | + DEBIAN_FRONTEND: noninteractive |
| 18 | + run: | |
| 19 | + sudo apt-get update |
| 20 | + sudo apt-get -y install git |
| 21 | +
|
| 22 | + - name: Trim History |
| 23 | + env: |
| 24 | + REPO_URL: https://github.com/vernesong/OpenClash.git |
| 25 | + BRANCH: ${{ matrix.branch }} |
| 26 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | +
|
| 30 | + if [ "${BRANCH}" = "core" ]; then |
| 31 | + MAX_COMMITS=20 |
| 32 | + LIMIT_COMMITS=10 |
| 33 | + else |
| 34 | + MAX_COMMITS=50 |
| 35 | + LIMIT_COMMITS=30 |
| 36 | + fi |
| 37 | +
|
| 38 | + echo "Start full clone for history check (branch: ${BRANCH})..." |
| 39 | + mkdir -p ../full_clone_tmp |
| 40 | + cd ../full_clone_tmp |
| 41 | + git clone --branch "${BRANCH}" --single-branch --no-tags --progress --depth=1 "$REPO_URL" . |
| 42 | +
|
| 43 | + MAX_FETCH_RETRIES=3 |
| 44 | + FETCH_OK=false |
| 45 | + for i in $(seq 1 $MAX_FETCH_RETRIES); do |
| 46 | + if git fetch --unshallow; then |
| 47 | + FETCH_OK=true |
| 48 | + break |
| 49 | + else |
| 50 | + echo "git fetch --unshallow failed (attempt $i/$MAX_FETCH_RETRIES), retrying..." >&2 |
| 51 | + sleep 2 |
| 52 | + fi |
| 53 | + done |
| 54 | + if [ "$FETCH_OK" = false ]; then |
| 55 | + echo "Warning: git fetch --unshallow failed after $MAX_FETCH_RETRIES attempts, proceeding with available history." >&2 |
| 56 | + fi |
| 57 | +
|
| 58 | + count=$(git rev-list --count HEAD 2>/dev/null || echo 0) |
| 59 | + echo "Current commit count on ${BRANCH} branch: $count" |
| 60 | + if [ "$count" -le "$MAX_COMMITS" ]; then |
| 61 | + echo "No trimming needed (commit count: $count <= $MAX_COMMITS)." |
| 62 | + cd .. |
| 63 | + rm -rf full_clone_tmp |
| 64 | + exit 0 |
| 65 | + fi |
| 66 | +
|
| 67 | + git config user.name 'github-actions[bot]' |
| 68 | + git config user.email 'github-actions[bot]@users.noreply.github.com' |
| 69 | +
|
| 70 | + echo "Collecting recent commits..." |
| 71 | + mapfile -t recent_commits < <(git rev-list --max-count=$LIMIT_COMMITS HEAD) |
| 72 | +
|
| 73 | + echo "Looking for sync commit (grep 'Release: Auto sync')..." |
| 74 | + sync_commit=$(git log --grep='Release: Auto sync' --format='%H' | tail -n 1 || true) |
| 75 | +
|
| 76 | + keep_commits=("${recent_commits[@]}") |
| 77 | + if [ -n "$sync_commit" ]; then |
| 78 | + found=false |
| 79 | + for c in "${recent_commits[@]}"; do |
| 80 | + if [ "$c" = "$sync_commit" ]; then |
| 81 | + found=true |
| 82 | + break |
| 83 | + fi |
| 84 | + done |
| 85 | + if [ "$found" = false ]; then |
| 86 | + keep_commits+=("$sync_commit") |
| 87 | + fi |
| 88 | + fi |
| 89 | +
|
| 90 | + echo "Rebuilding trimmed branch from kept commits (no extra commits)..." |
| 91 | + ORIG_HEAD=$(git rev-parse HEAD) |
| 92 | + LAST_COMMIT="" |
| 93 | + for c in $(printf "%s\n" "${keep_commits[@]}" | tac); do |
| 94 | + echo "Processing commit $c ..." |
| 95 | + git checkout -- . || true |
| 96 | + git checkout "$c" -- . || true |
| 97 | + git add -A || true |
| 98 | + TREE_ID=$(git write-tree) |
| 99 | + AUTHOR_NAME=$(git show -s --format='%an' $c) |
| 100 | + AUTHOR_EMAIL=$(git show -s --format='%ae' $c) |
| 101 | + COMMIT_DATE=$(git show -s --format='%ad' --date=iso-strict $c) |
| 102 | + COMMIT_MSG=$(git log --format=%B -n 1 $c) |
| 103 | + echo "Commit message for $c:" |
| 104 | + printf "%s\n" "$COMMIT_MSG" |
| 105 | +
|
| 106 | + MSG_FILE=$(mktemp) |
| 107 | + printf "%s\n" "$COMMIT_MSG" > "$MSG_FILE" |
| 108 | +
|
| 109 | + if [ -z "$LAST_COMMIT" ]; then |
| 110 | + 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") |
| 111 | + else |
| 112 | + 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") |
| 113 | + fi |
| 114 | + rm -f "$MSG_FILE" |
| 115 | + LAST_COMMIT=$NEW_COMMIT |
| 116 | + done |
| 117 | +
|
| 118 | + if [ -n "$LAST_COMMIT" ]; then |
| 119 | + git update-ref refs/heads/__tmp_history_trim $LAST_COMMIT |
| 120 | + echo "Built commit: $LAST_COMMIT" |
| 121 | +
|
| 122 | + if [ -n "${GITHUB_TOKEN:-}" ]; then |
| 123 | + git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/vernesong/OpenClash.git" |
| 124 | + fi |
| 125 | +
|
| 126 | + git push --force --quiet origin "$LAST_COMMIT:refs/heads/${BRANCH}" || true |
| 127 | + echo "Trim complete. New commit count (remote unchanged locally until fetched): $(git rev-list --count HEAD)" |
| 128 | + else |
| 129 | + echo "No kept commits; skipping trim." >&2 |
| 130 | + fi |
| 131 | +
|
| 132 | + cd .. |
| 133 | + rm -rf full_clone_tmp |
0 commit comments