|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +name: Github Merge Conflict Check |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [main, dev, 1.0*, 2.0*, 3.0*, fasttrack/*] |
| 9 | + pull_request: |
| 10 | + branches: [main, dev, 1.0*, 2.0*, 3.0*, fasttrack/*] |
| 11 | + |
| 12 | +jobs: |
| 13 | + spec-check: |
| 14 | + name: Github Merge Conflict Check |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + # Checkout the branch of our repo that triggered this action |
| 19 | + - name: Workflow trigger checkout |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Get base commit for PRs |
| 23 | + if: ${{ github.event_name == 'pull_request' }} |
| 24 | + run: | |
| 25 | + git fetch origin ${{ github.base_ref }} |
| 26 | + echo "base_sha=$(git rev-parse origin/${{ github.base_ref }})" >> $GITHUB_ENV |
| 27 | + echo "Merging ${{ github.sha }} into ${{ github.base_ref }}" |
| 28 | +
|
| 29 | + - name: Get base commit for Pushes |
| 30 | + if: ${{ github.event_name == 'push' }} |
| 31 | + run: | |
| 32 | + git fetch origin ${{ github.event.before }} |
| 33 | + echo "base_sha=${{ github.event.before }}" >> $GITHUB_ENV |
| 34 | + echo "Merging ${{ github.sha }} into ${{ github.event.before }}" |
| 35 | +
|
| 36 | + - name: Check for merge conflicts |
| 37 | + run: | |
| 38 | + echo "Files changed: '$(git diff-tree --no-commit-id --name-only -r ${{ env.base_sha }} ${{ github.sha }})'" |
| 39 | + changed_files=$(git diff-tree --diff-filter=d --no-commit-id --name-only -r ${{ env.base_sha }} ${{ github.sha }}) |
| 40 | +
|
| 41 | + merge_conflict_found=false |
| 42 | + for file in $changed_files ; do |
| 43 | + if [ -f $file ]; then |
| 44 | + echo "Checking for merge conflicts in $file" |
| 45 | + if grep -H -r "^<<<<<<< HEAD$" $file; then |
| 46 | + echo "Merge conflict found in $file" |
| 47 | + merge_conflict_found=true |
| 48 | + fi |
| 49 | +
|
| 50 | + if grep -H -r "^>>>>>>>$" $file; then |
| 51 | + echo "Merge conflict found in $file" |
| 52 | + merge_conflict_found=true |
| 53 | + fi |
| 54 | +
|
| 55 | + if grep -H -r "^=======$" $file; then |
| 56 | + echo "Merge conflict found in $file" |
| 57 | + merge_conflict_found=true |
| 58 | + fi |
| 59 | + fi |
| 60 | + done |
| 61 | +
|
| 62 | + if [[ $merge_conflict_found =~ [Tt]rue ]]; then |
| 63 | + echo "Merge conflict found in one or more files" |
| 64 | + exit 1 |
| 65 | + fi |
0 commit comments