3 # clang-format-all: a tool to run clang-format on an entire project
4 # Copyright (C) 2016 Evan Klitzke <evan@eklitzke.org>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 echo "Usage: $0 DIR..."
28 # Variable that will hold the name of the clang-format command
31 # Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent
32 # that the version number be part of the command. We prefer clang-format if
33 # that's present, otherwise we work backwards from highest version to lowest
35 for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do
36 if which "$clangfmt" &>/dev/null; then
42 # Check if we found a working clang-format
43 if [ -z "$FMT" ]; then
44 echo "failed to find clang-format"
48 # Check all of the arguments first to make sure they're all directories
50 if [ ! -d "${dir}" ]; then
51 echo "${dir} is not a directory"
56 # Find a dominating file, starting from a given directory and going up.
57 find-dominating-file() {
58 if [ -r "$1"/"$2" ]; then
61 if [ "$1" = "/" ]; then
64 find-dominating-file "$(realpath "$1"/..)" "$2"
68 # Run clang-format -i on all of the things
70 pushd "${dir}" &>/dev/null
71 if ! find-dominating-file . _clang-format; then
72 echo "Failed to find dominating .clang-format starting at $PWD"
82 -exec "${FMT}" -i -verbose '{}' \;