name: Check Line Counts on: pull_request_target: # Cancel the workflow in progress in newer build is about to start. concurrency: group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} cancel-in-progress: true jobs: checkbranch: name: Check PR Branch status runs-on: ubuntu-latest outputs: branchstat: ${{ steps.brstat.outputs.stat}} steps: - name: Check code from PR branch uses: actions/checkout@v6 with: repository: ${{ github.event.pull_request.head.repo.full_name }} ref: ${{ github.event.pull_request.head.sha }} fetch-depth: 0 # PR code is only inspected with git rev-list, never executed allow-unsafe-pr-checkout: true persist-credentials: false - name: Check whether branch is up-to-date id: brstat run: | # fetch master from the base repo (tinygrad/tinygrad on GitHub, the mirror on Gitea), not the PR head remote git fetch "${{ github.event.pull_request.base.repo.clone_url }}" master echo "${{ github.event.pull_request.head.sha }}" git rev-list --left-right --count FETCH_HEAD...${{ github.event.pull_request.head.sha }} | awk '{print "Behind "$1" - Ahead "$2""}' count=$(git rev-list --left-right --count FETCH_HEAD...${{ github.event.pull_request.head.sha }} | awk '{print $1}') if [ $count -gt 0 ] then echo "Current branch is behind ${{ github.event.pull_request.base.repo.full_name }} master branch!" echo "stat=true" >> "$GITHUB_OUTPUT" else echo "stat=false" >> "$GITHUB_OUTPUT" fi szdiff: name: Core Library Line Difference permissions: contents: read pull-requests: write runs-on: ubuntu-latest needs: checkbranch if: needs.checkbranch.outputs.branchstat == 'false' steps: - name: Checkout code from PR branch uses: actions/checkout@v6 with: repository: ${{ github.event.pull_request.head.repo.full_name }} ref: ${{ github.event.pull_request.head.sha }} path: pr # PR code is only line-counted by master's sz.py, never executed allow-unsafe-pr-checkout: true persist-credentials: false # the base default to tinygrad master and cannot be other fork branch for security purpose - name: Checkout code from tinygrad master uses: actions/checkout@v6 with: path: base - name: Set up Python 3.12 uses: actions/setup-python@v6 with: python-version: '3.12' - name: Count Line Diff run: | BASE="$GITHUB_WORKSPACE/base" PR="$GITHUB_WORKSPACE/pr" pip install tabulate $BASE cp "$BASE/sz.py" . python sz.py "$BASE" "$PR" > loc_content.txt - name: Comment Code Line Diff continue-on-error: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} MARKER: "### Changes" BODY_FILE: loc_content.txt # note: run the script from the base checkout, never from the PR checkout run: python3 "$GITHUB_WORKSPACE/base/.github/workflows/sticky_comment.py" rebase: name: Core Library Line Difference permissions: pull-requests: write runs-on: ubuntu-latest needs: checkbranch if: needs.checkbranch.outputs.branchstat == 'true' steps: # pull_request_target: a plain checkout gets the base repo, so no PR code is executed - uses: actions/checkout@v6 - name: Comment Rebase continue-on-error: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} MARKER: "line count difference bot is disabled" MESSAGE: | This branch currently is behind ${{ github.event.pull_request.base.repo.full_name }} master. The line count difference bot is disabled. run: python3 .github/workflows/sticky_comment.py