diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000000..c618a9fd130 --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +BasedOnStyle: Google +IndentWidth: 4 +ColumnLimit: 100 +AllowShortFunctionsOnASingleLine: Empty +DerivePointerAlignment: false +PointerAlignment: Left +ReflowComments: true diff --git a/.github/workflows/pre-formatting.yml b/.github/workflows/pre-formatting.yml new file mode 100644 index 00000000000..f0cdff4c7bf --- /dev/null +++ b/.github/workflows/pre-formatting.yml @@ -0,0 +1,104 @@ +name: "Pre-commit Format & Lint" +on: + pull_request: + branches: + - develop + - release-staging/rocm-rel-* +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - name: Generate a token + id: generate-token + uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + + - name: Checkout code (initial) + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + ref: refs/pull/${{ github.event.pull_request.number }}/merge + sparse-checkout: .github + sparse-checkout-cone-mode: true + token: ${{ steps.generate-token.outputs.token }} + + - name: Set up Python + uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pydantic requests pre-commit + sudo apt-get update + sudo apt-get install -y clang-format + + - name: Configure Git + run: | + git config user.name "assistant-librarian[bot]" + git config user.email "assistant-librarian[bot]@users.noreply.github.com" + + - name: Detect changed subtrees + id: detect + env: + GH_TOKEN: ${{ steps.generate-token.outputs.token }} + run: | + python .github/scripts/pr_detect_changed_subtrees.py \ + --repo "${{ github.repository }}" \ + --pr "${{ github.event.pull_request.number }}" \ + --config ".github/repos-config.json" + + - name: Checkout full repo with changed subtrees + if: steps.detect.outputs.subtrees + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + with: + ref: refs/pull/${{ github.event.pull_request.number }}/merge + sparse-checkout: | + .github + ${{ steps.detect.outputs.subtrees }} + fetch-depth: 0 + token: ${{ steps.generate-token.outputs.token }} + submodules: false + + - name: Get list of changed files + id: changed-files + shell: bash + run: | + git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1 + changed=$(git diff --name-only FETCH_HEAD HEAD) + files=$(echo "$changed" | tr '\n' ' ' | sed 's/ *$//') + echo "all_modified_files=$files" >> $GITHUB_OUTPUT + + - name: Show changed-files output + run: | + echo "Changed files are: ${{ steps.changed-files.outputs.all_modified_files }}" + + - name: Disable submodule recursion + run: git config submodule.recurse false + + - name: Run and auto-commit pre-commit fixes + env: + GIT_AUTHOR_NAME: "assistant-librarian[bot]" + GIT_AUTHOR_EMAIL: "assistant-librarian[bot]@users.noreply.github.com" + GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} + run: | + var="${{ steps.changed-files.outputs.all_modified_files }}" + read -r -a files <<<"$var" + if [ ${#files[@]} -eq 0 ]; then + echo "No files changed, skipping pre-commit." + exit 0 + fi + echo "Running pre-commit on these files:" + printf " %s\n" "${files[@]}" + set +e + pre-commit run --files "${files[@]}" --show-diff-on-failure + set -e + git add "${files[@]}" + git fetch origin "${{github.head_ref}}" + if ! git diff --cached --quiet; then + git commit -m "ci: apply pre-commit fixes" + git push --force-with-lease origin HEAD:${{ github.head_ref }} + fi diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000000..cfd07ac7865 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,25 @@ +exclude: | + third_party/ + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + + - repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black + language_version: python3 + + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v18.1.4 + hooks: + - id: clang-format + name: clang-format (C/C++/ObjC) + entry: clang-format -i -style=file + files: '\.(c|cpp|cc|h|hpp|m|mm)$'