-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclang-format-all.sh
executable file
·110 lines (94 loc) · 2.73 KB
/
clang-format-all.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
#
# clang-format-all: a tool to run clang-format on an entire project
# Copyright (C) 2021 Huang Weiyao <[email protected]>
# Copyright (C) 2016 Evan Klitzke <[email protected]>
#
# Usage: $0 DIR...
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function progress_bar {
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
printf "\rProgress($1/$2): [${_fill// /\#}${_empty// /-}] ${_progress}%%"
}
function usage {
echo "Usage: $0 DIR..."
exit 1
}
# Variable that will hold the name of the clang-format command
FMT=""
# Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent
# that the version number be part of the command. We prefer clang-format if
# that's present, otherwise we work backwards from highest version to lowest
# version.
for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do
if which "$clangfmt" &>/dev/null; then
FMT="$clangfmt"
break
fi
done
# Check if we found a working clang-format
if [ -z "$FMT" ]; then
echo "failed to find clang-format"
exit 1
fi
# Check all of the arguments first to make sure they're all directories
for dir in "$@"; do
if [ ! -d "${dir}" ]; then
echo "${dir} is not a directory"
usage
fi
done
# Find a dominating file, starting from a given directory and going up.
find_dominating_file() {
if [ -r "$1"/"$2" ]; then
return 0
fi
if [ "$1" = "/" ]; then
return 1
fi
find_dominating_file "$(realpath "$1"/..)" "$2"
return $?
}
if [ $# -eq 0 ]; then
usage
exit 0
fi
# Run clang-format -i on all of the things
for dir in "$@"; do
pushd "${dir}" &>/dev/null # cd to $dir
if ! find_dominating_file . .clang-format; then
echo "Failed to find dominating .clang-format starting at $PWD"
continue
fi
list=( $(find . \
-name '*.c' \
-o -name '*.cc' \
-o -name '*.cpp' \
-o -name '*.h' \
-o -name '*.hh' \
-o -name '*.hpp') )
for index in ${!list[@]}; do
progress_bar $((index+1)) ${#list[@]}
if [[ -f ${list[index]} ]]; then
$FMT -i ${list[index]}
fi
done
echo ""
popd &>/dev/null
done