Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/Scripts/clang-format.bash
3148 views
1
#!/usr/bin/env bash
2
#=============================================================================
3
# Copyright 2015-2017 Kitware, Inc.
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#=============================================================================
17
18
usage='usage: clang-format.bash [<options>] [--]
19
20
--help Print usage plus more detailed help.
21
22
--clang-format <tool> Use given clang-format tool.
23
24
--amend Filter files changed by HEAD.
25
--cached Filter files locally staged for commit.
26
--modified Filter files locally modified from HEAD.
27
--tracked Filter files tracked by Git.
28
'
29
30
help="$usage"'
31
Example to format locally modified files:
32
33
Utilities/Scripts/clang-format.bash --modified
34
35
Example to format locally modified files staged for commit:
36
37
Utilities/Scripts/clang-format.bash --cached
38
39
Example to format files modified by the most recent commit:
40
41
Utilities/Scripts/clang-format.bash --amend
42
43
Example to format all files tracked by Git:
44
45
Utilities/Scripts/clang-format.bash --tracked
46
47
Example to format the current topic:
48
49
git filter-branch \
50
--tree-filter "Utilities/Scripts/clang-format.bash --tracked" \
51
master..
52
'
53
54
die() {
55
echo "$@" 1>&2; exit 1
56
}
57
58
#-----------------------------------------------------------------------------
59
60
# Parse command-line arguments.
61
clang_format=''
62
mode=''
63
while test "$#" != 0; do
64
case "$1" in
65
--amend) mode="amend" ;;
66
--cached) mode="cached" ;;
67
--clang-format) shift; clang_format="$1" ;;
68
--help) echo "$help"; exit 0 ;;
69
--modified) mode="modified" ;;
70
--tracked) mode="tracked" ;;
71
--) shift ; break ;;
72
-*) die "$usage" ;;
73
*) break ;;
74
esac
75
shift
76
done
77
test "$#" = 0 || die "$usage"
78
79
# Find a default tool.
80
tools='
81
clang-format-18
82
clang-format
83
'
84
if test "x$clang_format" = "x"; then
85
for tool in $tools; do
86
if type -p "$tool" >/dev/null; then
87
clang_format="$tool"
88
break
89
fi
90
done
91
fi
92
93
# Verify that we have a tool.
94
if ! type -p "$clang_format" >/dev/null; then
95
echo "Unable to locate a 'clang-format' tool."
96
exit 1
97
fi
98
99
if ! "$clang_format" --version | grep 'clang-format version 18' >/dev/null 2>/dev/null; then
100
echo "clang-format version 18 is required (exactly)"
101
exit 1
102
fi
103
104
# Select listing mode.
105
case "$mode" in
106
'') echo "$usage"; exit 0 ;;
107
amend) git_ls='git diff-tree --diff-filter=AM --name-only HEAD -r --no-commit-id' ;;
108
cached) git_ls='git diff-index --diff-filter=AM --name-only HEAD --cached' ;;
109
modified) git_ls='git diff-index --diff-filter=AM --name-only HEAD' ;;
110
tracked) git_ls='git ls-files' ;;
111
*) die "invalid mode: $mode" ;;
112
esac
113
114
# List files as selected above.
115
$git_ls |
116
117
# Select sources with our attribute.
118
git check-attr --stdin format.clang-format |
119
sed -n '/: format\.clang-format: \(set\|18\)$/ {s/:[^:]*:[^:]*$//p}' |
120
121
# Update sources in-place.
122
xargs -d '\n' "$clang_format" -i
123
124