Backup Fork and Sync #22
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Backup Fork and Sync | |
on: | |
schedule: | |
- cron: '0 0 * * 1' # 每週日午夜執行 | |
workflow_dispatch: # 允許手動觸發 | |
env: | |
UPSTREAM_REPO: ${{ vars.UPSTREAM_REPO || 'https://github.com/abetlen/llama-cpp-python.git' }} | |
MAIN_BRANCH: ${{ vars.MAIN_BRANCH || 'main' }} | |
jobs: | |
backup-and-sync: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.PAT_TOKEN }} | |
- name: Configure Git | |
run: | | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
- name: Tag current state | |
run: | | |
TIMESTAMP=$(date +"%Y%m%d%H%M%S") | |
git tag -a "pre-sync-$TIMESTAMP" -m "Pre-sync state on $TIMESTAMP" | |
git push origin "pre-sync-$TIMESTAMP" | |
- name: Add upstream repository | |
run: | | |
git remote add upstream ${{ env.UPSTREAM_REPO }} || git remote set-url upstream ${{ env.UPSTREAM_REPO }} | |
git fetch --all --tags | |
# 同步上游的標籤 | |
- name: Sync tags from upstream | |
run: | | |
echo "Syncing tags from upstream..." | |
# 保存同步狀態標籤 | |
SYNC_TAGS=$(git tag -l "pre-sync-*" "post-sync-*") | |
# 刪除非同步狀態的標籤 | |
git tag -l | grep -v "^pre-sync-\|^post-sync-" | xargs -r git tag -d | |
# 獲取上游標籤 | |
git fetch upstream --tags | |
# 重新添加同步狀態標籤(如果有) | |
if [ ! -z "$SYNC_TAGS" ]; then | |
echo "$SYNC_TAGS" | while read tag; do | |
git tag -f "$tag" $(git rev-list -n 1 "$tag") | |
done | |
fi | |
# 推送所有標籤 | |
git push origin --tags --force | |
- name: Sync and handle conflicts | |
run: | | |
branches=$(git branch -r | grep 'origin/' | grep -v 'origin/HEAD' | sed 's/origin\///') | |
for branch in $branches; do | |
echo "Processing branch: $branch" | |
if git ls-remote --exit-code --heads upstream $branch > /dev/null 2>&1; then | |
# Force reset the branch to its remote state | |
git checkout -B $branch origin/$branch --force | |
# Forcefully remove untracked files and directories | |
git clean -fdx | |
# Reset any changes in tracked files | |
git reset --hard origin/$branch | |
echo "Changes in upstream $branch:" | |
git log --oneline $branch..upstream/$branch | |
if git merge upstream/$branch --no-edit --allow-unrelated-histories; then | |
echo "Successfully merged changes for $branch" | |
else | |
echo "Merge conflict in $branch. Creating a new branch with upstream changes." | |
git merge --abort | |
conflict_branch="${branch}-upstream-changes" | |
git checkout -b $conflict_branch upstream/$branch | |
echo "Created new branch $conflict_branch with upstream changes" | |
git push origin $conflict_branch | |
echo "Please manually review and merge changes from $conflict_branch into $branch" | |
git checkout $branch | |
fi | |
git push origin $branch || echo "Failed to push $branch, please check and push manually if needed" | |
else | |
echo "Branch $branch does not exist in upstream. Skipping." | |
fi | |
done | |
- name: Create post-sync version tag | |
run: | | |
TIMESTAMP=$(date +"%Y%m%d%H%M%S") | |
git tag -a "post-sync-$TIMESTAMP" -m "Post-sync state on $TIMESTAMP" | |
git push origin "post-sync-$TIMESTAMP" | |
- name: Generate sync report | |
if: always() | |
run: | | |
echo "Sync Report" > sync_report.md | |
echo "===========" >> sync_report.md | |
echo "" >> sync_report.md | |
echo "## Tags Sync Status" >> sync_report.md | |
echo "Upstream tags:" >> sync_report.md | |
git ls-remote --tags upstream | awk '{print $2}' | sed 's/refs\/tags\///' >> sync_report.md | |
echo "" >> sync_report.md | |
echo "Local tags:" >> sync_report.md | |
git tag -l >> sync_report.md | |
echo "" >> sync_report.md | |
git branch -r | grep 'origin/' | grep -v 'origin/HEAD' | sed 's/origin\///' | while read branch; do | |
echo "## Branch: $branch" >> sync_report.md | |
if git log HEAD..origin/$branch --oneline | grep -q .; then | |
echo "Status: Changes synced" >> sync_report.md | |
echo "Changes:" >> sync_report.md | |
git log HEAD..origin/$branch --oneline >> sync_report.md | |
else | |
echo "Status: No changes or sync failed" >> sync_report.md | |
fi | |
echo "" >> sync_report.md | |
done | |
- name: Upload sync report | |
if: always() | |
uses: actions/upload-artifact@v4 | |
with: | |
name: sync-report | |
path: sync_report.md |