Skip to content

syncupstream

syncupstream #202

Workflow file for this run

name: syncupstream
on:
schedule:
- cron: '0 2 * * *'
workflow_dispatch:
jobs:
sync:
runs-on: ubuntu-latest
steps:
# fetch-depth is important for all to work well
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Git user
run: |
git config --local user.email "actions@github.com"
git config --local user.name "GitHub Actions"
- run: |
git remote add upstream https://github.com/minlearn/inst.git || true
git fetch origin
git fetch upstream
git checkout master
- name: Calculate ahead/behind
id: ab
run: |
ahead=$(git rev-list --count upstream/master..master)
behind=$(git rev-list --count master..upstream/master)
echo "ahead=$ahead"
echo "behind=$behind"
echo "ahead=$ahead" >> $GITHUB_OUTPUT
echo "behind=$behind" >> $GITHUB_OUTPUT
- name: Check for common ancestor
id: ancestor
run: |
ancestor=$(git merge-base master upstream/master || echo "")
if [ -z "$ancestor" ]; then
echo "common_ancestor=0" >> $GITHUB_OUTPUT
else
echo "common_ancestor=1" >> $GITHUB_OUTPUT
fi
- name: First merge (no common ancestor, allow unrelated histories)
if: steps.ancestor.outputs.common_ancestor == '0'
run: |
# 1,ours改为theirs
git merge upstream/master --allow-unrelated-histories -X theirs --no-edit
# 2,begin 特殊处理inst.sh为本地内容
if git ls-files --error-unmatch inst.sh > /dev/null 2>&1; then
git checkout --ours inst.sh
git add inst.sh
git commit --amend --no-edit || git commit -m "Preserve inst.sh (ours) on first merge"
fi
# end 2,特殊处理inst.sh为本地内容
git push origin master
echo "首次同步,无共同祖先,已合并 upstream 内容并推送。"
- name: Normal merge (has common ancestor and behind)
if: steps.ancestor.outputs.common_ancestor == '1' && steps.ab.outputs.behind != '0'
run: |
# 1,ours改为theirs
git merge upstream/master -X theirs --no-edit
# 2,begin 特殊处理inst.sh为本地内容
if git ls-files --error-unmatch inst.sh > /dev/null 2>&1; then
git checkout --ours inst.sh
git add inst.sh
git commit --amend --no-edit || git commit -m "Preserve inst.sh (ours) in merge"
fi
# end 2,特殊处理inst.sh为本地内容
git push origin master
echo "正常同步,已合并 upstream 的新内容并推送。"
- name: Already up-to-date
if: steps.ancestor.outputs.common_ancestor == '1' && steps.ab.outputs.behind == '0'
run: |
echo "本地已与 upstream 同步,无需合并。"