-
Notifications
You must be signed in to change notification settings - Fork 1
73 lines (61 loc) · 2.14 KB
/
Copy pathformat-code.yml
File metadata and controls
73 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
name: format-code
on: [push]
permissions:
contents: write
jobs:
format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
- name: Cache clang-format
uses: actions/cache@v2
with:
path: /usr/lib/llvm-10/bin/clang-format
key: ${{ runner.os }}-clang-format-${{ steps.date.outputs.date }}
restore-keys: ${{ runner.os }}-clang-format-
- name: Install clang-format
run: |
if [ ! -f /usr/lib/llvm-10/bin/clang-format ]; then
sudo apt-get install -y clang-format
fi
- name: Output clang-format version
run: clang-format --version
- name: Check for changes in .cpp, .hpp, .h files
id: check-changes
run: |
git fetch origin
changed_files=$(git diff --name-only ${{ github.event.before }}..${{ github.sha }} -- '*.cpp' '*.hpp' '*.h')
if [ -z "$changed_files" ]; then
echo "::set-output name=all_files::true"
else
echo "::set-output name=all_files::false"
fi
- name: Format code
run: |
if [ "${{ steps.check-changes.outputs.all_files }}" == "true" ]; then
echo "No .cpp, .hpp, or .h files were changed in the last commit. Formatting all .cpp, .hpp, and .h files in the repository."
find . -name '*.cpp' -or -name '*.hpp' -or -name '*.h' | while read filename; do
clang-format -i $filename
done
else
changed_files=$(git diff --name-only HEAD^)
echo "$changed_files" | while read filename; do
if [[ $filename == *.cpp || $filename == *.hpp || $filename == *.h ]]; then
clang-format -i $filename
fi
done
fi
- name: Check for changes
run: git diff
- name: Commit changes
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git diff --quiet || git commit -am "Automatically format code"
git push